#![allow(unsafe_code)]
use super::RawMutex;
pub struct LockApiGuard<'a, R: lock_api::RawMutex>(&'a R);
impl<R: lock_api::RawMutex> core::fmt::Debug for LockApiGuard<'_, R> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("LockApiGuard").finish_non_exhaustive()
}
}
impl<R: lock_api::RawMutex> Drop for LockApiGuard<'_, R> {
fn drop(&mut self) {
unsafe {
self.0.unlock();
}
}
}
unsafe impl<R: lock_api::RawMutex> RawMutex for R {
type Guard<'a>
= LockApiGuard<'a, R>
where
R: 'a;
fn new() -> Self {
R::INIT
}
fn lock(&self) -> Self::Guard<'_> {
lock_api::RawMutex::lock(self);
LockApiGuard(self)
}
fn try_lock(&self) -> Option<Self::Guard<'_>> {
if lock_api::RawMutex::try_lock(self) {
Some(LockApiGuard(self))
} else {
None
}
}
}