std_shims/
sync.rs

1pub use core::sync::atomic;
2#[cfg(all(feature = "alloc", not(feature = "std")))]
3pub use extern_alloc::sync::{Arc, Weak};
4#[cfg(feature = "std")]
5pub use std::sync::{Arc, Weak};
6
7mod mutex_shim {
8  #[cfg(not(feature = "std"))]
9  pub use spin::{Mutex, MutexGuard};
10  #[cfg(feature = "std")]
11  pub use std::sync::{Mutex, MutexGuard};
12
13  /// A shimmed `Mutex` with an API mutual to `spin` and `std`.
14  #[derive(Default, Debug)]
15  pub struct ShimMutex<T>(Mutex<T>);
16  impl<T> ShimMutex<T> {
17    /// Construct a new `Mutex`.
18    pub const fn new(value: T) -> Self {
19      Self(Mutex::new(value))
20    }
21
22    /// Acquire a lock on the contents of the `Mutex`.
23    ///
24    /// On no-`std` environments, this may spin until the lock is acquired. On `std` environments,
25    /// this may panic if the `Mutex` was poisoned.
26    pub fn lock(&self) -> MutexGuard<'_, T> {
27      #[cfg(feature = "std")]
28      let res = self.0.lock().unwrap();
29      #[cfg(not(feature = "std"))]
30      let res = self.0.lock();
31      res
32    }
33  }
34}
35pub use mutex_shim::{ShimMutex as Mutex, MutexGuard};
36
37#[rustversion::before(1.80)]
38#[cfg(not(feature = "std"))]
39pub use spin::Lazy as LazyLock;
40#[rustversion::since(1.80)]
41#[cfg(feature = "std")]
42pub use std::sync::LazyLock;