Skip to main content

Node

Struct Node 

Source
pub struct Node { /* private fields */ }

Implementations§

Source§

impl Node

Source

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.

Source

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();
Source

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()));
Source

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);
Source

pub fn send<A: Actor>( &self, msg: impl Into<A::Msg>, ) -> Result<(), RegistryError>

Sends a message to a registered Actor looked up by type.

§Example
node.send::<MetricsCollector>(MetricsMsg::RecordLatency(42))?;
Source

pub fn send_to<Msg: Send + 'static>( &self, name: &str, msg: impl Into<Msg>, ) -> Result<(), RegistryError>

Sends a message to a registered Actor looked up by name.

§Example
node.send_to("worker-1", WorkerMsg::Start)?;
Source

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.

Source

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

Source§

type Props = ()

Source§

type Msg = ()

Source§

type Err = ()

Source§

async fn init(_: &mut Ctx<Self>) -> Result<Self, Self::Err>

Constructs the actor. Called on initial spawn and on every restart. Read more
Source§

fn exit( this: Option<Self>, reason: ExitReason<Self>, ctx: &mut Ctx<Self>, ) -> impl Future<Output = ()> + Send

Cleanup hook called when the actor stops, restarts, or fails to init. this is None if init failed. Read more
Source§

fn sources( &self, ctx: &Ctx<Self>, ) -> impl Future<Output = Result<impl Sources<Self>, Self::Err>> + Send

Sets up message sources (streams, intervals) after init. Read more
Source§

fn handle( &mut self, msg: Self::Msg, ctx: &mut Ctx<Self>, ) -> impl Future<Output = Result<(), Self::Err>> + Send

Called everytime your Actor receives a message. Read more
Source§

impl Default for Node

Source§

fn default() -> Self

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

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.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Node

§

impl RefUnwindSafe for Node

§

impl Send for Node

§

impl Sync for Node

§

impl Unpin for Node

§

impl UnsafeUnpin for Node

§

impl UnwindSafe for Node

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.