pub struct Handle<T> { /* private fields */ }Expand description
A clonable handle that can be used to remotely execute a closure on the corresponding actor
Implementations§
Source§impl<T> Handle<T>
impl<T> Handle<T>
Sourcepub fn create_cache_from_default(&self) -> Cache<T>
pub fn create_cache_from_default(&self) -> Cache<T>
Creates a cache from a custom value that can locally synchronize with the remote actor It does this through subscribing to broadcasted updates from the actor As it is not initialized with the current value, any updates before construction are missed. In case no updates are processed yet, the default value is returned
Source§impl<T> Handle<T>
impl<T> Handle<T>
Sourcepub async fn set_if_changed(&self, val: T)
pub async fn set_if_changed(&self, val: T)
Overwrites the inner value of the actor with the new value But does not broadcast if the value being set is equal to the current value in the handle
§Examples
let handle = Handle::new(None);
handle.set(Some(1)).await.unwrap();
assert_eq!(handle.get().await.unwrap(), Some(1));Source§impl<T> Handle<T>
impl<T> Handle<T>
Sourcepub async fn create_cache(&self) -> Cache<T>
pub async fn create_cache(&self) -> Cache<T>
Creates an itialized cache that can locally synchronize with the remote actor. It does this through subscribing to broadcasted updates from the actor. As it is initialized with the current value, any updates before construction are included
Sourcepub async fn spawn_throttle<C, F>(
&self,
client: C,
call: fn(&C, F),
freq: Frequency,
)
pub async fn spawn_throttle<C, F>( &self, client: C, call: fn(&C, F), freq: Frequency, )
Spawns a throttle that fires given a specificed Frequency, given any broadcasted updates by the actor.
Sourcepub async fn get(&self) -> T
pub async fn get(&self) -> T
Receives a clone of the current value of the actor
§Examples
let handle = Handle::new(1);
let result = handle.get().await;
assert_eq!(result.unwrap(), 1);Sourcepub async fn set(&self, val: T)
pub async fn set(&self, val: T)
Overwrites the inner value of the actor with the new value
§Examples
let handle = Handle::new(None);
handle.set(Some(1)).await.unwrap();
assert_eq!(handle.get().await.unwrap(), Some(1));Sourcepub fn subscribe(&self) -> Receiver<T>
pub fn subscribe(&self) -> Receiver<T>
Returns a receiver that receives all updated values from the actor Note that the inner value might not actually have changed. It broadcasts on any method that has a mutable reference to the actor.
§Examples
let handle = Handle::new(None);
let mut rx = handle.subscribe();
handle.set(Some("testing!")).await.unwrap();
assert_eq!(rx.recv().await.unwrap(), Some("testing!"));pub fn new(val: T) -> Handle<T>
Sourcepub fn new_throttled<C, F>(
val: T,
client: C,
call: fn(&C, F),
freq: Frequency,
) -> Handle<T>
pub fn new_throttled<C, F>( val: T, client: C, call: fn(&C, F), freq: Frequency, ) -> Handle<T>
Creates a new Handle and initializes a corresponding throttle The throttle fires given a specificed Frequency
pub async fn send_job( &self, call: Box<dyn FnMut(&mut Actor<T>, Box<dyn Any + Send>) -> BoxFuture<'_, Box<dyn Any + Send>> + Send + Sync>, args: Box<dyn Any + Send>, ) -> Box<dyn Any + Send>
Trait Implementations§
Source§impl<T> Default for Handle<T>
Implement default for any inner type that implements default aswell
impl<T> Default for Handle<T>
Implement default for any inner type that implements default aswell
Source§impl<K, V> HashMapHandle<K, V> for Handle<HashMap<K, V>>
impl<K, V> HashMapHandle<K, V> for Handle<HashMap<K, V>>
Source§async fn get_key(&self, key: K) -> Option<V>
async fn get_key(&self, key: K) -> Option<V>
Returns a clone of the value corresponding to the key if it exists It is equivalent to the Hashmap get(), but the method name is changed to avoid conflicts with the get() method of the actor in general
§Examples
let handle = Handle::new(HashMap::new());
handle.insert("test", 10).await.unwrap();
let res = handle.get_key("test").await.unwrap();
assert_eq!(res, Some(10));Source§async fn insert(&self, key: K, val: V) -> Option<V>
async fn insert(&self, key: K, val: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old value is returned.
In that case the key is not updated.
§Examples
let handle = Handle::new(HashMap::new());
let res = handle.insert("test", 10).await.unwrap();
assert_eq!(res, None);
let handle = actify::Handle::new(std::collections::HashMap::new());
handle.insert("test", 10).await.unwrap();
let old_value = handle.insert("test", 20).await.unwrap();
assert_eq!(old_value, Some(10));Source§impl<K> HashSetHandle<K> for Handle<HashSet<K>>
impl<K> HashSetHandle<K> for Handle<HashSet<K>>
Source§async fn insert(&self, val: K) -> bool
async fn insert(&self, val: K) -> bool
Adds a value to the set. Returns whether the value was newly inserted. That is:
- If the set did not previously contain this value, true is returned.
- If the set already contained this value, false is returned, and the set is not modified: original value is not replaced, and the value passed as argument is dropped.
§Examples
let handle = Handle::new(HashSet::new());
let res = handle.insert(10).await.unwrap();
assert_eq!(res, true);
let handle = actify::Handle::new(std::collections::HashSet::new());
handle.insert(10).await.unwrap();
let res = handle.insert(10).await.unwrap();
assert_eq!(res, false);Source§impl<T> OptionHandle<T> for Handle<Option<T>>
impl<T> OptionHandle<T> for Handle<Option<T>>
Source§impl<T> VecHandle<T> for Handle<Vec<T>>
impl<T> VecHandle<T> for Handle<Vec<T>>
Source§async fn push(&self, value: T)
async fn push(&self, value: T)
Appends an element to the back of a collection.
§Examples
let handle = actify::Handle::new(vec![1, 2]);
handle.push(100).await.unwrap();
assert_eq!(handle.get().await.unwrap(), vec![1, 2, 100]);Source§async fn is_empty(&self) -> bool
async fn is_empty(&self) -> bool
Returns true if the vector contains no elements.
§Examples
let handle = Handle::new(Vec::<i32>::new());
assert!(handle.is_empty().await.unwrap());Source§async fn drain<R>(&self, range: R) -> Vec<T>
async fn drain<R>(&self, range: R) -> Vec<T>
Removes the complete range from the vector in bulk, and returns it as a new vector
§Examples
let handle = Handle::new(vec![1, 2]);
let res = handle.drain(..).await.unwrap();
assert_eq!(res, vec![1, 2]);
assert_eq!(handle.get().await.unwrap(), Vec::<i32>::new());