Crate lazy_rc

source ·
Expand description

lazy_rc provides implementations of Rc<T> and Arc<T> with lazy initialization.

In other words, the “inner” value of an LazyRc<T> or LazyArc<T> instance is created when it is accessed for the first time, using the supplied initialization function. Initialization may fail, in which case the error is passed through.

Example

use lazy_rc::LazyRc;
 
thread_local! {
    static INSTANCE: LazyRc<MyStruct>  = LazyRc::empty();
}
 
#[derive(Debug)]
struct MyStruct {
    /* ... */
}
 
impl MyStruct {
    fn new() -> Self {
        /* ... */
    }
 
    pub fn instance() -> Rc<Self> {
        INSTANCE.with(|instance| instance.or_init_with(Self::new))
    }
}

Structs

A thread-safe reference-counting pointer, akin to Arc<T>, but with lazy initialization
A single-threaded reference-counting pointer, akin to Rc<T>, but with lazy initialization