ActorBuilder

Struct ActorBuilder 

Source
pub struct ActorBuilder<A>
where A: Actor + 'static,
{ /* private fields */ }
Expand description

Used to create Actors in the ActorSystem

Each builder has access to all Mailbox<A> objects within the ActorSystem and is able to provide a copy to an existing ActorRef<A> if the address is already in use

See .spawn() for a detailed explanation

Implementations§

Source§

impl<A> ActorBuilder<A>
where A: Actor + Handler<SerializedMessage> + 'static,

Source

pub fn new( system: ActorSystem, system_state: SystemState, wakeup_manager: WakeupManager, internal_actor_manager: InternalActorManager, ) -> ActorBuilder<A>

This is called through ActorSystem.builder

Source

pub fn set_pool_name(self, pool_name: impl Into<String>) -> ActorBuilder<A>

Source

pub fn set_message_throughput( self, message_throughput: usize, ) -> ActorBuilder<A>

Source

pub fn set_mailbox_unbounded(self) -> ActorBuilder<A>

Source

pub fn set_mailbox_size(self, mailbox_size: usize) -> ActorBuilder<A>

Source

pub fn spawn<P>( &self, name: impl Into<String>, props: P, ) -> Result<ActorWrapper<A>, ActorError>
where P: ActorFactory<A> + 'static,

Creates the defined Actor on the ActorSystem

§Returns

Ok(ActorWrapper<A>) if actor was created successfully

Ok(ActorWrapper<A>) if the actor is already running on the system

Err(ActorError) see ActorError for detailed information

§Examples
use tyra::prelude::*;
use std::error::Error;
use std::time::Duration;

struct TestActor {}
impl TestActor {
    pub fn new() -> Self {
        Self {}
    }
}
impl Actor for TestActor {}

struct TestActorFactory {}
impl TestActorFactory {
    pub fn new() -> Self {
        Self {}
    }
}
impl ActorFactory<TestActor> for TestActorFactory {
    fn new_actor(&mut self, _context: ActorContext<TestActor>) -> Result<TestActor, Box<dyn Error>> {
        Ok(TestActor::new())
    }
}

struct SecondActor {}
impl SecondActor {
    pub fn new() -> Self {
        Self {}
    }
}
impl Actor for SecondActor {}

struct SecondActorFactory {}
impl SecondActorFactory {
    pub fn new() -> Self {
        Self {}
    }
}
impl ActorFactory<SecondActor> for SecondActorFactory {
    fn new_actor(&mut self, _context: ActorContext<SecondActor>) -> Result<SecondActor, Box<dyn Error>> {
        let error = std::io::Error::from_raw_os_error(1337);
        return Err(Box::new(error));
    }
}

#[ntest::timeout(10000)]
fn main() {
    let mut actor_config = TyraConfig::new().unwrap();
    actor_config.thread_pool.config.insert(String::from("default"), ThreadPoolConfig::new(1, 1, 1, 1.0));
    let actor_system = ActorSystem::new(actor_config);
    let actor_name = "test";

    //this works, because there's no actor called `test` yet on the pool
    let this_works = actor_system.builder().spawn(actor_name, TestActorFactory::new());
    assert!(this_works.is_ok(), "The actor could not be spawned");

    //this works, because there's already an actor called `test` with type `TestActor` on the pool, therefore the result is the same actor that was created in the previous spawn command
    let this_works_as_well = actor_system.builder().spawn(actor_name, TestActorFactory::new());
    assert!(this_works_as_well.is_ok(), "The `ActorWrapper` could not be fetched");

    //this does not work, because the pool is currently configured to only allow a single actor
    let pool_full = actor_system.builder().spawn("full", TestActorFactory::new());
    assert!(pool_full.is_err(), "The actor could not be spawned");
    let err = pool_full.err().unwrap();
    assert_eq!(err, ActorError::ThreadPoolHasTooManyActorsError, "Error is not correct");

    //this does not work, because the pool does not exist in the configuration
    let invalid_pool = actor_system.builder().set_pool_name("invalid").spawn(actor_name, TestActorFactory::new());
    assert!(invalid_pool.is_err(), "The Actor was spawned");
    let err = invalid_pool.err().unwrap();
    assert_eq!(err, ActorError::ThreadPoolDoesNotExistError, "Error is not correct");

    //this does not work, because although there's not yet an actor called `second` on the pool the `new_actor` method returns an error
    let this_is_not_working = actor_system.builder().spawn("second", SecondActorFactory::new());
    assert!(this_is_not_working.is_err(), "The SecondActor was spawned");
    let err = this_is_not_working.err().unwrap();
    assert_eq!(err, ActorError::InitError, "Error is not correct");

    //this does not work, because there's already an actor called `test` with a different type on the pool
    let this_is_not_working_either = actor_system.builder().spawn(actor_name, SecondActorFactory::new());
    assert!(this_is_not_working_either.is_err(), "Illegal Actor type conversion");
    let err = this_is_not_working_either.err().unwrap();
    assert_eq!(err, ActorError::InvalidActorTypeError, "Error is not correct");

    actor_system.stop(Duration::from_millis(1000));
    std::process::exit(actor_system.await_shutdown());
}

Trait Implementations§

Source§

impl<A> Clone for ActorBuilder<A>
where A: Actor + 'static + Clone,

Source§

fn clone(&self) -> ActorBuilder<A>

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

Auto Trait Implementations§

§

impl<A> Freeze for ActorBuilder<A>

§

impl<A> !RefUnwindSafe for ActorBuilder<A>

§

impl<A> Send for ActorBuilder<A>

§

impl<A> Sync for ActorBuilder<A>

§

impl<A> Unpin for ActorBuilder<A>

§

impl<A> !UnwindSafe for ActorBuilder<A>

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