[][src]Struct maybe_sync::Mutex

#[repr(transparent)]pub struct Mutex<T: ?Sized> { /* fields omitted */ }

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]

pub fn new(value: T) -> Self[src]

Creates a new mutex in an unlocked state ready for use.

impl<T> Mutex<T> where
    T: ?Sized
[src]

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

impl<T: Debug + ?Sized> Debug for Mutex<T>[src]

impl<T: Default + ?Sized> Default for Mutex<T>[src]

Auto Trait Implementations

impl<T: ?Sized> Send for Mutex<T> where
    T: Send

impl<T> !Sync for Mutex<T>

impl<T: ?Sized> Unpin for Mutex<T> where
    T: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.