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]

[src]

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

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

[src]

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

[src]

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

[src]

Get the task state

Returns a reference to the internal state of the task

[src]

Executes the closure within the task once

If the task resolves or rejects then the returned option will contain a result object.

[src]

Executes the closure within the task blocking until the task completes

[src]

[src]

[src]

[src]

[src]

impl<'a, T, E> Task<'a, T, E> where
    T: PartialEq + 'a,
    E: PartialEq + 'a, 
[src]

[src]

[src]

impl<'a, T, E> Task<'a, T, E> where
    T: PartialOrd + 'a,
    E: PartialOrd + 'a, 
[src]

[src]

[src]

[src]

[src]

Trait Implementations

impl<'a, T, E> Debug for Task<'a, T, E>
[src]

[src]

Formats the value using the given formatter.

impl<'a, T, E> Executable for Task<'a, T, E>
[src]

[src]

Execute the task or other custom type Read more

impl<'a, T, E> Send for Task<'a, T, E>
[src]

impl<'a, T, E> Sync for Task<'a, T, E>
[src]