Skip to main content

Packet

Struct Packet 

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

Payload-bearing packet (safe, validated, shareable).

This is the primary data structure passed around inside the crate and across FFI boundaries (via views / wrappers).

Implementations§

Source§

impl Packet

Source

pub fn new( ty: DataType, endpoints: &[DataEndpoint], sender: &str, timestamp: u64, payload: Arc<[u8]>, ) -> TelemetryResult<Self>

Create a packet from a raw payload, validating against message_meta(ty).

Checks:

  • endpoints is non-empty.
  • For static element count:
    • payload.len() == element_count * data_type_size(get_data_type(ty)).
  • For dynamic:
    • Length and encoding are validated by [validate_dynamic_len_and_content].
§Arguments
  • ty: logical message type (schema selector).
  • endpoints: destination endpoint list (must be non-empty).
  • sender: logical sender identifier (e.g. device or subsystem name).
  • timestamp: timestamp in milliseconds.
  • payload: raw payload bytes.
§Returns
  • Ok(Packet) if validation passes.
  • Err(TelemetryError) if validation fails.
§Errors
Source

pub fn new_with_nonce( ty: DataType, endpoints: &[DataEndpoint], sender: &str, timestamp: u64, nonce: u16, payload: Arc<[u8]>, ) -> TelemetryResult<Self>

Create a packet with an explicit nonce value.

This is mainly useful when callers need stable byte-for-byte wire output across repeated constructions, such as deterministic tests or precomputed fixtures.

Source

pub fn packet_id(&self) -> u64

Compute a stable 64-bit identifier for this packet.

This is not packed – it is derived locally from:

  • sender
  • logical type (DataType)
  • endpoints
  • timestamp
  • payload bytes

Identical packets on different boards/links will compute the same ID, so the Router/Relay can use it to drop duplicates.

Source

pub fn validate(&self) -> TelemetryResult<()>

Validate basic invariants:

  • endpoints is non-empty.
  • payload.len() == data_size.
  • For static element count:
    • data_size == element_count * data_type_size(get_data_type(ty)).
  • For dynamic:
    • Length and encoding are validated by [validate_dynamic_len_and_content].
§Returns
  • Ok(()) if validation passes.
  • Err(TelemetryError) if validation fails.
Source

pub fn data_type(&self) -> DataType

Get the message data type. This is the logical schema selector.

Source

pub fn sender(&self) -> &str

Get the sender identifier. This is typically a device or subsystem name.

Source

pub fn endpoints(&self) -> &[DataEndpoint]

Get the destination endpoints for this message.

Source

pub fn timestamp(&self) -> u64

Get the timestamp in milliseconds.

Source

pub fn nonce(&self) -> u16

Get the packet nonce.

Source

pub fn data_size(&self) -> usize

Get the payload size in bytes.

Source

pub fn payload(&self) -> &[u8]

Get the raw payload bytes.

Source

pub fn with_nonce(self, nonce: u16) -> Self

Override the packet nonce while keeping the rest of the packet intact.

Source

pub fn header_string(&self) -> String

Header-only string (no decoded data).

Example: Type: FOO, Data Size: 8, Sender: dev0, Endpoints: [EP_A, EP_B], Timestamp: 1234 (1s 234ms)

§Returns
  • Human-readable string with header fields.
Source

pub fn data_as_utf8_ref(&self) -> Option<&str>

Borrow the payload as UTF-8 without trailing NULs (no allocation).

Returns None if the message DataType is not a String type or if the payload is not valid UTF-8 (after trimming trailing NUL).

§Returns
  • Some(&str) if the payload is a valid UTF-8 string.
  • None otherwise.
Source

pub fn as_string(&self) -> String

Full pretty string including decoded data portion.

  • String payloads are rendered as "..."
  • Numeric/bool payloads are rendered as comma-separated values
  • Hex payloads are delegated to Packet::to_hex_string
§Returns
  • Human-readable string with header and decoded data.
Source

pub fn to_hex_string(&self) -> String

Hex dump variant of Packet::as_string.

Produces:

Type: ..., Data Size: ..., ..., Timestamp: ... (...), Data (hex): 0xNN 0xNN ...

§Returns
  • Human-readable string with header and hex-formatted data.
Source

pub fn data_as_f32(&self) -> TelemetryResult<Vec<f32>>

Decode payload as little-endian f32 values.

Source

pub fn data_as_f64(&self) -> TelemetryResult<Vec<f64>>

Decode payload as little-endian f64 values.

Source

pub fn data_as_u8(&self) -> TelemetryResult<Vec<u8>>

Decode payload as little-endian u8 values.

Source

pub fn data_as_u16(&self) -> TelemetryResult<Vec<u16>>

Decode payload as little-endian u16 values.

Source

pub fn data_as_u32(&self) -> TelemetryResult<Vec<u32>>

Decode payload as little-endian u32 values.

Source

pub fn data_as_u64(&self) -> TelemetryResult<Vec<u64>>

Decode payload as little-endian u64 values.

Source

pub fn data_as_u128(&self) -> TelemetryResult<Vec<u128>>

Decode payload as little-endian u128 values.

Source

pub fn data_as_i8(&self) -> TelemetryResult<Vec<i8>>

Decode payload as little-endian i8 values.

Source

pub fn data_as_i16(&self) -> TelemetryResult<Vec<i16>>

Decode payload as little-endian i16 values.

Source

pub fn data_as_i32(&self) -> TelemetryResult<Vec<i32>>

Decode payload as little-endian i32 values.

Source

pub fn data_as_i64(&self) -> TelemetryResult<Vec<i64>>

Decode payload as little-endian i64 values.

Source

pub fn data_as_i128(&self) -> TelemetryResult<Vec<i128>>

Decode payload as little-endian i128 values.

Source

pub fn data_as_bool(&self) -> TelemetryResult<Vec<bool>>

Decode payload as bools. Any non-zero byte is treated as true.

Source

pub fn data_as_string(&self) -> TelemetryResult<String>

Decode payload as a string (for String type).

Source

pub fn data_as_binary(&self) -> TelemetryResult<Vec<u8>>

Decode payload as raw bytes (for Binary type).

Source

pub fn from_prim_le_slice<T>( ty: DataType, values: &[T], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>
where T: Copy + 'static,

Same as from_prim_le_slice_with_sender but uses DEVICE_IDENTIFIER as the sender (mirrors from_u8_slice, from_f32_slice).

Source

pub fn from_u8_slice( ty: DataType, values: &[u8], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian u8 values.

Source

pub fn from_u16_slice( ty: DataType, values: &[u16], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian u16 values.

Source

pub fn from_i8_slice( ty: DataType, values: &[i8], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian i8 values.

Source

pub fn from_i16_slice( ty: DataType, values: &[i16], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian i16 values.

Source

pub fn from_u32_slice( ty: DataType, values: &[u32], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian u32 values.

Source

pub fn from_i32_slice( ty: DataType, values: &[i32], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian i32 values.

Source

pub fn from_u64_slice( ty: DataType, values: &[u64], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian u64 values.

Source

pub fn from_i64_slice( ty: DataType, values: &[i64], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian i64 values.

Source

pub fn from_u128_slice( ty: DataType, values: &[u128], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian u128 values.

Source

pub fn from_i128_slice( ty: DataType, values: &[i128], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian i128 values.

Source

pub fn from_f32_slice( ty: DataType, values: &[f32], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian f32 values.

Source

pub fn from_f64_slice( ty: DataType, values: &[f64], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Build a packet from a slice of little-endian f64 values.

Source

pub fn from_no_data( ty: DataType, endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Builds a packet with an empty payload for types whose schema allows zero bytes.

Source

pub fn from_bool_slice( ty: DataType, values: &[bool], endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

Bool constructor: encodes each bool as a single byte (0 / 1).

Source

pub fn from_str_slice( ty: DataType, s: &str, endpoints: &[DataEndpoint], timestamp: u64, ) -> TelemetryResult<Self>

String constructor (dynamic length). Trailing NULs are not added; new() + validate_dynamic_len_and_content will do UTF-8 validation.

Trait Implementations§

Source§

impl Clone for Packet

Source§

fn clone(&self) -> Packet

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 Packet

Source§

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

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

impl Display for Packet

Source§

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

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

impl Eq for Packet

Source§

impl PartialEq for Packet

Source§

fn eq(&self, other: &Packet) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Packet

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.