Skip to main content

Scheduler

Trait Scheduler 

Source
pub trait Scheduler {
    type D: Disposable + MaybeSend + 'static;

    // Required methods
    fn spawn_future(
        &self,
        future: impl Future<Output = ()> + MaybeSend + 'static,
    ) -> BoundDropDisposal<Self::D>;
    fn sleep(
        &self,
        duration: Duration,
    ) -> impl Future + MaybeSend + 'static + use<Self>;

    // Provided methods
    fn schedule(
        &self,
        task: impl FnOnce() + MaybeSend + 'static,
        delay: Option<Duration>,
    ) -> BoundDropDisposal<Self::D> { ... }
    fn schedule_recursively(
        &self,
        task: impl FnMut(usize) -> RecursionAction + MaybeSend + 'static,
        delay: Option<Duration>,
    ) -> BoundDropDisposal<Self::D>
       where Self: Clone + MaybeSend + 'static { ... }
    fn schedule_periodically(
        &self,
        task: impl FnMut(usize) -> bool + MaybeSend + 'static,
        period: Duration,
        delay: Option<Duration>,
    ) -> BoundDropDisposal<Self::D>
       where Self: Clone + MaybeSend + 'static { ... }
    fn schedule_stream<SM>(
        &self,
        stream: SM,
        result_callback: impl FnMut(Option<SM::Item>) -> bool + MaybeSend + 'static,
    ) -> BoundDropDisposal<Self::D>
       where SM: Stream + MaybeSend + 'static { ... }
}
Expand description

Core abstraction for driving asynchronous work across runtimes. See https://reactivex.io/documentation/scheduler.html This is why the task must be ’static: https://stackoverflow.com/a/65287449/9315497

Required Associated Types§

Source

type D: Disposable + MaybeSend + 'static

Required Methods§

Source

fn spawn_future( &self, future: impl Future<Output = ()> + MaybeSend + 'static, ) -> BoundDropDisposal<Self::D>

Source

fn sleep( &self, duration: Duration, ) -> impl Future + MaybeSend + 'static + use<Self>

Returns a future that completes duration after this call.

Contract for implementors: the deadline is captured when sleep is called, not when the returned future is first polled.

Provided Methods§

Source

fn schedule( &self, task: impl FnOnce() + MaybeSend + 'static, delay: Option<Duration>, ) -> BoundDropDisposal<Self::D>

Source

fn schedule_recursively( &self, task: impl FnMut(usize) -> RecursionAction + MaybeSend + 'static, delay: Option<Duration>, ) -> BoundDropDisposal<Self::D>
where Self: Clone + MaybeSend + 'static,

Repeatedly runs task until it returns RecursionAction::Stop.

The loop yields to the executor between iterations (even for ContinueImmediately and already-elapsed ContinueAt instants), so other tasks can make progress and disposal can take effect.

Source

fn schedule_periodically( &self, task: impl FnMut(usize) -> bool + MaybeSend + 'static, period: Duration, delay: Option<Duration>, ) -> BoundDropDisposal<Self::D>
where Self: Clone + MaybeSend + 'static,

Runs task at a fixed rate anchored to the time of this call (plus delay), until task returns false.

Fixed-rate semantics: if an execution overruns period, missed runs are executed back-to-back to catch up — they are never skipped.

§Panics

Panics if period is zero.

Source

fn schedule_stream<SM>( &self, stream: SM, result_callback: impl FnMut(Option<SM::Item>) -> bool + MaybeSend + 'static, ) -> BoundDropDisposal<Self::D>
where SM: Stream + MaybeSend + 'static,

Drives stream to completion, invoking result_callback with Some(item) for each element and a final None when the stream ends.

The callback’s answer is what keeps the stream running: returning false stops polling it right there, and the final None is then never delivered — the stream is dropped along with the task.

Disposal aborts the task without delivering the final None.

The loop yields to the executor after each element (even when the stream is always ready), so other tasks can make progress and disposal can take effect.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Scheduler for Handle

Leverages a Tokio runtime handle to drive scheduled tasks.

Source§

type D = JoinHandle<()>

Source§

fn spawn_future( &self, future: impl Future<Output = ()> + MaybeSend + 'static, ) -> BoundDropDisposal<Self::D>

Source§

fn sleep(&self, duration: Duration) -> impl Future + MaybeSend + 'static + use<>

Implementors§