std_shims/
sync.rs

1pub use core::sync::*;
2pub use alloc::sync::*;
3
4mod mutex_shim {
5  #[cfg(feature = "std")]
6  pub use std::sync::*;
7  #[cfg(not(feature = "std"))]
8  pub use spin::*;
9
10  #[derive(Default, Debug)]
11  pub struct ShimMutex<T>(Mutex<T>);
12  impl<T> ShimMutex<T> {
13    pub const fn new(value: T) -> Self {
14      Self(Mutex::new(value))
15    }
16
17    pub fn lock(&self) -> MutexGuard<'_, T> {
18      #[cfg(feature = "std")]
19      let res = self.0.lock().unwrap();
20      #[cfg(not(feature = "std"))]
21      let res = self.0.lock();
22      res
23    }
24  }
25}
26pub use mutex_shim::{ShimMutex as Mutex, MutexGuard};
27
28#[cfg(feature = "std")]
29pub use std::sync::OnceLock;
30#[cfg(not(feature = "std"))]
31pub use spin::Once as OnceLock;
32
33#[rustversion::before(1.80)]
34mod before_1_80_lazylock {
35  use core::ops::Deref;
36  use super::{Mutex, OnceLock};
37
38  /// Shim for `std::sync::LazyLock`.
39  pub struct LazyLock<T, F = fn() -> T> {
40    f: Mutex<Option<F>>,
41    once: OnceLock<T>,
42  }
43  impl<T, F: FnOnce() -> T> LazyLock<T, F> {
44    /// Shim for `std::sync::LazyLock::new`.
45    pub const fn new(f: F) -> Self {
46      Self { f: Mutex::new(Some(f)), once: OnceLock::new() }
47    }
48    /// Shim for `std::sync::LazyLock::get_or_init`.
49    pub fn get(&self) -> &T {
50      // Since this initializer will only be called once, the value in the Mutex will be `Some`
51      self.once.get_or_init(|| (self.f.lock().take().unwrap())())
52    }
53  }
54  impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
55    type Target = T;
56    fn deref(&self) -> &T {
57      self.get()
58    }
59  }
60}
61#[rustversion::before(1.80)]
62pub use before_1_80_lazylock::LazyLock;
63
64#[rustversion::since(1.80)]
65pub use std::sync::LazyLock;