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()
anddealloc()
methods are explicitly called ordrop_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>
impl<T> HeapCell<T>
Sourcepub fn new(val: T) -> Self
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.
Sourcepub unsafe fn as_mut(&self) -> &mut T
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.
Sourcepub unsafe fn as_ref(&self) -> &T
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.
Sourcepub unsafe fn take(&self) -> T
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.
Sourcepub unsafe fn drop_n_dealloc(&self)
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.
Sourcepub fn clone_inner(&self) -> HeapCell<T>where
T: Clone,
pub fn clone_inner(&self) -> HeapCell<T>where
T: Clone,
Sourcepub unsafe fn replace(&self, val: T) -> T
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