Skip to main content

Executor

Struct Executor 

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

Single-threaded async executor.

Manages task lifecycle: spawn, poll, complete, free. Tasks are allocated via Box (default) or slab (via spawn_slab). Each task’s header contains a free_fn that knows how to deallocate its own storage — the executor doesn’t know or care which allocator was used.

§UnsafeCell on incoming and deferred_free

These fields are wrapped in UnsafeCell to prevent a provenance aliasing violation. During poll(), raw pointers to these Vecs are stored in TLS for wakers to push into. Later in the same poll(), complete_task(&mut self) takes &mut self — which under Rust’s aliasing rules asserts exclusive access to ALL fields. Without UnsafeCell, this invalidates the TLS pointers because two &mut paths to the same memory exist. UnsafeCell opts these fields out of &mut’s exclusivity guarantee, telling the compiler they may be accessed through other paths (the TLS raw pointers).

This is NOT a performance concern — UnsafeCell is zero-sized and get() compiles to a no-op pointer cast. The only effect is that the compiler won’t optimize based on exclusive access to these fields.

Implementations§

Source§

impl Executor

Source

pub fn new(initial_capacity: usize) -> Self

Create an executor.

Source

pub fn spawn_boxed<F>(&mut self, future: F) -> JoinHandle<F::Output>
where F: Future + 'static, F::Output: 'static,

Spawn an async task via Box allocation. Returns a JoinHandle that can be awaited for the task’s output.

Source

pub fn poll(&mut self) -> usize

Poll all ready tasks once.

Source

pub fn task_count(&self) -> usize

Number of live tasks.

Source

pub fn has_ready(&self) -> bool

Returns true if any tasks are queued for polling.

Source

pub fn set_tasks_per_cycle(&mut self, limit: usize)

Set the maximum tasks to poll per cycle.

Trait Implementations§

Source§

impl Drop for Executor

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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.