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,

source

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

pub fn borrow(&self) -> Ref<'_, D>

Returns immutable reference to an underlying data.

source

pub fn when<F>( &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.

source§

impl<D> ObservableCell<D>where D: Clone + 'static,

source

pub fn get(&self) -> D

Returns copy of an underlying data.

source

pub fn subscribe(&self) -> LocalBoxStream<'static, D>

Returns Stream into which underlying data updates will be emitted.

source§

impl<D> ObservableCell<D>where D: PartialEq + 'static,

source

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,

source

pub fn set(&self, new_data: D)

Sets the new_data value as an underlying data.

source

pub fn replace(&self, new_data: D) -> D

Replaces the contained underlying data with the given new_data value, and returns the old one.

source

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§

source§

impl<D: Debug> Debug for ObservableCell<D>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.