pub struct Subject<T: Clone> { /* private fields */ }Expand description
A typed value holder with an observer callback list.
Subject<T> stores a current and previous value and a list of callbacks.
Calling set atomically updates the value and notifies
all registered observers synchronously, in subscription order.
§Type coverage in v1
Subject<i32>, Subject<bool>, Subject<Color>, and Subject<String>
cover the four PropertyValue variants.
Subjects are intentionally typed — callers subscribe to a Subject<i32>,
not to a Subject<PropertyValue>. Bridging the two is application-level
code.
§Object-identity-free
Subject<T> does not require object ids, WidgetId, or ObjectNode
references. Subjects are owned values; callers hold them directly. This
satisfies the LPAR-15 §5.I binding invariant.
Implementations§
Source§impl<T: Clone> Subject<T>
impl<T: Clone> Subject<T>
Sourcepub fn new(initial: T) -> Self
pub fn new(initial: T) -> Self
Create a new Subject with the given initial value.
prev_value is initialised to a clone of initial; no observers are
registered.
use rlvgl_core::observer::Subject;
let s: Subject<i32> = Subject::new(0);
assert_eq!(*s.get(), 0);
assert_eq!(*s.prev(), 0);Sourcepub fn prev(&self) -> &T
pub fn prev(&self) -> &T
Borrow the value from before the most recent set call.
On a freshly constructed subject prev() == get().
Sourcepub fn set(&mut self, value: T)
pub fn set(&mut self, value: T)
Store a new value, copy the old value to prev, then notify all
observers synchronously in subscription order.
Observers receive a shared reference &T to the new value. They
MUST NOT attempt to call set on this same subject; that is
structurally prevented because they do not hold a &mut Subject.
Cross-subject set (observer of A mutates B) is the intended
composition pattern and works correctly.
Sourcepub fn notify(&mut self)
pub fn notify(&mut self)
Notify all observers with the current value without changing it.
prev_value is also left unchanged. Useful for re-broadcasting after
an external mutation or initialisation.
If this method is already executing on the call stack (re-entrant call), the inner invocation is silently skipped. See the module-level reentrancy note for rationale.
Sourcepub fn subscribe<F: FnMut(&T) + 'static>(&mut self, cb: F)
pub fn subscribe<F: FnMut(&T) + 'static>(&mut self, cb: F)
Register an observer callback.
Callbacks are invoked in subscription order on every [set] or
explicit notify call. There is no unsubscribe
mechanism in v1; adding one is a Specification Required amendment that
does not break existing call sites.
F: 'static is required because the closure is heap-allocated behind a
Box<dyn FnMut> and may outlive the calling scope.