#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

#1101 in Data structures

Download history 2/week @ 2023-12-06 27/week @ 2023-12-13 9/week @ 2023-12-20 13/week @ 2024-01-03 77/week @ 2024-01-10 36/week @ 2024-01-17 25/week @ 2024-01-24 14/week @ 2024-01-31 5/week @ 2024-02-14 86/week @ 2024-02-21 31/week @ 2024-02-28 30/week @ 2024-03-06 24/week @ 2024-03-13 12/week @ 2024-03-20

112 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