use core::ops::{Deref, DerefMut};
cfg_select! {
feature = "parking_lot" => {
type LocalMutex<T> = parking_lot::Mutex<T>;
type LocalMutexGuard<'any, T> = parking_lot::MutexGuard<'any, T>;
}
feature = "std" => {
type LocalMutex<T> = std::sync::Mutex<T>;
type LocalMutexGuard<'any, T> = std::sync::MutexGuard<'any, T>;
}
_ => {
type LocalMutex<T> = crate::sync::NoStdMutex<T>;
type LocalMutexGuard<'any, T> = crate::sync::NoStdMutexGuard<'any, T>;
}
}
#[derive(Debug)]
pub struct SyncMutex<T>(LocalMutex<T>);
impl<T> SyncMutex<T> {
#[inline]
pub const fn new(elem: T) -> Self {
Self(LocalMutex::new(elem))
}
#[inline]
pub fn lock(&self) -> SyncMutexGuard<'_, T> {
cfg_select! {
feature = "parking_lot" => SyncMutexGuard(self.0.lock()),
feature = "std" => {
#[expect(clippy::unwrap_used, clippy::missing_panics_doc, reason = "poison is ignored")]
SyncMutexGuard(self.0.lock().unwrap())
}
_ => SyncMutexGuard(self.0.lock())
}
}
#[inline]
pub fn try_lock(&self) -> Option<SyncMutexGuard<'_, T>> {
cfg_select! {
feature = "parking_lot" => self.0.try_lock().map(SyncMutexGuard),
feature = "std" => self.0.try_lock().ok().map(SyncMutexGuard),
_ => self.0.try_lock().map(SyncMutexGuard)
}
}
}
#[clippy::has_significant_drop]
#[derive(Debug)]
pub struct SyncMutexGuard<'any, T>(LocalMutexGuard<'any, T>);
impl<T> Deref for SyncMutexGuard<'_, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&self.0
}
}
impl<T> DerefMut for SyncMutexGuard<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
}