diff options
| author | 2024-07-10 15:42:38 +0200 | |
|---|---|---|
| committer | 2024-07-10 15:42:38 +0200 | |
| commit | 70f44a6e264f7307531935bf0fb9e33a5ebd81c1 (patch) | |
| tree | 0b5a86af2116fced80f6f5cb20ba62fe5b89d7ba /runtime/src | |
| parent | e86920be5b9984b4eb511e5e69efdcbf6ef3d8e4 (diff) | |
| parent | 47f9554a82e65679c13ef17f3f3bf7fff5156184 (diff) | |
| download | iced-70f44a6e264f7307531935bf0fb9e33a5ebd81c1.tar.gz iced-70f44a6e264f7307531935bf0fb9e33a5ebd81c1.tar.bz2 iced-70f44a6e264f7307531935bf0fb9e33a5ebd81c1.zip  | |
Merge pull request #2496 from iced-rs/abortable-tasks
Abortable `Task`
Diffstat (limited to '')
| -rw-r--r-- | runtime/src/task.rs | 73 | 
1 files changed, 55 insertions, 18 deletions
diff --git a/runtime/src/task.rs b/runtime/src/task.rs index e037c403..b75aca89 100644 --- a/runtime/src/task.rs +++ b/runtime/src/task.rs @@ -55,24 +55,6 @@ impl<T> Task<T> {          Self::stream(stream.map(f))      } -    /// Creates a new [`Task`] that runs the given [`Future`] and produces -    /// its output. -    pub fn future(future: impl Future<Output = T> + MaybeSend + 'static) -> Self -    where -        T: 'static, -    { -        Self::stream(stream::once(future)) -    } - -    /// Creates a new [`Task`] that runs the given [`Stream`] and produces -    /// each of its items. -    pub fn stream(stream: impl Stream<Item = T> + MaybeSend + 'static) -> Self -    where -        T: 'static, -    { -        Self(Some(boxed_stream(stream.map(Action::Output)))) -    } -      /// Combines the given tasks and produces a single [`Task`] that will run all of them      /// in parallel.      pub fn batch(tasks: impl IntoIterator<Item = Self>) -> Self @@ -176,6 +158,61 @@ impl<T> Task<T> {              ))),          }      } + +    /// Creates a new [`Task`] that can be aborted with the returned [`Handle`]. +    pub fn abortable(self) -> (Self, Handle) +    where +        T: 'static, +    { +        match self.0 { +            Some(stream) => { +                let (stream, handle) = stream::abortable(stream); + +                (Self(Some(boxed_stream(stream))), Handle(Some(handle))) +            } +            None => (Self(None), Handle(None)), +        } +    } + +    /// Creates a new [`Task`] that runs the given [`Future`] and produces +    /// its output. +    pub fn future(future: impl Future<Output = T> + MaybeSend + 'static) -> Self +    where +        T: 'static, +    { +        Self::stream(stream::once(future)) +    } + +    /// Creates a new [`Task`] that runs the given [`Stream`] and produces +    /// each of its items. +    pub fn stream(stream: impl Stream<Item = T> + MaybeSend + 'static) -> Self +    where +        T: 'static, +    { +        Self(Some(boxed_stream(stream.map(Action::Output)))) +    } +} + +/// A handle to a [`Task`] that can be used for aborting it. +#[derive(Debug, Clone)] +pub struct Handle(Option<stream::AbortHandle>); + +impl Handle { +    /// Aborts the [`Task`] of this [`Handle`]. +    pub fn abort(&self) { +        if let Some(handle) = &self.0 { +            handle.abort(); +        } +    } + +    /// Returns `true` if the [`Task`] of this [`Handle`] has been aborted. +    pub fn is_aborted(&self) -> bool { +        if let Some(handle) = &self.0 { +            handle.is_aborted() +        } else { +            true +        } +    }  }  impl<T> Task<Option<T>> {  | 
