Struct staticslot::StaticSlot
[−]
[src]
pub struct StaticSlot<T> { /* fields omitted */ }
A container for a statically owned value.
A slot can either hold a value or contain NULL. By default, a slot starts out NULL and can be populated with a value later.
This container is meant to be used in conjunction with static
variables for more controlled allocation and
de-allocation of shared instances. This type is unsafe because destructors are not guaranteed to be run at all, let
alone in the correct order. You must clean up your resources manually using the drop()
method.
Behaves kind of like a RefCell<Option<Box<T>>>
with atomic swapping and manual destruction.
Methods
impl<T: 'static> StaticSlot<T>
[src]
const NULL: Self
NULL: Self = Self{address: ATOMIC_USIZE_INIT, _phantom: PhantomData,}
A static slot with its value set to NULL. Useful for static initialization.
fn new(value: T) -> Self
Create a new static slot that contains the given value.
fn is_null(&self) -> bool
Check if the slot contains NULL.
fn get(&self) -> Option<&mut T>
Gets a reference to the value in the slot, if set.
This method does not perform any initialization. For optimal performance, this performs a fast check if the slot is NULL and, if not, returns a reference.
unsafe fn set(&self, value: T)
Sets the static slot to a new value. If the slot was already set, the old value is dropped.
This method is marked as unsafe because it can introduce memory leaks if drop()
or take()
is not manually
called before the process exits.
fn with<R, F: FnOnce() -> R>(&self, value: T, f: F) -> R
Invokes a closure, with the slot set to a given value.
This method introduces a safe, controlled lifetime for the contained value. The value is shared for the duration of the execution of the closure. When the closure returns, the value is dropped.
fn take(&self) -> Option<T>
Takes the value out of the slot if it exists and frees any allocated heap memory.
fn drop(&self) -> bool
Drops the value in the slot if any, and returns if a value was dropped.
Trait Implementations
impl<T: 'static> Default for StaticSlot<T>
[src]
fn default() -> Self
Create a new NULL static slot.