[−][src]Struct futures_locks_pre::Mutex
A Futures-aware Mutex.
std::sync::Mutex
cannot be used in an asynchronous environment like Tokio,
because a mutex acquisition can block an entire reactor. This class can be
used instead. It functions much like std::sync::Mutex
. Unlike that
class, it also has a builtin Arc
, making it accessible from multiple
threads. It's also safe to clone
. Also unlike std::sync::Mutex
, this
class does not detect lock poisoning.
Examples
let mtx = Mutex::<u32>::new(0); let fut = mtx.lock().map(|mut guard| { *guard += 5; }); block_on(fut); assert_eq!(mtx.try_unwrap().unwrap(), 5);
Methods
impl<T> Mutex<T>
[src]
pub fn new(t: T) -> Mutex<T>
[src]
Create a new Mutex
in the unlocked state.
pub fn try_unwrap(self) -> Result<T, Mutex<T>>
[src]
Consumes the Mutex
and returns the wrapped data. If the Mutex
still
has multiple references (not necessarily locked), returns a copy of
self
instead.
impl<T: ?Sized> Mutex<T>
[src]
pub fn downgrade(this: &Mutex<T>) -> MutexWeak<T>
[src]
Create a MutexWeak
reference to this Mutex
.
pub fn get_mut(&mut self) -> Option<&mut T>
[src]
Returns a reference to the underlying data, if there are no other
clones of the Mutex
.
Since this call borrows the Mutex
mutably, no actual locking takes
place -- the mutable borrow statically guarantees no locks exist.
However, if the Mutex
has already been cloned, then None
will be
returned instead.
Examples
let mut mtx = Mutex::<u32>::new(0); *mtx.get_mut().unwrap() += 5; assert_eq!(mtx.try_unwrap().unwrap(), 5);
ⓘImportant traits for MutexFut<T>pub fn lock(&self) -> MutexFut<T>
[src]
Acquires a Mutex
, blocking the task in the meantime. When the
returned Future
is ready, this task will have sole access to the
protected data.
pub fn try_lock(&self) -> Result<MutexGuard<T>, ()>
[src]
Attempts to acquire the lock.
If the operation would block, returns Err
instead. Otherwise, returns
a guard (not a Future
).
Examples
let mut mtx = Mutex::<u32>::new(0); match mtx.try_lock() { Ok(mut guard) => *guard += 5, Err(()) => println!("Better luck next time!") };
pub fn ptr_eq(this: &Mutex<T>, other: &Mutex<T>) -> bool
[src]
Returns true if the two Mutex
point to the same data else false.
impl<T: 'static + ?Sized> Mutex<T>
[src]
pub fn with<B, F, R>(&self, f: F) -> impl Future<Output = R> where
F: FnOnce(MutexGuard<T>) -> B + Send + 'static,
B: Future<Output = R> + Send + 'static,
R: Send + 'static,
T: Send,
[src]
F: FnOnce(MutexGuard<T>) -> B + Send + 'static,
B: Future<Output = R> + Send + 'static,
R: Send + 'static,
T: Send,
feature="tokio"
only.Acquires a Mutex
and performs a computation on its guarded value in a
separate task. Returns a Future
containing the result of the
computation.
When using Tokio, this method will often hold the Mutex
for less time
than chaining a computation to lock
. The reason is
that Tokio polls all tasks promptly upon notification. However, Tokio
does not guarantee that it will poll all futures promptly when their
owning task gets notified. So it's best to hold Mutex
es within their
own tasks, lest their continuations get blocked by slow stacked
combinators.
Trait Implementations
impl<T: ?Sized> Clone for Mutex<T>
[src]
impl<T: Debug + ?Sized> Debug for Mutex<T>
[src]
impl<T: Default + ?Sized> Default for Mutex<T>
[src]
impl<T: ?Sized + Send> Send for Mutex<T>
[src]
impl<T: ?Sized + Send> Sync for Mutex<T>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for Mutex<T>
impl<T: ?Sized> Unpin for Mutex<T>
impl<T> !UnwindSafe for Mutex<T>
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> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
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>,