1#[cfg(not(feature = "std"))]
2pub use no_std_lock::*;
3#[cfg(feature = "std")]
4pub use std_lock::*;
5
6#[cfg(feature = "std")]
7mod std_lock {
8 use std::sync::Mutex as StdMutex;
9 pub use std::sync::MutexGuard;
10
11 #[derive(Debug)]
13 pub struct Mutex<T> {
14 inner: StdMutex<T>,
15 }
16
17 impl<T> Mutex<T> {
18 pub fn new(data: T) -> Self {
20 Self {
21 inner: StdMutex::new(data),
22 }
23 }
24
25 #[inline]
29 pub fn lock(&self) -> Option<MutexGuard<'_, T>> {
30 self.inner.lock().ok()
31 }
32 }
33}
34
35#[cfg(not(feature = "std"))]
36mod no_std_lock {
37 use alloc::boxed::Box;
38 use core::fmt::Debug;
39 use core::ops::DerefMut;
40
41 use crate::sync::Arc;
42
43 #[derive(Debug)]
45 pub struct Mutex<T> {
46 inner: Arc<dyn Lock<T>>,
47 }
48
49 impl<T: Send + 'static> Mutex<T> {
50 pub fn new<M>(val: T) -> Self
52 where
53 M: MakeMutex,
54 T: Send + 'static,
55 {
56 Self {
57 inner: M::make_mutex(val),
58 }
59 }
60
61 #[inline]
65 pub fn lock(&self) -> Option<MutexGuard<'_, T>> {
66 self.inner.lock().ok()
67 }
68 }
69
70 pub trait Lock<T>: Debug + Send + Sync {
72 fn lock(&self) -> Result<MutexGuard<'_, T>, Poisoned>;
74 }
75
76 pub trait MakeMutex {
78 fn make_mutex<T>(value: T) -> Arc<dyn Lock<T>>
80 where
81 T: Send + 'static;
82 }
83
84 pub type MutexGuard<'a, T> = Box<dyn DerefMut<Target = T> + 'a>;
86
87 pub struct Poisoned;
89}