CAD97 / CAD97/pointer-utils

FatErasedPtr

Open
#84 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
137
Forks
15
PR merge metrics
No merged PRs in 30d

Description

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

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

impl From

for FatErasedPtr {
#[inline(always)]
fn from(this: P) -> Self {
fn dropfn (this: ErasedPtr) {
unsafe {

::unerase(this) };
}

FatErasedPtr {
ptr: P::erase(this),
dropfn: dropfn::

,
}
}
}

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

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

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

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

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.