1use once_cell::sync::OnceCell;
8use std::ops::{Deref, DerefMut};
9
10pub struct OnceLock<T>(OnceCell<T>);
15
16impl<T> OnceLock<T> {
17 #[inline]
18 pub const fn new() -> OnceLock<T> {
19 OnceLock(OnceCell::new())
20 }
21
22 #[inline]
23 pub fn get_or_init(&self, f: impl FnOnce() -> T) -> &T {
24 self.0.get_or_init(f)
25 }
26
27 #[inline]
28 pub fn get_or_try_init<E>(&self, f: impl FnOnce() -> Result<T, E>) -> Result<&T, E> {
29 self.0.get_or_try_init(f)
30 }
31}
32
33impl<T> Default for OnceLock<T> {
34 fn default() -> OnceLock<T> {
35 OnceLock::new()
36 }
37}
38
39#[derive(Debug, Default)]
41pub struct RwLock<T>(std::sync::RwLock<T>);
42
43impl<T> RwLock<T> {
44 #[inline]
45 pub const fn new(val: T) -> RwLock<T> {
46 RwLock(std::sync::RwLock::new(val))
47 }
48
49 #[inline]
50 pub fn read(&self) -> impl Deref<Target = T> + '_ {
51 self.0.read().unwrap()
52 }
53
54 #[inline]
55 pub fn write(&self) -> impl DerefMut<Target = T> + '_ {
56 self.0.write().unwrap()
57 }
58}