Skip to main content

Environment

Struct Environment 

Source
pub struct Environment { /* private fields */ }
Expand description

High-level container that owns one or more runtimes, exposes a unified event receiver, and provides lifecycle helpers for running and shutting down the underlying actor system.

§Run lifecycle state machine

An internal [RuntimeLaunchState] records how runtimes were last started:

  • Idle — no active launch mode; safe to call run() or run_background().
  • Managedrun spawned a join handle tracked by this environment. Await it with wait or stop with shutdown.
  • Backgroundrun_background started runtimes without storing a handle on the environment. Call shutdown before run().

run() and run_background() both call reconcile_finished_managed_launch first. When a managed run task finished without wait() or shutdown(), that helper joins the stale handle and surfaces any runtime or join error before a new launch proceeds.

wait temporarily takes the managed join handle. If the future is dropped early (for example when another branch wins in tokio::select!), [RestoreRunHandleOnDrop] puts the handle back so a later shutdown() can still stop runtimes and join the task. A successful wait() clears the handle and returns the launch state to Idle.

Implementations§

Source§

impl Environment

Source

pub fn new(config: Option<EnvironmentConfig>) -> Self

Create a new environment with optional configuration.

Source

pub async fn register_runtime( &mut self, runtime: Arc<dyn Runtime>, ) -> Result<(), Error>

Register a runtime with this environment and make it the default if none is set yet.

Source

pub fn config(&self) -> &EnvironmentConfig

Access the environment configuration.

Source

pub async fn get_runtime( &self, runtime_id: &RuntimeID, ) -> Option<Arc<dyn Runtime>>

Get a runtime by its id, if present.

Source

pub async fn get_runtime_or_default( &self, runtime_id: Option<RuntimeID>, ) -> Result<Arc<dyn Runtime>, Error>

Get the specified runtime or the default one when None is passed.

Source

pub fn run(&mut self) -> Result<(), EnvironmentError>

Start all registered runtimes in the background.

Stores the spawned task handle so shutdown can stop runtimes and await completion. Returns EnvironmentError::AlreadyRunning if a run task is already in progress.

If a previous managed run task finished without wait or shutdown, its result is joined and returned before spawning a new run task.

Use wait to await the background run task, or shutdown to stop runtimes and join the task.

Source

pub async fn wait(&mut self) -> Result<Result<(), RuntimeError>, JoinError>

Await the background task started by run.

Returns Ok(Ok(())) when no run task has been started. After the task completes, the stored handle is cleared so subsequent calls return immediately.

If this future is dropped before completion (for example when a tokio::select! branch wins elsewhere), the join handle is restored on the environment so a later shutdown can still stop runtimes and join the run task.

Source

pub async fn run_background(&mut self) -> Result<(), EnvironmentError>

Start all registered runtimes and return immediately without waiting for completion.

Cannot be combined with run on the same environment instance without calling shutdown first.

Source

pub async fn take_event_receiver( &mut self, runtime_id: Option<RuntimeID>, ) -> Result<BoxEventStream<Event>, EnvironmentError>

Take the event receiver for a specific runtime (or the default one) so the caller can consume protocol events. This can only be taken once.

Source

pub async fn subscribe_events( &self, runtime_id: Option<RuntimeID>, ) -> Result<BoxEventStream<Event>, EnvironmentError>

Subscribe to runtime events without consuming the receiver.

Source

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

Request shutdown on all runtimes and await the run handle if present.

Source

pub fn is_running(&self) -> bool

Returns whether the environment has an active runtime launch.

For run this checks the managed join handle. For run_background this returns true until shutdown clears the launch state.

Auto Trait Implementations§

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Message for T
where T: Any + Send + 'static,

Source§

fn from_boxed(m: BoxedMessage) -> Result<Self, BoxedDowncastErr>

Convert a BoxedMessage to this concrete type
Source§

fn box_message(self, pid: &ActorId) -> Result<BoxedMessage, BoxedDowncastErr>

Convert this message to a BoxedMessage
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> State for T
where T: Any + Send + 'static,

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