#lazy-evaluation #immutability #cell #init #cache

yanked lazyonce

Use std::cell::OnceCell instead

new 2.0.1 Apr 21, 2024
1.0.0 Jul 20, 2019
0.3.0 Dec 9, 2018
0.2.0 Jun 14, 2018

#22 in #immutable

Download history 15/week @ 2024-02-26 45/week @ 2024-04-01 138/week @ 2024-04-15

183 downloads per month

CC0 license

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–6.5MB
~11K SLoC