Crates.io | speare |
lib.rs | speare |
version | 0.1.10 |
source | src |
created_at | 2023-10-08 20:43:50.085986 |
updated_at | 2024-04-29 17:26:41.532839 |
description | actor-like thin abstraction over tokio::task and flume channels |
homepage | https://github.com/vmenge/speare |
repository | https://github.com/vmenge/speare |
max_upload_size | |
id | 997431 |
size | 82,918 |
speare
is a minimalistic actor framework. A thin abstraction over tokio tasks and flume channels with supervision inspired by Akka.NET. Read The Speare Book for more details on how to use the library.
Below is an example of a very minimal Counter
Process
.
use speare::*;
use async_trait::async_trait;
use tokio::time;
struct Counter {
count: u32,
}
enum CounterMsg {
Add(u32),
Subtract(u32),
Print
}
#[async_trait]
impl Process for Counter {
type Props = ();
type Msg = CounterMsg;
type Err = ();
async fn init(ctx: &mut Ctx<Self>) -> Result<Self, Self::Err> {
Ok(Counter { count: 0 })
}
async fn handle(&mut self, msg: Self::Msg, ctx: &mut Ctx<Self>) -> Result<(), Self::Err> {
match msg {
CounterMsg::Add(n) => self.count += n,
CounterMsg::Subtract(n) => self.count -= n,
CounterMsg::Print => println!("Count is {}", self.count)
}
Ok(())
}
}
#[tokio::main]
async fn main() {
let mut node = Node::default();
let counter = node.spawn::<Counter>(());
counter.send(CounterMsg::Add(5));
counter.send(CounterMsg::Subtract(2));
counter.send(CounterMsg::Print); // will print 3
// We wait so the program doesn't end before we print.
time::sleep(Duration::from_millis(1)).await;
}
speare
?speare
is a minimal abstraction layer over tokio green threads and flume channels, offering functionality to manage these threads, and pass messages between these in a more practical manner. The question instead should be: "why message passing (channels) instead of sharing state (e.g. Arc<Mutex<T>>
)?"
speare
?I haven't benchmarked the newest versions yet, nor done any performance optimizations as well. There is an overhead due to boxing when sending messages, but overall it is pretty fast. Benchmarks TBD and added here in the future.
Not yet! But soon :-)
I built speare
because I wanted a very minimal actor framework providing just enough to abstract over tokio green threads and channels without introducing a lot of boilerplate or a lot of new concepts to my co-workers. You should use speare
if you like its design, if not then don't :-)
Sure! There are only two rules to contribute:
Keep in mind I want to keep this very minimalistic, but am very open to suggestions and new ideas :)