pub struct Endpoint<A: Actor> { /* private fields */ }Expand description
The primary handle for interacting with an actor.
Endpoint<A> is generic over actor type A, providing compile-time
guarantees that the actor can handle the message being sent. Endpoints
are cheap to clone and can be shared across tasks.
Under the hood, an endpoint is either local (in-memory channel) or remote (serializes over QUIC). The caller never knows which — the API is identical.
§Examples
// Obtain from system.start() or system.lookup()
let counter = system.start("counter/0", Counter, CounterState { count: 0 });
// Send a sync message
let count = counter.send(Increment { amount: 5 }).await?;
// Send an async message
let data = counter.send_async(FetchData { key: "x".into() }).await?;
// Clone and share across tasks
let counter2 = counter.clone();
tokio::spawn(async move {
counter2.send(Increment { amount: 1 }).await.ok();
});Implementations§
Source§impl<A: Actor + 'static> Endpoint<A>
impl<A: Actor + 'static> Endpoint<A>
Sourcepub async fn send<M>(&self, message: M) -> Result<M::Result, SendError>
pub async fn send<M>(&self, message: M) -> Result<M::Result, SendError>
Send a message to the actor. Identical API for local and remote.
Compile-time checked:
Amust implementHandler<M>Mmust implementRemoteMessage(serializable)
For local actors: uses the envelope pattern (zero serialization cost). For remote actors: serializes, sends over wire, awaits response.
§Examples
let count = counter.send(Increment { amount: 5 }).await?;
// Error handling
match counter.send(Increment { amount: 1 }).await {
Ok(new_count) => println!("Count: {new_count}"),
Err(SendError::MailboxClosed) => println!("Actor stopped"),
Err(e) => println!("Send failed: {e}"),
}Sourcepub async fn send_async<M>(&self, message: M) -> Result<M::Result, SendError>
pub async fn send_async<M>(&self, message: M) -> Result<M::Result, SendError>
Send a message to an async handler. Identical API for local and remote.
Like send but dispatches through AsyncHandler<M> so the
handler can .await inside its body.
§Examples
// For actors that implement AsyncHandler<FetchData>
let data = endpoint.send_async(FetchData { key: "users".into() }).await?;