Skip to main content

Tlv

Struct Tlv 

Source
pub struct Tlv {
    pub type_code: u8,
    pub opt: Opt,
    pub payload: Vec<u8>,
    pub children: Vec<Tlv>,
    pub trailer: Option<Trailer>,
}
Expand description

@brief A decoded TLV. For opaque TLVs (opt.PL=0) payload holds the bytes and children is empty; for structured TLVs (opt.PL=1) children holds the parsed sub-TLVs and payload is empty.

Fields§

§type_code: u8

@brief A type_code value (or a user/unknown nonzero code).

§opt: Opt

@brief The unpacked option bits.

§payload: Vec<u8>

@brief Opaque payload bytes (empty when opt.pl).

§children: Vec<Tlv>

@brief Parsed sub-TLVs (empty unless opt.pl).

§trailer: Option<Trailer>

@brief Optional trailer (present iff opt.ts || opt.cr).

Implementations§

Source§

impl Tlv

Source

pub fn first_child(&self, type_code: u8) -> Option<&Tlv>

@brief The first child with the given type_code, or None.

Source

pub fn child_at(&self, index: usize) -> Option<&Tlv>

@brief The child at index, or None if out of range.

Source

pub fn children_of_type(&self, type_code: u8) -> impl Iterator<Item = &Tlv>

@brief Iterate the children with the given type_code.

Source

pub fn payload_str(&self) -> Result<&str, BuildError>

@brief The opaque payload interpreted as UTF-8 (for NAME / DESCRIPTION).

§Errors

BuildError::InvalidUtf8 if the payload is not valid UTF-8.

Source

pub fn payload_uint(&self) -> u64

@brief The opaque payload interpreted as a little-endian unsigned integer (up to 8 bytes; extra leading zero-extension). Returns 0 for an empty payload.

Examples found in repository?
examples/fwd_write.rs (line 27)
14fn main() {
15    // Write 21500 (a temperature in milli-degrees) to /sensor/temp on a remote node.
16    let mut req = FwdRequest::new(fwd_op::WRITE, &["sensor", "temp"], &["client"]);
17    req.payload = Some(value_u32(21_500));
18    let frame = encode_fwd_bytes(&req).expect("a valid request");
19    println!("FWD{{WRITE}} ({} bytes): {:02x?}", frame.len(), frame);
20
21    // The receiving node reads the same four children back, positionally.
22    let parsed = decode_fwd(&frame).expect("a well-formed FWD");
23    assert_eq!(parsed.op, fwd_op::WRITE);
24    let dst = fwd_dst_path(&parsed).unwrap();
25    assert_eq!(dst, "/sensor/temp");
26    let value = parsed.payload.as_ref().expect("a WRITE carries its value");
27    assert_eq!(value.payload_uint(), 21_500);
28    println!("dst {dst} <- {}", value.payload_uint());
29
30    // The frame carries no correlation id: the reply finds its way home along `src`.
31    assert_eq!(parsed.src.payload, [6, b'c', b'l', b'i', b'e', b'n', b't']);
32}
More examples
Hide additional examples
examples/fwd_subscribe.rs (line 37)
16fn main() {
17    // Consumer -> producer: WRITE /sensor/temp:subscribers[] <- SUBSCRIBER{target=/client}.
18    let mut req = FwdRequest::new(fwd_op::WRITE, &["sensor", "temp"], &["client"]);
19    req.field = Some(FieldSel::Str(":subscribers[]"));
20    req.payload = Some(subscriber(&["client"]).unwrap());
21    let frame = encode_fwd_bytes(&req).unwrap();
22    println!("subscribe ({} bytes): {:02x?}", frame.len(), frame);
23
24    // The producer sees a field-write whose payload names where to deliver.
25    let seen = decode_fwd(&frame).unwrap();
26    assert!(seen.field.is_some(), "a subscribe is addressed to a field");
27    let target = subscriber_target_path(seen.payload.as_ref().unwrap()).unwrap();
28    assert_eq!(target.as_deref(), Some("/client"));
29    println!("producer will deliver to {}", target.unwrap());
30
31    // Producer -> consumer: a delivery is a WRITE of the new VALUE to that target.
32    let mut push = FwdRequest::new(fwd_op::WRITE, &["client"], &["sensor", "temp"]);
33    push.payload = Some(value_u32(21_750));
34    let delivery = decode_fwd(&encode_fwd_bytes(&push).unwrap()).unwrap();
35    assert_eq!(delivery.op, fwd_op::WRITE);
36    assert_eq!(fwd_dst_path(&delivery).unwrap(), "/client");
37    assert_eq!(delivery.payload.unwrap().payload_uint(), 21_750);
38    println!("delivery: WRITE /client <- 21750");
39}

Trait Implementations§

Source§

impl Clone for Tlv

Source§

fn clone(&self) -> Self

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 Tlv

Source§

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

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

impl Default for Tlv

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Tlv

Source§

impl PartialEq for Tlv

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Tlv

Auto Trait Implementations§

§

impl Freeze for Tlv

§

impl RefUnwindSafe for Tlv

§

impl Send for Tlv

§

impl Sync for Tlv

§

impl Unpin for Tlv

§

impl UnsafeUnpin for Tlv

§

impl UnwindSafe for Tlv

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.