pub trait Decode<'de>: Sized {
// Required method
fn decode(buf: &mut ReadBuf<'de>) -> Result<Self>;
}Expand description
A value that can be read from a ReadBuf.
The lifetime parameter 'de ties the decoded value to the borrowed input,
enabling zero-copy decoding of borrowed payloads (&'de [u8], &'de str).
§Example
use wire_codec::{Decode, ReadBuf, Result};
struct Tagged {
tag: u8,
value: u32,
}
impl<'de> Decode<'de> for Tagged {
fn decode(buf: &mut ReadBuf<'de>) -> Result<Self> {
let tag = buf.read_u8()?;
let value = buf.read_u32_be()?;
Ok(Self { tag, value })
}
}
let mut buf = ReadBuf::new(&[0x01, 0xCA, 0xFE, 0xBA, 0xBE]);
let tagged = Tagged::decode(&mut buf).unwrap();
assert_eq!(tagged.tag, 0x01);
assert_eq!(tagged.value, 0xCAFEBABE);Required Methods§
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<'de, A: Decode<'de>, B: Decode<'de>, C: Decode<'de>, D: Decode<'de>, E: Decode<'de>, F: Decode<'de>> Decode<'de> for (A, B, C, D, E, F)
impl<'de, A: Decode<'de>, B: Decode<'de>, C: Decode<'de>, D: Decode<'de>, E: Decode<'de>, F: Decode<'de>> Decode<'de> for (A, B, C, D, E, F)
Source§impl<'de, A: Decode<'de>, B: Decode<'de>, C: Decode<'de>, D: Decode<'de>, E: Decode<'de>> Decode<'de> for (A, B, C, D, E)
impl<'de, A: Decode<'de>, B: Decode<'de>, C: Decode<'de>, D: Decode<'de>, E: Decode<'de>> Decode<'de> for (A, B, C, D, E)
Source§impl<'de, A: Decode<'de>, B: Decode<'de>, C: Decode<'de>, D: Decode<'de>> Decode<'de> for (A, B, C, D)
impl<'de, A: Decode<'de>, B: Decode<'de>, C: Decode<'de>, D: Decode<'de>> Decode<'de> for (A, B, C, D)
Source§impl<'de> Decode<'de> for bool
Strict: only 0 and 1 are accepted, so every value has one encoding.
impl<'de> Decode<'de> for bool
Strict: only 0 and 1 are accepted, so every value has one encoding.