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 afterstart().run()combinesstart()andjoin(), blocking until shutdown.stop()graceful shutdown; lets actors consume active eventssend(event)emits events into the broker.
Implementations§
Source§impl<E: Event, T: Topic<E>> Supervisor<E, T>
impl<E: Event, T: Topic<E>> Supervisor<E, T>
Sourcepub fn new(config: Config) -> Self
pub fn new(config: Config) -> Self
Create a new supervisor with the given runtime configuration.
Sourcepub fn add_actor<A, F, S>(
&mut self,
name: &str,
factory: F,
topics: S,
) -> Result<ActorId>
pub fn add_actor<A, F, S>( &mut self, name: &str, factory: F, topics: S, ) -> Result<ActorId>
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 routingfactory- Closure that receives a Context and returns the actortopics- Slice of topics the actor subscribes to
§Example
supervisor.add_actor(
"processor",
|ctx| DataProcessor::new(ctx),
&[MyTopic::Data, MyTopic::Control]
)?;Sourcepub async fn start(&mut self) -> Result<()>
pub async fn start(&mut self) -> Result<()>
Start the broker loop in a background task. This returns immediately.
Sourcepub async fn join(&mut self) -> Result<()>
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.
Sourcepub async fn run(&mut self) -> Result<()>
pub async fn run(&mut self) -> Result<()>
Convenience method to start and then await completion of all tasks. Blocks until shutdown.
Sourcepub async fn send(&self, event: E) -> Result<()>
pub async fn send(&self, event: E) -> Result<()>
Emit an event into the broker from the supervisor.
Sourcepub async fn stop(&mut self) -> Result<()>
pub async fn stop(&mut self) -> Result<()>
Request a graceful shutdown, then await all actor tasks.
§Shutdown Process
- Waits for the broker to receive all pending events (up to 10 ms)
- Stops the broker and waits for it to drain actor queues
- Cancels all actors and waits for tasks t
pub fn monitors(&mut self) -> &mut MonitorRegistry<E, T>
monitoring only.Source§impl<E: Event, T: Topic<E> + Label> Supervisor<E, T>
impl<E: Event, T: Topic<E> + Label> Supervisor<E, T>
Sourcepub fn to_mermaid(&self) -> String
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)) --> loggerTopic names are obtained via Topic::name().
Source§impl<E, T> Supervisor<E, T>
impl<E, T> Supervisor<E, T>
Sourcepub fn to_json(&self) -> Result<String>
Available on crate feature serde only.
pub fn to_json(&self) -> Result<String>
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 namesubscriptions- topic labels the actor receives events from
§Semantics
- Actors registered with
Subscribe::all()are expanded to include all known topics discovered from explicit subscriptions. - Actors registered with
Subscribe::none()produce an empty list. - Topic names are obtained via
Label::label().
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}");