2.0.1 |
|
---|---|
1.0.0 |
|
0.3.0 |
|
0.2.0 |
|
#45 in #lazy
171 downloads per month
7KB
114 lines
This is obsolete.
There is standard OnceCell
, and there's a better crate now.
lib.rs
:
Wrapper type for lazy initialization
Store "immutable" cell that computes its value once, when it's first accessed. Allows lazy initialization as an implementation detail, without need to expose any mutable methods.
It's like lazy_static
, but not static.
It's like std::sync::Once
, but holds a value.
It's thread-safe (Send
and Sync
).
use lazyonce::LazyOnce;
struct Oracle {
answer: LazyOnce<u32>,
}
impl Oracle {
pub fn get_answer(&self) -> &u32 {
self.answer.get(|| think()) // think() is called only once
}
}
Dependencies
~0.4–5MB
~11K SLoC