pub struct OnceOptionBox<T> {
inner: AtomicPtr<T>,
}
Expand description
A lazily initialized box. Similar to an Option<Box<T>>
, but can be initialized atomically.
It is designed for implementing shared data. Therefore, methods with &self
, namely
OnceOptionBox::get
and the OnceOptionBox::get_or_init
methods, only return shared
references to the content (&T
). The user should use types that support multi-threaded
accesses, such as mutexes or atomic types, if the inner type is supposed to be modified
concurrently.
Once initialized, this object will own its content. The content is allocated in the heap, and will be dropped and deallocated when this instance is dropped.
§Comparison to existing data structures
std::sync::OnceLock
also provides thread-safe lazily-initialized cells. But as its name
suggests, it uses locks for synchronization, whereas OnceOptionBox
is lock-free. OnceLock
also has a field of std::sync::Once
which increases the space overhead. OnceOptionBox
only has one atomic pointer field and is more suitable for large arrays of lazily initialized
elements.
Fields§
§inner: AtomicPtr<T>
Implementations§
source§impl<T> OnceOptionBox<T>
impl<T> OnceOptionBox<T>
sourcepub fn new() -> OnceOptionBox<T>
pub fn new() -> OnceOptionBox<T>
Create an empty OnceOptionBox
instance.
sourcepub fn get(&self, order: Ordering) -> Option<&T>
pub fn get(&self, order: Ordering) -> Option<&T>
Get a reference to the content of this box, or None
if not yet initialized.
sourcepub fn get_or_init(
&self,
order_load: Ordering,
order_store: Ordering,
init: impl FnOnce() -> T,
) -> &T
pub fn get_or_init( &self, order_load: Ordering, order_store: Ordering, init: impl FnOnce() -> T, ) -> &T
Get a reference to the content of this box. If not initialized, it will call init
to
initialize this box.
When multiple threads attempt to initialize this box concurrently, all threads may call
their supplied init
closure, but only one thread will successfully initialize this box to
the return value of init
. Other threads will drop their return values of init
. All
callers will return the reference to the value created by the successful thread.
Trait Implementations§
source§impl<T> Drop for OnceOptionBox<T>
impl<T> Drop for OnceOptionBox<T>
Auto Trait Implementations§
impl<T> !Freeze for OnceOptionBox<T>
impl<T> RefUnwindSafe for OnceOptionBox<T>
impl<T> Send for OnceOptionBox<T>
impl<T> Sync for OnceOptionBox<T>
impl<T> Unpin for OnceOptionBox<T>
impl<T> UnwindSafe for OnceOptionBox<T>where
T: RefUnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
, which can then be
downcast
into Box<dyn ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
, which can then be further
downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more