[][src]Struct xaynet_core::message::LengthValueBuffer

pub struct LengthValueBuffer<T> { /* fields omitted */ }

A helper for encoding and decoding Length-Value (LV) fields.

Note that the 4 bytes length() field gives the length of the total Length-Value field, i.e. the length of the value, plus the 4 extra bytes of the length field itself.

Examples

Decoding a LV field

let bytes = vec![
    0x00, 0x00, 0x00, 0x05, // Length = 5
    0xff, // Value = 0xff
    0x11, 0x22, // Extra bytes
];
let buffer = LengthValueBuffer::new(&bytes).unwrap();
assert_eq!(buffer.length(), 5);
assert_eq!(buffer.value_length(), 1);
assert_eq!(buffer.value(), &[0xff][..]);

Encoding a LV field

let mut bytes = vec![0xff; 9];
let mut buffer = LengthValueBuffer::new_unchecked(&mut bytes);
// It is important to set the length field before setting the value, otherwise, `value_mut()` will panic.
buffer.set_length(8);
buffer.value_mut().copy_from_slice(&[0, 1, 2, 3][..]);
let expected = vec![
    0x00, 0x00, 0x00, 0x08, // Length = 8
    0x00, 0x01, 0x02, 0x03, // Value
    0xff, // unchanged
];

assert_eq!(bytes, expected);

Implementations

impl<T: AsRef<[u8]>> LengthValueBuffer<T>[src]

pub fn new(bytes: T) -> Result<Self, DecodeError>[src]

Returns a new LengthValueBuffer.

Errors

This method performs bound checks and returns an error if the given buffer is not a valid Length-Value item.

Examples

// truncated length:
assert!(LengthValueBuffer::new(&vec![0x00, 0x00, 0x00]).is_err());

// truncated value:
let bytes = vec![
    0x00, 0x00, 0x00, 0x08, // length: 8
    0x11, 0x22, 0x33, // value
];
assert!(LengthValueBuffer::new(&bytes).is_err());

// valid Length-Value item
let bytes = vec![
    0x00, 0x00, 0x00, 0x08, // length: 8
    0x11, 0x22, 0x33, 0x44, // value
    0xaa, 0xbb, // extra bytes are ignored
];
let buf = LengthValueBuffer::new(&bytes).unwrap();
assert_eq!(buf.length(), 8);
assert_eq!(buf.value(), &[0x11, 0x22, 0x33, 0x44][..]);

pub fn new_unchecked(bytes: T) -> Self[src]

Create a new LengthValueBuffer without any bound checks.

pub fn check_buffer_length(&self) -> Result<(), DecodeError>[src]

Check that the buffer is a valid Length-Value item.

pub fn length(&self) -> u32[src]

Returns the length field. Note that the value of the length field includes the length of the field itself (4 bytes).

Panics

This method may panic if buffer is not a valid Length-Value item.

pub fn value_length(&self) -> usize[src]

Returns the length of the value.

impl<T: AsMut<[u8]>> LengthValueBuffer<T>[src]

pub fn set_length(&mut self, value: u32)[src]

Sets the length field to the given value.

Panics

This method may panic if buffer is not a valid Length-Value item.

impl<'a, T: AsRef<[u8]> + AsMut<[u8]> + ?Sized> LengthValueBuffer<&'a mut T>[src]

pub fn value_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the value field.

Panics

This method may panic if buffer is not a valid Length-Value item.

pub fn bytes_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the underlying buffer.

Panics

This method may panic if buffer is not a valid Length-Value item.

impl<'a, T: AsRef<[u8]> + ?Sized> LengthValueBuffer<&'a T>[src]

pub fn value(&self) -> &'a [u8][src]

Gets a reference to the value field.

Panics

This method may panic if buffer is not a valid Length-Value item.

pub fn bytes(self) -> &'a [u8][src]

Gets a reference to the underlying buffer.

Panics

This method may panic if buffer is not a valid Length-Value item.

Auto Trait Implementations

impl<T> RefUnwindSafe for LengthValueBuffer<T> where
    T: RefUnwindSafe

impl<T> Send for LengthValueBuffer<T> where
    T: Send

impl<T> Sync for LengthValueBuffer<T> where
    T: Sync

impl<T> Unpin for LengthValueBuffer<T> where
    T: Unpin

impl<T> UnwindSafe for LengthValueBuffer<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,