[][src]Struct piper::Mutex

pub struct Mutex<T> { /* fields omitted */ }

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]

Creates a new mutex.

Examples

use piper::Mutex;

let mutex = Mutex::new(10);

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]

impl<'_, T: AsyncRead + Unpin> AsyncRead for &'_ Mutex<T>[src]

impl<T: AsyncWrite + Unpin> AsyncWrite for Mutex<T>[src]

impl<'_, T: AsyncWrite + Unpin> AsyncWrite for &'_ Mutex<T>[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

impl<T> UnwindSafe for Mutex<T> where
    T: UnwindSafe

Blanket Implementations

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

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized
[src]

impl<W> AsyncWriteExt for W where
    W: AsyncWrite + ?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<!> for T[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.