Skip to main content

PropertyChain

Struct PropertyChain 

Source
pub struct PropertyChain<S, V> { /* private fields */ }
Expand description

A path from an object to a value through properties, any step of which may be null (the expression of WhenPropertyChanged and WhenValueChanged, split into its ObservablePropertyParts).

Its value is None when a step is null (upstream’s UnobtainableValue). Each step that notifies its changes is observed on the object it is read from; when a step changes, the steps after it are observed again on the new objects, and none is observed past a null step.

use ferroui_dynamic_data::binding::PropertyChain;
use ferroui_system::component_model::{INotifyPropertyChanged, PropertyPath};
use ferroui_system::events::Event;
use std::cell::{Cell, RefCell};
use std::rc::Rc;

struct Child { age: Cell<i32>, changed: Event<str> }
impl Child { fn age(&self) -> i32 { self.age.get() } }
impl INotifyPropertyChanged for Child { fn property_changed(&self) -> &Event<str> { &self.changed } }

struct Parent { child: RefCell<Option<Rc<Child>>>, changed: Event<str> }
impl Parent { fn child(&self) -> Option<Rc<Child>> { self.child.borrow().clone() } }
impl INotifyPropertyChanged for Parent { fn property_changed(&self) -> &Event<str> { &self.changed } }

// x => x.Child!.Age
let chain = PropertyChain::nullable(ferroui_system::prop!(Parent => child)).then(ferroui_system::prop!(Child => age));
let parent = Rc::new(Parent { child: RefCell::new(None), changed: Event::new() });
assert_eq!(chain.get(&parent), None);
*parent.child.borrow_mut() = Some(Rc::new(Child { age: Cell::new(3), changed: Event::new() }));
assert_eq!(chain.get(&parent), Some(3));
assert_eq!(chain.path(), "Child.Age");

Implementations§

Source§

impl<S: 'static, V: 'static> PropertyChain<S, V>

Source

pub fn new(path: PropertyPath<S, V>) -> Self

The chain of a path whose steps are never null.

Source

pub fn nullable(path: PropertyPath<S, Option<V>>) -> Self

The chain of a path whose last step may be null (x => x.Child!): its value is None when the step is.

Source

pub fn names(&self) -> &[String]

The names of the properties along the chain.

Source

pub fn path(&self) -> String

The chain as text, Child.Age.

Source

pub fn get(&self, source: &S) -> Option<V>

The value of the chain on source; None when a step is null (the walk of ReadCurrent).

Source

pub fn getter(&self) -> Rc<dyn Fn(&S) -> Option<V>>

The getter of the chain.

Source

pub fn observe(&self, source: &S, changed: Rc<dyn Fn()>) -> Rc<dyn IDisposable>

Runs changed whenever the value of the chain on source may have changed, until the result is disposed (the subscriptions of the levels of DeepChainSubscription).

Source

pub fn then<P: IntoPropertyChain<V>>( self, next: P, ) -> PropertyChain<S, P::Value>
where S: Clone, V: Clone, P::Value: 'static,

The chain that continues with next from the value of this one (x => x.A.B): when the object of this step changes, the steps of next are observed on the new object, and on none when it is null.

Source

pub fn then_nullable<W: 'static>( self, next: PropertyPath<V, Option<W>>, ) -> PropertyChain<S, W>
where S: Clone, V: Clone,

then with a step that may be null (x => x.A!.B).

Source

pub fn map<W: 'static>( self, convert: impl Fn(V) -> W + 'static, ) -> PropertyChain<S, W>

The chain whose value is converted by convert (an expression’s Convert node, such as x => (decimal)x.Amount or x => ((IHasAge)x.Child).Age); the steps observed are the same.

Trait Implementations§

Source§

impl<S, V> Clone for PropertyChain<S, V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S, V> IntoPropertyChain<S> for PropertyChain<S, V>

Source§

type Value = V

The type of the value at the end of the path.
Source§

fn into_property_chain(self) -> PropertyChain<S, V>

The path as a chain.

Auto Trait Implementations§

§

impl<S, V> !RefUnwindSafe for PropertyChain<S, V>

§

impl<S, V> !Send for PropertyChain<S, V>

§

impl<S, V> !Sync for PropertyChain<S, V>

§

impl<S, V> !UnwindSafe for PropertyChain<S, V>

§

impl<S, V> Freeze for PropertyChain<S, V>
where Rc<dyn Fn(&S) -> Option<V>>: Freeze, Rc<dyn Fn(&S, Rc<dyn Fn()>) -> Rc<dyn IDisposable>>: Freeze,

§

impl<S, V> Unpin for PropertyChain<S, V>
where Rc<dyn Fn(&S) -> Option<V>>: Unpin, Rc<dyn Fn(&S, Rc<dyn Fn()>) -> Rc<dyn IDisposable>>: Unpin,

§

impl<S, V> UnsafeUnpin for PropertyChain<S, V>
where Rc<dyn Fn(&S) -> Option<V>>: UnsafeUnpin, Rc<dyn Fn(&S, Rc<dyn Fn()>) -> Rc<dyn IDisposable>>: 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> 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.