Skip to main content

Crate zenobuf_macros

Crate zenobuf_macros 

Source
Expand description

§Zenobuf Macros - Procedural macros for the Zenobuf framework

This crate provides procedural macros that simplify working with the Zenobuf framework, particularly for integrating Protocol Buffer messages with the type-safe messaging system.

§Overview

The main macro provided is ZenobufMessage, which automatically implements the [zenobuf_core::Message] trait for Protocol Buffer messages generated by Prost.

§Quick Start

§1. Add to your Cargo.toml

[dependencies]
zenobuf-core = "0.3"
zenobuf-macros = "0.3"
prost = "0.14"

[build-dependencies]
prost-build = "0.14"

§2. Setup automatic derive in build.rs

fn main() -> std::io::Result<()> {
    prost_build::Config::new()
        .type_attribute(".", "#[derive(zenobuf_macros::ZenobufMessage)]")
        .compile_protos(&["protos/messages.proto"], &["protos"])?;
    Ok(())
}

§3. Use in your code

// Generated protobuf code automatically has ZenobufMessage derived
pub mod proto {
    include!(concat!(env!("OUT_DIR"), "/my_messages.rs"));
}

use zenobuf_core::Node;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let node = Node::new("my_node").await?;

    // Your protobuf messages now work seamlessly with Zenobuf
    let publisher = node
        .publisher::<proto::MyMessage>("topic")
        .build()
        .await?;

    Ok(())
}

§Manual Usage

You can also manually derive the macro on your own types:

use zenobuf_macros::ZenobufMessage;

#[derive(Clone, PartialEq, Default, ZenobufMessage)]
pub struct MyMessage {
    pub value: i32,
}

§What the Macro Does

The ZenobufMessage derive macro implements the [zenobuf_core::Message] trait, which provides:

  • Type name information for debugging and introspection
  • Integration with Zenobuf’s type-safe messaging system
  • Automatic serialization/deserialization support

§Requirements

Types that derive ZenobufMessage must also implement:

  • Clone
  • PartialEq
  • Default
  • prost::Message (for Protocol Buffer types)
  • Send + Sync + 'static (automatically satisfied by most types)

Derive Macros§

ZenobufMessage
Derives the [zenobuf_core::Message] trait for Protocol Buffer messages