pub struct DataBinding<T>{ /* private fields */ }
Expand description
Wraps internal data and optionally notifies an Application
of changes to it
See module docs for a full example.
Implementations§
Source§impl<T> DataBinding<T>
impl<T> DataBinding<T>
Sourcepub fn new(data: T) -> DataBinding<T>
pub fn new(data: T) -> DataBinding<T>
Create a new DataBinding
that wraps data
It will create the data as unbound meaning that changes to the data will not notify any
Application
. The DataBinding
can afterwards be
bound with the bind
method.
Sourcepub fn new_bound(data: T, notifier: ChangeNotifier) -> DataBinding<T>
pub fn new_bound(data: T, notifier: ChangeNotifier) -> DataBinding<T>
Create a new DataBinding
that wraps data
and notifies an
Application
’s ChangeNotifier
when the data is
mutated.
Sourcepub fn bind(&mut self, notifier: ChangeNotifier)
pub fn bind(&mut self, notifier: ChangeNotifier)
Bind this DataBinding
to a ChangeNotifier
Sourcepub fn unbind(&mut self)
pub fn unbind(&mut self)
Unbind this DataBinding
so that changes to the data no longer trigger application
updates
Sourcepub fn access<F, R>(&self, f: F) -> Option<R>
pub fn access<F, R>(&self, f: F) -> Option<R>
Access the inner data of the binding inside of the provided closure
This will return None
and will not run the supplied closure if a lock to the inner
data cannot be obtained due to lock poisoning.
§Example
let binding = DataBinding::new(false);
let x = binding.access(|data| {
// Return the opposite of what's in our data
!data
});
assert_eq!(x, Some(true));
Sourcepub fn read_cloned(&self) -> Option<T>where
T: Clone,
pub fn read_cloned(&self) -> Option<T>where
T: Clone,
Sourcepub fn read_cloned_or_default(&self) -> Twhere
T: Clone,
pub fn read_cloned_or_default(&self) -> Twhere
T: Clone,
Attempt to obtain a clone of the inner data or otherwise return the type’s default value
Sourcepub fn mutate<F, R>(&mut self, f: F) -> Option<R>
pub fn mutate<F, R>(&mut self, f: F) -> Option<R>
Use a closure to mutate the inner data and notify the ChangeNotifier
( if set )
This will return None
and will not run the supplied closure if a lock to the inner
data cannot be obtained due to lock poisoning.
§Example
let mut binding = DataBinding::new(false);
let x = binding.mutate(|data| {
// Update the data
*data = true;
*data
});
assert_eq!(x, Some(true));
assert_eq!(binding.read_cloned(), Some(true));
Sourcepub fn write(&mut self, v: T)
pub fn write(&mut self, v: T)
Set the inner data directly and notify the ChangeNotifier
( if set )
Trait Implementations§
Source§impl<T> Clone for DataBinding<T>
impl<T> Clone for DataBinding<T>
Source§fn clone(&self) -> DataBinding<T>
fn clone(&self) -> DataBinding<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more