Skip to main content

ActorRuntime

Struct ActorRuntime 

Source
pub struct ActorRuntime(/* private fields */);
Expand description

Represents the initialized and active Acton actor system runtime.

This struct is obtained after successfully launching the system via [ActonApp::launch_async().await]. It holds the internal state of the running system, including a reference to the central message broker and a registry of top-level actors.

ActorRuntime provides the primary methods for interacting with the system as a whole, such as creating new top-level actors (new_actor, spawn_actor, etc.) and initiating a graceful shutdown of all actors (shutdown_all).

It is cloneable, allowing different parts of an application to hold references to the runtime environment.

Implementations§

Source§

impl ActorRuntime

Source

pub fn new_actor_with_name<State>( &mut self, name: String, ) -> ManagedActor<Idle, State>
where State: Default + Send + Debug + 'static,

Creates a new top-level actor builder (ManagedActor<Idle, State>) with a specified root name.

This method initializes a ManagedActor in the Idle state, configured with a root Ern derived from the provided name and linked to the system’s broker. The actor is registered as a top-level actor within the runtime.

The returned actor is ready for further configuration (e.g., adding message handlers via act_on) before being started by calling .start() on it.

§Type Parameters
  • State: The user-defined state type for the actor. Must implement Default, Send, Debug, and be 'static.
§Arguments
  • name: A string that will form the root name of the actor’s Ern.
§Returns

A ManagedActor<Idle, State> instance, ready for configuration and starting.

§Panics

Panics if creating the root Ern from the provided name fails or if creating the internal ActorConfig fails.

Source

pub fn new_actor<State>(&mut self) -> ManagedActor<Idle, State>
where State: Default + Send + Debug + 'static,

Creates a new top-level actor builder (ManagedActor<Idle, State>) with a default name (“actor”).

Similar to ActorRuntime::new_actor_with_name, but uses a default root name “actor” for the actor’s Ern. The actor is registered as a top-level actor within the runtime.

The returned actor is ready for further configuration before being started via .start().

§Type Parameters
  • State: The user-defined state type for the actor. Must implement Default, Send, Debug, and be 'static.
§Returns

A ManagedActor<Idle, State> instance, ready for configuration and starting.

§Panics

Panics if creating the internal ActorConfig fails.

Source

pub fn actor_count(&self) -> usize

Returns the number of top-level actors currently registered in the runtime.

This count only includes actors directly created via the ActorRuntime and does not include child actors supervised by other actors.

Source

pub fn new_actor_with_config<State>( &mut self, config: ActorConfig, ) -> ManagedActor<Idle, State>
where State: Default + Send + Debug + 'static,

Creates a new top-level actor builder (ManagedActor<Idle, State>) using a provided configuration.

This method initializes a ManagedActor in the Idle state using the specified ActorConfig. It ensures the actor is configured with the system’s broker if not already set in the config. The actor is registered as a top-level actor within the runtime.

The returned actor is ready for further configuration before being started via .start().

§Type Parameters
  • State: The user-defined state type for the actor. Must implement Default, Send, Debug, and be 'static.
§Arguments
  • config: The ActorConfig to use for the new actor. The broker field will be overridden with the system broker if it’s None.
§Returns

A ManagedActor<Idle, State> instance, ready for configuration and starting.

Source

pub fn broker(&self) -> ActorHandle

Returns a clone of the handle ([BrokerRef]) to the system’s central message broker.

Source

pub async fn spawn_actor_with_setup_fn<State>( &mut self, config: ActorConfig, setup_fn: impl FnOnce(ManagedActor<Idle, State>) -> Pin<Box<dyn Future<Output = ActorHandle> + Send + 'static>>, ) -> Result<ActorHandle>
where State: Default + Send + Debug + 'static,

Creates, configures, and starts a top-level actor using a provided configuration and setup function.

This method combines actor creation (using config), custom asynchronous setup (setup_fn), and starting the actor. The setup_fn receives the actor in the Idle state, performs necessary configurations (like adding message handlers), and must call .start() to transition the actor to the Started state, returning its ActorHandle.

The actor is registered as a top-level actor within the runtime.

§Type Parameters
  • State: The state type of the actor. Must implement Default, Send, Debug, and be 'static.
§Arguments
  • config: The ActorConfig to use for creating the actor. The broker field will be overridden with the system broker if it’s None.
  • setup_fn: An asynchronous closure that takes the ManagedActor<Idle, State>, configures it, calls .start(), and returns the resulting ActorHandle. The closure must be Send + 'static.
§Returns

A Result containing the ActorHandle of the successfully spawned actor, or an error if actor creation or the setup_fn fails.

Source

pub async fn shutdown_all(&mut self) -> Result<()>

Initiates a graceful shutdown of the entire Acton system.

This method attempts to stop all registered top-level actors (and consequently their descendant children through the stop propagation mechanism) by sending them a [SystemSignal::Terminate]. It waits for all top-level actor tasks to complete. Finally, it stops the central message broker actor.

§Returns

An anyhow::Result<()> indicating whether the shutdown process completed successfully. Errors during the stopping of individual actors or the broker will be propagated.

Source

pub async fn spawn_actor<State>( &mut self, setup_fn: impl FnOnce(ManagedActor<Idle, State>) -> Pin<Box<dyn Future<Output = ActorHandle> + Send + 'static>>, ) -> Result<ActorHandle>
where State: Default + Send + Debug + 'static,

Creates, configures, and starts a top-level actor using a default configuration and a setup function.

This is a convenience method similar to ActorRuntime::spawn_actor_with_setup_fn, but it automatically creates a default ActorConfig (with a default name and the system broker). The provided setup_fn configures and starts the actor.

The actor is registered as a top-level actor within the runtime.

§Type Parameters
  • State: The state type of the actor. Must implement Default, Send, Debug, and be 'static.
§Arguments
  • setup_fn: An asynchronous closure that takes the ManagedActor<Idle, State>, configures it, calls .start(), and returns the resulting ActorHandle. The closure must be Send + 'static.
§Returns

A Result containing the ActorHandle of the successfully spawned actor, or an error if actor creation or the setup_fn fails.

§Errors

Returns an error if the default ActorConfig cannot be created.

Trait Implementations§

Source§

impl Clone for ActorRuntime

Source§

fn clone(&self) -> ActorRuntime

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 Debug for ActorRuntime

Source§

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

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

impl Default for ActorRuntime

Source§

fn default() -> ActorRuntime

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

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> ActonMessage for T
where T: Any + Send + Sync + Debug + DynClone + 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Implementation of as_any for the blanket impl.

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Implementation of as_any_mut for the blanket impl.

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more