pub trait DecodeWithKeyOptional<'a>: 'a + Sized {
type Key;
// Required method
fn decode(
key: Self::Key,
src: &'a [u8],
length: usize,
) -> Result<Option<(Self, usize)>, DecodeError>;
}Expand description
Trait for decoding optional SMPP values from a slice with a specified key and length.
§Implementation
#[derive(Debug, PartialEq, Eq)]
enum Foo<'a> {
A,
B(u16),
C(AnyOctetString<'a>),
}
impl<'a> DecodeWithKeyOptional<'a> for Foo<'a> {
type Key = u32;
fn decode(
key: Self::Key,
src: &'a [u8],
length: usize,
) -> Result<Option<(Self, usize)>, DecodeError> {
if length == 0 {
match key {
0x00000000 => return Ok(Some((Foo::A, 0))),
_ => return Ok(None),
}
}
match key {
0x01020304 => {
let (a, size) = Decode::decode(src)?;
Ok(Some((Foo::B(a), size)))
}
0x04030201 => {
let (b, size) = AnyOctetString::decode(src, length)?;
Ok(Some((Foo::C(b), size)))
}
_ => Err(DecodeError::unsupported_key(key)),
}
}
}
// Received over the wire
let length = 4;
// Key is A
let buf = &[
0x00, 0x00, 0x00, 0x00, // Key
0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
];
let index = 0;
let (key, size) = Decode::decode(buf).unwrap();
let index = index + size;
let (foo, size) = Foo::decode(key, &buf[index..], length - index)
.unwrap()
.unwrap();
let index = index + size;
let expected = Foo::A;
assert_eq!(size, 0);
assert_eq!(foo, expected);
assert_eq!(&buf[index..], &[0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B]);
// Received over the wire
let length = 4;
// Key is B, but the received length indicates no value
let buf = &[
0x01, 0x02, 0x03, 0x04, // Key
0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
];
let index = 0;
let (key, size) = Decode::decode(buf).unwrap();
let index = index + size;
let value = Foo::decode(key, &buf[index..], length - index).unwrap();
assert!(value.is_none());
assert_eq!(&buf[index..], &[0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B]);
// Received over the wire
let length = 8;
// Key is B
let buf = &[
0x01, 0x02, 0x03, 0x04, // Key
0x05, 0x06, // Value
0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
];
let index = 0;
let (key, size) = Decode::decode(buf).unwrap();
let index = index + size;
let (foo, size) = Foo::decode(key, &buf[index..], length - index)
.unwrap()
.unwrap();
let index = index + size;
let expected = Foo::B(0x0506);
assert_eq!(size, 2);
assert_eq!(foo, expected);
assert_eq!(&buf[index..], &[0x07, 0x08, 0x09, 0x0A, 0x0B]);
// Received over the wire
let length = 8;
// Key is C
let buf = &[
0x04, 0x03, 0x02, 0x01, // Key
0x05, 0x06, 0x07, 0x08, // Value
0x09, 0x0A, 0x0B, // Rest
];
let index = 0;
let (key, size) = Decode::decode(buf).unwrap();
let index = index + size;
let (foo, size) = Foo::decode(key, &buf[index..], length - index)
.unwrap()
.unwrap();
let index = index + size;
let expected = Foo::C(AnyOctetString::new(&[0x05, 0x06, 0x07, 0x08]));
assert_eq!(size, 4);
assert_eq!(foo, expected);
assert_eq!(&buf[index..], &[0x09, 0x0A, 0x0B]);Required Associated Types§
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.