pub trait ActorExt: Actor {
// Provided methods
fn with<L: Launch<Message = Self::Message>>(
self,
launch: L,
) -> L::Result<Self> { ... }
fn start_with(
self,
inbox: impl Inbox<Item = Self::Message> + 'static,
) -> JoinHandle<()> { ... }
fn start_with_mailbox_capacity(
self,
mailbox_capacity: usize,
) -> BoundedOutbox<Self::Message> { ... }
fn start(self) -> UnboundedOutbox<Self::Message> { ... }
}Provided Methods§
fn with<L: Launch<Message = Self::Message>>(self, launch: L) -> L::Result<Self>
fn start_with( self, inbox: impl Inbox<Item = Self::Message> + 'static, ) -> JoinHandle<()>
fn start_with_mailbox_capacity( self, mailbox_capacity: usize, ) -> BoundedOutbox<Self::Message>
Sourcefn start(self) -> UnboundedOutbox<Self::Message>
fn start(self) -> UnboundedOutbox<Self::Message>
Examples found in repository?
examples/echo.rs (line 39)
37async fn main() {
38 // 启动 actor 并获得与之绑定的 outbox
39 let echo = Echo.start();
40
41 // 向 Actor 发送消息
42 // send 返回 Result,当 Actor 已关闭时会返回 Err
43 // 这里使用 unwrap_or(()) 忽略可能的错误
44 echo.send("Hello".to_string()).unwrap_or(());
45 echo.send("World".to_string()).unwrap_or(());
46 echo.send("!".to_string()).unwrap_or(());
47
48 // 解除 outbox 与 actor 的绑定,
49 // 确保 actor 收到所有消息后自然结束
50 echo.detach().await;
51}More examples
examples/counter.rs (line 64)
62async fn main() {
63 // 启动计数器 Actor
64 let counter = Counter.start();
65
66 // 发送三次自增指令
67 counter.send(CounterMessage::Increment).unwrap_or(());
68 counter.send(CounterMessage::Increment).unwrap_or(());
69 counter.send(CounterMessage::Increment).unwrap_or(());
70
71 // 创建 oneshot 通道以获取当前计数值
72 let (rx, tx) = tokio::sync::oneshot::channel();
73 counter.send(CounterMessage::GetValue(rx)).unwrap_or(());
74
75 // 异步等待结果并打印
76 println!("count: {}", tx.await.unwrap());
77
78 // 解除 outbox 与 Actor 的绑定,确保 Actor 结束
79 counter.detach().await;
80}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.