Skip to main content

Message

Struct Message 

Source
pub struct Message { /* private fields */ }
Expand description

Builder for constructing ZeroMQ multipart messages.

Provides a fluent API for adding frames to a message with automatic conversions from common types (strings, bytes, JSON, etc.).

§Examples

use monocoque_core::message_builder::Message;

// Simple text frames
let msg = Message::new()
    .push_str("topic")
    .push_str("Hello, World!")
    .into_frames();

// Mixed types
let msg = Message::new()
    .push(&b"routing_id"[..])
    .push_str("command:execute")
    .push(&[1u8, 2, 3, 4][..])
    .into_frames();

§With Serde JSON (optional)

#[derive(Serialize)]
struct Task {
    id: u64,
    name: String,
}

let task = Task { id: 42, name: "Process data".to_string() };
let msg = Message::new()
    .push_str("tasks")
    .push_json(&task)?
    .into_frames();

Implementations§

Source§

impl Message

Source

pub const fn new() -> Self

Create a new empty message builder.

§Examples
use monocoque_core::message_builder::Message;

let msg = Message::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Create a message with pre-allocated capacity.

Useful when you know the number of frames in advance to avoid reallocations.

§Examples
use monocoque_core::message_builder::Message;

let msg = Message::with_capacity(4)
    .push_str("frame1")
    .push_str("frame2")
    .push_str("frame3")
    .push_str("frame4");
Source

pub fn push(self, frame: impl Into<Bytes>) -> Self

Add a frame from any type that can be converted to Bytes.

§Examples
use monocoque_core::message_builder::Message;
use bytes::Bytes;

let msg = Message::new()
    .push(&b"raw bytes"[..])
    .push(vec![1, 2, 3])
    .push(Bytes::from_static(b"static"));
Source

pub fn push_str(self, s: &str) -> Self

Add a string frame.

Convenience method for adding UTF-8 strings.

§Examples
use monocoque_core::message_builder::Message;

let msg = Message::new()
    .push_str("Hello")
    .push_str("World");
Source

pub fn push_empty(self) -> Self

Add an empty frame.

Empty frames are often used as delimiters in ZeroMQ envelope patterns.

§Examples
use monocoque_core::message_builder::Message;

// ROUTER envelope: [identity, empty, body]
let msg = Message::new()
    .push(&b"client-123"[..])
    .push_empty()
    .push_str("Hello");
Source

pub fn push_u32(self, value: u32) -> Self

Add a frame containing a big-endian u32.

Useful for protocol headers, message IDs, etc.

§Examples
use monocoque_core::message_builder::Message;

let msg = Message::new()
    .push_u32(12345) // Message ID
    .push_str("payload");
Source

pub fn push_u64(self, value: u64) -> Self

Add a frame containing a big-endian u64.

Source

pub fn len(&self) -> usize

Get the number of frames in the message.

§Examples
use monocoque_core::message_builder::Message;

let msg = Message::new()
    .push_str("frame1")
    .push_str("frame2");

assert_eq!(msg.len(), 2);
Source

pub fn is_empty(&self) -> bool

Check if the message is empty (has no frames).

Source

pub fn into_frames(self) -> Vec<Bytes>

Consume the builder and return the frames as a Vec<Bytes>.

This is the final step to get the message ready for sending.

§Examples
use monocoque_core::message_builder::Message;

let frames = Message::new()
    .push_str("Hello")
    .push_str("World")
    .into_frames();
assert_eq!(frames.len(), 2);
Source

pub fn frames(&self) -> &[Bytes]

Get a reference to the frames without consuming the builder.

Source

pub const fn from_frames(frames: Vec<Bytes>) -> Self

Create a message from existing frames.

§Examples
use monocoque_core::message_builder::Message;
use bytes::Bytes;

let frames = vec![
    Bytes::from_static(b"frame1"),
    Bytes::from_static(b"frame2"),
];

let msg = Message::from_frames(frames);
assert_eq!(msg.len(), 2);

Trait Implementations§

Source§

impl Clone for Message

Source§

fn clone(&self) -> Message

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Message

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Message

Source§

fn default() -> Message

Returns the “default value” for a type. Read more
Source§

impl From<Message> for Vec<Bytes>

Source§

fn from(msg: Message) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Bytes>> for Message

Source§

fn from(frames: Vec<Bytes>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more