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§
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§
Implementors§
impl<'a> DecodeWithLength<'a> for AlertNotification<'a>
impl<'a> DecodeWithLength<'a> for BindReceiverResp<'a>
impl<'a> DecodeWithLength<'a> for BindTransceiverResp<'a>
impl<'a> DecodeWithLength<'a> for BindTransmitterResp<'a>
impl<'a> DecodeWithLength<'a> for QueryBroadcastSm<'a>
impl<'a> DecodeWithLength<'a> for ReplaceSm<'a>
impl<'a> DecodeWithLength<'a> for AnyOctetString<'a>
impl<'a> DecodeWithLength<'a> for BroadcastAreaIdentifier<'a>
impl<'a> DecodeWithLength<'a> for MessagePayload<'a>
impl<'a> DecodeWithLength<'a> for Subaddress<'a>
impl<'a> DecodeWithLength<'a> for MsValidity
impl<'a, T: Decode<'a>> DecodeWithLength<'a> for T
Everything that implements Decode also implements DecodeWithLength by ignoring the length.