#future #mutex

yanked futures-mutex

A Mutex for the Future(s)

Uses old Rust 2015

0.2.1 Jan 5, 2018
0.2.0 Jul 23, 2017
0.1.2 Jun 16, 2017
0.1.1 Apr 16, 2017
0.1.0 Apr 16, 2017

#154 in #mutex


Used in tokio-mqttc

Apache-2.0

14KB
238 lines

futures-mutex [DEPRECATED]

I will no longer be supporting this project. It is no longer used by myself and it has significant issues relating to deadlocks. A deprecation warning will appear in Rust stating this.

A Mutex for the Future(s)

Crates.io Docs.rs

Usage

Add this to your Cargo.toml:

[dependencies]
futures-mutex = "0.2.1"

Then, add this to your crate:

extern crate futures_mutex;

FutMutex<T> follows a very similar API to futures::sync::BiLock, however it can have more than two handles.

License

futures-mutex is distributed under the Apache License v2.0. See the LICENSE file for the full text of the license.


lib.rs:

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();
}

Dependencies

~68KB