DecodeWithLength

Trait DecodeWithLength 

Source
pub trait DecodeWithLength<'a>: 'a + Sized {
    // Required method
    fn decode(
        src: &'a [u8],
        length: usize,
    ) -> Result<(Self, usize), DecodeError>;
}
Expand description

Trait for decoding SMPP values from a slice with a specified length.

§Implementation


#[derive(Debug, PartialEq, Eq)]
struct Foo<'a> {
    a: u8,
    b: u16,
    c: AnyOctetString<'a>,
}

impl<'a> DecodeWithLength<'a> for Foo<'a> {
    fn decode(src: &'a [u8], length: usize) -> Result<(Self, usize), DecodeError> {
        let index = 0;

        let (a, size) = Decode::decode(&src[index..])?;
        let index = index + size;

        let (b, size) = Decode::decode(&src[index..])?;
        let index = index + size;

        let (c, size) = AnyOctetString::decode(&src[index..], length - index)?;
        let index = index + size;

        Ok((Foo { a, b, c }, index))
    }
}

// Received over the wire
let length = 8;

let buf = &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];

let expected = Foo {
    a: 0x01,
    b: 0x0203,
    c: AnyOctetString::new(&[0x04, 0x05, 0x06, 0x07, 0x08]),
};

let (foo, size) = Foo::decode(buf, length).unwrap();

assert_eq!(size, 8);
assert_eq!(foo, expected);
assert_eq!(&buf[size..], &[0x09]);

Required Methods§

Source

fn decode(src: &'a [u8], length: usize) -> Result<(Self, usize), DecodeError>

Decode a value from a slice, with a specified length

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a, const N: usize, T: Decode<'a>> DecodeWithLength<'a> for Vec<T, N>

Source§

fn decode(src: &'a [u8], length: usize) -> Result<(Self, usize), DecodeError>

Implementors§

Source§

impl<'a> DecodeWithLength<'a> for AlertNotification<'a>

Source§

impl<'a> DecodeWithLength<'a> for BindReceiverResp<'a>

Source§

impl<'a> DecodeWithLength<'a> for BindTransceiverResp<'a>

Source§

impl<'a> DecodeWithLength<'a> for BindTransmitterResp<'a>

Source§

impl<'a> DecodeWithLength<'a> for QueryBroadcastSm<'a>

Source§

impl<'a> DecodeWithLength<'a> for ReplaceSm<'a>

Source§

impl<'a> DecodeWithLength<'a> for AnyOctetString<'a>

Source§

impl<'a> DecodeWithLength<'a> for BroadcastAreaIdentifier<'a>

Source§

impl<'a> DecodeWithLength<'a> for MessagePayload<'a>

Source§

impl<'a> DecodeWithLength<'a> for Subaddress<'a>

Source§

impl<'a> DecodeWithLength<'a> for MsValidity

Source§

impl<'a, T: Decode<'a>> DecodeWithLength<'a> for T

Everything that implements Decode also implements DecodeWithLength by ignoring the length.

Source§

impl<'a, const MIN: usize, const MAX: usize> DecodeWithLength<'a> for OctetString<'a, MIN, MAX>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for Command<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for BroadcastSm<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for BroadcastSmResp<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for CancelBroadcastSm<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for DataSm<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for DataSmResp<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for DeliverSm<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for DeliverSmResp<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for QueryBroadcastSmResp<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for SubmitMulti<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for SubmitMultiResp<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for SubmitSm<'a, N>

Source§

impl<'a, const N: usize> DecodeWithLength<'a> for SubmitSmResp<'a, N>