Skip to main content

DecodeWithLength

Trait DecodeWithLength 

Source
pub trait DecodeWithLength: Sized {
    // Required method
    fn decode(
        src: &mut BytesMut,
        length: usize,
    ) -> Result<(Self, usize), DecodeError>;
}
Available on crate feature alloc only.
Expand description

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

§Implementation


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

impl DecodeWithLength for Foo {
    fn decode(src: &mut BytesMut, length: usize) -> Result<(Self, usize), DecodeError> {
        let index = 0;

        let (a, size) = Decode::decode(src)?;
        let index = index + size;

        let (b, size) = Decode::decode(src)?;
        let index = index + size;

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

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

// Received over the wire
let length = 8;

let mut buf = BytesMut::from(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09][..]);

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

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

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

Required Methods§

Source

fn decode( src: &mut BytesMut, length: usize, ) -> Result<(Self, usize), DecodeError>

Decode a value from a buffer, with a specified length

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: Decode> DecodeWithLength for Vec<T>

Source§

fn decode( src: &mut BytesMut, length: usize, ) -> Result<(Self, usize), DecodeError>

Implementors§