2019-05-29 08:27:53 +00:00
|
|
|
//! Provides utilities to deal with futures, such as a `Cancellable` future.
|
|
|
|
|
2019-08-23 10:22:15 +00:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2019-05-29 08:27:53 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-08-23 10:22:15 +00:00
|
|
|
use std::task::{Context, Poll};
|
2019-05-29 08:27:53 +00:00
|
|
|
|
|
|
|
use failure::Error;
|
2019-08-23 10:22:15 +00:00
|
|
|
use futures::future::FutureExt;
|
2019-05-29 08:27:53 +00:00
|
|
|
|
2019-08-23 10:22:15 +00:00
|
|
|
use crate::tools::async_mutex::{AsyncLockGuard, AsyncMutex, LockFuture};
|
2019-05-29 08:27:53 +00:00
|
|
|
|
|
|
|
/// Make a future cancellable.
|
|
|
|
///
|
|
|
|
/// This simply performs a `select()` on the future and something waiting for a signal. If the
|
|
|
|
/// future finishes successfully, it yields `Some(T::Item)`. If it was cancelled, it'll yield
|
|
|
|
/// `None`.
|
|
|
|
///
|
|
|
|
/// In order to cancel the future, a `Canceller` is used.
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2019-08-23 10:22:15 +00:00
|
|
|
/// # use std::future::Future;
|
2019-05-29 08:27:53 +00:00
|
|
|
/// # use failure::Error;
|
2019-08-23 10:22:15 +00:00
|
|
|
/// # use futures::future::FutureExt;
|
2019-05-29 09:49:54 +00:00
|
|
|
/// # use proxmox_backup::tools::futures::Cancellable;
|
|
|
|
/// # fn doc<T>(future: T) -> Result<(), Error>
|
|
|
|
/// # where
|
2019-08-23 10:22:15 +00:00
|
|
|
/// # T: Future<Output = i32> + Unpin + Send + Sync + 'static,
|
2019-05-29 09:49:54 +00:00
|
|
|
/// # {
|
|
|
|
/// let (future, canceller) = Cancellable::new(future)?;
|
2019-08-23 10:22:15 +00:00
|
|
|
/// tokio::spawn(future.map(|res| {
|
2019-05-29 09:49:54 +00:00
|
|
|
/// match res {
|
|
|
|
/// Some(value) => println!("Future finished with {}", value),
|
|
|
|
/// None => println!("Future was cancelled"),
|
|
|
|
/// }
|
|
|
|
/// }));
|
2019-05-29 08:27:53 +00:00
|
|
|
/// // Do something
|
|
|
|
/// canceller.cancel();
|
2019-05-29 09:49:54 +00:00
|
|
|
/// # Ok(())
|
2019-05-29 08:27:53 +00:00
|
|
|
/// # }
|
|
|
|
/// ```
|
2019-08-23 10:22:15 +00:00
|
|
|
pub struct Cancellable<T: Future + Unpin> {
|
2019-05-29 08:27:53 +00:00
|
|
|
/// Our core: we're waiting on a future, on on a lock. The cancel method just unlocks the
|
|
|
|
/// lock, so that our LockFuture finishes.
|
2019-08-23 10:22:15 +00:00
|
|
|
inner: futures::future::Select<T, LockFuture<()>>,
|
2019-05-29 08:27:53 +00:00
|
|
|
|
|
|
|
/// When this future is created, this holds a guard. When a `Canceller` wants to cancel the
|
|
|
|
/// future, it'll drop this guard, causing our inner future to resolve to `None`.
|
|
|
|
guard: Arc<Mutex<Option<AsyncLockGuard<()>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reference to a cancellable future. Multiple instances may exist simultaneously.
|
|
|
|
///
|
|
|
|
/// This allows cancelling another future. If the future already finished, nothing happens.
|
2019-05-29 09:56:52 +00:00
|
|
|
///
|
|
|
|
/// This can be cloned to be used in multiple places.
|
2019-05-29 08:27:53 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Canceller(Arc<Mutex<Option<AsyncLockGuard<()>>>>);
|
|
|
|
|
|
|
|
impl Canceller {
|
|
|
|
/// Cancel the associated future.
|
|
|
|
///
|
|
|
|
/// This does nothing if the future already finished successfully.
|
|
|
|
pub fn cancel(&self) {
|
|
|
|
*self.0.lock().unwrap() = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 10:22:15 +00:00
|
|
|
impl<T: Future + Unpin> Cancellable<T> {
|
2019-05-29 08:27:53 +00:00
|
|
|
/// Make a future cancellable.
|
|
|
|
///
|
|
|
|
/// Returns a future and a `Canceller` which can be cloned and used later to cancel the future.
|
|
|
|
pub fn new(inner: T) -> Result<(Self, Canceller), Error> {
|
|
|
|
// we don't even need to sture the mutex...
|
|
|
|
let (mutex, guard) = AsyncMutex::new_locked(())?;
|
|
|
|
let this = Self {
|
2019-08-23 10:22:15 +00:00
|
|
|
inner: futures::future::select(inner, mutex.lock()),
|
2019-05-29 08:27:53 +00:00
|
|
|
guard: Arc::new(Mutex::new(Some(guard))),
|
|
|
|
};
|
|
|
|
let canceller = this.canceller();
|
|
|
|
Ok((this, canceller))
|
|
|
|
}
|
|
|
|
|
2019-05-29 09:56:52 +00:00
|
|
|
/// Create another `Canceller` for this future.
|
2019-05-29 08:27:53 +00:00
|
|
|
pub fn canceller(&self) -> Canceller {
|
|
|
|
Canceller(self.guard.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make a future cancellable.
|
|
|
|
///
|
|
|
|
/// This is a shortcut for `Cancellable::new`
|
2019-08-23 10:22:15 +00:00
|
|
|
pub fn cancellable<T: Future + Unpin>(future: T) -> Result<(Cancellable<T>, Canceller), Error> {
|
2019-05-29 08:27:53 +00:00
|
|
|
Cancellable::new(future)
|
|
|
|
}
|
|
|
|
|
2019-08-23 10:22:15 +00:00
|
|
|
impl<T: Future + Unpin> Future for Cancellable<T> {
|
|
|
|
type Output = Option<<T as Future>::Output>;
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
2019-05-29 08:27:53 +00:00
|
|
|
use futures::future::Either;
|
2019-08-23 10:22:15 +00:00
|
|
|
match self.inner.poll_unpin(cx) {
|
|
|
|
Poll::Ready(Either::Left((output, _))) => Poll::Ready(Some(output)),
|
|
|
|
Poll::Ready(Either::Right(_)) => Poll::Ready(None),
|
|
|
|
Poll::Pending => Poll::Pending,
|
2019-05-29 08:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|