Struct oxygengine_user_interface::raui::core::data_binding::DataBinding
source · pub struct DataBinding<T>where
T: Debug + Default + Send + Sync,{ /* 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>where
T: Debug + Default + Send + Sync,
impl<T> DataBinding<T>where T: Debug + Default + Send + Sync,
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>where
F: FnMut(&T) -> R,
pub fn access<F, R>(&self, f: F) -> Option<R>where F: FnMut(&T) -> 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>where
F: FnMut(&mut T) -> R,
pub fn mutate<F, R>(&mut self, f: F) -> Option<R>where F: FnMut(&mut T) -> 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>where
T: Clone + Debug + Default + Send + Sync,
impl<T> Clone for DataBinding<T>where T: Clone + Debug + Default + Send + Sync,
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