Crate futures_mutex [] [src]

A Mutex for the Future(s)

API is similar to futures::sync::BiLock However, it can be cloned into as many handles as desired.

extern crate futures;
extern crate futures_mutex;

use futures::{Future, Poll, Async};
use futures_mutex::FutMutex;

struct AddTwo {
    lock: FutMutex<usize>
}

impl Future for AddTwo {
    type Item = usize;
    type Error = ();
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.lock.poll_lock() {
            Async::Ready(mut g) => {
                *g += 2;
                Ok(Async::Ready(*g))
            },
            Async::NotReady => Ok(Async::NotReady)
        }
    }
}

fn main() {
    let lock1: FutMutex<usize> = FutMutex::new(0);
    let lock2 = lock1.clone();

    let future = AddTwo { lock: lock2 };

    // This future will return the current value and the recovered lock.
    let used_lock = lock1.lock().map(|b| (*b, b.unlock()));

    let _ = future.join(used_lock).map(|(add_two, (value, _))| {
        assert_eq!(add_two, value);
    }).wait().unwrap();
}

Structs

FutMutex

A Mutex designed for use inside Futures. Works like BiLock<T> from the futures crate, but with more than 2 handles.

FutMutexAcquire

Future returned by FutMutex::lock which resolves to a guard when a lock is acquired.

FutMutexAcquired

Resolved value of FutMutexAcquire<T> future

FutMutexGuard

Returned RAII guard from the poll_lock method.