Skip to main content

Transition

Struct Transition 

Source
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

Source

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))?;
Source

pub fn fan_out<T, Input>( node_id: &NodeId<T>, context: Input, ) -> FanOutTransition
where T: TaskNode + ?Sized, Input: Into<T::Input>,

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())
})?;
Source

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);
Source

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());
Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<To: TaskNode + ?Sized> From<MarkedTransition<To>> for Transition

Source§

fn from(marked_transition: MarkedTransition<To>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more