pub struct Mutex<T>(/* private fields */);
Expand description
A mutex that implements async I/O traits.
This is a blocking mutex that adds the following impls:
impl<T> AsyncRead for Mutex<T> where T: AsyncRead + Unpin {}
impl<T> AsyncRead for &Mutex<T> where T: AsyncRead + Unpin {}
impl<T> AsyncWrite for Mutex<T> where T: AsyncWrite + Unpin {}
impl<T> AsyncWrite for &Mutex<T> where T: AsyncWrite + Unpin {}
impl<T> AsyncSeek for Mutex<T> where T: AsyncSeek + Unpin {}
impl<T> AsyncSeek for &Mutex<T> where T: AsyncSeek + Unpin {}
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, blocking the current thread until it is able to do so.
Returns a guard that releases the mutex when dropped.
Examples
use async_dup::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>>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the mutex, returning the underlying data.
Examples
use async_dup::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 async_dup::Mutex;
let mut mutex = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.lock(), 10);
Trait Implementations§
source§impl<T: AsyncRead + Unpin> AsyncRead for &Mutex<T>
impl<T: AsyncRead + Unpin> AsyncRead for &Mutex<T>
source§impl<T: AsyncRead + Unpin> AsyncRead for Mutex<T>
impl<T: AsyncRead + Unpin> AsyncRead for Mutex<T>
source§impl<T: AsyncWrite + Unpin> AsyncWrite for &Mutex<T>
impl<T: AsyncWrite + Unpin> AsyncWrite for &Mutex<T>
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
Attempt to write bytes from
buf
into the object. Read moresource§fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize>>
fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize>>
Attempt to write bytes from
bufs
into the object using vectored
IO operations. Read moresource§impl<T: AsyncWrite + Unpin> AsyncWrite for Mutex<T>
impl<T: AsyncWrite + Unpin> AsyncWrite for Mutex<T>
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
Attempt to write bytes from
buf
into the object. Read moresource§fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize>>
fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize>>
Attempt to write bytes from
bufs
into the object using vectored
IO operations. Read moreAuto Trait Implementations§
impl<T> !RefUnwindSafe for Mutex<T>
impl<T> Send for Mutex<T>where
T: Send,
impl<T> Sync for Mutex<T>where
T: Send,
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