| 1 |
//! Synchronization primitives for lazy evaluation. |
| 2 |
//! |
| 3 |
//! Implementation adapted from the `SyncLazy` type of the standard library. See: |
| 4 |
//! <https://doc.rust-lang.org/std/lazy/struct.SyncLazy.html> |
| 5 |
|
| 6 |
use crate::{once::Once, RelaxStrategy, Spin}; |
| 7 |
use core::{cell::Cell, fmt, ops::Deref}; |
| 8 |
|
| 9 |
/// A value which is initialized on the first access. |
| 10 |
/// |
| 11 |
/// This type is a thread-safe `Lazy`, and can be used in statics. |
| 12 |
/// |
| 13 |
/// # Examples |
| 14 |
/// |
| 15 |
/// ``` |
| 16 |
/// use std::collections::HashMap; |
| 17 |
/// use spin::Lazy; |
| 18 |
/// |
| 19 |
/// static HASHMAP: Lazy<HashMap<i32, String>> = Lazy::new(|| { |
| 20 |
/// println!("initializing"); |
| 21 |
/// let mut m = HashMap::new(); |
| 22 |
/// m.insert(13, "Spica".to_string()); |
| 23 |
/// m.insert(74, "Hoyten".to_string()); |
| 24 |
/// m |
| 25 |
/// }); |
| 26 |
/// |
| 27 |
/// fn main() { |
| 28 |
/// println!("ready"); |
| 29 |
/// std::thread::spawn(|| { |
| 30 |
/// println!("{:?}", HASHMAP.get(&13)); |
| 31 |
/// }).join().unwrap(); |
| 32 |
/// println!("{:?}", HASHMAP.get(&74)); |
| 33 |
/// |
| 34 |
/// // Prints: |
| 35 |
/// // ready |
| 36 |
/// // initializing |
| 37 |
/// // Some("Spica") |
| 38 |
/// // Some("Hoyten") |
| 39 |
/// } |
| 40 |
/// ``` |
| 41 |
pub struct Lazy<T, F = fn() -> T, R = Spin> { |
| 42 |
cell: Once<T, R>, |
| 43 |
init: Cell<Option<F>>, |
| 44 |
} |
| 45 |
|
| 46 |
impl<T: fmt::Debug, F, R> fmt::Debug for Lazy<T, F, R> { |
| 47 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 48 |
let mut d = f.debug_tuple("Lazy"); |
| 49 |
let d = if let Some(x) = self.cell.get() { |
| 50 |
d.field(&x) |
| 51 |
} else { |
| 52 |
d.field(&format_args!("<uninit>")) |
| 53 |
}; |
| 54 |
d.finish() |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// We never create a `&F` from a `&Lazy<T, F>` so it is fine |
| 59 |
// to not impl `Sync` for `F` |
| 60 |
// we do create a `&mut Option<F>` in `force`, but this is |
| 61 |
// properly synchronized, so it only happens once |
| 62 |
// so it also does not contribute to this impl. |
| 63 |
unsafe impl<T, F: Send> Sync for Lazy<T, F> where Once<T>: Sync {} |
| 64 |
// auto-derived `Send` impl is OK. |
| 65 |
|
| 66 |
impl<T, F, R> Lazy<T, F, R> { |
| 67 |
/// Creates a new lazy value with the given initializing |
| 68 |
/// function. |
| 69 |
pub const fn new(f: F) -> Self { |
| 70 |
Self { |
| 71 |
cell: Once::new(), |
| 72 |
init: Cell::new(Some(f)), |
| 73 |
} |
| 74 |
} |
| 75 |
/// Retrieves a mutable pointer to the inner data. |
| 76 |
/// |
| 77 |
/// This is especially useful when interfacing with low level code or FFI where the caller |
| 78 |
/// explicitly knows that it has exclusive access to the inner data. Note that reading from |
| 79 |
/// this pointer is UB until initialized or directly written to. |
| 80 |
pub fn as_mut_ptr(&self) -> *mut T { |
| 81 |
self.cell.as_mut_ptr() |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
impl<T, F: FnOnce() -> T, R: RelaxStrategy> Lazy<T, F, R> { |
| 86 |
/// Forces the evaluation of this lazy value and |
| 87 |
/// returns a reference to result. This is equivalent |
| 88 |
/// to the `Deref` impl, but is explicit. |
| 89 |
/// |
| 90 |
/// # Examples |
| 91 |
/// |
| 92 |
/// ``` |
| 93 |
/// use spin::Lazy; |
| 94 |
/// |
| 95 |
/// let lazy = Lazy::new(|| 92); |
| 96 |
/// |
| 97 |
/// assert_eq!(Lazy::force(&lazy), &92); |
| 98 |
/// assert_eq!(&*lazy, &92); |
| 99 |
/// ``` |
| 100 |
pub fn force(this: &Self) -> &T { |
| 101 |
this.cell.call_once(|| match this.init.take() { |
| 102 |
Some(f) => f(), |
| 103 |
None => panic!("Lazy instance has previously been poisoned"), |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
impl<T, F: FnOnce() -> T, R: RelaxStrategy> Deref for Lazy<T, F, R> { |
| 109 |
type Target = T; |
| 110 |
|
| 111 |
fn deref(&self) -> &T { |
| 112 |
Self::force(self) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
impl<T: Default, R> Default for Lazy<T, fn() -> T, R> { |
| 117 |
/// Creates a new lazy value using `Default` as the initializing function. |
| 118 |
fn default() -> Self { |
| 119 |
Self::new(T::default) |
| 120 |
} |
| 121 |
} |