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

Returns the supervisor’s configuration.

Source

pub fn monitors(&mut self) -> &mut MonitorRegistry<E, T>

Available on crate feature monitoring only.
Source§

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

Source

pub fn to_mermaid(&self) -> String

Generate a Mermaid flowchart showing actor subscriptions.

Topics are shown as circles, actors as boxes. Arrows indicate that an actor subscribes to (receives events from) a topic.

Actors with Subscribe::all() are connected to all known topics. Actors with Subscribe::none() appear isolated (no incoming arrows).

§Example output
flowchart LR
    SensorData((SensorData)) --> processor
    SensorData --> logger
    Alert((Alert)) --> logger

Topic names are obtained via Topic::name().

Source§

impl<E, T> Supervisor<E, T>
where E: Event, T: Topic<E> + Label + Clone + Eq + Hash,

Source

pub fn to_json(&self) -> Result<String>

Available on crate feature serde only.

Export actor subscription topology as JSON.

This method provides a machine-readable representation of which actors are subscribed to which topics. It mirrors the information shown by to_mermaid, but returns structured JSON suitable for inspection, tooling, or testing.

The output is a flat list where each entry contains:

  • actor_id - the actor name
  • subscriptions - topic labels the actor receives events from
§Semantics

This export reflects declared routing configuration only. It does not represent runtime message flow, event producers, or supervision hierarchy.

§Errors

Returns any serialization error produced by serde_json.

§Example
let json = supervisor.to_json()?;
println!("{json}");

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

§

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

§

impl<E, T> UnsafeUnpin 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> 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, 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