pub struct Transition { /* private fields */ }Expand description
Describes how task execution should continue after a node completes.
Most transitions are created either through NodeId::transitions_with for the linear case or
Transition::fan_out plus FanOutTransition::join_with for branching. Use
Transition::pause and Transition::error when a transition closure needs to control task
execution directly.
§Examples
use swiftide_tasks::{NodeError, Task, TaskRunOutcome, Transition};
let mut task = Task::<i32, i32>::new();
let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
let left = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
let right = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 2) });
let join = task.register_node_fn(
|input: &swiftide_tasks::JoinInput<i32>| -> Result<i32, NodeError> {
Ok(input.iter().copied().sum())
},
);
task.starts_with(start);
task.register_transition(start, move |value| {
Transition::fan_out(&left, value)
.and(&right, value)
.join_with(join.join())
.concurrency_model(swiftide_tasks::ConcurrencyModel::Parallel)
})?;
task.register_transition(left, join.join())?;
task.register_transition(right, join.join())?;
task.register_transition(join, task.transitions_to_finish())?;
assert_eq!(task.run(1).await?, TaskRunOutcome::Completed(5));Implementations§
Source§impl Transition
impl Transition
Sourcepub fn next<T: TaskNode + ?Sized>(
node_id: &NodeId<T>,
context: T::Input,
) -> MarkedTransition<T>
pub fn next<T: TaskNode + ?Sized>( node_id: &NodeId<T>, context: T::Input, ) -> MarkedTransition<T>
Continues execution at node_id with the provided input.
§Examples
use swiftide_tasks::{NodeError, Task, Transition};
let mut task = Task::<i32, i32>::new();
let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
let finish = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input * 2) });
task.starts_with(start);
task.register_transition(start, move |value| Transition::next(&finish, value))?;Sourcepub fn fan_out<T, Input>(
node_id: &NodeId<T>,
context: Input,
) -> FanOutTransition
pub fn fan_out<T, Input>( node_id: &NodeId<T>, context: Input, ) -> FanOutTransition
Builds a fan-out that schedules one or more branches from the current node output.
The returned FanOutTransition must attach an explicit join with
FanOutTransition::join_with before it can be returned from a task transition.
Branches in that fan-out are joined branches, not independent task endings. To express
multiple logical endings, transition each branch to a join node and finish the task from the
join node.
§Examples
use swiftide_tasks::{NodeError, Task, Transition};
let mut task = Task::<i32, i32>::new();
let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
let left = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 1) });
let right = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input + 2) });
task.starts_with(start);
let join = task.register_node_fn(
|input: &swiftide_tasks::JoinInput<i32>| -> Result<i32, NodeError> {
Ok(input.iter().copied().sum())
},
);
task.register_transition(start, move |value| {
Transition::fan_out(&left, value)
.and(&right, value)
.join_with(join.join())
})?;Sourcepub fn pause() -> Self
pub fn pause() -> Self
Pauses the current branch.
§Examples
use swiftide_tasks::{NodeError, Task, TaskRunOutcome, Transition};
let mut task = Task::<i32, ()>::new();
let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
task.starts_with(start);
task.register_transition(start, |_value| Transition::pause())?;
assert_eq!(task.run(1).await?, TaskRunOutcome::Paused);Sourcepub fn error(error: impl Into<Box<dyn Error + Send + Sync>>) -> Self
pub fn error(error: impl Into<Box<dyn Error + Send + Sync>>) -> Self
Fails the current branch with the provided error.
§Examples
use std::io::Error;
use swiftide_tasks::{NodeError, Task, Transition};
let mut task = Task::<i32, i32>::new();
let start = task.register_node_fn(|input: &i32| -> Result<i32, NodeError> { Ok(*input) });
task.starts_with(start);
task.register_transition(start, |_value| Transition::error(Error::other("boom")))?;
assert!(task.run(1).await.is_err());Sourcepub fn concurrency_model(self, concurrency_model: ConcurrencyModel) -> Self
pub fn concurrency_model(self, concurrency_model: ConcurrencyModel) -> Self
Overrides the concurrency model for work scheduled by this transition.
Trait Implementations§
Source§impl Debug for Transition
impl Debug for Transition
Source§impl<To: TaskNode + ?Sized> From<MarkedTransition<To>> for Transition
impl<To: TaskNode + ?Sized> From<MarkedTransition<To>> for Transition
Source§fn from(marked_transition: MarkedTransition<To>) -> Self
fn from(marked_transition: MarkedTransition<To>) -> Self
Auto Trait Implementations§
impl !RefUnwindSafe for Transition
impl !UnwindSafe for Transition
impl Forget for Transition
impl Freeze for Transition
impl Send for Transition
impl Sync for Transition
impl Unpin for Transition
impl UnsafeUnpin for Transition
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more