#pub-sub #async #macro

macro async_pub_sub_macros

A library containing macros used by the async_pub_sub library to make async pub-sub easier in Rust

1 unstable release

Uses new Rust 2024

new 0.1.2 Apr 13, 2025
0.1.1 Apr 13, 2025
0.1.0 Apr 13, 2025

#1267 in Procedural macros

25 downloads per month
Used in 2 crates (via async_pub_sub)

Custom license

40KB
825 lines

Async Pub Sub Macros

This crate provides procedural macros for the async_pub_sub crate, simplifying the implementation of publishers, subscribers, and RPC interfaces.

Features

  • DerivePublisher: A derive macro to automatically implement the Publisher trait for structs. It supports single and multi-publisher scenarios, including specifying message types via attributes.
  • DeriveSubscriber: A derive macro to automatically implement the Subscriber trait for structs. It supports single and multi-subscriber scenarios.
  • rpc_interface: An attribute macro that generates the necessary code for defining RPC interfaces, including message enums (with optional derive attributes), client traits, and server traits.
  • route and routes: Macros for easily connecting publishers and subscribers.

Usage

  1. Add async_pub_sub and async_pub_sub_macros to your Cargo.toml:
[dependencies]
async_pub_sub = "0.1.0" # Replace with the latest version
async_pub_sub_macros = "0.1.0" # Replace with the latest version

Alternatively, you can use the macros feature on the async_pub_sub crate

[dependencies]
async_pub_sub = { version = "0.1.0", features = ["macros"] } # Replace with the latest version
  1. Use the derive and attribute macros in your code:
use async_pub_sub::{Publisher, Subscriber, PublisherImpl, Result};
use async_pub_sub_macros::{DerivePublisher, DeriveSubscriber, rpc_interface};

// Publisher example
#[derive(DerivePublisher)]
struct MyPublisher {
    #[publisher(i32)]
    publisher: PublisherImpl<i32>,
}

// Subscriber example
#[derive(DeriveSubscriber)]
struct MySubscriber<S> 
where 
    S: Subscriber<Message = i32>
{
    subscriber: S,
}

// RPC interface example
#[rpc_interface]
trait MyRpcInterface {
    async fn my_method(&self, arg: i32) -> String;
}

// RPC interface with custom derives for the message enum
#[rpc_interface(Debug)]
trait MyLoggableRpcInterface {
    async fn get_data(&self) -> String;
    async fn set_data(&mut self, data: String);
}

See the tests/expand directory for more detailed usage examples and to see the code generated by the macros.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

~0.8–1.5MB
~30K SLoC