Skip to content

Add 'with()' and 'with_mut()' as associated functions to ErasedPtr #82

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions crates/erasable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,60 @@ pub unsafe trait ErasablePtr {
///
/// The erased pointer must have been created by `erase`.
unsafe fn unerase(this: ErasedPtr) -> Self;

/// Run a closure on a borrow of the real pointer. Unlike the `Thin<T>` wrapper this does
/// not carry the original type around. Thus it is required to specify the original impl
/// type when calling this function.
///
/// ```
/// # use {erasable::*, std::rc::Rc};
/// let rc: Rc<i32> = Rc::new(123);
///
/// let erased: ErasedPtr = ErasablePtr::erase(rc);
///
/// let cloned = unsafe {
/// <Rc<i32> as ErasablePtr>::with(&erased, |rc| rc.clone())
/// };
///
/// assert_eq!(*cloned, 123);
/// # unsafe {<Rc<i32> as ErasablePtr>::unerase(erased)}; // drop it
/// ```
///
/// The main purpose of this function is to be able implement recursive types that would
/// be otherwise not representable in rust.
///
/// # Safety
///
/// * The erased pointer must have been created by `erase`.
/// * The specified impl type must be the original type.
unsafe fn with<F, T>(this: &ErasedPtr, f: F) -> T
where
Self: Sized,
F: FnOnce(&Self) -> T,
{
f(&ManuallyDrop::new(Self::unerase(*this)))
}

/// Run a closure on a mutable borrow of the real pointer. Unlike the `Thin<T>` wrapper
/// this does not carry the original type around. Thus it is required to specify the
/// original impl type when calling this function.
///
/// # Safety
///
/// * The erased pointer must have been created by `erase`.
/// * The specified impl type must be the original type.
unsafe fn with_mut<F, T>(this: &mut ErasedPtr, f: F) -> T
where
Self: Sized,
F: FnOnce(&mut Self) -> T,
{
// SAFETY: guard is required to write potentially changed pointer value, even on unwind
let mut that = scopeguard::guard(ManuallyDrop::new(Self::unerase(*this)), |unerased| {
ptr::write(this, ErasablePtr::erase(ManuallyDrop::into_inner(unerased)));
});

f(&mut that)
}
}

/// A pointee type that supports type-erased pointers (thin pointers).
Expand Down
55 changes: 55 additions & 0 deletions crates/erasable/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,58 @@ fn thinning() {
Thin::with_mut(&mut thin, |thin| *thin = Default::default());
let boxed = Thin::into_inner(thin);
}

#[test]
fn with_fn() {
let boxed: Box<Big> = Default::default();

let erased: ErasedPtr = ErasablePtr::erase(boxed);

unsafe {
<Box<Big> as ErasablePtr>::with(&erased, |bigbox| {
assert_eq!(*bigbox, Default::default());
})
}

// drop it, otherwise we would leak memory here
unsafe { <Box<Big> as ErasablePtr>::unerase(erased) };
}

#[test]
fn with_mut_fn() {
let boxed: Box<Big> = Default::default();

let mut erased: ErasedPtr = ErasablePtr::erase(boxed);

unsafe {
<Box<Big> as ErasablePtr>::with_mut(&mut erased, |bigbox| {
bigbox.0[0] = 123456;
assert_ne!(*bigbox, Default::default());
})
}

// drop it, otherwise we would leak memory here
unsafe { <Box<Big> as ErasablePtr>::unerase(erased) };
}

#[test]
fn with_mut_fn_replacethis() {
let boxed: Box<Big> = Default::default();

let mut erased: ErasedPtr = ErasablePtr::erase(boxed);
let e1 = erased.as_ptr();
unsafe {
<Box<Big> as ErasablePtr>::with_mut(&mut erased, |bigbox| {
let mut newboxed: Box<Big> = Default::default();
newboxed.0[0] = 123456;
*bigbox = newboxed;
assert_ne!(*bigbox, Default::default());
})
}

let e2 = erased.as_ptr();
assert_ne!(e1, e2);

// drop it, otherwise we would leak memory here
unsafe { <Box<Big> as ErasablePtr>::unerase(erased) };
}