Skip to main content

ExecutorNotified

Trait ExecutorNotified 

Source
pub trait ExecutorNotified: 'static {
    // Required method
    fn request_cancel(&mut self);
}
Expand description

Allows executors to receive notifications about observer lifecycle events.

This trait enables executors to be notified when observers request task cancellation. While implementing cancellation support is optional, it can improve efficiency by allowing executors to stop polling cancelled tasks.

§Using Without Notifications

If your executor doesn’t need notifications, use std::convert::Infallible as the type parameter and pass None where executor notifiers are expected:

use std::convert::Infallible;
use some_executor::observer::{Observer, ObserverNotified, TypedObserver};
use some_executor::{BoxedSendObserver, BoxedSendObserverFuture, DynExecutor, ObjSafeTask, SomeExecutor};
use some_executor::task::Task;


// Define an executor that doesn't use notifications
#[derive(Debug)]
struct MyExecutor;

impl SomeExecutor for MyExecutor {
    type ExecutorNotifier = Infallible;

fn spawn<F: Future + Send + 'static, Notifier: ObserverNotified<F::Output> + Send>(&mut self, task: Task<F, Notifier>) -> impl Observer<Value=F::Output> + Send where Self: Sized, F::Output: Send + Unpin {
         todo!() as TypedObserver::<F::Output, Infallible>
    }

fn spawn_async<'s, F: Future + Send + 'static, Notifier: ObserverNotified<F::Output> + Send>(&'s mut self, task: Task<F, Notifier>) -> impl Future<Output=impl Observer<Value=F::Output>> + Send + 's where Self: Sized, F::Output: Send + Unpin {
        async { todo!() as TypedObserver::<F::Output, Infallible>}
    }

fn spawn_objsafe(&mut self, task: ObjSafeTask) -> BoxedSendObserver {
        todo!()
    }

fn spawn_objsafe_async<'s>(&'s mut self, task: ObjSafeTask) -> BoxedSendObserverFuture<'s> {
        Box::new(async { todo!() })
    }

fn clone_box(&self) -> Box<DynExecutor> {
        todo!()
    }

fn executor_notifier(&mut self) -> Option<Self::ExecutorNotifier> {
        None
    }
     
    // Implement the required methods...
    // (implementation details omitted for brevity)
}

§Examples

use some_executor::observer::ExecutorNotified;

// A simple notifier that tracks cancellation requests
struct CancellationTracker {
    task_id: u64,
    cancelled: bool,
}

impl ExecutorNotified for CancellationTracker {
    fn request_cancel(&mut self) {
        self.cancelled = true;
        println!("Task {} cancellation requested", self.task_id);
        // Executor can now stop polling this task
    }
}

Required Methods§

Source

fn request_cancel(&mut self)

Called when an observer requests cancellation of its associated task.

Executors can use this notification to stop polling the task, though implementing this is optional. The cancellation is already tracked through other mechanisms, so this is purely an optimization opportunity.

Trait Implementations§

Source§

impl ExecutorNotified for Box<dyn ExecutorNotified + '_>

Allow a Box<dyn ExecutorNotified> to be used as an ExecutorNotified directly.

The implementation proceeds by dyanmic dispatch.

Source§

fn request_cancel(&mut self)

Called when an observer requests cancellation of its associated task. Read more
Source§

impl ExecutorNotified for Box<dyn ExecutorNotified + Send>

Source§

fn request_cancel(&mut self)

Called when an observer requests cancellation of its associated task. Read more

Implementations on Foreign Types§

Source§

impl ExecutorNotified for Infallible

Source§

impl ExecutorNotified for Box<dyn ExecutorNotified + '_>

Allow a Box<dyn ExecutorNotified> to be used as an ExecutorNotified directly.

The implementation proceeds by dyanmic dispatch.

Source§

impl ExecutorNotified for Box<dyn ExecutorNotified + Send>

Implementors§