lunatic-message-request

Crates.iolunatic-message-request
lib.rslunatic-message-request
version0.1.1
sourcesrc
created_at2023-02-27 09:52:23.607893
updated_at2023-03-07 08:58:32.078705
descriptionMessage responses using regular lunatic processes.
homepage
repositoryhttps://github.com/tqwewe/lunatic-message-request
max_upload_size
id795824
size29,823
Ari Seyhun (tqwewe)

documentation

README

Message responses using regular lunatic processes.

Regular lunatic processes don't typically support sending responses in messages. This library provides a type MessageRequest which adds support for replying back with a value.

Example

use lunatic::spawn_link;
use lunatic_message_request::{MessageRequest, ProcessRequest};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
enum Message {
    Inc,
    Dec,
    Count(MessageRequest<(), i32>),
}

let counter = spawn_link!(|mailbox: Mailbox<Message>| {
    let mut count = 0;

    loop {
        let msg = mailbox.receive();
        match msg {
            Message::Inc => count += 1,
            Message::Dec => count -= 1,
            Message::Count(req) => req.reply(count),
        }
    }
});

// Initial count should be 0
let count = counter.request(Message::Count, ());
assert_eq!(count, 0);

// Increment count
counter.send(Message::Inc);

// Count should now be 1
let count = counter.request(Message::Count, ());
assert_eq!(count, 1);

License

Licensed under either

at your option.

Commit count: 3

cargo fmt