Skip to content

FatErasedPtr #84

@cehteh

Description

@cehteh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions