Skip to main content

ActorManager

Struct ActorManager 

Source
pub struct ActorManager<A: AsyncActor, const E: usize = 256> { /* private fields */ }
Expand description

子 actor 管理器。

ActorManager 本身实现了 AsyncActor,因此可以像普通 actor 一样被 AsyncActor::spawn 启动和持有。

它管理一组同类型的子 actor,对外提供:

  • 启动/停止子 actor
  • 向指定子 actor 转发命令
  • 聚合所有子 actor 的状态、进度和事件

§类型参数

  • A: 子 actor 的类型,必须实现 AsyncActor。
  • E: 管理器事件通道(broadcast)的容量。

§示例

let manager = ActorManager::<MyActor>::new();
let handle = manager.spawn();

// 启动子 actor,返回分配的 id
let (id_tx,id_rx) = tokio::sync::oneshot::channel();
handle.send_cmd(ManagerCmd::SpawnActor{actor:MyActor,id_tx:Some(id_tx)}).unwrap();
let id = id_rx.await.unwrap();

Implementations§

Source§

impl<A: AsyncActor, const E: usize> ActorManager<A, E>

Source

pub fn new() -> Self

创建一个空的 ActorManager。

初始时不管理任何子 actor,需要启动后通过 ManagerCmd::SpawnActor 添加。

Source

pub fn set_update_interval(&mut self, interval: Duration)

Trait Implementations§

Source§

impl<A: AsyncActor, const E: usize> ActorCore for ActorManager<A, E>

Source§

const EVENT_CAPACITY: usize = E

actor 的事件通道容量
Source§

type Cmd = ManagerCmd<A>

actor 命令枚举 Read more
Source§

type State = ManagerState<<A as ActorCore>::State>

actor 里的低频状态,如果不需要,用 type State = ();。
Source§

type Progress = ManagerProgress<<A as ActorCore>::Progress>

actor 里的高频状态,如果没有高频进度时,用 type Progress = ();。
Source§

type Event = ManagerEvent<<A as ActorCore>::Event>

actor 里的事件结构体,需要实现Clone
Source§

fn init_state_and_progress(&self) -> (Self::State, Self::Progress)

Source§

fn channel( init_state: Self::State, init_progress: Self::Progress, ) -> (ActorContext<Self>, ActorHandle<Self>)

创建 actor 的通道。 ActorContext 是 actor 的后台端,ActorHandle 是 actor 的控制端。
Source§

impl<A: AsyncActor, const E: usize> AsyncActor for ActorManager<A, E>

Source§

fn run( self, context: ActorContext<Self>, ) -> impl Future<Output = ()> + Send + 'static

actor 的主循环。 Read more
Source§

fn spawn(self) -> ActorHandle<Self>

启动 actor:把 run 返回的 future 交给 tokio::spawn。 Read more
Source§

impl<A: AsyncActor, const E: usize> Default for ActorManager<A, E>

Source§

fn default() -> Self

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

Auto Trait Implementations§

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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