Crates.io | message-bus-macros |
lib.rs | message-bus-macros |
version | 0.1.1 |
source | src |
created_at | 2023-03-27 12:21:16.288442 |
updated_at | 2023-03-28 09:13:37.199526 |
description | Generate a pub-sub bus for use in async applications, macros crate |
homepage | |
repository | https://github.com/korken89/message-bus |
max_upload_size | |
id | 822085 |
size | 24,525 |
make-message-bus
static
pub-sub bus for async applications
use make_message_bus::make_message_bus;
//
// Topic defintion:
// TopicName [optional buffer size] => payload,
//
// Subtopic definition:
// module_name::SubtopicName => { ... },
//
make_message_bus!(
bus::Toplevel => { // Toplevel topic
Topic1 [10] => u8,
Topic2 => u16,
t1::SubTopic4 => {
Topic5 [20] => u8,
Topic6 => u16,
},
t2::SubTopic8 => {
Topic9 [30] => u8,
Topic10 => u16,
t3::SubTopic12 => {
Topic13 [40] => u8,
Topic14 => u16,
},
},
},
);
#[tokio::main]
async fn main() {
// Subscirbe to all topics
let mut sub_all = bus::Toplevel::subscribe();
let mut sub_topic_13 = bus::t2::t3::Topic13::subscribe();
// Publish on the bottom most topic
bus::t2::t3::Topic13::publish(18);
// Receive on the toplevel topic
assert!(!sub_all.is_empty());
let val = sub_all.try_recv().unwrap();
println!("Toplevel val = {val:?}");
assert!(matches!(
val,
bus::Toplevel::SubTopic8(bus::SubTopic8::SubTopic12(bus::t2::SubTopic12::Topic13(18)))
));
// Receive on the specific topic
assert!(!sub_topic_13.is_empty());
let val = sub_topic_13.try_recv().unwrap();
println!("Specific topic val = {val:?}");
assert!(val == 18);
}
Licensed under either of
Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.