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

make-message-bus

Generate a pub-sub bus for use in async applications

2 releases

0.1.1 Mar 28, 2023
0.1.0 Mar 27, 2023

#1088 in Data structures

Download history 6/week @ 2024-01-01 72/week @ 2024-01-08 39/week @ 2024-01-15 16/week @ 2024-01-22 32/week @ 2024-01-29 58/week @ 2024-02-19 57/week @ 2024-02-26 18/week @ 2024-03-04 41/week @ 2024-03-11 12/week @ 2024-03-18 21/week @ 2024-03-25 50/week @ 2024-04-01 45/week @ 2024-04-08 43/week @ 2024-04-15

160 downloads per month

MIT/Apache

15KB
260 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

~2.4–4MB
~66K SLoC