Skip to main content

ActorHandle

Struct ActorHandle 

Source
pub struct ActorHandle<A: ActorCore> { /* private fields */ }
Expand description

actor 的控制端。

由 ActorCore::channel 创建,通常由外部控制方持有。用于发送命令、读取状态/进度、 订阅事件,以及注册异步监听。

§线程安全

ActorHandle<A> 的 Send/Sync 只取决于四个关联类型,与 actor 本身是否 Send 无关: 即使 A 是 !Send(比如握着 Rc<...> 资源的线程 actor),它照样可以放进 GUI props、 跨线程传递、塞进 Arc。

§发送命令

send_cmd 是同步的:不阻塞、不返回 future,唯一的失败原因是 actor 已停止(或所有后台端已退出)。因此可以在 GUI 回调、Drop、同步宿主里直接调用。

§示例

let (context, handle) = MyActor::channel( (), ());

handle.send_cmd(()).unwrap();
let state = handle.get_state();

let listener = handle.state_listen_async(|s| async move { });
listener.cancel();

Implementations§

Source§

impl<A: AsyncActor> ActorHandle<A>

Source

pub fn state_listen_async<F, Fut>(&self, op: F) -> CancelToken
where F: FnMut(A::State) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send,

异步监听状态变化。

每次状态变化时会调用 op,传入最新状态;返回的 CancelToken 调用 CancelToken::cancel 后停止监听。

§注意

内部使用 tokio::spawn,调用线程必须有 tokio runtime,否则会 panic (there is no reactor running)。同步宿主(GUI 回调、GTK/iOS 主线程等)请改用 watch::Receiver 的方式自己拉:get_state() 轮询,或基于 get_events_rx() 每帧 try_recv()。

Source

pub fn progress_listen_async<F, Fut>(&self, op: F) -> CancelToken
where F: FnMut(A::Progress) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send,

异步监听进度变化。

每次进度变化时调用 op。进度是 watch 语义,中间值可能被跳过, 只保证拿到的是当时的最新值;返回的 CancelToken 调用 cancel() 后停止监听。

§注意

与 ActorHandle::state_listen_async 相同:内部 tokio::spawn,需要 tokio runtime。

Source

pub fn events_listen_async<F, Fut>(&self, op: F) -> CancelToken
where F: FnMut(A::Event) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send,

异步监听事件流。

每收到一个事件调用一次 op。落后超过事件通道容量时会收到 Lagged, 内部只记一条 warn 然后继续(丢事件,不重放);返回的 CancelToken 调用 cancel() 后停止监听。

§注意

与 ActorHandle::state_listen_async 相同:内部 tokio::spawn,需要 tokio runtime。

Source§

impl<A: ActorCore> ActorHandle<A>

Source

pub fn send_cmd(&self, cmd: A::Cmd) -> Result<(), SendCmdError>

向 actor 发送一条命令。

同步、不阻塞(命令通道是无界的),不会阻塞调用线程。失败只会在 actor 已停止 (ActorHandle::stop 之后,或后台端已退出)时发生。

Source

pub fn get_state(&self) -> A::State

获取 actor 的最新状态。

Source

pub fn get_progress(&self) -> A::Progress

获取 actor 的最新进度。

进度通道是 watch:读者只会看到最新值,中间值可能被跳过。 适合进度条、实时指标,不要拿它当消息队列用。

Source

pub fn get_events_rx(&self) -> Receiver<A::Event>

订阅事件流,返回一个 broadcast 接收端。

这是同步用法:在 GUI 帧循环里 try_recv() 即可,不需要 async runtime。 落后太多时 recv() 会返回 Lagged,需要自行决定是跳过还是重试。

Source

pub fn stop(&self)

请求 actor 停止。

只是发出取消信号(CancelToken::cancel),不会强行中断 run: 实现者需要在自己的循环里检查取消信号(如 select! 到 cancel.cancelled(), 或每轮 is_cancelled()),否则 actor 会继续跑到 run 返回为止。

Source

pub fn is_alive(&self) -> bool

Source

pub fn alive_rx(&self) -> Receiver<bool>

订阅“存活状态“,actor 结束时变成 false。

用来替代轮询 is_alive:GUI 每帧 has_changed() 一次即可(watch 语义,中间变化可丢,只关心最终值)。

后台端 drop 后 sender 也随之消失,因此 changed() 可能返回 Err —— 那同样意味着 actor 已经结束,按 false 处理即可。

Trait Implementations§

Source§

impl<A: ActorCore> Clone for ActorHandle<A>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<A> Freeze for ActorHandle<A>

§

impl<A> RefUnwindSafe for ActorHandle<A>

§

impl<A> Send for ActorHandle<A>

§

impl<A> Sync for ActorHandle<A>

§

impl<A> Unpin for ActorHandle<A>

§

impl<A> UnsafeUnpin for ActorHandle<A>

§

impl<A> UnwindSafe for ActorHandle<A>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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