Struct Task

Source
pub struct Task<'a, T = (), E = ()> { /* private fields */ }
Expand description

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

Implementations§

Source§

impl<'a, T, E> Task<'a, T, E>
where T: 'a, E: 'a,

Source

pub fn new<F>(task: F) -> Self
where 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()));
Source

pub fn from(val: T) -> Self

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

pub fn with<F>(with: F) -> Self
where 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());
Source

pub fn join<U>(self, task: Task<'a, U, E>) -> Task<'a, (T, U), E>
where 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);
Source

pub fn state(&self) -> &State<T, E>

Get the task state

Returns a reference to the internal state of the task

Source

pub fn poll(&mut self) -> Option<Result<T, E>>

Executes the closure within the task once

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

Source

pub fn wait(self) -> Option<Result<T, E>>

Executes the closure within the task blocking until the task completes

Source

pub fn map<F, U>(self, map: F) -> Task<'a, U, E>
where F: FnMut(T) -> U + 'a, U: 'a,

Source

pub fn then<F, U>(self, task: F) -> Task<'a, U, E>
where F: FnMut(T) -> State<U, E> + 'a, U: 'a,

Source

pub fn done<F>(self, done: F) -> Task<'a, (), E>
where F: FnMut(T) + 'a,

Source

pub fn recover<F, O>(self, recover: F) -> Task<'a, T, O>
where F: FnMut(E) -> State<T, O> + 'a, O: 'a,

Source

pub fn catch<F>(self, catch: F) -> Task<'a, T, ()>
where F: FnMut(E) + 'a,

Source§

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

Source

pub fn eq(self, task: Task<'a, T, E>) -> Task<'a, bool, E>

Source

pub fn ne(self, task: Task<'a, T, E>) -> Task<'a, bool, E>

Source§

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

Source

pub fn lt(self, task: Task<'a, T, E>) -> Task<'a, bool, E>

Source

pub fn le(self, task: Task<'a, T, E>) -> Task<'a, bool, E>

Source

pub fn gt(self, task: Task<'a, T, E>) -> Task<'a, bool, E>

Source

pub fn ge(self, task: Task<'a, T, E>) -> Task<'a, bool, E>

Trait Implementations§

Source§

impl<'a, T, E> Debug for Task<'a, T, E>

Source§

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

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

impl<'a, T, E> Executable for Task<'a, T, E>

Source§

fn exec(&mut self) -> bool

Execute the task or other custom type Read more
Source§

impl<'a, T, E> Send for Task<'a, T, E>

Source§

impl<'a, T, E> Sync for Task<'a, T, E>

Auto Trait Implementations§

§

impl<'a, T, E> Freeze for Task<'a, T, E>
where T: Freeze, E: Freeze,

§

impl<'a, T = (), E = ()> !RefUnwindSafe for Task<'a, T, E>

§

impl<'a, T, E> Unpin for Task<'a, T, E>
where T: Unpin, E: Unpin,

§

impl<'a, T = (), E = ()> !UnwindSafe for Task<'a, T, E>

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.