Skip to main content

Supervisor

Struct Supervisor 

Source
pub struct Supervisor<E: Event, T: Topic<E> = DefaultTopic> { /* private fields */ }
Expand description

Coordinates actors and the broker, and owns the top-level runtime.

§Actor Registration

// Subscribe to specific topics
supervisor.add_actor("processor", |ctx| Processor::new(ctx), &[MyTopic::Data])?;

// Subscribe to all topics (e.g., monitoring)
supervisor.add_actor("monitor", |ctx| Monitor::new(ctx), Subscribe::all())?;

// Subscribe to no topics (pure event producer)
supervisor.add_actor("producer", |ctx| Producer::new(ctx), Subscribe::none())?;

§Runtime Control

  • start() spawns the broker loop and returns immediately (non-blocking).
  • join() awaits all actor tasks to finish; typically used after start().
  • run() combines start() and join(), blocking until shutdown.
  • stop() graceful shutdown; lets actors consume active events
  • send(event) emits events into the broker.

See also: Actor, Context, Topic.

Implementations§

Source§

impl<E: Event, T: Topic<E>> Supervisor<E, T>

Source

pub fn new(config: Config) -> Self

Create a new supervisor with the given runtime configuration.

Source

pub fn add_actor<A, F, S>( &mut self, name: &str, factory: F, topics: S, ) -> Result<ActorId>
where A: Actor<Event = E>, F: FnOnce(Context<E>) -> A, S: Into<Subscribe<E, T>>,

Register a new actor with a factory that receives a Context<E>.

This is the primary way to register actors with the supervisor.

§Arguments
  • name - Actor identifier used for metadata and routing
  • factory - Closure that receives a Context and returns the actor
  • topics - Slice of topics the actor subscribes to
§Example
supervisor.add_actor(
    "processor",
    |ctx| DataProcessor::new(ctx),
    &[MyTopic::Data, MyTopic::Control]
)?;
Source

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

Start the broker loop in a background task. This returns immediately.

Source

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

Waits until at least one of the actor tasks completes then triggers a shutdown if not already requested.

Source

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

Convenience method to start and then await completion of all tasks. Blocks until shutdown.

Source

pub async fn send(&self, event: E) -> Result<()>

Emit an event into the broker from the supervisor.

Source

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

Request a graceful shutdown, then await all actor tasks.

§Shutdown Process
  1. Waits for the broker to receive all pending events (up to 10 ms)
  2. Stops the broker and waits for it to drain actor queues
  3. Cancels all actors and waits for tasks t
Source

pub fn config(&self) -> &Config

Trait Implementations§

Source§

impl<E: Event, T: Topic<E>> Default for Supervisor<E, T>

Source§

fn default() -> Self

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

impl<E: Event, T: Topic<E>> Drop for Supervisor<E, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<E, T> Freeze for Supervisor<E, T>

§

impl<E, T = DefaultTopic> !RefUnwindSafe for Supervisor<E, T>

§

impl<E, T> Send for Supervisor<E, T>

§

impl<E, T> Sync for Supervisor<E, T>

§

impl<E, T> Unpin for Supervisor<E, T>

§

impl<E, T = DefaultTopic> !UnwindSafe for Supervisor<E, 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, 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.