generated from opensound-org/template-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (future): Introduce
FutureExt::with_cancel_signal()
and `WithCanc…
…elSignal` Future
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
/// A `Future` that can `select` whether a `Future` is successfully completed or cancelled | ||
/// by a cancellation signal. | ||
/// | ||
/// Use [`FutureExt::with_cancel_signal`] to construct. | ||
/// | ||
/// If the execution of the original `Future` is completed, `.await` will resolve to `Ok` | ||
/// with the `Output` of the original `Future`; otherwise (if cancelled by the cancellation | ||
/// signal halfway), `.await` will resolve to `Err` with the `Output` of the cancellation | ||
/// signal `Future`. | ||
/// | ||
/// Note: This `Future` will acquire the ownership of these two `Future`s, and [`Box::pin`] | ||
/// them. This allows the two `Future`s to be arbitrary, including those that are not | ||
/// [`Unpin`] (such as those generated by the `async block`). | ||
#[derive(Debug)] | ||
pub struct WithCancelSignal<F: Future, C: Future> { | ||
future: Pin<Box<F>>, | ||
cancel: Pin<Box<C>>, | ||
} | ||
|
||
impl<F, C> Future for WithCancelSignal<F, C> | ||
where | ||
F: Future, | ||
C: Future, | ||
{ | ||
type Output = Result<F::Output, C::Output>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
if let Poll::Ready(o) = Pin::new(&mut self.future).poll(cx) { | ||
return Poll::Ready(Ok(o)); | ||
} | ||
|
||
if let Poll::Ready(o) = Pin::new(&mut self.cancel).poll(cx) { | ||
return Poll::Ready(Err(o)); | ||
} | ||
|
||
Poll::Pending | ||
} | ||
} | ||
|
||
/// [`Future`] extension trait. | ||
/// | ||
/// This trait has been implemented for all [`Sized`] `Future`s. | ||
pub trait FutureExt: Future + Sized { | ||
/// Construct a [`WithCancelSignal`] Future that can be selected and resolved to | ||
/// [`Result`] according to "whether it is cancelled by a cancellation signal". | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use est::future::FutureExt; | ||
/// use std::time::Duration; | ||
/// use tokio::time::sleep; | ||
/// | ||
/// #[tokio::main] | ||
/// async fn main() { | ||
/// let future = sleep(Duration::from_millis(100)); | ||
/// let cancel = sleep(Duration::from_millis(50)); | ||
/// assert!(future.with_cancel_signal(cancel).await.is_err()); | ||
/// | ||
/// let future = sleep(Duration::from_millis(100)); | ||
/// let cancel = sleep(Duration::from_millis(200)); | ||
/// assert!(future.with_cancel_signal(cancel).await.is_ok()); | ||
/// } | ||
/// ``` | ||
fn with_cancel_signal<C: Future>(self, cancel: C) -> WithCancelSignal<Self, C> { | ||
WithCancelSignal { | ||
future: Box::pin(self), | ||
cancel: Box::pin(cancel), | ||
} | ||
} | ||
} | ||
|
||
impl<T: Future + Sized> FutureExt for T {} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn with_cancel_signal() { | ||
use std::time::Duration; | ||
use tokio::time::sleep; | ||
|
||
let cancel = async move { sleep(Duration::from_millis(100)).await }; | ||
let future = async move { sleep(Duration::from_millis(200)).await }; | ||
assert!(future.with_cancel_signal(cancel).await.is_err()); | ||
|
||
let cancel = async move { sleep(Duration::from_millis(100)).await }; | ||
let future = async move { sleep(Duration::from_millis(50)).await }; | ||
assert!(future.with_cancel_signal(cancel).await.is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters