telecomande

Crates.iotelecomande
lib.rstelecomande
version1.2.2
sourcesrc
created_at2022-05-14 11:55:30.658409
updated_at2022-08-21 18:19:38.905303
descriptionA small crate providing a primitive for the execution of asynchronous tasks by processor through commands.
homepage
repositoryhttps://github.com/MajorBarnulf/telecomande/
max_upload_size
id586636
size11,121
Matthieu JOLIMAITRE (MajorBarnulf)

documentation

README

telecomande

note: the typo is voluntary, it makes it easier to find.

Description

A small crate providing a primitive for the execution of asynchronous tasks by processors through commands.

Example

#[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);
}
Commit count: 11

cargo fmt