pub struct ManuallyStaticPtr<T> { /* private fields */ }
Expand description
ManuallyStaticPtr<T>
allocates a value T
on the heap and provides
a raw pointer to it. It requires manual deallocation
via the free
method.
In debug builds, it tracks if the pointer has already been freed,
causing a panic if free
is called multiple times or if the pointer
is dereferenced after being freed.
§Example
use manually_static::ManuallyStaticPtr;
use std::sync::Mutex;
use std::array;
const N: usize = 10280;
const PAR: usize = 16;
#[allow(dead_code, reason = "It is an example.")]
struct Pool(Mutex<([Vec<u8>; N], usize)>);
fn main() {
let pool = ManuallyStaticPtr::new(Pool(Mutex::new((array::from_fn(|_| Vec::new()), 0))));
let mut joins = Vec::with_capacity(PAR);
for _ in 0..PAR {
#[allow(unused_variables, reason = "It is an example.")]
let pool = pool.clone();
joins.push(std::thread::spawn(move || {
/* ... do some work ... */
}));
}
for join in joins {
join.join().unwrap();
}
unsafe { pool.free(); }
}
Implementations§
Source§impl<T> ManuallyStaticPtr<T>
impl<T> ManuallyStaticPtr<T>
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Allocates a new ManuallyStaticPtr
instance by moving value
to the heap.
§Examples
use manually_static::ManuallyStaticPtr;
let my_ptr = ManuallyStaticPtr::new(42);
assert_eq!(*my_ptr, 42);
// Don't forget to call `free` when done!
unsafe { my_ptr.free(); }
Sourcepub unsafe fn free(self)
pub unsafe fn free(self)
Deallocates the memory associated with this ManuallyStaticPtr
.
§Safety
This function is unsafe
because:
- It must be called exactly once for each
ManuallyStaticPtr
instance. Calling it more than once will result in a double-free, leading to undefined behavior. In debug builds, this will panic. - Not calling
free
will result in a memory leak. - The raw pointer must not be aliased or used after
free
is called.
§Panics
In debug builds, this function will panic if the pointer has already been freed.
§Examples
use manually_static::ManuallyStaticPtr;
let my_ptr = ManuallyStaticPtr::new(vec![1, 2, 3]);
// ... use my_ptr ...
unsafe { my_ptr.free(); } // Explicitly free the memory
// my_ptr is now consumed and cannot be used
Trait Implementations§
Source§impl<T> Clone for ManuallyStaticPtr<T>
impl<T> Clone for ManuallyStaticPtr<T>
Source§impl<T> Deref for ManuallyStaticPtr<T>
impl<T> Deref for ManuallyStaticPtr<T>
Source§impl<T> Drop for ManuallyStaticPtr<T>
impl<T> Drop for ManuallyStaticPtr<T>
impl<T: Send> Send for ManuallyStaticPtr<T>
impl<T: Sync> Sync for ManuallyStaticPtr<T>
Auto Trait Implementations§
impl<T> Freeze for ManuallyStaticPtr<T>
impl<T> RefUnwindSafe for ManuallyStaticPtr<T>where
T: RefUnwindSafe,
impl<T> Unpin for ManuallyStaticPtr<T>
impl<T> UnwindSafe for ManuallyStaticPtr<T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more