Improve StopFlag's implementation

This commit is contained in:
Maybe Waffle 2022-09-09 21:39:39 +04:00
parent 9527f82608
commit 0807eb57e1

View file

@ -5,7 +5,7 @@
//! [flag]: StopFlag //! [flag]: StopFlag
//! [listeners]: crate::dispatching::update_listeners //! [listeners]: crate::dispatching::update_listeners
use std::{future::Future, pin::Pin, task}; use std::{convert::Infallible, future::Future, pin::Pin, task};
use futures::future::{pending, AbortHandle, Abortable, Pending}; use futures::future::{pending, AbortHandle, Abortable, Pending};
@ -31,7 +31,7 @@ pub struct StopToken(AbortHandle);
/// [`is_stopped`]: StopFlag::is_stopped /// [`is_stopped`]: StopFlag::is_stopped
#[pin_project::pin_project] #[pin_project::pin_project]
#[derive(Clone)] #[derive(Clone)]
pub struct StopFlag(#[pin] Abortable<Pending<()>>); pub struct StopFlag(#[pin] Abortable<Pending<Infallible>>);
impl StopToken { impl StopToken {
/// "Stops" the flag associated with this token. /// "Stops" the flag associated with this token.
@ -56,12 +56,9 @@ impl Future for StopFlag {
type Output = (); type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
self.project().0.poll(cx).map(|res| { self.project().0.poll(cx).map(|res| match res {
debug_assert!( Err(_aborted) => (),
res.is_err(), Ok(unreachable) => match unreachable {},
"Pending Future can't ever be resolved, so Abortable is only resolved when \
canceled"
);
}) })
} }
} }