#![allow(unsafe_code)]
extern crate std;
use std::sync;
use super::RawMutex;
pub struct StdMutex {
inner: sync::Mutex<()>,
}
impl StdMutex {
#[must_use]
pub const fn const_new() -> Self {
Self {
inner: sync::Mutex::new(()),
}
}
}
impl Default for StdMutex {
fn default() -> Self {
Self::const_new()
}
}
impl core::fmt::Debug for StdMutex {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("StdMutex").finish_non_exhaustive()
}
}
unsafe impl RawMutex for StdMutex {
type Guard<'a> = sync::MutexGuard<'a, ()>;
fn new() -> Self {
Self::const_new()
}
#[allow(clippy::expect_used)] fn lock(&self) -> Self::Guard<'_> {
self.inner
.lock()
.expect("surelock: mutex poisoned (a panic occurred in a critical section)")
}
#[allow(clippy::panic)] fn try_lock(&self) -> Option<Self::Guard<'_>> {
match self.inner.try_lock() {
Ok(guard) => Some(guard),
Err(sync::TryLockError::WouldBlock) => None,
Err(sync::TryLockError::Poisoned(_)) => {
panic!("surelock: mutex poisoned (a panic occurred in a critical section)");
}
}
}
}
unsafe impl Send for StdMutex {}
unsafe impl Sync for StdMutex {}