Struct task_kit::task::Task
[−]
[src]
pub struct Task<'a, T = (), E = ()> { /* fields omitted */ }
Tasks can be used to execute code in Task Kit's runner thread pool. This is the key primive of this crate. It can be used to build and organize asyncronous code paths.
For more on how to use tasks see the crate documentation.
Examples
Polling example:
let mut i = 0; let task: Task<u32, ()> = Task::new(move || { if i < 100 { i += 1; Pending } else { Resolve(i) } });
Long running example:
let task: Task<u32, ()> = Task::with(move || { let mut i = 0; while i < 100 { i += 1; } i });
Methods
impl<'a, T, E> Task<'a, T, E> where
T: 'a,
E: 'a,
[src]
T: 'a,
E: 'a,
fn new<F>(task: F) -> Self where
F: FnMut() -> State<T, E> + 'a,
[src]
F: FnMut() -> State<T, E> + 'a,
Create a new task from a closure returning a State
Provide the new function a closure that contains the logic you wish
to execute asyncronously. This closure will be executed upon the thread
pool within the runner until your closure returns an instance of
State::Resolve
containing a value or an instance of State::Reject
containing an error value.
Arguments
task
- A closure containing code to be executed asyncronously by the runner.
Examples
let task: Task<String, ()> = Task::new(|| Resolve(do_something_blocking()));
fn from(val: T) -> Self
[src]
Create a new task from a value.
Useful only in cases where you need to pass a task to something, but already have the value you wish to resolve.
Arguments
val
- The value you'd like the task to resolve
Examples
let task: Task<String, ()> = Task::from(my_string);
fn with<F>(with: F) -> Self where
F: FnMut() -> T + 'a,
[src]
F: FnMut() -> T + 'a,
Create a new task from a closure returning a value.
The closure will only be executed once by the runner, and is expected to return the value you wish to resolve.
Arguments
with
- A closure that will return the value you'd like the task to resolve.
Examples
let task: Task<String, ()> = Task::with(|| do_something_blocking());
fn join<U>(self, task: Task<'a, U, E>) -> Task<'a, (T, U), E> where
U: 'a,
[src]
U: 'a,
Create a new merged task from the current task instance and a second task
Join will return a new task that will resolve a tuple containing the
results from both the task join
is called upon, and the task passed in.
Both the current task and the second task passed in will still execute in parallel.
Arguments
task
- A second task to join with the current task
Examples
let merged_task = my_task.join(my_other_task);
fn state(&self) -> &State<T, E>
[src]
Get the task state
Returns a reference to the internal state of the task
fn poll(&mut self) -> Option<Result<T, E>>
[src]
Executes the closure within the task once
If the task resolves or rejects then the returned option will contain a result object.
fn wait(self) -> Option<Result<T, E>>
[src]
Executes the closure within the task blocking until the task completes
fn map<F, U>(self, map: F) -> Task<'a, U, E> where
F: FnMut(T) -> U + 'a,
U: 'a,
[src]
F: FnMut(T) -> U + 'a,
U: 'a,
fn then<F, U>(self, task: F) -> Task<'a, U, E> where
F: FnMut(T) -> State<U, E> + 'a,
U: 'a,
[src]
F: FnMut(T) -> State<U, E> + 'a,
U: 'a,
fn done<F>(self, done: F) -> Task<'a, (), E> where
F: FnMut(T) + 'a,
[src]
F: FnMut(T) + 'a,
fn recover<F, O>(self, recover: F) -> Task<'a, T, O> where
F: FnMut(E) -> State<T, O> + 'a,
O: 'a,
[src]
F: FnMut(E) -> State<T, O> + 'a,
O: 'a,
fn catch<F>(self, catch: F) -> Task<'a, T, ()> where
F: FnMut(E) + 'a,
[src]
F: FnMut(E) + 'a,
impl<'a, T, E> Task<'a, T, E> where
T: PartialEq + 'a,
E: PartialEq + 'a,
[src]
T: PartialEq + 'a,
E: PartialEq + 'a,
fn eq(self, task: Task<'a, T, E>) -> Task<'a, bool, E>
[src]
fn ne(self, task: Task<'a, T, E>) -> Task<'a, bool, E>
[src]
impl<'a, T, E> Task<'a, T, E> where
T: PartialOrd + 'a,
E: PartialOrd + 'a,
[src]
T: PartialOrd + 'a,
E: PartialOrd + 'a,