[−][src]Struct drone_core::sync::Mutex
A mutual exclusion primitive useful for protecting shared data.
The mutex can be statically initialized or created via a Mutex::new
constructor. Each mutex has a type parameter which represents the data that
it is protecting. The data can only be accessed through the RAII guard
returned from Mutex::try_lock
, which guarantees that the data is only
ever accessed when the mutex is locked.
Implementations
impl<T> Mutex<T>
[src]
pub const fn new(data: T) -> Self
[src]
Creates a new mutex in an unlocked state ready for use.
Examples
use drone_core::sync::Mutex; let mutex = Mutex::new(0);
pub fn into_inner(self) -> T
[src]
Consumes this mutex, returning the underlying data.
Examples
use drone_core::sync::Mutex; let mutex = Mutex::new(0); assert_eq!(mutex.into_inner(), 0);
impl<T: ?Sized> Mutex<T>
[src]
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
[src]
Attempts to acquire this lock.
If the lock could not be acquired at this time, then None
is returned.
Otherwise, an RAII guard is returned. The lock will be unlocked when the
guard is dropped.
Examples
use drone_core::sync::Mutex; use std::{sync::Arc, thread}; let mutex = Arc::new(Mutex::new(0)); let c_mutex = Arc::clone(&mutex); thread::spawn(move || { let mut lock = c_mutex.try_lock(); if let Some(ref mut mutex) = lock { **mutex = 10; } else { println!("try_lock failed"); } }) .join() .expect("thread::spawn failed"); assert_eq!(*mutex.try_lock().unwrap(), 10);
pub fn get_mut(&mut self) -> &mut T
[src]
Returns a mutable reference to the underlying data.
Since this call borrows the Mutex
mutably, no actual locking needs to
take place -- the mutable borrow statically guarantees no locks exist.
Examples
use drone_core::sync::Mutex; let mut mutex = Mutex::new(0); *mutex.get_mut() = 10; assert_eq!(*mutex.try_lock().unwrap(), 10);
Trait Implementations
impl<T: ?Sized + Debug> Debug for Mutex<T>
[src]
impl<T: ?Sized + Default> Default for Mutex<T>
[src]
impl<T> From<T> for Mutex<T>
[src]
pub fn from(data: T) -> Self
[src]
Creates a new mutex in an unlocked state ready for use. This is
equivalent to Mutex::new
.
impl<T: ?Sized + Send> Send for Mutex<T>
[src]
impl<T: ?Sized + Send> Sync for Mutex<T>
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<!> for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
[src]
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,