mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-24 00:00:11 +02:00
future: add adapters of CancellationToken for FutureExt (#7475)
This commit is contained in:
@@ -281,34 +281,6 @@ impl CancellationToken {
|
||||
where
|
||||
F: Future,
|
||||
{
|
||||
pin_project! {
|
||||
/// A Future that is resolved once the corresponding [`CancellationToken`]
|
||||
/// is cancelled or a given Future gets resolved. It is biased towards the
|
||||
/// Future completion.
|
||||
#[must_use = "futures do nothing unless polled"]
|
||||
struct RunUntilCancelledFuture<'a, F: Future> {
|
||||
#[pin]
|
||||
cancellation: WaitForCancellationFuture<'a>,
|
||||
#[pin]
|
||||
future: F,
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, F: Future> Future for RunUntilCancelledFuture<'a, F> {
|
||||
type Output = Option<F::Output>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = self.project();
|
||||
if let Poll::Ready(res) = this.future.poll(cx) {
|
||||
Poll::Ready(Some(res))
|
||||
} else if this.cancellation.poll(cx).is_ready() {
|
||||
Poll::Ready(None)
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if self.is_cancelled() {
|
||||
None
|
||||
} else {
|
||||
@@ -439,3 +411,77 @@ impl Future for WaitForCancellationFutureOwned {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
/// A Future that is resolved once the corresponding [`CancellationToken`]
|
||||
/// is cancelled or a given Future gets resolved. It is biased towards the
|
||||
/// Future completion.
|
||||
#[must_use = "futures do nothing unless polled"]
|
||||
pub(crate) struct RunUntilCancelledFuture<'a, F: Future> {
|
||||
#[pin]
|
||||
cancellation: WaitForCancellationFuture<'a>,
|
||||
#[pin]
|
||||
future: F,
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, F: Future> RunUntilCancelledFuture<'a, F> {
|
||||
pub(crate) fn new(cancellation_token: &'a CancellationToken, future: F) -> Self {
|
||||
Self {
|
||||
cancellation: cancellation_token.cancelled(),
|
||||
future,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, F: Future> Future for RunUntilCancelledFuture<'a, F> {
|
||||
type Output = Option<F::Output>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = self.project();
|
||||
if let Poll::Ready(res) = this.future.poll(cx) {
|
||||
Poll::Ready(Some(res))
|
||||
} else if this.cancellation.poll(cx).is_ready() {
|
||||
Poll::Ready(None)
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
/// A Future that is resolved once the corresponding [`CancellationToken`]
|
||||
/// is cancelled or a given Future gets resolved. It is biased towards the
|
||||
/// Future completion.
|
||||
#[must_use = "futures do nothing unless polled"]
|
||||
pub(crate) struct RunUntilCancelledFutureOwned<F: Future> {
|
||||
#[pin]
|
||||
cancellation: WaitForCancellationFutureOwned,
|
||||
#[pin]
|
||||
future: F,
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Future> Future for RunUntilCancelledFutureOwned<F> {
|
||||
type Output = Option<F::Output>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = self.project();
|
||||
if let Poll::Ready(res) = this.future.poll(cx) {
|
||||
Poll::Ready(Some(res))
|
||||
} else if this.cancellation.poll(cx).is_ready() {
|
||||
Poll::Ready(None)
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Future> RunUntilCancelledFutureOwned<F> {
|
||||
pub(crate) fn new(cancellation_token: CancellationToken, future: F) -> Self {
|
||||
Self {
|
||||
cancellation: cancellation_token.cancelled_owned(),
|
||||
future,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ pub use cancellation_token::{
|
||||
guard::DropGuard, guard_ref::DropGuardRef, CancellationToken, WaitForCancellationFuture,
|
||||
WaitForCancellationFutureOwned,
|
||||
};
|
||||
pub(crate) use cancellation_token::{RunUntilCancelledFuture, RunUntilCancelledFutureOwned};
|
||||
|
||||
mod mpsc;
|
||||
pub use mpsc::{PollSendError, PollSender};
|
||||
|
||||
Reference in New Issue
Block a user