[−][src]Struct maybe_sync::Mutex
Mutex implementation to use in conjunction with MaybeSync
bound.
A type alias to parking_lot::Mutex
when "sync" feature is enabled.
A wrapper type around std::cell::RefCell
when "sync" feature is not enabled.
Example
fn maybe_sends<T: MaybeSend + Debug + 'static>(val: Arc<Mutex<T>>) { #[cfg(feature = "sync")] { // If this code is compiled then `MaybeSend` is alias to `std::marker::Send`, // and `Mutex` is `parking_lot::Mutex`. std::thread::spawn(move || { println!("{:?}", *val.lock()) }); } } // `maybe_sync::Mutex<T>` would always satisfy `MaybeSync` and `MaybeSend` // bounds when `T: MaybeSend`, // even if feature "sync" is enabeld. maybe_sends(Arc::new(Mutex::new(42)));
Implementations
impl<T> Mutex<T>
[src]
impl<T> Mutex<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn lock(&self) -> RefMut<T>
[src]
Acquires a mutex, blocking the current thread until it is able to do so.
This function will block the local thread until it is available to acquire the mutex.
Upon returning, the thread is the only thread with the mutex held.
An RAII guard is returned to allow scoped unlock of the lock.
When the guard goes out of scope, the mutex will be unlocked.
Attempts to lock a mutex in the thread which already holds the lock will result in a deadlock.
pub fn try_lock(&self) -> Option<RefMut<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.
This function does not block.
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.
Trait Implementations
Auto Trait Implementations
impl<T: ?Sized> Send for Mutex<T> where
T: Send,
T: Send,
impl<T> !Sync for Mutex<T>
impl<T: ?Sized> Unpin for Mutex<T> where
T: Unpin,
T: Unpin,
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,
fn borrow_mut(&mut self) -> &mut 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, 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.
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>,