Skip to content

Commit

Permalink
Merge pull request #2503 from iced-rs/task-abort-on-drop
Browse files Browse the repository at this point in the history
Add `abort_on_drop` to `task::Handle`
  • Loading branch information
hecrj authored Jul 12, 2024
2 parents 65ea5fa + c1c9789 commit be06060
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions runtime/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,21 @@ impl<T> Task<T> {
Some(stream) => {
let (stream, handle) = stream::abortable(stream);

(Self(Some(boxed_stream(stream))), Handle(Some(handle)))
(
Self(Some(boxed_stream(stream))),
Handle {
raw: Some(handle),
abort_on_drop: false,
},
)
}
None => (Self(None), Handle(None)),
None => (
Self(None),
Handle {
raw: None,
abort_on_drop: false,
},
),
}
}

Expand All @@ -195,26 +207,49 @@ impl<T> Task<T> {

/// A handle to a [`Task`] that can be used for aborting it.
#[derive(Debug, Clone)]
pub struct Handle(Option<stream::AbortHandle>);
pub struct Handle {
raw: Option<stream::AbortHandle>,
abort_on_drop: bool,
}

impl Handle {
/// Aborts the [`Task`] of this [`Handle`].
pub fn abort(&self) {
if let Some(handle) = &self.0 {
if let Some(handle) = &self.raw {
handle.abort();
}
}

/// Returns a new [`Handle`] that will call [`Handle::abort`] whenever
/// it is dropped.
///
/// This can be really useful if you do not want to worry about calling
/// [`Handle::abort`] yourself.
pub fn abort_on_drop(mut self) -> Self {
Self {
raw: self.raw.take(),
abort_on_drop: true,
}
}

/// Returns `true` if the [`Task`] of this [`Handle`] has been aborted.
pub fn is_aborted(&self) -> bool {
if let Some(handle) = &self.0 {
if let Some(handle) = &self.raw {
handle.is_aborted()
} else {
true
}
}
}

impl Drop for Handle {
fn drop(&mut self) {
if self.abort_on_drop {
self.abort();
}
}
}

impl<T> Task<Option<T>> {
/// Executes a new [`Task`] after this one, only when it produces `Some` value.
///
Expand Down

0 comments on commit be06060

Please sign in to comment.