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
impl Packet
Sourcepub fn new(
ty: DataType,
endpoints: &[DataEndpoint],
sender: &str,
timestamp: u64,
payload: Arc<[u8]>,
) -> TelemetryResult<Self>
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:
endpointsis 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].
- Length and encoding are validated by [
§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
TelemetryError::EmptyEndpointsifendpointsis empty.TelemetryError::SizeMismatchif the payload size does not match the expected size for static element counts, or is not a multiple of the element width for dynamic types.TelemetryError::InvalidUtf8if the payload is a string type and is not valid UTF-8 .
Sourcepub fn new_with_nonce(
ty: DataType,
endpoints: &[DataEndpoint],
sender: &str,
timestamp: u64,
nonce: u16,
payload: Arc<[u8]>,
) -> TelemetryResult<Self>
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.
Sourcepub fn packet_id(&self) -> u64
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.
Sourcepub fn validate(&self) -> TelemetryResult<()>
pub fn validate(&self) -> TelemetryResult<()>
Validate basic invariants:
endpointsis 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].
- Length and encoding are validated by [
§Returns
Ok(())if validation passes.Err(TelemetryError)if validation fails.
Sourcepub fn data_type(&self) -> DataType
pub fn data_type(&self) -> DataType
Get the message data type. This is the logical schema selector.
Sourcepub fn sender(&self) -> &str
pub fn sender(&self) -> &str
Get the sender identifier. This is typically a device or subsystem name.
Sourcepub fn endpoints(&self) -> &[DataEndpoint]
pub fn endpoints(&self) -> &[DataEndpoint]
Get the destination endpoints for this message.
Sourcepub fn with_nonce(self, nonce: u16) -> Self
pub fn with_nonce(self, nonce: u16) -> Self
Override the packet nonce while keeping the rest of the packet intact.
Sourcepub fn header_string(&self) -> String
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.
Sourcepub fn data_as_utf8_ref(&self) -> Option<&str>
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.Noneotherwise.
Sourcepub fn as_string(&self) -> String
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.
Sourcepub fn to_hex_string(&self) -> String
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.
Sourcepub fn data_as_f32(&self) -> TelemetryResult<Vec<f32>>
pub fn data_as_f32(&self) -> TelemetryResult<Vec<f32>>
Decode payload as little-endian f32 values.
Sourcepub fn data_as_f64(&self) -> TelemetryResult<Vec<f64>>
pub fn data_as_f64(&self) -> TelemetryResult<Vec<f64>>
Decode payload as little-endian f64 values.
Sourcepub fn data_as_u8(&self) -> TelemetryResult<Vec<u8>>
pub fn data_as_u8(&self) -> TelemetryResult<Vec<u8>>
Decode payload as little-endian u8 values.
Sourcepub fn data_as_u16(&self) -> TelemetryResult<Vec<u16>>
pub fn data_as_u16(&self) -> TelemetryResult<Vec<u16>>
Decode payload as little-endian u16 values.
Sourcepub fn data_as_u32(&self) -> TelemetryResult<Vec<u32>>
pub fn data_as_u32(&self) -> TelemetryResult<Vec<u32>>
Decode payload as little-endian u32 values.
Sourcepub fn data_as_u64(&self) -> TelemetryResult<Vec<u64>>
pub fn data_as_u64(&self) -> TelemetryResult<Vec<u64>>
Decode payload as little-endian u64 values.
Sourcepub fn data_as_u128(&self) -> TelemetryResult<Vec<u128>>
pub fn data_as_u128(&self) -> TelemetryResult<Vec<u128>>
Decode payload as little-endian u128 values.
Sourcepub fn data_as_i8(&self) -> TelemetryResult<Vec<i8>>
pub fn data_as_i8(&self) -> TelemetryResult<Vec<i8>>
Decode payload as little-endian i8 values.
Sourcepub fn data_as_i16(&self) -> TelemetryResult<Vec<i16>>
pub fn data_as_i16(&self) -> TelemetryResult<Vec<i16>>
Decode payload as little-endian i16 values.
Sourcepub fn data_as_i32(&self) -> TelemetryResult<Vec<i32>>
pub fn data_as_i32(&self) -> TelemetryResult<Vec<i32>>
Decode payload as little-endian i32 values.
Sourcepub fn data_as_i64(&self) -> TelemetryResult<Vec<i64>>
pub fn data_as_i64(&self) -> TelemetryResult<Vec<i64>>
Decode payload as little-endian i64 values.
Sourcepub fn data_as_i128(&self) -> TelemetryResult<Vec<i128>>
pub fn data_as_i128(&self) -> TelemetryResult<Vec<i128>>
Decode payload as little-endian i128 values.
Sourcepub fn data_as_bool(&self) -> TelemetryResult<Vec<bool>>
pub fn data_as_bool(&self) -> TelemetryResult<Vec<bool>>
Decode payload as bools. Any non-zero byte is treated as true.
Sourcepub fn data_as_string(&self) -> TelemetryResult<String>
pub fn data_as_string(&self) -> TelemetryResult<String>
Decode payload as a string (for String type).
Sourcepub fn data_as_binary(&self) -> TelemetryResult<Vec<u8>>
pub fn data_as_binary(&self) -> TelemetryResult<Vec<u8>>
Decode payload as raw bytes (for Binary type).
Sourcepub fn from_prim_le_slice<T>(
ty: DataType,
values: &[T],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>where
T: Copy + 'static,
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).
Sourcepub fn from_u8_slice(
ty: DataType,
values: &[u8],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_u16_slice(
ty: DataType,
values: &[u16],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_i8_slice(
ty: DataType,
values: &[i8],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_i16_slice(
ty: DataType,
values: &[i16],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_u32_slice(
ty: DataType,
values: &[u32],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_i32_slice(
ty: DataType,
values: &[i32],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_u64_slice(
ty: DataType,
values: &[u64],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_i64_slice(
ty: DataType,
values: &[i64],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_u128_slice(
ty: DataType,
values: &[u128],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_i128_slice(
ty: DataType,
values: &[i128],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_f32_slice(
ty: DataType,
values: &[f32],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_f64_slice(
ty: DataType,
values: &[f64],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_no_data(
ty: DataType,
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.
Sourcepub fn from_bool_slice(
ty: DataType,
values: &[bool],
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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).
Sourcepub fn from_str_slice(
ty: DataType,
s: &str,
endpoints: &[DataEndpoint],
timestamp: u64,
) -> TelemetryResult<Self>
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.