Struct remoc_obs::hash_map::ObservableHashMap
source · [−]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
sourceimpl<K, V, Codec> ObservableHashMap<K, V, Codec> where
K: Eq + Hash + Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec,
impl<K, V, Codec> ObservableHashMap<K, V, Codec> where
K: Eq + Hash + Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec,
sourcepub fn set_error_handler<E>(&mut self, on_err: E) where
E: Fn(SendError) + Send + Sync + 'static,
pub fn set_error_handler<E>(&mut self, on_err: E) where
E: Fn(SendError) + Send + Sync + 'static,
Sets the error handler function that is called when sending an event fails.
sourcepub fn subscribe(&self, buffer: usize) -> HashMapSubscription<K, V, Codec>
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>
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
pub fn subscriber_count(&self) -> usize
Current number of subscribers.
sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
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> where
K: Borrow<Q>,
Q: Hash + Eq,
pub fn remove<Q>(&mut self, k: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Hash + Eq,
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)
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) where
F: FnMut(&K, &mut V) -> bool,
pub fn retain<F>(&mut self, f: F) where
F: FnMut(&K, &mut V) -> bool,
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 get_mut<Q>(&mut self, k: &Q) -> Option<RefMut<'_, K, V, Codec>> where
K: Borrow<Q>,
Q: Hash + Eq,
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<RefMut<'_, K, V, Codec>> where
K: Borrow<Q>,
Q: Hash + Eq,
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>ⓘNotable traits for IterMut<'a, K, V, Codec>impl<'a, K, V, Codec> Iterator for IterMut<'a, K, V, Codec> where
K: Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec, type Item = RefMut<'a, K, V, Codec>;
pub fn iter_mut(&mut self) -> IterMut<'_, K, V, Codec>ⓘNotable traits for IterMut<'a, K, V, Codec>impl<'a, K, V, Codec> Iterator for IterMut<'a, K, V, Codec> where
K: Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec, type Item = RefMut<'a, K, V, Codec>;
K: Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec, type Item = RefMut<'a, 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)
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)
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
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>
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);
}
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);
}
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: {} val: {}", key, val);
}
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::collections::hash_map::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> where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get<Q>(&self, k: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
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)> where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)> where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Returns the key-value pair corresponding to the supplied key.
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;
let mut map = HashMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);
1.0.0 · sourcepub fn contains_key<Q>(&self, k: &Q) -> bool where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn contains_key<Q>(&self, k: &Q) -> bool where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
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);
sourcepub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S>
🔬 This is a nightly-only experimental API. (hash_raw_entry
)
pub fn raw_entry(&self) -> RawEntryBuilder<'_, K, V, S>
hash_raw_entry
)Creates a raw immutable entry builder for the HashMap.
Raw entries provide the lowest level of control for searching and manipulating a map. They must be manually initialized with a hash and then manually searched.
This is useful for
- Hash memoization
- Using a search key that doesn’t work with the Borrow trait
- Using custom comparison logic without newtype wrappers
Unless you are in such a situation, higher-level and more foolproof APIs like
get
should be preferred.
Immutable raw entries have very limited use; you might instead want raw_entry_mut
.
Trait Implementations
sourceimpl<K, V, Codec> Debug for ObservableHashMap<K, V, Codec> where
K: Debug,
V: Debug,
impl<K, V, Codec> Debug for ObservableHashMap<K, V, Codec> where
K: Debug,
V: Debug,
sourceimpl<K, V, Codec> Default for ObservableHashMap<K, V, Codec> where
K: Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec,
impl<K, V, Codec> Default for ObservableHashMap<K, V, Codec> where
K: Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec,
sourceimpl<K, V, Codec> Deref for ObservableHashMap<K, V, Codec>
impl<K, V, Codec> Deref for ObservableHashMap<K, V, Codec>
sourceimpl<K, V, Codec> Extend<(K, V)> for ObservableHashMap<K, V, Codec> where
K: Eq + Hash + Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec,
impl<K, V, Codec> Extend<(K, V)> for ObservableHashMap<K, V, Codec> where
K: Eq + Hash + Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec,
sourcefn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<K, V, Codec> From<HashMap<K, V, RandomState>> for ObservableHashMap<K, V, Codec> where
K: Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec,
impl<K, V, Codec> From<HashMap<K, V, RandomState>> for ObservableHashMap<K, V, Codec> where
K: Clone + RemoteSend,
V: Clone + RemoteSend,
Codec: Codec,
sourceimpl<K, V, Codec> From<ObservableHashMap<K, V, Codec>> for HashMap<K, V>
impl<K, V, Codec> From<ObservableHashMap<K, V, Codec>> for HashMap<K, V>
sourcefn from(ohm: ObservableHashMap<K, V, Codec>) -> Self
fn from(ohm: ObservableHashMap<K, V, Codec>) -> Self
Performs the conversion.
Auto Trait Implementations
impl<K, V, Codec = Default> !RefUnwindSafe for ObservableHashMap<K, V, Codec>
impl<K, V, Codec> Send for ObservableHashMap<K, V, Codec> where
Codec: Send,
K: Send,
V: Send,
impl<K, V, Codec> Sync for ObservableHashMap<K, V, Codec> where
Codec: Send,
K: Send + Sync,
V: Send + Sync,
impl<K, V, Codec> Unpin for ObservableHashMap<K, V, Codec> where
K: Unpin,
V: Unpin,
impl<K, V, Codec = Default> !UnwindSafe for ObservableHashMap<K, V, Codec>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
pub fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more