Struct medea_reactive::ObservableCell [−][src]
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
impl<D> ObservableCell<D> where
D: 'static,
[src]
D: 'static,
pub fn new(data: D) -> Self
[src]
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.
pub fn borrow(&self) -> Ref<'_, D>
[src]
Returns immutable reference to an underlying data.
pub fn when<F>(
&self,
assert_fn: F
) -> LocalBoxFuture<'static, Result<(), DroppedError>> where
F: Fn(&D) -> bool + 'static,
[src]
&self,
assert_fn: F
) -> LocalBoxFuture<'static, Result<(), DroppedError>> where
F: Fn(&D) -> bool + 'static,
Returns Future
which will resolve only on modifications that
the given assert_fn
returns true
on.
impl<D> ObservableCell<D> where
D: Clone + 'static,
[src]
D: Clone + 'static,
pub fn get(&self) -> D
[src]
Returns copy of an underlying data.
pub fn subscribe(&self) -> LocalBoxStream<'static, D>
[src]
Returns Stream
into which underlying data updates will be emitted.
impl<D> ObservableCell<D> where
D: PartialEq + 'static,
[src]
D: PartialEq + 'static,
pub fn when_eq(
&self,
should_be: D
) -> LocalBoxFuture<'static, Result<(), DroppedError>>
[src]
&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.
impl<D> ObservableCell<D> where
D: Clone + PartialEq + 'static,
[src]
D: Clone + PartialEq + 'static,
pub fn set(&self, new_data: D)
[src]
Sets the new_data
value as an underlying data.
pub fn replace(&self, new_data: D) -> D
[src]
Replaces the contained underlying data with the given new_data
value,
and returns the old one.
pub fn mutate<F>(&self, f: F) where
F: FnOnce(MutObservableFieldGuard<'_, D, RefCell<Vec<UniversalSubscriber<D>>>>),
[src]
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,
D: Unpin,
impl<D> !UnwindSafe for ObservableCell<D>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,