FreezeBox<T>
is a container that can have two possible states:
* uninitialized: deref is not allowed.
* initialized: deref to a &T
is possible.
To upgrade a FreezeBox
to the initialized state, call lazy_init
.
lazy_init
does not require a mutable reference, making FreezeBox
suitable for sharing objects first and initializing them later.
Attempting to lazy_init
more than once, or deref while uninitialized
will cause a panic.
This example creates a shared data structure, then circles back to initialize one member.
```rust use freezebox::FreezeBox; use std::sync::Arc;
struct Resources {
name: FreezeBox
let resources = Arc::new(Resources::default()); let res2 = resources.clone();
let func = move || { assert_eq!(*res2.name, "Hello!"); };
resources.name.lazyinit("Hello!".tostring()); func(); ```
There are many similar crates out there: - lazystatic - oncecell - double-checked-cell - mitochondria