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
- Created via
Task::new()orTask::delayed() - Posted to
Schedulerviapost_task() - Scheduler puts it in the correct priority queue
- Event loop picks highest-priority ready task
- Task executes (callback consumed)
Implementations§
Source§impl Task
impl Task
Sourcepub fn new(priority: TaskPriority, callback: impl FnOnce() + 'static) -> Self
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");
});Sourcepub fn delayed(
priority: TaskPriority,
delay: Duration,
callback: impl FnOnce() + 'static,
) -> Self
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");
});Sourcepub fn priority(&self) -> TaskPriority
pub fn priority(&self) -> TaskPriority
The task’s priority level.
Sourcepub fn time_until_ready(&self) -> Duration
pub fn time_until_ready(&self) -> Duration
Time remaining until this task is ready.
Returns Duration::ZERO if already ready.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more