diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-10 13:11:11 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-10 13:11:11 -0600 |
| commit | 01959b16a21b22b5df5f16569c2a8e8f92beecef (patch) | |
| tree | 32afa5d747c5466345c59ec52161a7cba3d6d755 /vendor/rustls/src/lock.rs | |
| parent | ff30574117a996df332e23d1fb6f65259b316b5b (diff) | |
chore: vendor dependencies
Diffstat (limited to 'vendor/rustls/src/lock.rs')
| -rw-r--r-- | vendor/rustls/src/lock.rs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/vendor/rustls/src/lock.rs b/vendor/rustls/src/lock.rs new file mode 100644 index 00000000..b632b2c5 --- /dev/null +++ b/vendor/rustls/src/lock.rs @@ -0,0 +1,89 @@ +#[cfg(not(feature = "std"))] +pub use no_std_lock::*; +#[cfg(feature = "std")] +pub use std_lock::*; + +#[cfg(feature = "std")] +mod std_lock { + use std::sync::Mutex as StdMutex; + pub use std::sync::MutexGuard; + + /// A wrapper around [`std::sync::Mutex`]. + #[derive(Debug)] + pub struct Mutex<T> { + inner: StdMutex<T>, + } + + impl<T> Mutex<T> { + /// Creates a new mutex in an unlocked state ready for use. + pub fn new(data: T) -> Self { + Self { + inner: StdMutex::new(data), + } + } + + /// Acquires the mutex, blocking the current thread until it is able to do so. + /// + /// This will return `None` in the case the mutex is poisoned. + #[inline] + pub fn lock(&self) -> Option<MutexGuard<'_, T>> { + self.inner.lock().ok() + } + } +} + +#[cfg(not(feature = "std"))] +mod no_std_lock { + use alloc::boxed::Box; + use core::fmt::Debug; + use core::ops::DerefMut; + + use crate::sync::Arc; + + /// A no-std compatible wrapper around [`Lock`]. + #[derive(Debug)] + pub struct Mutex<T> { + inner: Arc<dyn Lock<T>>, + } + + impl<T: Send + 'static> Mutex<T> { + /// Creates a new mutex in an unlocked state ready for use. + pub fn new<M>(val: T) -> Self + where + M: MakeMutex, + T: Send + 'static, + { + Self { + inner: M::make_mutex(val), + } + } + + /// Acquires the mutex, blocking the current thread until it is able to do so. + /// + /// This will return `None` in the case the mutex is poisoned. + #[inline] + pub fn lock(&self) -> Option<MutexGuard<'_, T>> { + self.inner.lock().ok() + } + } + + /// A lock protecting shared data. + pub trait Lock<T>: Debug + Send + Sync { + /// Acquire the lock. + fn lock(&self) -> Result<MutexGuard<'_, T>, Poisoned>; + } + + /// A lock builder. + pub trait MakeMutex { + /// Create a new mutex. + fn make_mutex<T>(value: T) -> Arc<dyn Lock<T>> + where + T: Send + 'static; + } + + /// A no-std compatible mutex guard. + pub type MutexGuard<'a, T> = Box<dyn DerefMut<Target = T> + 'a>; + + /// A marker type used to indicate `Lock::lock` failed due to a poisoned lock. + pub struct Poisoned; +} |
