[−][src]Struct piper::Mutex
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 {}
This mutex is ensures fairness by handling lock operations in the first-in first-out order.
While primarily designed for wrapping async I/O objects, this mutex can also be used as a
regular blocking mutex. It's not quite as efficient as parking_lot::Mutex
, but it's still
an improvement over std::sync::Mutex
.
Examples
use futures::io; use futures::prelude::*; use piper::Mutex; // Reads data from a stream and echoes it back. async fn echo(stream: impl AsyncRead + AsyncWrite + Unpin) -> io::Result<u64> { let stream = Mutex::new(stream); io::copy(&stream, &mut &stream).await }
Implementations
impl<T> Mutex<T>
[src]
pub fn new(data: T) -> Mutex<T>
[src]
pub fn lock(&self) -> MutexGuard<T>
[src]
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 piper::Mutex; let mutex = Mutex::new(10); let guard = mutex.lock(); assert_eq!(*guard, 10);
pub fn try_lock(&self) -> Option<MutexGuard<T>>
[src]
Attempts to acquire the mutex.
If the mutex could not be acquired at this time, then None
is returned. Otherwise, a
guard is returned that releases the mutex when dropped.
Examples
use piper::Mutex; let mutex = Mutex::new(10); if let Ok(guard) = mutex.try_lock() { assert_eq!(*guard, 10); }
pub fn into_inner(self) -> T
[src]
Consumes the mutex, returning the underlying data.
Examples
use piper::Mutex; let mutex = Mutex::new(10); assert_eq!(mutex.into_inner(), 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 takes place -- the mutable borrow statically guarantees the mutex is not already acquired.
Examples
use piper::Mutex; let mut mutex = Mutex::new(0); *mutex.get_mut() = 10; assert_eq!(*mutex.lock(), 10);
Trait Implementations
impl<T: AsyncRead + Unpin> AsyncRead for Mutex<T>
[src]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
impl<'_, T: AsyncRead + Unpin> AsyncRead for &'_ Mutex<T>
[src]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8]
) -> Poll<Result<usize>>
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [IoSliceMut]
) -> Poll<Result<usize>>
impl<T: AsyncWrite + Unpin> AsyncWrite for Mutex<T>
[src]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
impl<'_, T: AsyncWrite + Unpin> AsyncWrite for &'_ Mutex<T>
[src]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize>>
[src]
self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[IoSlice]
) -> Poll<Result<usize>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<()>>
[src]
impl<T: Debug> Debug for Mutex<T>
[src]
impl<T: Default> Default for Mutex<T>
[src]
impl<T> From<T> for Mutex<T>
[src]
impl<T: Send> Send for Mutex<T>
[src]
impl<T: Send> Sync for Mutex<T>
[src]
Auto Trait Implementations
impl<T> !RefUnwindSafe for Mutex<T>
impl<T> Unpin for Mutex<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for Mutex<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
[src]
R: AsyncRead + ?Sized,
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
[src]
R: AsyncRead,
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn read_to_string(&'a mut self, buf: &'a mut String) -> ReadToString<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>) where
Self: AsyncWrite,
[src]
Self: AsyncWrite,
fn take(self, limit: u64) -> Take<Self>
[src]
impl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
[src]
W: AsyncWrite + ?Sized,
fn flush(&mut self) -> Flush<Self> where
Self: Unpin,
[src]
Self: Unpin,
fn close(&mut self) -> Close<Self> where
Self: Unpin,
[src]
Self: Unpin,
fn write(&'a mut self, buf: &'a [u8]) -> Write<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self> where
Self: Unpin,
fn write_all(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
fn into_sink<Item>(self) -> IntoSink<Self, Item> where
Item: AsRef<[u8]>,
[src]
Item: AsRef<[u8]>,
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<!> 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, 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>,