Struct medea_reactive::ObservableCell
source · pub struct ObservableCell<D>(_);
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.
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>where
D: Clone + PartialEq + 'static,
impl<D> ObservableCell<D>where D: Clone + PartialEq + 'static,
sourcepub fn replace(&self, new_data: D) -> D
pub fn replace(&self, new_data: D) -> D
Replaces the contained underlying data with the given new_data
value,
and returns the old one.
sourcepub fn mutate<F>(&self, f: F)where
F: FnOnce(MutObservableFieldGuard<'_, D, RefCell<Vec<UniversalSubscriber<D>>>>),
pub fn mutate<F>(&self, f: F)where F: FnOnce(MutObservableFieldGuard<'_, D, RefCell<Vec<UniversalSubscriber<D>>>>),
Updates an underlying data using the provided function, which will accept a mutable reference to an underlying data.
Trait Implementations§
Auto Trait Implementations§
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