Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FatErasedPtr #84

Open
cehteh opened this issue Feb 2, 2023 · 0 comments
Open

FatErasedPtr #84

cehteh opened this issue Feb 2, 2023 · 0 comments

Comments

@cehteh
Copy link

cehteh commented Feb 2, 2023

I had just fun: Type erased pointers that Drop properly:

pub struct FatErasedPtr {
    ptr: ErasedPtr,
    dropfn: fn(ErasedPtr),
}

impl<P: ErasablePtr + 'static> From<P> for FatErasedPtr {
    #[inline(always)]
    fn from(this: P) -> Self {
        fn dropfn<P: ErasablePtr> (this: ErasedPtr) {
            unsafe { <P as ErasablePtr>::unerase(this) };
        }

        FatErasedPtr {
            ptr: P::erase(this),
            dropfn: dropfn::<P>,
        }
    }
}

impl Drop for FatErasedPtr {
    fn drop(&mut self) {
        (self.dropfn)(self.ptr)
    }
}

#[test]
fn faterasedptr() {
    use alloc::rc::Rc;
    let rc: Rc<i32> = Rc::new(123);

    let fat_erased: FatErasedPtr = FatErasedPtr::from(rc);

    assert_eq!(core::mem::size_of_val(&fat_erased), core::mem::size_of::<usize>()*2);
}

Was just a curious experiment here. I can complete that and send a PR if this is interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant