Struct workflow_task::Task

source ·
pub struct Task<A, T>
where A: Send, T: 'static,
{ /* private fields */ }
Expand description

Task{self::Task} struct allows you to spawn an async fn that can run in a loop as a task (similar to a thread), checking for a termination signal (so that execution can be aborted), upon completion returning a value to the creator.

You can pass a channel as an argument to the async function if you wish to communicate with the task.

NOTE: You should always call task.join().await to await for the task completion if re-using the task.

use workflow_task::{task, TaskResult};


let task = task!(
    |args : (), stop : Receiver<()>| async move {
        let mut index = args;
        loop {
            if stop.try_recv().is_ok() {
                break;
            }
            // ... do something ...
            index += 1;
        }
        return index;
    }
);

// spawn the task instance ...
// passing 256 as the `args` argument
task.run(256)?;

// signal termination ...
task.stop()?;

// await for the task completion ...
// the `result` is the returned `index` value
let result = task.join().await?;

// rinse and repeat if needed
task.run(256)?;

Implementations§

source§

impl<A, T> Task<A, T>
where A: Send + Sync + 'static, T: Send + 'static,

source

pub fn new<FN>(task_fn: FN) -> Task<A, T>
where FN: Send + Sync + Fn(A, Receiver<()>) -> FnReturn<T> + 'static,

Create a new Task instance by supplying it with an async closure that has 2 arguments:

use workflow_task::task;

task!(|args:bool, signal| async move {
    // ...
    return true;
});
source

pub fn blank() -> Self

Create an instance of the task without any task function. The task function can be passed later via Task::set_task_fn().

source

pub fn set_task_fn<FN>(&self, task_fn: FN)
where FN: Send + Sync + Fn(A, Receiver<()>) -> FnReturn<T> + 'static,

Replace task fn with an alternate function. The task must be restarted for the replacement to take effect. The function passed does not need to be boxed.

source

pub fn run(&self, args: A) -> TaskResult<&Self>

Run the task supplying the provided argument to the closure supplied at creation.

source

pub fn stop(&self) -> TaskResult<()>

Signal termination on the channel supplied to the task closure; The task has to check for the signal periodically or await on the future of the signal.

source

pub async fn join(&self) -> TaskResult<T>

Blocks until the task exits. Resolves immediately if the task is not running.

source

pub async fn stop_and_join(&self) -> TaskResult<T>

Signals termination and blocks until the task exits.

source

pub fn is_running(&self) -> bool

Returns true if the task is running, otherwise returns false.

Trait Implementations§

source§

impl<A, T> Clone for Task<A, T>
where A: Send + Clone, T: 'static + Clone,

source§

fn clone(&self) -> Task<A, T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<A, T> Default for Task<A, T>
where A: Send + Sync + 'static, T: Send + Sync + 'static,

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<A, T> RefUnwindSafe for Task<A, T>
where A: RefUnwindSafe,

§

impl<A, T> Send for Task<A, T>
where A: Sync, T: Send,

§

impl<A, T> Sync for Task<A, T>
where A: Sync, T: Send,

§

impl<A, T> Unpin for Task<A, T>

§

impl<A, T> UnwindSafe for Task<A, T>
where A: RefUnwindSafe,

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.

§

impl<T> Instrument for T

§

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

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

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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