medea_reactive::collections::hash_map

Struct HashMap

Source
pub struct HashMap<K, V, S: SubscribersStore<(K, V), O>, O> { /* private fields */ }
Expand description

Reactive hash map based on HashMap.

§Usage

use medea_reactive::collections::ObservableHashMap;

let mut map = ObservableHashMap::new();

// You can subscribe on insert action:
let mut inserts = map.on_insert();
map.insert("foo", "bar");
let (key, val) = inserts.next()
    .await
    .unwrap();
assert_eq!(key, "foo");
assert_eq!(val, "bar");

// Also you can subscribe on remove action:
let mut removals = map.on_remove();
map.remove(&"foo");
let (key, val) = removals.next()
    .await
    .unwrap();
assert_eq!(key, "foo");
assert_eq!(val, "bar");

// Remove subscription will also receive all items of the HashMap when it
// will be dropped:
map.insert("foo-1", "bar-1");
map.insert("foo-2", "bar-2");
drop(map);
let removed_items: HashMap<_, _> = removals.take(2)
    .collect()
    .await;
assert_eq!(removed_items["foo-1"], "bar-1");
assert_eq!(removed_items["foo-2"], "bar-2");

§Waiting for subscribers to complete

use medea_reactive::collections::ProgressableHashMap;

let mut hash_map = ProgressableHashMap::new();

let mut on_insert = hash_map.on_insert();
hash_map.insert(1, 1);

// hash_map.when_insert_processed().await; <- wouldn't be resolved
let value = on_insert.next().await.unwrap();
// hash_map.when_insert_processed().await; <- wouldn't be resolved
drop(value);

hash_map.when_insert_processed().await; // will be resolved

Implementations§

Source§

impl<K, V> HashMap<K, V, SubStore<(K, V)>, Guarded<(K, V)>>
where K: Hash + Eq + Clone + 'static, V: Clone + 'static,

Source

pub fn when_insert_processed(&self) -> Processed<'static>

Returns Future resolving when all insertion updates will be processed by HashMap::on_insert() subscribers.

Source

pub fn when_remove_processed(&self) -> Processed<'static>

Returns Future resolving when all remove updates will be processed by HashMap::on_remove() subscribers.

Source

pub fn when_all_processed(&self) -> AllProcessed<'static>

Returns Future resolving when all insert and remove updates will be processed by subscribers.

Source§

impl<K, V, S: SubscribersStore<(K, V), O>, O> HashMap<K, V, S, O>

Source

pub fn new() -> Self

Creates new empty HashMap.

Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Iterator visiting all key-value pairs in an arbitrary order.

Source

pub fn values(&self) -> Values<'_, K, V>

Iterator visiting all values in an arbitrary order.

Source

pub fn on_insert(&self) -> LocalBoxStream<'static, O>

Returns Stream yielding inserted key-value pairs to this HashMap.

Source

pub fn on_remove(&self) -> LocalBoxStream<'static, O>

Returns Stream yielding removed key-value pairs from this HashMap.

Note, that this Stream will yield all key-value pairs of this HashMap on Drop.

Source§

impl<K, V, S, O> HashMap<K, V, S, O>
where K: Clone, V: Clone, S: SubscribersStore<(K, V), O>, O: 'static,

Source

pub fn replay_on_insert(&self) -> LocalBoxStream<'static, O>

Returns Stream containing values from this HashMap.

Returned Stream contains only current values. It won’t update on new inserts, but you can merge returned Stream with a HashMap::on_insert() Stream if you want to process current values and values that will be inserted.

Source

pub fn on_insert_with_replay(&self) -> LocalBoxStream<'static, O>

Source§

impl<K, V, S, O> HashMap<K, V, S, O>
where K: Hash + Eq, S: SubscribersStore<(K, V), O>,

Source

pub fn get(&self, key: &K) -> Option<&V>

Returns a reference to the value corresponding to the key.

Source

pub fn get_mut(&mut self, key: &K) -> Option<&mut V>

Returns a mutable reference to the value corresponding to the key.

Note, that mutating of the returned value wouldn’t work same as Observables and doesn’t spawns HashMap::on_insert() or HashMap::on_remove() events. If you need subscriptions on value changes then just wrap the value into an Observable and subscribe to it.

Source§

impl<K, V, S, O> HashMap<K, V, S, O>
where K: Hash + Eq + Clone, V: Clone, S: SubscribersStore<(K, V), O>,

Source

pub fn remove_not_present<A>(&mut self, other: &HashMap<K, A>)

Removes all entries which are not present in the provided HashMap.

Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Inserts a key-value pair to this HashMap.

Emits HashMap::on_insert() event and may emit HashMap::on_remove() event if insert replaces a value contained in this HashMap.

Source

pub fn remove(&mut self, key: &K) -> Option<V>

Removes the key from this HashMap, returning the value behind it, if any.

Emits HashMap::on_remove() event if value with provided key is removed from this HashMap.

Trait Implementations§

Source§

impl<K: Clone, V: Clone, S: Clone + SubscribersStore<(K, V), O>, O: Clone> Clone for HashMap<K, V, S, O>

Source§

fn clone(&self) -> HashMap<K, V, S, O>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug, S: Debug + SubscribersStore<(K, V), O>, O: Debug> Debug for HashMap<K, V, S, O>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<K, V, S: SubscribersStore<(K, V), O>, O> Default for HashMap<K, V, S, O>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K, V, S: SubscribersStore<(K, V), O>, O> Drop for HashMap<K, V, S, O>

Source§

fn drop(&mut self)

Sends all key-values of a dropped HashMap to the HashMap::on_remove subs.

Source§

impl<K, V, S: SubscribersStore<(K, V), O>, O> From<HashMap<K, V>> for HashMap<K, V, S, O>

Source§

fn from(from: HashMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K, V, S: SubscribersStore<(K, V), O>, O> FromIterator<(K, V)> for HashMap<K, V, S, O>
where K: Hash + Eq,

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, K, V, S: SubscribersStore<(K, V), O>, O> IntoIterator for &'a HashMap<K, V, S, O>

Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V, S, O> Freeze for HashMap<K, V, S, O>
where S: Freeze,

§

impl<K, V, S, O> RefUnwindSafe for HashMap<K, V, S, O>

§

impl<K, V, S, O> Send for HashMap<K, V, S, O>
where S: Send, O: Send, K: Send, V: Send,

§

impl<K, V, S, O> Sync for HashMap<K, V, S, O>
where S: Sync, O: Sync, K: Sync, V: Sync,

§

impl<K, V, S, O> Unpin for HashMap<K, V, S, O>
where S: Unpin, O: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S, O> UnwindSafe for HashMap<K, V, S, O>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.