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
impl Message
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new empty message builder.
§Examples
use monocoque_core::message_builder::Message;
let msg = Message::new();Sourcepub fn with_capacity(capacity: usize) -> Self
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");Sourcepub fn push(self, frame: impl Into<Bytes>) -> Self
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"));Sourcepub fn push_str(self, s: &str) -> Self
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");Sourcepub fn push_empty(self) -> Self
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");Sourcepub fn push_u32(self, value: u32) -> Self
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");Sourcepub fn len(&self) -> usize
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);Sourcepub fn into_frames(self) -> Vec<Bytes>
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);Sourcepub const fn from_frames(frames: Vec<Bytes>) -> Self
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§
Auto Trait Implementations§
impl Freeze for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnsafeUnpin for Message
impl UnwindSafe for Message
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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