2 个版本

0.1.1 2023 年 3 月 28 日
0.1.0 2023 年 3 月 27 日

#843数据结构

34 每月下载量

MIT/Apache

15KB
260

make-message-bus

static 为异步应用程序提供的 pub-sub 总线

文档

变更日志

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

许可证

在以下任一许可证下授权:

由您选择。

贡献

除非您明确声明,否则任何旨在包含在作品中的贡献,如 Apache-2.0 许可证中定义的,应按照上述方式双重许可,不附加任何额外条款或条件。

依赖关系

~2.4–4MB
~66K SLoC