Struct actify::Handle

source ·
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>
where T: Clone + Debug + Send + Sync + 'static + Default,

source

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

source

pub async fn create_cache(&self) -> Result<Cache<T>, ActorError>

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

source

pub fn capacity(&self) -> usize

Returns the current capacity of the channel

source

pub fn spawn_throttle<C, F>( &self, client: C, call: fn(_: &C, _: F), freq: Frequency ) -> Result<(), ActorError>
where C: Send + Sync + 'static, T: Throttled<F>, F: Clone + Send + Sync + 'static,

Spawns a throttle that fires given a specificed Frequency, given any broadcasted updates by the actor.

source

pub async fn get(&self) -> Result<T, ActorError>

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);
source

pub async fn set(&self, val: T) -> Result<(), ActorError>

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));
source

pub async fn shutdown(&self) -> Result<(), ActorError>

Performs a shutdown of the actor Any subsequent calls with lead to an error being returned

§Examples
let handle = Handle::new(1);
handle.shutdown().await.unwrap();
assert!(handle.get().await.is_err());
source

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!"));
source

pub fn new(val: T) -> Handle<T>

source

pub fn new_throttled<C, F>( val: T, client: C, call: fn(_: &C, _: F), freq: Frequency ) -> Result<Handle<T>, ActorError>
where C: Send + Sync + 'static, T: Throttled<F>, F: Clone + Send + Sync + 'static,

Creates a new Handle and initializes a corresponding throttle The throttle fires given a specificed Frequency

source

pub async fn send_job( &self, call: FnType<T>, args: Box<dyn Any + Send> ) -> Result<Box<dyn Any + Send>, ActorError>

Trait Implementations§

source§

impl<T: Clone> Clone for Handle<T>

source§

fn clone(&self) -> Handle<T>

Returns a copy 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: Debug> Debug for Handle<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T> Default for Handle<T>
where T: Default + Clone + Debug + Send + Sync + 'static,

Implement default for any inner type that implements default aswell

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<K, V> HashMapHandle<K, V> for Handle<HashMap<K, V>>
where K: Clone + Debug + Eq + Hash + Send + Sync + 'static, V: Clone + Debug + Send + Sync + 'static,

source§

fn get_key<'life0, 'async_trait>( &'life0 self, key: K ) -> Pin<Box<dyn Future<Output = Result<Option<V>, ActorError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

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§

fn insert<'life0, 'async_trait>( &'life0 self, key: K, val: V ) -> Pin<Box<dyn Future<Output = Result<Option<V>, ActorError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

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§

fn is_empty<'life0, 'async_trait>( &'life0 self ) -> Pin<Box<dyn Future<Output = Result<bool, ActorError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Returns true if the map contains no elements.

§Examples
let handle = Handle::new(HashMap::<&str, i32>::new());
assert!(handle.is_empty().await.unwrap());
source§

impl<T> OptionHandle<T> for Handle<Option<T>>
where T: Clone + Debug + Send + Sync + 'static,

source§

fn is_some<'life0, 'async_trait>( &'life0 self ) -> Pin<Box<dyn Future<Output = Result<bool, ActorError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Returns true if the option is a Some value.

§Examples
let handle = Handle::new(Some(1));
assert_eq!(handle.is_some().await.unwrap(), true);
source§

fn is_none<'life0, 'async_trait>( &'life0 self ) -> Pin<Box<dyn Future<Output = Result<bool, ActorError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Returns true if the option is a None value.

§Examples
let handle = Handle::new(Option::<i32>::None);
assert!(handle.is_none().await.unwrap());
source§

impl<T> VecHandle<T> for Handle<Vec<T>>
where T: Clone + Debug + Send + Sync + 'static,

source§

fn push<'life0, 'async_trait>( &'life0 self, value: T ) -> Pin<Box<dyn Future<Output = Result<(), ActorError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

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§

fn is_empty<'life0, 'async_trait>( &'life0 self ) -> Pin<Box<dyn Future<Output = Result<bool, ActorError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Returns true if the vector contains no elements.

§Examples
let handle = Handle::new(Vec::<i32>::new());
assert!(handle.is_empty().await.unwrap());
source§

fn drain<'life0, 'async_trait, R>( &'life0 self, range: R ) -> Pin<Box<dyn Future<Output = Result<Vec<T>, ActorError>> + Send + 'async_trait>>
where R: RangeBounds<usize> + Send + Sync + 'static + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

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());

Auto Trait Implementations§

§

impl<T> Freeze for Handle<T>

§

impl<T> !RefUnwindSafe for Handle<T>

§

impl<T> Send for Handle<T>
where T: Send,

§

impl<T> Sync for Handle<T>
where T: Send,

§

impl<T> Unpin for Handle<T>

§

impl<T> !UnwindSafe for Handle<T>

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

§

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

§

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

§

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.