[][src]Struct maxim::actors::Aid

pub struct Aid { /* fields omitted */ }

Encapsulates an Actor ID and is used to send messages to the actor.

This is a unique reference to the actor within the entire cluster and can be used to send messages to the actor regardless of location. The Aid does the heavy lifting of deciding where the actor is and sending the message. However it is important that the user at least has some notion of where the actor is for developing an efficient actor architecture. This aid can also be serialized to a remote system and then back to the system hosting the actor without issue. Often Aids are passed around an actor system so this is a common use case.

Methods

impl Aid[src]

pub fn send(&self, message: Message) -> Result<(), AidError>[src]

Attempts to send a message to the actor with the given Aid and returns std::Result::Ok when the send was successful or a std::Result::Err<MaximError> if something went wrong with the send. Note that if a user just calls send(msg).unwrap(), a panic could take down the dispatcher thread and thus eventually hang the process.

Examples

use maxim::prelude::*;
use std::sync::Arc;
use std::time::Duration;

let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2));

let aid = system
    .spawn()
    .with(
        0 as usize,
        |state: usize, context: Context, message: Message| async move {
            if let Some(_) = message.content_as::<i32>() {
                context.system.trigger_shutdown();
            }
            Ok(Status::done(state))
       },
    )
    .unwrap();

match aid.send(Message::new(11)) {
    Ok(_) => println!("OK Then!"),
    Err(e) => println!("Ooops {:?}", e),
}

system.await_shutdown(None);

pub fn send_arc<T>(&self, value: Arc<T>) -> Result<(), AidError> where
    T: 'static + ActorMessage
[src]

Shortcut for calling send(Message::from_arc(arc)) This method will internally wrap the Arc passed into a Message and try to send it. Note that using this method is much more efficient than send_new if you want to send an Arc that you already have. The Arc sent will be transferred to the ownership of the Aid.

use maxim::prelude::*;
use std::sync::Arc;
use std::time::Duration;

let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2));

let aid = system
    .spawn()
    .with(
        0 as usize,
        |state: usize, context: Context, message: Message| async move {
            if let Some(_) = message.content_as::<i32>() {
                context.system.trigger_shutdown();
            }
            Ok(Status::done(state))
       },
    )
    .unwrap();

let arc = Arc::new(11 as i32);
match aid.send_arc(arc.clone()) {
    Ok(_) => println!("OK Then!"),
    Err(e) => println!("Ooops {:?}", e),
}

system.await_shutdown(None);

pub fn send_new<T>(&self, value: T) -> Result<(), AidError> where
    T: 'static + ActorMessage
[src]

Shortcut for calling send(Message::new(value)) This method will internally wrap whatever it is passed into a Message and try to send it. This method would not be appropriate if you want to re-send a message as it would wrap the message again with the same result as if the the code called aid.send(Message::new(Message::new(value))). If the code wishes to resend a message it should just call just call send(msg).

use maxim::prelude::*;
use std::sync::Arc;
use std::time::Duration;

let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2));

let aid = system
    .spawn()
    .with(
        0 as usize,
        |state: usize, context: Context, message: Message| async move {
            if let Some(_) = message.content_as::<i32>() {
                context.system.trigger_shutdown();
            }
            Ok(Status::done(state))
       },
    )
    .unwrap();

match aid.send_new(11) {
    Ok(_) => println!("OK Then!"),
    Err(e) => println!("Ooops {:?}", e),
}

system.await_shutdown(None);

pub fn send_after(
    &self,
    message: Message,
    duration: Duration
) -> Result<(), AidError>
[src]

Schedules the given message to be sent after a minimum of the specified duration. Note that Maxim doesn't guarantee that the message will be sent on exactly now + duration but rather that at least the duration will pass before the message is sent to the actor. Maxim will try to send as close as possible without going under the amount but precise timing should not be depended on. This method will return an Err if the actor has been stopped or Ok if the message was scheduled to be sent. If the actor is stopped before the duration passes then the scheduled message will never get to the actor.

Examples

use maxim::prelude::*;
use std::sync::Arc;
use std::time::Duration;

let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2));

let aid = system
    .spawn()
    .with(
        0 as usize,
        |state: usize, context: Context, message: Message| async move {
            if let Some(_) = message.content_as::<i32>() {
                context.system.trigger_shutdown();
            }
            Ok(Status::done(state))
       },
    )
    .unwrap();

match aid.send_after(Message::new(11), Duration::from_millis(1)) {
    Ok(_) => println!("OK Then!"),
    Err(e) => println!("Ooops {:?}", e),
}

system.await_shutdown(None);

pub fn send_arc_after<T>(
    &self,
    value: Arc<T>,
    duration: Duration
) -> Result<(), AidError> where
    T: 'static + ActorMessage
[src]

Shortcut for calling send_after(Message::from_arc(arc)) This method will internally wrap the Arc passed into a Message and try to send it. Note that using this method is much more efficient than send_new_after if you want to send an Arc that you already have.

Examples

use maxim::prelude::*;
use std::sync::Arc;
use std::time::Duration;

let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2));

let aid = system
    .spawn()
    .with(
        0 as usize,
        |state: usize, context: Context, message: Message| async move {
            if let Some(_) = message.content_as::<i32>() {
                context.system.trigger_shutdown();
            }
            Ok(Status::done(state))
       },
    )
    .unwrap();

let arc = Arc::new(11);
match aid.send_arc_after(arc.clone(), Duration::from_millis(1)) {
    Ok(_) => println!("OK Then!"),
    Err(e) => println!("Ooops {:?}", e),
}

system.await_shutdown(None);

pub fn send_new_after<T>(
    &self,
    value: T,
    duration: Duration
) -> Result<(), AidError> where
    T: 'static + ActorMessage
[src]

Shortcut for calling send_after(Message::new(value)) This method will internally wrap whatever it is passed into a Message and try to send it. This method would not be appropriate if you want to re-send a message as it would wrap the message again with the same result as if the the code called aid.send_after(Message::new(Message::new(value))). If the code wishes to resend a message it should just call just call send(msg).

Examples

use maxim::prelude::*;
use std::sync::Arc;
use std::time::Duration;

let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2));

let aid = system
    .spawn()
    .with(
        0 as usize,
        |state: usize, context: Context, message: Message| async move {
            if let Some(_) = message.content_as::<i32>() {
                context.system.trigger_shutdown();
            }
            Ok(Status::done(state))
       },
    )
    .unwrap();

match aid.send_new_after(11, Duration::from_millis(1)) {
    Ok(_) => println!("OK Then!"),
    Err(e) => println!("Ooops {:?}", e),
}

system.await_shutdown(None);

pub fn uuid(&self) -> Uuid[src]

The unique UUID for this actor within the entire cluster. The UUID for an Aid is generated with a v4 random UUID so the chances of collision are not worth considering.

pub fn system_uuid(&self) -> Uuid[src]

The unique UUID for the actor system that this actor lives on. As with uuid this value is a v4 UUID and so the chances of two systems having the same uuid is inconsequential.

pub fn name(&self) -> Option<String>[src]

The name of the actor as assigned by the user at spawn time if any. Note that this name is guaranteed to be unique only within the actor system in which the actor was spawned; no guarantees are made that the name will be unique within a cluster of actor systems.

pub fn name_or_uuid(&self) -> String[src]

Returns the name assigned to the Aid if it is not a None and otherwise returns the uuid of the actor as a string.

pub fn is_local(&self) -> bool[src]

Determines if this actor lives on the local actor system or another system in the same process. Actors that are local to each other can exchange large amounts of data efficiently through passing Arcs.

pub fn sent(&self) -> Result<usize, AidError>[src]

Determines how many messages the actor with the Aid has been sent. This method works only for local Aids, remote Aids will return an error if this is called.

pub fn received(&self) -> Result<usize, AidError>[src]

Determines how many messages the actor with the Aid has received. This method works only for local Aids, remote Aids will return an error if this is called.

pub fn ptr_eq(left: &Aid, right: &Aid) -> bool[src]

Checks to see if the left and right aid actually point at the exact same actor.

Trait Implementations

impl AidPool for Aid[src]

fn send(&mut self, message: Message) -> Result<(), AidError>[src]

fn send_arc<T>(&mut self, value: Arc<T>) -> Result<(), AidError> where
    T: 'static + ActorMessage
[src]

fn send_new<T>(&mut self, value: T) -> Result<(), AidError> where
    T: 'static + ActorMessage
[src]

fn send_after(
    &mut self,
    message: Message,
    duration: Duration
) -> Result<(), AidError>
[src]

fn send_arc_after<T>(
    &mut self,
    value: Arc<T>,
    duration: Duration
) -> Result<(), AidError> where
    T: 'static + ActorMessage
[src]

fn send_new_after<T>(
    &mut self,
    value: T,
    duration: Duration
) -> Result<(), AidError> where
    T: 'static + ActorMessage
[src]

impl Clone for Aid[src]

impl Debug for Aid[src]

impl<'de> Deserialize<'de> for Aid[src]

impl Display for Aid[src]

impl Eq for Aid[src]

impl Hash for Aid[src]

impl Ord for Aid[src]

impl PartialEq<Aid> for Aid[src]

impl PartialOrd<Aid> for Aid[src]

impl Serialize for Aid[src]

Auto Trait Implementations

impl !RefUnwindSafe for Aid

impl Send for Aid

impl Sync for Aid

impl Unpin for Aid

impl !UnwindSafe for Aid

Blanket Implementations

impl<T> ActorMessage for T where
    T: 'static + Serialize + DeserializeOwned + Sync + Send + Any + ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]

impl<T> Erased for T

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SyncAidPool for T where
    T: 'static + AidPool + Clone + Send + Sync
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,