minion_rs/
scoped_ptr.rs

1/// A light scoped wrapper over a raw *mut pointer.
2///
3/// Implements destruction of the pointer when it goes out of scope, but provides no other
4/// guarantees.
5#[non_exhaustive]
6pub struct Scoped<T> {
7    pub ptr: *mut T,
8    destructor: fn(*mut T),
9}
10
11// Could use
12// https://doc.rust-lang.org/std/alloc/trait.Allocator.html with box (in nightly only)
13// or
14// https://docs.rs/scopeguard/latest/scopeguard/
15// instead
16impl<T> Scoped<T> {
17    pub unsafe fn new(ptr: *mut T, destructor: fn(*mut T)) -> Scoped<T> {
18        Scoped { ptr, destructor }
19    }
20}
21
22// https://doc.rust-lang.org/nomicon/destructors.html
23impl<T> Drop for Scoped<T> {
24    fn drop(&mut self) {
25        (self.destructor)(self.ptr);
26    }
27}