Skip to content

Commit cd77ea9

Browse files
committed
Fix: ErasablePtr::with()
* remove references of the unerased value * fix memory leaks in tests by unerase/drop the erased values
1 parent c397f52 commit cd77ea9

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

crates/erasable/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ pub unsafe trait ErasablePtr {
201201
/// };
202202
///
203203
/// assert_eq!(*cloned, 123);
204+
/// # unsafe {<Rc<i32> as ErasablePtr>::unerase(erased)}; // drop it
204205
/// ```
205206
///
206207
/// The main purpose of this function is to be able implement recursive types that would
@@ -215,7 +216,7 @@ pub unsafe trait ErasablePtr {
215216
Self: Sized,
216217
F: FnOnce(&Self) -> T,
217218
{
218-
f(&ManuallyDrop::new(&Self::unerase(*this)))
219+
f(&ManuallyDrop::new(Self::unerase(*this)))
219220
}
220221

221222
/// Run a closure on a mutable borrow of the real pointer. Unlike the `Thin<T>` wrapper

crates/erasable/tests/smoke.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn thinning() {
3434
}
3535

3636
#[test]
37-
fn withfn() {
37+
fn with_fn() {
3838
let boxed: Box<Big> = Default::default();
3939

4040
let erased: ErasedPtr = ErasablePtr::erase(boxed);
@@ -44,6 +44,9 @@ fn withfn() {
4444
assert_eq!(*bigbox, Default::default());
4545
})
4646
}
47+
48+
// drop it, otherwise we would leak memory here
49+
unsafe { <Box<Big> as ErasablePtr>::unerase(erased) };
4750
}
4851

4952
#[test]

0 commit comments

Comments
 (0)