pub struct Mutex<T> { /* private fields */ }Expand description
A simple mutex.
Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub fn lock(&self) -> MutexGuard<'_, T>
pub fn lock(&self) -> MutexGuard<'_, T>
Acquires the mutex.
Returns a guard that releases the mutex when dropped.
§Examples
use simple_mutex::Mutex;
let mutex = Mutex::new(10);
let guard = mutex.lock();
assert_eq!(*guard, 10);Sourcepub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
Attempts to acquire the mutex.
If the mutex could not be acquired at this time, then None is returned. Otherwise, a
guard is returned that releases the mutex when dropped.
§Examples
use simple_mutex::Mutex;
let mutex = Mutex::new(10);
if let Some(guard) = mutex.try_lock() {
assert_eq!(*guard, 10);
}Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the mutex, returning the underlying data.
§Examples
use simple_mutex::Mutex;
let mutex = Mutex::new(10);
assert_eq!(mutex.into_inner(), 10);Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying data.
Since this call borrows the mutex mutably, no actual locking takes place – the mutable borrow statically guarantees the mutex is not already acquired.
§Examples
use simple_mutex::Mutex;
let mut mutex = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.lock(), 10);Trait Implementations§
impl<T: Send> Send for Mutex<T>
impl<T: Send> Sync for Mutex<T>
Auto Trait Implementations§
impl<T> !Freeze for Mutex<T>
impl<T> !RefUnwindSafe for Mutex<T>
impl<T> Unpin for Mutex<T>where
T: Unpin,
impl<T> UnwindSafe for Mutex<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more