computation_process/
generatable.rs

1use crate::{Completable, DynGeneratable};
2use cancel_this::Cancellable;
3
4/// An alternative to [`crate::Computable`] which is intended for generators.
5///
6/// The computation is finished once [`Generatable::try_next`] returns `None`.
7pub trait Generatable<T>: Iterator<Item = Cancellable<T>> {
8    /// Try to advance the generator and return the next item.
9    ///
10    /// Returns:
11    /// - `Some(Ok(item))` when an item is available
12    /// - `Some(Err(Incomplete::Suspended))` when the generator needs to yield control
13    /// - `Some(Err(Incomplete::Cancelled(_)))` when the computation was canceled
14    /// - `None` when the generator is exhausted
15    fn try_next(&mut self) -> Option<Completable<T>>;
16
17    /// Utility method to convert this [`Generatable`] to a dynamic type.
18    fn dyn_generatable(self) -> DynGeneratable<T>
19    where
20        Self: Sized + 'static,
21    {
22        Box::new(self)
23    }
24}