-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy_initializer.rs
59 lines (51 loc) · 1.48 KB
/
lazy_initializer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
pub struct LazyInit<T> {
initializer: Mutex<Option<Box<dyn FnOnce() -> T + Send>>>,
value: OnceCell<T>,
initialized: AtomicBool,
}
impl<T> LazyInit<T> {
pub fn new<F>(initializer: F) -> Self
where
F: FnOnce() -> T + Send + 'static,
{
LazyInit {
initializer: Mutex::new(Some(Box::new(initializer))),
value: OnceCell::new(),
initialized: AtomicBool::new(false),
}
}
pub fn get_or_init(&self) -> &T {
self.value.get_or_init(|| {
let initializer = self.initializer.lock().take().unwrap();
let v = initializer();
self.initialized.store(true, SeqCst);
v
})
}
pub fn is_initialized(&self) -> bool {
self.initialized.load(SeqCst)
}
}
#[cfg(test)]
mod tests {
use crate::lazy_initializer::LazyInit;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::Arc;
#[test]
fn test() {
let tag = Arc::new(AtomicU64::new(0));
let tag_fork = tag.clone();
let lazy_value = LazyInit::new(move || {
println!("Initializing...");
tag_fork.fetch_add(1, SeqCst);
});
let value = lazy_value.get_or_init();
let value = lazy_value.get_or_init();
assert_eq!(tag.load(SeqCst), 1);
}
}