From c2d4b75988bfeff20eb49e40e618d73732af2f98 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Fri, 5 Jan 2018 11:58:27 +0100 Subject: [PATCH] Remove Mutex::into_lock --- futures-mutex/src/lib.rs | 118 +-------------------------------------- 1 file changed, 2 insertions(+), 116 deletions(-) diff --git a/futures-mutex/src/lib.rs b/futures-mutex/src/lib.rs index 0aee079a..e146e4a3 100644 --- a/futures-mutex/src/lib.rs +++ b/futures-mutex/src/lib.rs @@ -3,45 +3,6 @@ //! API is similar to [`futures::sync::BiLock`](https://docs.rs/futures/0.1.11/futures/sync/struct.BiLock.html) //! 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::Mutex; -//! -//! struct AddTwo { -//! lock: Mutex -//! } -//! -//! impl Future for AddTwo { -//! type Item = usize; -//! type Error = (); -//! fn poll(&mut self) -> Poll { -//! match self.lock.poll_lock() { -//! Async::Ready(mut g) => { -//! *g += 2; -//! Ok(Async::Ready(*g)) -//! }, -//! Async::NotReady => Ok(Async::NotReady) -//! } -//! } -//! } -//! -//! fn main() { -//! let lock1: Mutex = Mutex::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.into_lock().map(|b| (*b, b.unlock())); -//! -//! let _ = future.join(used_lock).map(|(add_two, (value, _))| { -//! assert_eq!(add_two, value); -//! }).wait().unwrap(); -//! } -//! ``` extern crate futures; extern crate crossbeam; @@ -131,17 +92,6 @@ impl Mutex { } } - /// Convert this lock into a future that resolves to a guard that allows access to the data. - /// This function returns `MutexAcquire`, which resolves to a `MutexGuard` - /// guard type. - /// - /// The returned future will never return an error. - pub fn into_lock(self) -> MutexIntoAcquire { - MutexIntoAcquire { - inner: self - } - } - /// We unlock the mutex and wait for someone to lock. We try and unpark as many tasks as we /// can to prevents dead tasks from deadlocking the mutex, or tasks that have finished their /// critical section and were awakened. @@ -211,66 +161,6 @@ impl<'a, T> Future for MutexAcquire<'a, T> { } } -/// Future returned by `FutMutex::lock` which resolves to a guard when a lock is acquired. -#[derive(Debug)] -pub struct MutexIntoAcquire { - inner: Mutex -} - -impl Future for MutexIntoAcquire { - type Item = MutexAcquired; - type Error = (); - - fn poll(&mut self) -> Poll { - match self.inner.poll_lock() { - Async::Ready(r) => { - mem::forget(r); - Ok(MutexAcquired { - inner: Mutex{ inner: self.inner.inner.clone() } - }.into()) - }, - Async::NotReady => Ok(Async::NotReady) - } - } -} - -#[derive(Debug)] -/// Resolved value of `FutMutexAcquire` future -/// -/// This value works like `FutMutexGuard`, providing a RAII guard to the value `T` through -/// `Deref` and `DerefMut`. Will unlock the lock when dropped; the original `FutMutex` can be -/// recovered with `unlock()`. -pub struct MutexAcquired { - inner: Mutex -} - -impl MutexAcquired { - pub fn unlock(self) -> Mutex { - Mutex { - inner: self.inner.inner.clone() - } - } -} - -impl Deref for MutexAcquired { - type Target = T; - fn deref(&self) -> &Self::Target { - unsafe { &*self.inner.inner.data.get() } - } -} - -impl DerefMut for MutexAcquired { - fn deref_mut(&mut self) -> &mut T { - unsafe { &mut *self.inner.inner.data.get() } - } -} - -impl Drop for MutexAcquired { - fn drop(&mut self) { - self.inner.unlock(); - } -} - #[cfg(test)] mod tests { use super::*; @@ -337,12 +227,7 @@ mod tests { a: Some(lock1), remaining: N, }; - let b = stream::iter_ok::<_, ()>(0..N).fold(lock2, |b, _n| { - b.into_lock().map(|mut b| { - *b += 1; - b.unlock() - }) - }); + let b = a.clone(); let t1 = thread::spawn(move || a.wait()); let b = b.wait().expect("b error"); @@ -357,6 +242,7 @@ mod tests { Async::NotReady => panic!("poll not ready"), } + #[derive(Clone)] struct Increment { remaining: usize, a: Option>,