pub struct Node { /* private fields */ }Implementations§
Source§impl Node
impl Node
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new Node, the root of an actor supervision tree.
A Node is the entry point for spawning actors. It holds shared state
(registry, pub/sub bus) that all actors in the tree can access.
Sourcepub fn actor<'a, Child>(
&'a mut self,
props: Child::Props,
) -> SpawnBuilder<'a, Node, Child>where
Child: Actor,
pub fn actor<'a, Child>(
&'a mut self,
props: Child::Props,
) -> SpawnBuilder<'a, Node, Child>where
Child: Actor,
Creates a SpawnBuilder for spawning a top-level Actor. The actor type
is passed as a generic parameter and its props as the argument.
§Example
let handle = node.actor::<MyActor>(my_props)
.supervision(Supervision::Stop)
.spawn();Sourcepub fn get_handle_for<A: Actor>(&self) -> Result<Handle<A::Msg>, RegistryError>
pub fn get_handle_for<A: Actor>(&self) -> Result<Handle<A::Msg>, RegistryError>
Looks up a registered Actor’s Handle by its type. The actor must have been
spawned with SpawnBuilder::spawn_registered.
§Example
let logger = node.get_handle_for::<Logger>()?;
logger.send(LogMsg::Info("hello".into()));Sourcepub fn get_handle<Msg: Send + 'static>(
&self,
name: &str,
) -> Result<Handle<Msg>, RegistryError>
pub fn get_handle<Msg: Send + 'static>( &self, name: &str, ) -> Result<Handle<Msg>, RegistryError>
Looks up a registered Actor’s Handle by name. The actor must have been
spawned with SpawnBuilder::spawn_named.
§Example
let worker = node.get_handle::<WorkerMsg>("worker-1")?;
worker.send(WorkerMsg::Start);Sourcepub fn send_to<Msg: Send + 'static>(
&self,
name: &str,
msg: impl Into<Msg>,
) -> Result<(), RegistryError>
pub fn send_to<Msg: Send + 'static>( &self, name: &str, msg: impl Into<Msg>, ) -> Result<(), RegistryError>
Sourcepub fn publish<T: Send + 'static>(
&self,
topic: &str,
msg: T,
) -> Result<(), PubSubError>
pub fn publish<T: Send + 'static>( &self, topic: &str, msg: T, ) -> Result<(), PubSubError>
Publishes a message to all subscribers of a topic. The message is cloned for each subscriber.
Publishing to a topic with no subscribers is a no-op and returns Ok(()).
Returns PubSubError::TypeMismatch if the topic exists with a different type.
Sourcepub async fn shutdown(&mut self)
pub async fn shutdown(&mut self)
Stops all children and waits for each to fully terminate before returning.
Prefer calling this over letting the Node be dropped. The Drop implementation
sends stop signals to children but spawns a background tokio task to await their
acknowledgments. If the tokio runtime is shutting down at the same time (e.g. the
end of #[tokio::main]), that background task may never execute, leaving children
in a partially-stopped state.
Trait Implementations§
Source§impl Actor for Node
impl Actor for Node
type Props = ()
type Msg = ()
type Err = ()
Source§async fn init(_: &mut Ctx<Self>) -> Result<Self, Self::Err>
async fn init(_: &mut Ctx<Self>) -> Result<Self, Self::Err>
Source§fn exit(
this: Option<Self>,
reason: ExitReason<Self>,
ctx: &mut Ctx<Self>,
) -> impl Future<Output = ()> + Send
fn exit( this: Option<Self>, reason: ExitReason<Self>, ctx: &mut Ctx<Self>, ) -> impl Future<Output = ()> + Send
this is None if init failed. Read moreSource§impl Drop for Node
Sends stop signals to all children and spawns a background task to await their
acknowledgments. Because the awaiting happens in a spawned tokio task, the children
may not fully shut down if the tokio runtime is also shutting down (e.g. at the
end of #[tokio::main]). For guaranteed cleanup, call Node::shutdown before
dropping the Node.
impl Drop for Node
Sends stop signals to all children and spawns a background task to await their
acknowledgments. Because the awaiting happens in a spawned tokio task, the children
may not fully shut down if the tokio runtime is also shutting down (e.g. at the
end of #[tokio::main]). For guaranteed cleanup, call Node::shutdown before
dropping the Node.