rustls/lock.rs
1pub use std_lock::*;
2
3mod std_lock {
4 pub use std::sync::MutexGuard;
5 use std::sync::{LockResult, Mutex as StdMutex};
6
7 /// A wrapper around [`std::sync::Mutex`].
8 #[derive(Debug)]
9 pub struct Mutex<T> {
10 inner: StdMutex<T>,
11 }
12
13 impl<T> Mutex<T> {
14 /// Creates a new mutex in an unlocked state ready for use.
15 pub fn new(data: T) -> Self {
16 Self {
17 inner: StdMutex::new(data),
18 }
19 }
20
21 /// Acquires the mutex, blocking the current thread until it is able to do so.
22 ///
23 /// This will return `None` in the case the mutex is poisoned.
24 #[inline]
25 pub fn lock(&self) -> Option<MutexGuard<'_, T>> {
26 self.inner.lock().ok()
27 }
28
29 /// Consumes the mutex, returning the underlying data.
30 pub fn into_inner(self) -> LockResult<T> {
31 self.inner.into_inner()
32 }
33 }
34}