pub struct ActorContext<A: ActorCore> { /* private fields */ }Expand description
actor 的后台端。
由 ActorCore::channel 创建,交给 run 持有。用于接收命令、更新状态/进度、广播事件。
§同步 API
这里的写入方法(update_state、
update_progress、
send_event)和 check_cmd
都是同步的,不需要 runtime;只有 wait_for_cmd 返回 future。
§收命令
check_cmd:非阻塞try_recv,适合“每轮循环顺带看一眼“;wait_for_cmd:recv().await,适合异步壳;recv_cmd_timeout/recv_cmd:阻塞版本,只在ThreadActor的上下文里才有(见thread_actor模块)。
命令通道关闭(所有 ActorHandle 都 drop 了)后两者分别返回 None,
实现者据此退出循环即可。
Implementations§
Source§impl<A: ActorCore> ActorContext<A>
impl<A: ActorCore> ActorContext<A>
Sourcepub fn get_events_clone(&self) -> Sender<A::Event>
pub fn get_events_clone(&self) -> Sender<A::Event>
获取额外的事件发送端。
用于把事件发送端交给别的线程/任务(比如 IO 回调里直接发事件出队列)。
Sourcepub fn send_event(&self, event: A::Event)
pub fn send_event(&self, event: A::Event)
发送事件(同步,永不失败;没有订阅者时事件被丢弃)。
Sourcepub fn update_state(&self, state: A::State)
pub fn update_state(&self, state: A::State)
更新状态。
watch 语义:读者只会看到最新值,中间值可能被跳过。
状态应是低频、可幂等覆盖的数据;高频数据用 update_progress。
Sourcepub fn update_progress(&self, progress: A::Progress)
pub fn update_progress(&self, progress: A::Progress)
更新进度。
watch 语义:读者只会看到最新值,中间值可能被跳过 —— 对进度条是优点,
但不要拿它当消息队列(每一帧都要的话走事件)。
Sourcepub fn wait_for_cmd(&mut self) -> impl Future<Output = Option<A::Cmd>>
pub fn wait_for_cmd(&mut self) -> impl Future<Output = Option<A::Cmd>>
等待下一条命令(async)。
命令通道关闭时返回 None,可据此结束 run。
Sourcepub fn cancel_clone(&self) -> CancelToken
pub fn cancel_clone(&self) -> CancelToken
获取取消信号,用于在自己循环里检查/等待停止。
异步壳一般写 select! { _ = cancel.cancelled() => break, ... }。
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
actor 是否已被要求停止。
Source§impl<A: ThreadActor> ActorContext<A>
impl<A: ThreadActor> ActorContext<A>
Auto Trait Implementations§
impl<A> Forget for ActorContext<A>
impl<A> Freeze for ActorContext<A>
impl<A> Move for ActorContext<A>
impl<A> RefUnwindSafe for ActorContext<A>where
UnboundedReceiver<<A as ActorCore>::Cmd>: RefUnwindSafe,
Sender<<A as ActorCore>::State>: RefUnwindSafe,
Sender<<A as ActorCore>::Event>: RefUnwindSafe,
Sender<<A as ActorCore>::Progress>: RefUnwindSafe,
impl<A> Send for ActorContext<A>
impl<A> Sync for ActorContext<A>
impl<A> Unpin for ActorContext<A>
impl<A> UnsafeUnpin for ActorContext<A>where
UnboundedReceiver<<A as ActorCore>::Cmd>: UnsafeUnpin,
Sender<<A as ActorCore>::State>: UnsafeUnpin,
Sender<<A as ActorCore>::Event>: UnsafeUnpin,
Sender<<A as ActorCore>::Progress>: UnsafeUnpin,
impl<A> UnwindSafe for ActorContext<A>where
UnboundedReceiver<<A as ActorCore>::Cmd>: UnwindSafe,
Sender<<A as ActorCore>::State>: UnwindSafe,
Sender<<A as ActorCore>::Event>: UnwindSafe,
Sender<<A as ActorCore>::Progress>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more