Skip to main content

Task

Struct Task 

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

A unit of work to be executed by the scheduler.

Like Chrome’s base::OnceClosure wrapped with base::TaskTraits. Each task has a priority and an optional delay.

§Lifecycle

  1. Created via Task::new() or Task::delayed()
  2. Posted to Scheduler via post_task()
  3. Scheduler puts it in the correct priority queue
  4. Event loop picks highest-priority ready task
  5. Task executes (callback consumed)

Implementations§

Source§

impl Task

Source

pub fn new(priority: TaskPriority, callback: impl FnOnce() + 'static) -> Self

Create a task with the given priority.

Task::new(TaskPriority::Normal, || {
    println!("hello from task");
});
Source

pub fn delayed( priority: TaskPriority, delay: Duration, callback: impl FnOnce() + 'static, ) -> Self

Create a delayed task.

Like Chrome’s PostDelayedTask(). The task won’t execute until delay has elapsed. Equivalent to setTimeout(callback, delay).

Task::delayed(TaskPriority::Timer, Duration::from_millis(100), || {
    println!("fires after 100ms");
});
Source

pub fn priority(&self) -> TaskPriority

The task’s priority level.

Source

pub fn is_ready(&self) -> bool

Whether this task is ready to execute (delay has elapsed).

Source

pub fn time_until_ready(&self) -> Duration

Time remaining until this task is ready. Returns Duration::ZERO if already ready.

Source

pub fn run_at(&self) -> Option<Instant>

The scheduled run time, if delayed.

Source

pub fn run(self)

Execute this task, consuming the callback.

Trait Implementations§

Source§

impl Debug for Task

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Task

§

impl !RefUnwindSafe for Task

§

impl !Send for Task

§

impl !Sync for Task

§

impl Unpin for Task

§

impl UnsafeUnpin for Task

§

impl !UnwindSafe for Task

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.