1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
//! Theatre: A concise async actor model implementation //! //! Example: //! ```rust //! use theatre::prelude::*; //! //! #[derive(Default)] //! struct Accumulator { //! count: i32, //! } //! //! impl Actor for Accumulator {} //! //! // A message to increase the count of the accumulator by one //! struct Increment; //! impl Message for Increment { //! type Return = (); //! } //! //! // A message to get the current count of the accumulator //! struct GetCount; //! impl Message for GetCount { //! type Return = i32; //! } //! //! // Handle `Increment` messages for the accumulator //! #[async_trait] //! impl Handler<Increment> for Accumulator { //! async fn handle( //! &mut self, //! _msg: Increment, //! _context: &mut Context<Self>, //! ) -> <Increment as Message>::Return { //! println!("Adding 1"); //! self.count += 1 //! } //! } //! //! // Handle `GetCount` //! #[async_trait] //! impl Handler<GetCount> for Accumulator { //! async fn handle( //! &mut self, //! _msg: GetCount, //! _context: &mut Context<Self>, //! ) -> <GetCount as Message>::Return { //! self.count //! } //! } //! //! #[tokio::main] //! async fn main() { //! let mut acc = Accumulator::default().spawn().await; //! //! assert_eq!(0, acc.send(GetCount).await.unwrap()); //! //! acc.send(Increment).await.unwrap(); // Output: `Adding 1` //! acc.send(Increment).await.unwrap(); // Output: `Adding 1` //! acc.send(Increment).await.unwrap(); // Output: `Adding 1` //! acc.send(Increment).await.unwrap(); // Output: `Adding 1` //! //! println!("Value: {}", acc.send(GetCount).await.unwrap()); // Output: Value: 4 //! assert_eq!(4, acc.send(GetCount).await.unwrap()); //! } //! ``` mod actor; pub mod actors; mod context; mod error; mod handler; pub mod mailbox; pub use actor::Config; pub use actor::{start, Actor, ActorExt, ActorHandle}; pub use context::Context; pub use error::TheatreError; pub use handler::Handler; pub mod prelude { //! Convenience module for common actor types pub use super::actor::{Actor, ActorExt}; pub use super::context::Context; pub use super::error::TheatreError; pub use super::handler::Handler; pub use super::mailbox::{Addr, Message, Recipient}; pub use async_trait::async_trait; }