A tiny, fast, and safe actor framework. It is modelled around Actix (copyright and license here).
For better ergonomics with xtra, try the spaad crate.
futures
and async_trait
by default.async
/await
syntax even when borrowing self
.Actor::spawn
convenience method implemented out of the box).```rust use xtra::prelude::*; use asynctrait::asynctrait;
struct Printer { times: usize, }
impl Printer { fn new() -> Self { Printer { times: 0 } } }
impl Actor for Printer {}
struct Print(String);
impl Message for Print { type Result = (); }
// In the real world, the synchronous SyncHandler trait would be better-suited
impl Handler
async fn main() {
let addr = Printer::new().spawn();
loop {
// Likewise, in the real world the .do_send
method should be used here as it is about 2x as fast
addr.send(Print("hello".to_string()))
.await
.expect("Printer should not be dropped");
}
}
```
For a longer example, check out Vertex, a chat application written with xtra and spaad on the server.
Too verbose? Check out the spaad sister-crate!
Check out the docs and the examples
to get started! Enabling the with-tokio-0_2
, with-async_std-1
, with-smol-0_3
, or with-wasm-bindgen-0_2
features
is recommended in order to enable some convenience methods (such as Actor::spawn
). Which you enable will depend on
which executor you want to use (check out their docs to learn more about each). If you have any questions, feel free to
open an issue or message me on the Rust discord.
SyncHandler
trait has been removed. This simplifies the API and should not change the performance on stable.
SyncHandler
trait to the normal Handler
trait.Actor
lifecycle messages are now async. This allows to do more kinds of things in lifecycle methods,
while adding no restrictions.
async
to the function definition of all actor lifecycle methods.Actor
now requires Send
to implement. Previously, the trait itself did not, but using it did require Send
.
Send
actor in the first place.{Weak}Address::attach_stream
method now requires that the actor implements Handler<M>
where
M: Into<KeepRunning> + Send
. This is automatically implemented for ()
, returning KeepRunning::Yes
. This allows
the user more control over the future spawned by attach_stream
, but is breaking if the message returned did not
implement Into<KeepRunning>
/
Into<KeepRunning>
for all message types used in attach_stream
. To mimic previous
behaviour, return KeepRunning::Yes
in the implementation.See the full list of breaking changes by version here