Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Abortable::new to accept &mut AbortRegistration #2881

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions futures-util/src/abortable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::task::AtomicWaker;
use alloc::sync::Arc;
use core::borrow::BorrowMut;
use core::fmt;
use core::marker::PhantomData;
use core::pin::Pin;
use core::sync::atomic::{AtomicBool, Ordering};
use futures_core::future::Future;
Expand All @@ -12,14 +14,15 @@ pin_project! {
/// A future/stream which can be remotely short-circuited using an `AbortHandle`.
#[derive(Debug, Clone)]
#[must_use = "futures/streams do nothing unless you poll them"]
pub struct Abortable<T> {
pub struct Abortable<T, R = AbortRegistration> {
#[pin]
task: T,
inner: Arc<AbortInner>,
phantom: PhantomData<R>,
}
}

impl<T> Abortable<T> {
impl<T, R> Abortable<T, R> {
/// Creates a new `Abortable` future/stream using an existing `AbortRegistration`.
/// `AbortRegistration`s can be acquired through `AbortHandle::new`.
///
Expand Down Expand Up @@ -55,8 +58,11 @@ impl<T> Abortable<T> {
/// assert_eq!(stream.next().await, None);
/// # });
/// ```
pub fn new(task: T, reg: AbortRegistration) -> Self {
Self { task, inner: reg.inner }
pub fn new(task: T, mut reg: R) -> Self
where
R: BorrowMut<AbortRegistration>,
{
Self { task, inner: reg.borrow_mut().inner.clone(), phantom: PhantomData }
}

/// Checks whether the task has been aborted. Note that all this
Expand Down Expand Up @@ -129,7 +135,7 @@ impl fmt::Display for Aborted {
#[cfg(feature = "std")]
impl std::error::Error for Aborted {}

impl<T> Abortable<T> {
impl<T, R> Abortable<T, R> {
fn try_poll<I>(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -160,7 +166,7 @@ impl<T> Abortable<T> {
}
}

impl<Fut> Future for Abortable<Fut>
impl<Fut, R> Future for Abortable<Fut, R>
where
Fut: Future,
{
Expand All @@ -171,7 +177,7 @@ where
}
}

impl<St> Stream for Abortable<St>
impl<St, R> Stream for Abortable<St, R>
where
St: Stream,
{
Expand Down
Loading