#message-bus #static #topic #pub-sub #applications #async #generate

macro message-bus-macros

Generate a pub-sub bus for use in async applications, macros crate

2 releases

0.1.1 Mar 28, 2023
0.1.0 Mar 27, 2023

#21 in #message-bus

Download history 74/week @ 2024-01-07 45/week @ 2024-01-14 19/week @ 2024-01-21 35/week @ 2024-01-28 2/week @ 2024-02-04 55/week @ 2024-02-18 62/week @ 2024-02-25 16/week @ 2024-03-03 43/week @ 2024-03-10 15/week @ 2024-03-17 21/week @ 2024-03-24 54/week @ 2024-03-31 51/week @ 2024-04-07 49/week @ 2024-04-14 22/week @ 2024-04-21

177 downloads per month
Used in make-message-bus

MIT/Apache

23KB
536 lines

make-message-bus

static pub-sub bus for async applications

Documentation

Change log

Example API

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

License

Licensed under either of

at your option.

Contribution

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.

Dependencies

~0.4–0.9MB
~20K SLoC