Skip to main content

Subject

Struct Subject 

Source
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>

Source

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

pub fn get(&self) -> &T

Borrow the current value.

Source

pub fn prev(&self) -> &T

Borrow the value from before the most recent set call.

On a freshly constructed subject prev() == get().

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Subject<T>

§

impl<T> !Send for Subject<T>

§

impl<T> !Sync for Subject<T>

§

impl<T> !UnwindSafe for Subject<T>

§

impl<T> Freeze for Subject<T>
where T: Freeze,

§

impl<T> Unpin for Subject<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Subject<T>
where T: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.