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.

Think of it as an optimized RefCell<Option<Box<T>>> with atomic swapping and manual destruction.

Methods

impl<T: 'static> StaticSlot<T>
[src]

NULL: Self = Self{address: ATOMIC_USIZE_INIT, _phantom: PhantomData,}

A static slot with its value set to NULL. Useful for static initialization.

[src]

Create a new static slot that contains the given value.

[src]

Check if the slot contains NULL.

[src]

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.

[src]

Get a mutable reference to the value in the slot.

If doing a null check every time you call get() is unnacceptable, then this unsafe variant will let you bypass that. Note that if the slot has not been initialized, the returned reference will be invalid and improper use could cause a segmentation fault.

[src]

Returns an unsafe pointer to the contained value.

If the slot is empty, will return a null pointer.

[src]

Returns an unsafe mutable pointer to the contained value.

If the slot is empty, will return a null pointer.

[src]

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.

[src]

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.

[src]

Takes the value out of the slot if it exists and frees any allocated heap memory.

[src]

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]

[src]

Create a new static slot initialized with NULL.

impl<T: Send> Send for StaticSlot<T>
[src]

impl<T: Sync> Sync for StaticSlot<T>
[src]