Crate atticus

source ·
Expand description

atticus: A simple implementation of an actor in Tokio.

Actors provide a way to invoke messages or requests among asynchronous tasks. This avoids the need to use Arc<Mutex<T>> instances of an object to be passed around so shared state can be made. It makes use of channels to exchange data.

Actors aim to clarify ownership data structures.

Create an actor by implementing the Actor trait.

use atticus::Actor;
use async_trait::async_trait;

struct IntToString;

#[async_trait]
impl Actor for IntToString {
   type Request = i32;
   type Response = String;
   async fn handle(&mut self, request: Self::Request) -> Option<Self::Response> {
       Some(request.to_string())
   }
}

#[tokio::main(flavor="current_thread")]
async fn main() {
   // Spawn using [actor::run]
   let handle = atticus::actor::run(IntToString{}, 1);

   // Send a request to convert 5 to String.
   let response = handle.requestor.request(5).await;

   assert!(response.is_ok());
   assert_eq!(response.unwrap(), Some(String::from("5")));
}

Re-exports§

Modules§

  • Defines the interface on how to glue in your custom Actor
  • Defines the error types that this crate provides.
  • Re-export tokio symbols that we use A multi-producer, single-consumer queue for sending values between asynchronous tasks.

Structs§

  • Re-export tokio symbols that we use An owned permission to join on a task (await its termination).