pub struct EventMessage(/* private fields */);Expand description
A strongly-validated, zero-cost wrapper around a &'static str intended for
use in event messages, logs, telemetry, or any system where input strings
must meet strict formatting requirements.
§Overview
EventMessage wraps a &'static str, but with compile-time validation
whenever it is constructed in a const context. The validation enforces:
- The message must not exceed
EventMessage::MAX_LENbytes. - The message must not contain newline characters (
'\n'or'\r').
When constructed using the accompanying event_message! macro, all
validation is guaranteed to occur at compile time, ensuring:
- Zero runtime cost (no loops, no checks, no panics).
- Static enforcement of message correctness.
- Identical runtime performance to using a plain
&'static str.
§Why use this type?
This type is useful when you need a runtime message type that is:
- Guaranteed to be short enough for logging systems or wire protocols.
- Guaranteed to be newline-free (e.g., single-line logs).
- Immutable and
'static. - Just as cheap as storing a
&'static str.
Once constructed, an EventMessage is simply a thin wrapper around a string
slice. Accessing the inner string via EventMessage::as_str is fully
inlined and has no measurable overhead.
§Compile-time vs runtime construction
- Using
EventMessage::newinside aconstcontext performs validation at compile time. - Using
EventMessage::newat runtime may incur runtime checking cost. - Using the
event_message!macro always validates at compile time.
Prefer event_message! for maximal performance and strictness.
§Examples
§Compile-time validated constant
use limen_core::prelude::event_message::EventMessage;
const HELLO: EventMessage = EventMessage::new("hello");§Preferable usage: enforced via event_message!
use limen_core::prelude::event_message::EventMessage;
use limen_core::event_message;
let msg: EventMessage = event_message!("system_ready");
println!("{}", msg.as_str());Attempting to use invalid strings results in a compile-time error:
use limen_core::prelude::event_message::EventMessage;
const BAD: EventMessage = EventMessage::new("line1\nline2"); // contains newlineuse limen_core::prelude::event_message::EventMessage;
const TOO_LONG: EventMessage = EventMessage::new(
"12345678901234567890123456789012345678901234567890\
12345678901234567890123456789012345678901234567890\
123456789012345678901234567890"
);Implementations§
Source§impl EventMessage
impl EventMessage
Sourcepub const MAX_LEN: usize = 128
pub const MAX_LEN: usize = 128
The maximum allowed message length in bytes.
Messages longer than this value will cause a compile-time error if
validated in a const context, or a panic if validated at runtime.
Sourcepub const fn new(s: &'static str) -> Self
pub const fn new(s: &'static str) -> Self
Creates a new EventMessage from a &'static str, performing
full validation when invoked in a const context.
§Validation
This function checks:
- The string’s length does not exceed
EventMessage::MAX_LEN. - The string contains no newline characters (
'\n'or'\r').
§Compile-Time Guarantees
When invoked in a const context:
- Validation happens during compilation.
- Violations cause a compile-time error.
- No runtime code is emitted for the check.
§Runtime Behavior
If invoked at runtime:
- Validation executes at runtime.
- Violations cause a runtime panic.
For most usage, the event_message! macro ensures compile-time
validation in all cases.
Trait Implementations§
Source§impl Clone for EventMessage
impl Clone for EventMessage
Source§fn clone(&self) -> EventMessage
fn clone(&self) -> EventMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more