pub trait Decode<'a>: 'a + Sized {
// Required method
fn decode(src: &'a [u8]) -> Result<(Self, usize), DecodeError>;
}Expand description
Trait for decoding SMPP values from a slice.
§Implementation
#[derive(Debug, PartialEq, Eq)]
struct Foo {
a: u8,
b: u16,
c: u32,
}
impl<'a> Decode<'a> for Foo {
fn decode(src: &'a [u8]) -> 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) = Decode::decode(&src[index..])?;
let index = index + size;
Ok((Foo { a, b, c }, index))
}
}
let buf = &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
let expected = Foo {
a: 0x01,
b: 0x0203,
c: 0x04050607,
};
let (foo, size) = Foo::decode(buf).unwrap();
assert_eq!(size, 7);
assert_eq!(foo, expected);
assert_eq!(&buf[size..], &[0x08]);Required Methods§
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.