ferroui_system/i_observable.rs
1//! `System.IObservable<T>` and `System.IObserver<T>`: the interfaces of push-based notification.
2
3use crate::IDisposable;
4use std::rc::Rc;
5
6/// The error type carried by [`IObserver::on_error`]: a shared error, so that it can be delivered to
7/// several observers and kept by the operators that replay it. Errors compare by identity where the
8/// tests of the reactive libraries compare exceptions.
9pub type ObservableError = Rc<dyn std::error::Error>;
10
11/// Receives notifications from an [`IObservable`].
12pub trait IObserver<T> {
13 fn on_next(&self, value: T);
14 fn on_error(&self, _error: ObservableError) {}
15 fn on_completed(&self) {}
16}
17
18/// A push-based source of values.
19pub trait IObservable<T> {
20 /// Subscribes `observer`; disposing the returned handle unsubscribes it.
21 fn subscribe(&self, observer: Rc<dyn IObserver<T>>) -> Rc<dyn IDisposable>;
22}
23
24/// Sequences compare by identity (reference equality), so that they can be the items of a list.
25impl<T> PartialEq for dyn IObservable<T> {
26 fn eq(&self, other: &Self) -> bool {
27 std::ptr::addr_eq(self as *const Self, other as *const Self)
28 }
29}