pub struct ObservableHashMap<K, V, Codec = Default> { /* private fields */ }Expand description
A hash map that emits an event for each change.
Use subscribe to obtain an event stream that can be used for building a mirror of this hash map.
Implementationsยง
Sourceยงimpl<K, V, Codec> ObservableHashMap<K, V, Codec>
impl<K, V, Codec> ObservableHashMap<K, V, Codec>
Sourcepub fn new() -> Self
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn new() -> Self
Creates an empty observable hash map.
Sourcepub fn set_error_handler<E>(&mut self, on_err: E)
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn set_error_handler<E>(&mut self, on_err: E)
Sets the error handler function that is called when sending an event fails.
Sourcepub fn subscribe(&self, buffer: usize) -> HashMapSubscription<K, V, Codec>
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn subscribe(&self, buffer: usize) -> HashMapSubscription<K, V, Codec>
Subscribes to change events from this observable hash map.
The current contents of the hash map is included with the subscription.
buffer specifies the maximum size of the event buffer for this subscription in number of events.
If it is exceeded the subscription is shed and the receiver gets a RecvError::Lagged.
Sourcepub fn subscribe_incremental(
&self,
buffer: usize,
) -> HashMapSubscription<K, V, Codec>
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn subscribe_incremental( &self, buffer: usize, ) -> HashMapSubscription<K, V, Codec>
Subscribes to change events from this observable hash map with incremental sending of the current contents.
The current contents of the hash map are sent incrementally.
buffer specifies the maximum size of the event buffer for this subscription in number of events.
If it is exceeded the subscription is shed and the receiver gets a RecvError::Lagged.
Sourcepub fn subscriber_count(&self) -> usize
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn subscriber_count(&self) -> usize
Current number of subscribers.
Sourcepub fn notifier(&self) -> ChangeNotifier
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn notifier(&self) -> ChangeNotifier
Returns a change notifier that can be used locally to be notified of changes to this collection.
Sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn insert(&mut self, k: K, v: V) -> Option<V>
Inserts a value under a key.
A HashMapEvent::Set change event is sent.
Returns the value previously stored under the key, if any.
ยงPanics
Panics when done has been called before.
Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<V>
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
Removes the value under the specified key.
A HashMapEvent::Remove change event is sent.
The value is returned.
ยงPanics
Panics when done has been called before.
Sourcepub fn clear(&mut self)
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn clear(&mut self)
Removes all items.
A HashMapEvent::Clear change event is sent.
ยงPanics
Panics when done has been called before.
Sourcepub fn retain<F>(&mut self, f: F)
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
A HashMapEvent::Remove change event is sent for every element that is removed.
ยงPanics
Panics when done has been called before.
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V, Codec>
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, Codec>
Sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<RefMut<'_, K, V, Codec>>
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<RefMut<'_, K, V, Codec>>
Gets a mutable reference to the value under the specified key.
A HashMapEvent::Set change event is sent if the reference is accessed mutably.
ยงPanics
Panics when done has been called before.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V, Codec> โ
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn iter_mut(&mut self) -> IterMut<'_, K, V, Codec> โ
Mutably iterates over the key-value pairs.
A HashMapEvent::Set change event is sent for each value that is accessed mutably.
ยงPanics
Panics when done has been called before.
Sourcepub fn shrink_to_fit(&mut self)
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the hash map as much as possible.
A HashMapEvent::ShrinkToFit change event is sent.
ยงPanics
Panics when done has been called before.
Sourcepub fn done(&mut self)
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn done(&mut self)
Prevents further changes of this hash map and notifies are subscribers that no further events will occur.
Methods that modify the hash map will panic after this has been called. It is still possible to subscribe to this observable hash map.
Sourcepub fn is_done(&self) -> bool
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn is_done(&self) -> bool
Returns true if done has been called and further
changes are prohibited.
Methods that modify the hash map will panic in this case.
Sourcepub fn into_inner(self) -> HashMap<K, V>
๐Deprecated: remoc-obs has been integrated into remoc as module remoc::robs. Please update your references.
pub fn into_inner(self) -> HashMap<K, V>
Extracts the underlying hash map.
If done has not been called before this method, subscribers will receive an error.
Methods from Deref<Target = HashMap<K, V>>ยง
1.0.0 ยท Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
This number is a lower bound; the HashMap<K, V> might be able to hold
more, but is guaranteed to be able to hold at least this many.
ยงExamples
use std::collections::HashMap;
let map: HashMap<i32, i32> = HashMap::with_capacity(100);
assert!(map.capacity() >= 100);1.0.0 ยท Sourcepub fn keys(&self) -> Keys<'_, K, V>
pub fn keys(&self) -> Keys<'_, K, V>
An iterator visiting all keys in arbitrary order.
The iterator element type is &'a K.
ยงExamples
use std::collections::HashMap;
let map = HashMap::from([
("a", 1),
("b", 2),
("c", 3),
]);
for key in map.keys() {
println!("{key}");
}ยงPerformance
In the current implementation, iterating over keys takes O(capacity) time instead of O(len) because it internally visits empty buckets too.
1.0.0 ยท Sourcepub fn values(&self) -> Values<'_, K, V>
pub fn values(&self) -> Values<'_, K, V>
An iterator visiting all values in arbitrary order.
The iterator element type is &'a V.
ยงExamples
use std::collections::HashMap;
let map = HashMap::from([
("a", 1),
("b", 2),
("c", 3),
]);
for val in map.values() {
println!("{val}");
}ยงPerformance
In the current implementation, iterating over values takes O(capacity) time instead of O(len) because it internally visits empty buckets too.
1.0.0 ยท Sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
An iterator visiting all key-value pairs in arbitrary order.
The iterator element type is (&'a K, &'a V).
ยงExamples
use std::collections::HashMap;
let map = HashMap::from([
("a", 1),
("b", 2),
("c", 3),
]);
for (key, val) in map.iter() {
println!("key: {key} val: {val}");
}ยงPerformance
In the current implementation, iterating over map takes O(capacity) time instead of O(len) because it internally visits empty buckets too.
1.0.0 ยท Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the map.
ยงExamples
use std::collections::HashMap;
let mut a = HashMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);1.0.0 ยท Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
ยงExamples
use std::collections::HashMap;
let mut a = HashMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());1.9.0 ยท Sourcepub fn hasher(&self) -> &S
pub fn hasher(&self) -> &S
Returns a reference to the mapโs BuildHasher.
ยงExamples
use std::collections::HashMap;
use std::hash::RandomState;
let hasher = RandomState::new();
let map: HashMap<i32, i32> = HashMap::with_hasher(hasher);
let hasher: &RandomState = map.hasher();1.0.0 ยท Sourcepub fn get<Q>(&self, k: &Q) -> Option<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the mapโs key type, but
Hash and Eq on the borrowed form must match those for
the key type.
ยงExamples
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);1.40.0 ยท Sourcepub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key. This is potentially useful:
- for key types where non-identical keys can be considered equal;
- for getting the
&Kstored key value from a borrowed&Qlookup key; or - for getting a reference to a key with the same lifetime as the collection.
The supplied key may be any borrowed form of the mapโs key type, but
Hash and Eq on the borrowed form must match those for
the key type.
ยงExamples
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
#[derive(Clone, Copy, Debug)]
struct S {
id: u32,
name: &'static str, // ignored by equality and hashing operations
}
impl PartialEq for S {
fn eq(&self, other: &S) -> bool {
self.id == other.id
}
}
impl Eq for S {}
impl Hash for S {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
let j_a = S { id: 1, name: "Jessica" };
let j_b = S { id: 1, name: "Jess" };
let p = S { id: 2, name: "Paul" };
assert_eq!(j_a, j_b);
let mut map = HashMap::new();
map.insert(j_a, "Paris");
assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
assert_eq!(map.get_key_value(&p), None);1.0.0 ยท Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Returns true if the map contains a value for the specified key.
The key may be any borrowed form of the mapโs key type, but
Hash and Eq on the borrowed form must match those for
the key type.
ยงExamples
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);Trait Implementationsยง
Sourceยงimpl<K, V, Codec> Debug for ObservableHashMap<K, V, Codec>
impl<K, V, Codec> Debug for ObservableHashMap<K, V, Codec>
Sourceยงimpl<K, V, Codec> Default for ObservableHashMap<K, V, Codec>
impl<K, V, Codec> Default for ObservableHashMap<K, V, Codec>
Sourceยงimpl<K, V, Codec> Deref for ObservableHashMap<K, V, Codec>
impl<K, V, Codec> Deref for ObservableHashMap<K, V, Codec>
Sourceยงimpl<K, V, Codec> Extend<(K, V)> for ObservableHashMap<K, V, Codec>
impl<K, V, Codec> Extend<(K, V)> for ObservableHashMap<K, V, Codec>
Sourceยงfn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Sourceยงfn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Sourceยงfn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)