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]
6
pub 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
16
impl<T> Scoped<T> {
17
76860
    pub unsafe fn new(ptr: *mut T, destructor: fn(*mut T)) -> Scoped<T> {
18
76860
        Scoped { ptr, destructor }
19
76860
    }
20
}
21

            
22
// https://doc.rust-lang.org/nomicon/destructors.html
23
impl<T> Drop for Scoped<T> {
24
76860
    fn drop(&mut self) {
25
76860
        (self.destructor)(self.ptr);
26
76860
    }
27
}