Struct ManuallyStaticPtr

Source
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>

Source

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(); }
Source

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>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Deref for ManuallyStaticPtr<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> Drop for ManuallyStaticPtr<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Send> Send for ManuallyStaticPtr<T>

Source§

impl<T: Sync> Sync for ManuallyStaticPtr<T>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.