Skip to main content

ReadHandle

Struct ReadHandle 

Source
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>

Source

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>

Source

pub async fn with<R, F>(&self, f: F) -> R
where F: FnOnce(&T) -> R + Send + 'static, R: Send + 'static,

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: Clone + Send + Sync + 'static, V> ReadHandle<T, V>

Source

pub async fn get(&self) -> T

Receives a clone of the current value of the actor.

§Examples
let handle = Handle::new(1);
let read_handle = handle.get_read_handle();
let result = read_handle.get().await;
assert_eq!(result, 1);
Source§

impl<T, V: Clone + Send + Sync + 'static> ReadHandle<T, V>

Source

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>

Source

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>
where T: Clone + BroadcastAs<V> + Send + Sync + 'static, V: Clone + Send + Sync + 'static,

Source

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.

Trait Implementations§

Source§

impl<T, V> Clone for ReadHandle<T, V>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<T, V> Debug for ReadHandle<T, V>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, V> Freeze for ReadHandle<T, V>

§

impl<T, V = T> !RefUnwindSafe for ReadHandle<T, V>

§

impl<T, V> Send for ReadHandle<T, V>
where V: Send,

§

impl<T, V> Sync for ReadHandle<T, V>
where V: Send,

§

impl<T, V> Unpin for ReadHandle<T, V>

§

impl<T, V> UnsafeUnpin for ReadHandle<T, V>

§

impl<T, V = T> !UnwindSafe for ReadHandle<T, V>

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> BroadcastAs<T> for T
where T: Clone,

Source§

fn to_broadcast(&self) -> T

Source§

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

Source§

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

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

Source§

fn parse(&self) -> T

Implement this parse function on the type to be sent by the throttle
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.