Crates.io | lunatic-message-request |
lib.rs | lunatic-message-request |
version | 0.1.1 |
source | src |
created_at | 2023-02-27 09:52:23.607893 |
updated_at | 2023-03-07 08:58:32.078705 |
description | Message responses using regular lunatic processes. |
homepage | |
repository | https://github.com/tqwewe/lunatic-message-request |
max_upload_size | |
id | 795824 |
size | 29,823 |
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.
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);
Licensed under either
at your option.