pub struct SubscriptionMap<K, V>(/* private fields */)
where
K: Clone + Debug + Eq + Hash + Ord,
V: Clone + Debug;
Expand description
A concurrent and self cleaning map of observable values to easily communicate dynamically across tasks.
let map = SubscriptionMap::<usize, usize>::default();
let mut subscription = map.get_or_insert(1, 0).await;
task::spawn(async move {
// somewhere else in your program
let mut subscription = map.get_or_insert(1, 0).await;
log::info!("received update throguh map: {}", subscription.next().await);
});
// wait for some event and publish the state
subscription.publish(1);
// just drop the ref as soon as you are done with it to trigger the cleanup
drop(subscription);
Implementations§
Source§impl<K, V> SubscriptionMap<K, V>
impl<K, V> SubscriptionMap<K, V>
Sourcepub async fn get_or_insert(&self, key: K, value: V) -> SubscriptionRef<K, V>
pub async fn get_or_insert(&self, key: K, value: V) -> SubscriptionRef<K, V>
Either creates a ref to a existing subscription or initializes a new one.
Source§impl<K, V> SubscriptionMap<K, V>
impl<K, V> SubscriptionMap<K, V>
Sourcepub async fn publish_if_changed(&self, key: &K, value: V) -> Result<bool>
pub async fn publish_if_changed(&self, key: &K, value: V) -> Result<bool>
Check if the provided value differs from the observable and return the info if a publish was made.
let map = SubscriptionMap::<usize, usize>::default();
let mut subscription = map.get_or_insert(1, 0).await;
assert_eq!(subscription.latest(), 0);
map.publish_if_changed(&1, 1);
assert_eq!(subscription.next().await, 1);
map.publish_if_changed(&1, 1);
// this will never resolve since we did not publish an update!
subscription.next().await
Sourcepub async fn modify_and_publish<F, R>(&self, key: &K, modify: F) -> Result<()>
pub async fn modify_and_publish<F, R>(&self, key: &K, modify: F) -> Result<()>
Modify the value contained in the subscription through a mutable reference and notify others.
This is handy for expensive data structures such as vectors, trees or maps.
let map = SubscriptionMap::<usize, usize>::default();
let mut subscription = map.get_or_insert(1, 0).await;
assert_eq!(subscription.latest(), 0);
map.modify_and_publish(&1, |mut v| *v = 1);
assert_eq!(subscription.latest(), 1);
Trait Implementations§
Source§impl<K, V> Clone for SubscriptionMap<K, V>
impl<K, V> Clone for SubscriptionMap<K, V>
Source§fn clone(&self) -> SubscriptionMap<K, V>
fn clone(&self) -> SubscriptionMap<K, V>
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<K, V> Debug for SubscriptionMap<K, V>
impl<K, V> Debug for SubscriptionMap<K, V>
Auto Trait Implementations§
impl<K, V> Freeze for SubscriptionMap<K, V>
impl<K, V> !RefUnwindSafe for SubscriptionMap<K, V>
impl<K, V> Send for SubscriptionMap<K, V>
impl<K, V> Sync for SubscriptionMap<K, V>
impl<K, V> Unpin for SubscriptionMap<K, V>
impl<K, V> !UnwindSafe for SubscriptionMap<K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more