pub struct LocalExecutor { /* private fields */ }Expand description
A !Send executor pinned to a single session. Tasks spawned on it run in
order on whichever thread drains the dispatch destination supplied at
construction time — typically the main thread for the default session, or
a dedicated OS thread for sessions created by spawn_dedicated_thread.
Implementations§
Source§impl LocalExecutor
impl LocalExecutor
Sourcepub fn new(
session_id: SessionId,
scheduler: Arc<dyn Scheduler>,
dispatch: impl Fn(Runnable<RunnableMeta>) + Send + Sync + 'static,
) -> Self
pub fn new( session_id: SessionId, scheduler: Arc<dyn Scheduler>, dispatch: impl Fn(Runnable<RunnableMeta>) + Send + Sync + 'static, ) -> Self
Constructs a local executor that runs spawned tasks by sending their
runnables through dispatch. The scheduler is retained for access to
clocks, timers, and other scheduler-level services.
For the common case of routing runnables through
Scheduler::schedule_local, callers pass a closure that does exactly
that. spawn_dedicated_thread instead passes a closure that sends to
the dedicated thread’s channel.
pub fn session_id(&self) -> SessionId
pub fn scheduler(&self) -> &Arc<dyn Scheduler> ⓘ
pub fn spawn<F>(&self, future: F) -> Task<F::Output> ⓘ
Sourcepub fn spawn_with_dispatch<F>(
&self,
future: F,
dispatch: impl Fn(Runnable<RunnableMeta>) + Send + Sync + 'static,
) -> Task<F::Output> ⓘ
pub fn spawn_with_dispatch<F>( &self, future: F, dispatch: impl Fn(Runnable<RunnableMeta>) + Send + Sync + 'static, ) -> Task<F::Output> ⓘ
Like Self::spawn, but routes the task’s runnables through the
given dispatch destination instead of this executor’s own. The
destination must deliver runnables to this executor’s thread: the
future is polled and dropped on the spawning thread.
pub fn block_on<Fut: Future>(&self, future: Fut) -> Fut::Output
Sourcepub fn block_with_timeout<Fut: Future>(
&self,
timeout: Duration,
future: Fut,
) -> Result<Fut::Output, impl Future<Output = Fut::Output> + use<Fut>>
pub fn block_with_timeout<Fut: Future>( &self, timeout: Duration, future: Fut, ) -> Result<Fut::Output, impl Future<Output = Fut::Output> + use<Fut>>
Block until the future completes or timeout occurs. Returns Ok(output) if completed, Err(future) if timed out.
pub fn timer(&self, duration: Duration) -> Timer ⓘ
pub fn now(&self) -> Instant
Sourcepub fn spawn_dedicated<F, Fut>(&self, f: F) -> Task<Fut::Output> ⓘ
pub fn spawn_dedicated<F, Fut>(&self, f: F) -> Task<Fut::Output> ⓘ
Spawn a closure on a fresh session pinned to its own LocalExecutor.
The closure runs on a new OS thread under PlatformScheduler, or on
the test scheduler’s loop under TestScheduler.
The returned Task represents the dedicated work: dropping it cancels
the dedicated closure, .awaiting it yields the closure’s return
value, .detach()ing it lets the dedicated work run independently of
the caller.