Struct async_observable::Observable
source · [−]pub struct Observable<T> where
T: Clone, { /* private fields */ }
Expand description
Wraps a value and lets you fork the state to synchronize it between tasks and threads.
Creating New Observables
There are several ways to create a new observable, altough using the new
function should be
the preferred way.
let mut using_new = Observable::new(0);
let mut using_from = Observable::from(0);
let mut using_into: Observable<u8> = 0.into();
Publishing New Values
Publishing a new version is done by a single call to the publish()
method.
observable.publish(1);
observable.publish(2);
observable.publish(3);
Receiving Updates
let mut observable = Observable::new(0);
let mut fork = observable.clone();
observable.publish(1);
observable.publish(2);
observable.publish(3);
assert_eq!(fork.next().await, 3);
Important
Keep in mind that if you publish multiple versions directly after each other there no
guarantees that all forked observables will receive every change! But as long as every
observable is constently asking for changes (via next()
) you are guaranteed that every
observable received the latest version.
Implementations
sourceimpl<T> Observable<T> where
T: Clone,
impl<T> Observable<T> where
T: Clone,
sourcepub fn modify<M>(&mut self, modify: M) where
M: FnOnce(&mut T),
pub fn modify<M>(&mut self, modify: M) where
M: FnOnce(&mut T),
Modify the underlying value and notify forks.
sourcepub fn modify_conditional<C, M>(&mut self, condition: C, modify: M) -> bool where
C: FnOnce(&T) -> bool,
M: FnOnce(&mut T),
pub fn modify_conditional<C, M>(&mut self, condition: C, modify: M) -> bool where
C: FnOnce(&T) -> bool,
M: FnOnce(&mut T),
If the condition is met, modify the underlying value and notify forks.
Returns true
if the modification was executed.
let mut observable = Observable::new(0);
let mut fork = observable.clone();
observable.modify_conditional(|i| *i == 0, |i| *i = 1); // modify
assert_eq!(fork.next().await, 1);
observable.modify_conditional(|i| *i == 0, |i| *i = 2); // doesnt modify
fork.next().await; // runs forever
sourcepub fn clone_and_reset(&self) -> Observable<T>
pub fn clone_and_reset(&self) -> Observable<T>
Same as clone, but the reset causes the fork to instantly have a change available with the current state.
let mut observable = Observable::new(0);
let mut fork = observable.clone_and_reset();
assert_eq!(fork.next().await, 0);
sourcepub fn latest(&self) -> T
pub fn latest(&self) -> T
Creates a clone of latest version of the observable value, without consuming the change!
let mut observable = Observable::new(0);
let mut fork = observable.clone_and_reset();
observable.publish(1);
assert_eq!(fork.latest(), 1);
assert_eq!(fork.next().await, 1);
sourcepub async fn next(&mut self) -> T
pub async fn next(&mut self) -> T
Wait until a new version of the observable was published and return a clone of the new version.
let mut observable = Observable::new(0);
let mut fork = observable.clone_and_reset();
observable.publish(1);
assert_eq!(fork.next().await, 1);
observable.publish(2);
assert_eq!(fork.next().await, 2);
fork.next().await; // runs forever!
sourcepub fn synchronize(&mut self) -> T
pub fn synchronize(&mut self) -> T
Skip any potential updates and retrieve the latest version of the observed value.
let mut observable = Observable::new(0);
let mut fork = observable.clone();
observable.publish(1);
observable.publish(2);
observable.publish(3);
assert_eq!(fork.synchronize(), 3);
fork.next().await; // runs forever!
sourceimpl<T> Observable<T> where
T: Clone + Eq,
impl<T> Observable<T> where
T: Clone + Eq,
sourcepub fn publish_if_changed(&mut self, value: T) -> bool
pub fn publish_if_changed(&mut self, value: T) -> bool
Publish a change if the new value differs from the current one.
Returns true
if a change was made.
Trait Implementations
sourceimpl<T: Clone> Clone for Observable<T> where
T: Clone,
impl<T: Clone> Clone for Observable<T> where
T: Clone,
sourcefn clone(&self) -> Observable<T>
fn clone(&self) -> Observable<T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<T> Debug for Observable<T> where
T: Clone + Debug,
impl<T> Debug for Observable<T> where
T: Clone + Debug,
Auto Trait Implementations
impl<T> RefUnwindSafe for Observable<T>
impl<T> Send for Observable<T> where
T: Send,
impl<T> Sync for Observable<T> where
T: Send,
impl<T> Unpin for Observable<T>
impl<T> UnwindSafe for Observable<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more