pub struct ReadHandle<T, V = T>(/* private fields */);Expand description
A clonable read-only handle that can only be used to read the internal value.
Obtained via Handle::get_read_handle. Supports ReadHandle::get,
ReadHandle::with, ReadHandle::subscribe, and ReadHandle::create_cache.
Implementations§
Source§impl<T, V> ReadHandle<T, V>
impl<T, V> ReadHandle<T, V>
Sourcepub fn subscribe(&self) -> Receiver<V>
pub fn subscribe(&self) -> Receiver<V>
Returns a tokio::sync::broadcast::Receiver that receives all broadcasted values.
§Examples
let handle = Handle::new(None);
let read_handle = handle.get_read_handle();
let mut rx = read_handle.subscribe();
handle.set(Some("testing!")).await;
assert_eq!(rx.recv().await.unwrap(), Some("testing!"));Source§impl<T: Send + Sync + 'static, V> ReadHandle<T, V>
impl<T: Send + Sync + 'static, V> ReadHandle<T, V>
Sourcepub async fn with<R, F>(&self, f: F) -> R
pub async fn with<R, F>(&self, f: F) -> R
Runs a read-only closure on the actor’s value and returns the result.
This is especially useful for non-Clone types where ReadHandle::get
is not available, but it works with any actor type.
§Examples
// A non-Clone type — get() is not available on ReadHandle
struct Inventory { items: Vec<String> }
#[derive(Clone, Debug)]
struct Count(usize);
impl BroadcastAs<Count> for Inventory {
fn to_broadcast(&self) -> Count { Count(self.items.len()) }
}
let handle: Handle<Inventory, Count> = Handle::new(Inventory {
items: vec!["sword".into(), "shield".into()],
});
let read_handle = handle.get_read_handle();
// Read parts of the value without cloning the whole thing
let count = read_handle.with(|inv| inv.items.len()).await;
assert_eq!(count, 2);
let first = read_handle.with(|inv| inv.items[0].clone()).await;
assert_eq!(first, "sword");Source§impl<T, V: Clone + Send + Sync + 'static> ReadHandle<T, V>
impl<T, V: Clone + Send + Sync + 'static> ReadHandle<T, V>
Sourcepub fn create_cache_from(&self, initial_value: V) -> Cache<V>
pub fn create_cache_from(&self, initial_value: V) -> Cache<V>
Creates a Cache initialized with the given value that locally synchronizes
with broadcasted updates from the actor.
Source§impl<T, V: Default + Clone + Send + Sync + 'static> ReadHandle<T, V>
impl<T, V: Default + Clone + Send + Sync + 'static> ReadHandle<T, V>
Sourcepub fn create_cache_from_default(&self) -> Cache<V>
pub fn create_cache_from_default(&self) -> Cache<V>
Creates a Cache initialized with V::default() that locally synchronizes
with broadcasted updates from the actor.
Source§impl<T, V> ReadHandle<T, V>
impl<T, V> ReadHandle<T, V>
Sourcepub async fn create_cache(&self) -> Cache<V>
pub async fn create_cache(&self) -> Cache<V>
Creates an initialized Cache that locally synchronizes with the remote actor.
As it is initialized with the current value, any updates before construction are included.