#async #processor #typo #commands #task #find #execution #primitive #tasks #note

telecomande

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

6 releases (stable)

1.2.2 Aug 21, 2022
1.0.1 Aug 15, 2022
0.1.0 May 14, 2022

MIT license

11KB
179 lines

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);
}

Dependencies

~2.4–7MB
~112K SLoC