ObserverNotified

Trait ObserverNotified 

Source
pub trait ObserverNotified<T: ?Sized>: Unpin + 'static {
    // Required method
    fn notify(&mut self, value: &T);
}
Expand description

Provides inline notifications when an observed task completes.

Unlike Observer which requires polling or awaiting, ObserverNotified allows you to receive immediate notification when a task completes. The notifier’s notify method is called inline by the executor when the task finishes.

§Cancellation Behavior

When a task is cancelled, the notifier is dropped without calling notify. This allows you to detect cancellation by implementing Drop for your notifier type.

§Design Note

This trait requires Unpin to allow the notifier to be moved during task execution. This design choice enables notifiers to maintain mutable state without requiring interior mutability patterns.

§Examples

use some_executor::observer::ObserverNotified;
use std::sync::mpsc;

// A simple notifier that sends results through a channel
struct ChannelNotifier<T> {
    sender: mpsc::Sender<T>,
}

impl<T: Clone + 'static> ObserverNotified<T> for ChannelNotifier<T> {
    fn notify(&mut self, value: &T) {
        let _ = self.sender.send(value.clone());
    }
}

// A notifier that logs completion
struct LogNotifier {
    task_name: String,
}

impl<T: std::fmt::Debug + ?Sized> ObserverNotified<T> for LogNotifier {
    fn notify(&mut self, value: &T) {
        println!("Task '{}' completed with: {:?}", self.task_name, value);
    }
}

impl Drop for LogNotifier {
    fn drop(&mut self) {
        println!("Task '{}' was cancelled or notifier dropped", self.task_name);
    }
}

Required Methods§

Source

fn notify(&mut self, value: &T)

Called inline when the observed task completes successfully.

This method is invoked by the executor immediately when the task finishes. It will not be called if the task is cancelled.

§Parameters
  • value: A reference to the task’s output value

Trait Implementations§

Source§

impl ObserverNotified<Box<dyn Any>> for Box<dyn ObserverNotified<dyn Any + 'static>>

Source§

fn notify(&mut self, value: &Box<dyn Any + 'static>)

Called inline when the observed task completes successfully. Read more
Source§

impl ObserverNotified<Box<dyn Any + Send>> for Box<dyn ObserverNotified<dyn Any + Send + 'static> + Send>

Source§

fn notify(&mut self, value: &Box<dyn Any + Send + 'static>)

Called inline when the observed task completes successfully. Read more

Implementations on Foreign Types§

Source§

impl ObserverNotified<Box<dyn Any + Send>> for Box<dyn ObserverNotified<dyn Any + Send + 'static> + Send>

Source§

fn notify(&mut self, value: &Box<dyn Any + Send + 'static>)

Source§

impl ObserverNotified<Box<dyn Any>> for Box<dyn ObserverNotified<dyn Any + 'static>>

Source§

fn notify(&mut self, value: &Box<dyn Any + 'static>)

Source§

impl<T> ObserverNotified<T> for Infallible

Source§

fn notify(&mut self, _value: &T)

Implementors§