Coder Social home page Coder Social logo

message-bus's Introduction

make-message-bus

static pub-sub bus for async applications

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.

message-bus's People

Contributors

korken89 avatar

Watchers

 avatar  avatar

Forkers

perlindgren

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.