Skip to main content

Task

Struct Task 

Source
pub struct Task<Msg> { /* private fields */ }
Expand description

Work to run in the background with progress, cancellation and an outcome.

use std::time::Duration;
use qframe::runtime::{Command, Task, TaskEvent};

enum Msg {
    Task(TaskEvent),
    Built(String),
}

let task = Task::new("Build image", |cx| {
    for step in 0..4 {
        if !cx.sleep(Duration::from_millis(300)) {
            return Err("stopped".into());
        }
        cx.progress((step + 1) as f32 / 4.0);
    }
    Ok(Msg::Built("sha256:4f2a".into()))
})
.on_event(Msg::Task);
let id = task.id(); // keep it to cancel the task later
let command: Command<Msg> = Command::task(task);

Implementations§

Source§

impl<Msg: Send + 'static> Task<Msg>

Source

pub fn new( label: impl Into<String>, work: impl FnOnce(&TaskCx<Msg>) -> Result<Msg, String> + Send + 'static, ) -> Self

A task labelled label running work. Ok delivers its message; Err fails the task with a reason.

Source

pub fn on_event( self, message: impl Fn(TaskEvent) -> Msg + Send + Sync + 'static, ) -> Self

Turns start, progress and the outcome into messages, e.g. to update a Tasks model.

Source

pub fn id(&self) -> TaskId

The task’s id, known before it starts.

Source

pub fn label(&self) -> &str

The task’s label.

Auto Trait Implementations§

§

impl<Msg> !RefUnwindSafe for Task<Msg>

§

impl<Msg> !Sync for Task<Msg>

§

impl<Msg> !UnwindSafe for Task<Msg>

§

impl<Msg> Freeze for Task<Msg>
where Box<dyn FnOnce(&TaskCx<Msg>) -> Result<Msg, String> + Send>: Freeze, Option<Arc<dyn Fn(TaskEvent) -> Msg + Send + Sync>>: Freeze,

§

impl<Msg> Send for Task<Msg>
where Box<dyn FnOnce(&TaskCx<Msg>) -> Result<Msg, String> + Send>: Send, Option<Arc<dyn Fn(TaskEvent) -> Msg + Send + Sync>>: Send,

§

impl<Msg> Unpin for Task<Msg>
where Box<dyn FnOnce(&TaskCx<Msg>) -> Result<Msg, String> + Send>: Unpin, Option<Arc<dyn Fn(TaskEvent) -> Msg + Send + Sync>>: Unpin,

§

impl<Msg> UnsafeUnpin for Task<Msg>
where Box<dyn FnOnce(&TaskCx<Msg>) -> Result<Msg, String> + Send>: UnsafeUnpin, Option<Arc<dyn Fn(TaskEvent) -> Msg + Send + Sync>>: UnsafeUnpin,

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.