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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
//! # vin
//! A lightweight, ergonomic and unconventional actor crate.
//!
//! ## Overview
//!
//! Vin's goal is to be an ergonomic, unconventional actor library. Vin doesn't follow the conventional implementations for actor libraries, but tries to be as simple as possible, while still providing an ergonomic and rich interface by integrating itself with [`tokio`] as much as possible. Each actor gets its own task to poll messages and execute handlers on. Its address is shared by a simple `Arc`. Vin also provides a way to gracefully shutdown all actors without having to do the manual labour yourself. Actor data is stored in its actor context and is retrievable for reading with `Actor::ctx()` and for writing with `Actor::ctx_mut()` which acquire a `RwLock` to the data. Vin also provides a "task actor" which is simply a [`tokio`] task spun up and synchronized with Vin's shutdown system.
//!
//! Vin completely relies on [`tokio`](https://github.com/tokio-rs/tokio) (for the async runtime), [`log`](https://github.com/rust-lang/log) (for diagnostics), [`async_trait`](https://github.com/dtolnay/async-trait) and [`anyhow`](https://github.com/dtolnay/anyhow) (as the handler error type).
//!
//! ## Examples
//!
//! ### Regular actors
//! Basic usage of [`vin`].
//!
//! ```rust
//! use vin::*;
//! use std::time::Duration;
//! use tracing::Level;
//!
//! #[vin::message]
//! #[derive(Debug, Clone)]
//! pub enum Msg {
//! Foo,
//! Bar,
//! Baz,
//! }
//!
//! #[vin::message(result = u32)]
//! struct MsgWithResult;
//!
//! #[vin::actor]
//! #[vin::handles(Msg)]
//! #[vin::handles(MsgWithResult)]
//! struct MyActor {
//! pub number: u32,
//! }
//!
//! #[async_trait]
//! impl vin::LifecycleHook for MyActor {}
//!
//! #[async_trait]
//! impl vin::Handler<Msg> for MyActor {
//! async fn handle(&self, msg: Msg) -> anyhow::Result<()> {
//! let ctx = self.ctx().await;
//! println!("The message is: {:?} and the number is {}", msg, ctx.number);
//!
//! Ok(())
//! }
//! }
//!
//! #[async_trait]
//! impl vin::Handler<MsgWithResult> for MyActor {
//! async fn handle(&self, _: MsgWithResult) -> anyhow::Result<<MsgWithResult as Message>::Result> {
//! Ok(42)
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! tracing_subscriber::fmt()
//! .with_max_level(Level::TRACE)
//! .init();
//!
//! let ctx = VinContextMyActor { number: 42 };
//! let actor = MyActor::start("test", ctx).await.unwrap();
//! actor.send(Msg::Bar).await;
//! tokio::time::sleep(Duration::from_millis(500)).await;
//! let res = actor.send_and_wait(MsgWithResult).await.unwrap();
//! assert_eq!(res, 42);
//! vin::shutdown();
//! vin::wait_for_shutdowns().await;
//! }
//! ```
//!
//! ### Task actors
//! Basic usage of task actors in [`vin`].
//!
//! ```rust
//! use vin::*;
//! use std::time::Duration;
//! use tracing::Level;
//!
//! #[vin::task]
//! #[derive(Debug, Clone, PartialEq, Eq)]
//! struct MyTaskActor {
//! pub number: u32,
//! }
//! #[async_trait]
//! impl vin::Task for MyTaskActor {
//! async fn task(self) -> anyhow::Result<()> {
//! for i in 0..self.number {
//! log::info!("{}. iteration", i);
//! }
//!
//! Ok(())
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! tracing_subscriber::fmt()
//! .with_max_level(Level::TRACE)
//! .init();
//!
//! MyTaskActor{ number: 5 }.start("test_task").await;
//! tokio::time::sleep(Duration::from_millis(100)).await;
//! vin::shutdown();
//! vin::wait_for_shutdowns().await;
//! }
//! ```
pub use vin_core::{
self, State, Message, Addr, Actor, ActorId, Handler,
StrongAddr, WeakAddr, StrongErasedAddr, WeakErasedAddr,
BoxedMessage, shutdown, shutdown_future, add_actor, remove_actor,
wait_for_shutdowns, ActorQueryError, query_actor, query_actor_erased,
send_at, erased_send_at, LifecycleHook, TaskCloseHandle, TaskActor, Task,
};
pub use vin_macros::{actor, handles, task, message};
#[doc(hidden)] pub use ::vin_core::anyhow;
#[doc(hidden)] pub use ::vin_core::async_channel;
#[doc(hidden)] pub use ::vin_core::async_trait::{self, async_trait};
#[doc(hidden)] pub use ::vin_core::crossbeam;
#[doc(hidden)] pub use ::vin_core::downcast_rs;
#[doc(hidden)] pub use ::vin_core::futures;
#[doc(hidden)] pub use ::vin_core::tokio;
#[doc(hidden)] pub use ::vin_core::log;