Crates.io | telecomande |
lib.rs | telecomande |
version | 1.2.2 |
source | src |
created_at | 2022-05-14 11:55:30.658409 |
updated_at | 2022-08-21 18:19:38.905303 |
description | A small crate providing a primitive for the execution of asynchronous tasks by processor through commands. |
homepage | |
repository | https://github.com/MajorBarnulf/telecomande/ |
max_upload_size | |
id | 586636 |
size | 11,121 |
note: the typo is voluntary, it makes it easier to find.
A small crate providing a primitive for the execution of asynchronous tasks by processors through commands.
#[tokio::test]
async fn example() {
use telecomande::Executor;
// the commands you will send to the processor.
#[derive(Debug)]
pub enum Command {
Greet,
Say(String),
}
// the processor that handles commands.
pub struct Proc {
greeting: String,
}
#[telecomande::async_trait]
impl telecomande::Processor for Proc {
type Command = Command;
type Error = ();
async fn handle(&mut self, command: Self::Command) -> Result<(), ()> {
match command {
Command::Greet => println!("{}", self.greeting),
Command::Say(text) => println!("{text}"),
};
Ok(())
}
}
// launches an async task to run the processor when it receives commands.
let handle = telecomande::SimpleExecutor::new(Proc {
greeting: "Hello".into(),
})
.spawn();
// remotes can be clonned and passed between threads.
let remote = handle.remote();
remote.send(Command::Greet).unwrap();
remote.send(Command::Say("telecomande".into())).unwrap();
// output:
// Hello
// telecomande
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
drop(handle);
}