pub struct ObservableCell<D>(/* private fields */);Expand description
Observable analogue of Cell.
Subscription to changes works the same way as ObservableField,
but working with underlying data of ObservableCell is different.
§ObservableCell underlying data access
§For Copy types
use medea_reactive::ObservableCell;
let foo = ObservableCell::new(0i32);
// If data implements `Copy` then you can get a copy of the current value:
assert_eq!(foo.get(), 0);§Reference to an underlying data
use medea_reactive::ObservableCell;
struct Foo(i32);
impl Foo {
pub fn new(num: i32) -> Self {
Self(num)
}
pub fn get_num(&self) -> i32 {
self.0
}
}
let foo = ObservableCell::new(Foo::new(100));
assert_eq!(foo.borrow().get_num(), 100);§Mutation of an underlying data
use medea_reactive::ObservableCell;
let foo = ObservableCell::new(0i32);
// You can just set some data:
foo.set(100);
assert_eq!(foo.get(), 100);
// Or replace data with new data and get the old data:
let old_value = foo.replace(200);
assert_eq!(old_value, 100);
assert_eq!(foo.get(), 200);
// Or mutate this data:
foo.mutate(|mut data| *data = 300);
assert_eq!(foo.get(), 300);Implementations§
Source§impl<D> ObservableCell<D>where
D: 'static,
impl<D> ObservableCell<D>where
D: 'static,
Sourcepub const fn new(data: D) -> Self
pub const fn new(data: D) -> Self
Returns new ObservableCell with subscribable mutations.
Also, you can subscribe to concrete mutation with
ObservableCell::when or ObservableCell::when_eq methods.
This container can mutate internally. See ObservableCell docs
for more info.
Sourcepub fn when<F>(
&self,
assert_fn: F,
) -> LocalBoxFuture<'static, Result<(), DroppedError>>
pub fn when<F>( &self, assert_fn: F, ) -> LocalBoxFuture<'static, Result<(), DroppedError>>
Returns Future which will resolve only on modifications that
the given assert_fn returns true on.
Source§impl<D> ObservableCell<D>where
D: Clone + 'static,
impl<D> ObservableCell<D>where
D: Clone + 'static,
Source§impl<D> ObservableCell<D>where
D: PartialEq + 'static,
impl<D> ObservableCell<D>where
D: PartialEq + 'static,
Sourcepub fn when_eq(
&self,
should_be: D,
) -> LocalBoxFuture<'static, Result<(), DroppedError>>
pub fn when_eq( &self, should_be: D, ) -> LocalBoxFuture<'static, Result<(), DroppedError>>
Returns Future which will resolve only when data of this
ObservableCell will become equal to the provided should_be value.
Source§impl<D> ObservableCell<D>
impl<D> ObservableCell<D>
Trait Implementations§
Auto Trait Implementations§
impl<D> !Freeze for ObservableCell<D>
impl<D> !RefUnwindSafe for ObservableCell<D>
impl<D> !Send for ObservableCell<D>
impl<D> !Sync for ObservableCell<D>
impl<D> Unpin for ObservableCell<D>where
D: Unpin,
impl<D> !UnwindSafe for ObservableCell<D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more