Skip to main content

Task

Struct Task 

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

A unit of deferred work.

Tasks encapsulate a closure to be executed later by the scheduler. Each task has a unique ID, priority, and execution state.

§Example

use reovim_kernel::api::v1::*;

// Create a high-priority task with a name
let mut task = Task::with_priority(Priority::HIGH, || println!("Hello!"))
    .with_name("greeting");

assert_eq!(task.state(), TaskState::Pending);
assert_eq!(task.priority(), Priority::HIGH);

// Execute the task
let result = task.execute();
assert!(result.is_ok());
assert_eq!(task.state(), TaskState::Completed);

Implementations§

Source§

impl Task

Source

pub fn new<F>(work: F) -> Self
where F: FnOnce() + Send + 'static,

Create a new task with normal priority.

Source

pub fn with_priority<F>(priority: Priority, work: F) -> Self
where F: FnOnce() + Send + 'static,

Create a task with specific priority.

Source

pub const fn with_name(self, name: &'static str) -> Self

Set the task name for debugging.

Source

pub const fn id(&self) -> TaskId

Get the task ID.

Source

pub const fn priority(&self) -> Priority

Get the task priority.

Source

pub const fn state(&self) -> TaskState

Get the current state.

Source

pub const fn name(&self) -> Option<&'static str>

Get the task name (if set).

Source

pub const fn is_executable(&self) -> bool

Check if the task can still be executed.

Source

pub fn execute(&mut self) -> Result<(), &'static str>

Execute the task.

Consumes the work closure and transitions state to Completed.

§Errors

Returns an error if:

  • Task has already been executed
  • Task was cancelled (work is None)
Source

pub fn mark_failed(&mut self)

Mark the task as failed.

Used by the executor when a panic is caught during execution.

Source

pub fn cancel(&mut self)

Cancel the task.

Removes the work closure without executing it.

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.