Skip to main content

EventMessage

Struct EventMessage 

Source
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_LEN bytes.
  • 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::new inside a const context performs validation at compile time.
  • Using EventMessage::new at 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 newline
use limen_core::prelude::event_message::EventMessage;

const TOO_LONG: EventMessage = EventMessage::new(
    "12345678901234567890123456789012345678901234567890\
12345678901234567890123456789012345678901234567890\
123456789012345678901234567890"
);

Implementations§

Source§

impl EventMessage

Source

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.

Source

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.

Source

pub const fn as_str(&self) -> &'static str

Returns the inner &'static str.

This method is const and #[inline], and therefore has no runtime overhead. It is equivalent in performance to using a plain &'static str directly.

Trait Implementations§

Source§

impl Clone for EventMessage

Source§

fn clone(&self) -> EventMessage

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for EventMessage

Source§

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

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

impl Copy for EventMessage

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, 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> 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.