pub struct Tasks { /* private fields */ }Expand description
Task set.
This data type represents a set of tasks that can either be consumed through
iteration, or executed recursively via Tasks::execute. Anything returned
by Task::execute must be convertible into Tasks, including a single
task, multiple tasks, and the unit value.
§Examples
use zrx_executor::task::Tasks;
use zrx_executor::Executor;
// Create executor and submit task
let executor = Executor::default();
executor.submit(|| {
println!("Task 1");
// Create subtasks
let mut tasks = Tasks::new();
tasks.add(|| println!("Task 1.1"));
tasks.add(|| println!("Task 1.2"));
tasks.add(|| println!("Task 1.3"));
tasks
})?;Implementations§
Source§impl Tasks
impl Tasks
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a task set.
§Examples
use zrx_executor::task::Tasks;
// Create task set
let tasks = Tasks::new();Sourcepub fn add<T>(&mut self, task: T) -> &mut Selfwhere
T: Task,
pub fn add<T>(&mut self, task: T) -> &mut Selfwhere
T: Task,
Adds a task to the task set.
This method adds a Task to the set, which can either be consumed
via Tasks::into_iter or executed via Tasks::execute.
§Examples
use zrx_executor::task::Tasks;
// Create task set and add tasks
let mut tasks = Tasks::new();
tasks.add(|| println!("Task 1"));
tasks.add(|| println!("Task 2"));
tasks.add(|| println!("Task 3"));Sourcepub fn execute(self)
pub fn execute(self)
Executes all tasks in the task set.
This method executes all tasks recursively in depth-first order, so if a task returns further subtasks, they are executed before all others.
§Examples
use zrx_executor::task::Tasks;
// Create task set and add tasks
let mut tasks = Tasks::new();
tasks.add(|| println!("Task 1"));
tasks.add(|| println!("Task 2"));
tasks.add(|| println!("Task 3"));
// Execute task set
tasks.execute();Trait Implementations§
Source§impl From<()> for Tasks
impl From<()> for Tasks
Source§fn from((): ()) -> Self
fn from((): ()) -> Self
Creates a task set from the unit value.
This implementation makes the API more flexible, as it allows to just return nothing from a task, which is probably the common case.
§Examples
use zrx_executor::task::Tasks;
// Create task set from unit value
let tasks = Tasks::from(());
assert!(tasks.is_empty());Source§impl<T> From<T> for Taskswhere
T: Task,
impl<T> From<T> for Taskswhere
T: Task,
Source§fn from(task: T) -> Self
fn from(task: T) -> Self
Creates a task set from a task.
This implementation creates a task set from a single task, which allows to conveniently return a single closure from a task.
§Examples
use zrx_executor::task::Tasks;
// Create task set from task
let tasks = Tasks::from(|| println!("Task"));
assert_eq!(tasks.len(), 1);Source§impl<I> FromIterator<I> for Taskswhere
I: Task,
impl<I> FromIterator<I> for Taskswhere
I: Task,
Source§fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = I>,
fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = I>,
Creates a task set from an iterator.
§Examples
use zrx_executor::task::Tasks;
// Create task set from iterator
let tasks = Tasks::from_iter([
|| println!("Task 1"),
|| println!("Task 2"),
|| println!("Task 3"),
]);Source§impl IntoIterator for Tasks
impl IntoIterator for Tasks
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates a consuming iterator over the task set.
§Examples
use zrx_executor::task::Tasks;
// Create task set and add tasks
let mut tasks = Tasks::new();
tasks.add(|| println!("Task 1"));
tasks.add(|| println!("Task 2"));
tasks.add(|| println!("Task 3"));
// Create iterator over tasks
for task in tasks {
task.execute();
}