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);
}
}