Struct HeapCell

Source
pub struct HeapCell<T> { /* private fields */ }
Expand description

A HeapCell is Heap allocated type pointer. Functions like NonNull + UnsafeCell

§Note

  • It moves the data onto the heap and stores a pointer to it.
  • It never drops or deallocates the data it wraps until drop() and dealloc() methods are explicitly called or drop_n_dealloc() is called.

§Uses

HeapCell can be accessed mutably through the as_mut method and immutably through the as_ref method without requiring the container to be mutable.

§Examples

use speedy_refs::HeapCell;

let cell = HeapCell::new(42);
assert_eq!(unsafe { *cell.as_ref() }, 42);

unsafe {
    *cell.as_mut() = 7;
    assert_eq!(*cell.as_ref(), 7);

    let val = cell.take();
    assert_eq!(val, 7);

    cell.replace(42);
    assert_eq!(*cell.as_ref(), 42);

    cell.drop_n_dealloc();
}

Implementations§

Source§

impl<T> HeapCell<T>

Source

pub fn new(val: T) -> Self

Creates a new HeapCell containing the given value.

§Safety

This function takes ownership of the given value and stores it behind a raw pointer. It is the responsibility of the caller to ensure that the value passed in is valid and the HeapCell is not used after the value has been dropped or moved.

§Panics

This function will panic if memory allocation fails.

Source

pub unsafe fn as_mut(&self) -> &mut T

Creates a mutable reference to T from an immutable reference to self.

§Returns
  • &mut T - A mutable reference to the self.inner value
§Safety

This function returns a mutable reference to T by dereferencing a raw pointer. It is the responsibility of the caller to ensure that this method is not invoked while there is an outstanding mutable or immutable reference this same T, as that would lead to data races.

Source

pub unsafe fn as_ref(&self) -> &T

Creates an immutable reference to self from an immutable reference to self.

§Returns
  • &T - An immutable reference to T, the self.inner value
§Safety

This function returns an immutable reference to T by dereferencing a raw pointer. It is the responsibility of the caller to ensure that this method is not invoked while there is an outstanding mutable reference this same T, as that would lead to data races.

Source

pub unsafe fn take(&self) -> T

Takes ownership of the value stored in the HeapCell.

§Safety

This function is marked as unsafe because it assumes ownership of the value behind the raw pointer held by the HeapCell. The caller must ensure that it is safe to take ownership of the value.

§Returns

This function returns the owned value of type T that was stored in the HeapCell.

§Note

The caller must ensure that the HeapCell is not used after calling take, as the HeapCell will be left in an invalid state.

Source

pub unsafe fn drop_n_dealloc(&self)

Drops the content and deallocates its memory.

This function first calls drop on T and then deallocates the memory associated with it

§Safety

The caller must ensure that the HeapCell is not used after calling deallocate as self.inner will then point to an invalid memory.

Source

pub fn clone_inner(&self) -> HeapCell<T>
where T: Clone,

Creates a new cell wrapping a clone of the inner value.

§Returns

The new cloned value

Source

pub unsafe fn replace(&self, val: T) -> T

Replaces the wraped value with val and returns the original one

§Safety

It is up to the caller to make sure that this method is not called while T is borrowed

Source

pub unsafe fn drop(&self)

Source

pub unsafe fn dealloc(&self)

Trait Implementations§

Source§

impl<T> Clone for HeapCell<T>

Source§

fn clone(&self) -> Self

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

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for HeapCell<T>

§

impl<T> RefUnwindSafe for HeapCell<T>
where T: RefUnwindSafe,

§

impl<T> !Send for HeapCell<T>

§

impl<T> !Sync for HeapCell<T>

§

impl<T> Unpin for HeapCell<T>

§

impl<T> UnwindSafe for HeapCell<T>
where T: RefUnwindSafe,

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