pub trait Decode<'a>: Sized {
// Required method
fn decode(r: &mut &'a [u8]) -> Result<Self>;
}Expand description
The Decode trait allows objects to be read from the Minecraft protocol. It
is the inverse of Encode.
Decode is parameterized by a lifetime. This allows the decoded value to
borrow data from the byte slice it was read from.
§Deriving
This trait can be implemented automatically for structs and enums by using
the Decode derive macro. All components of the type must
implement Decode. Components are decoded in the order they appear in the
type definition.
For enums, the variant to decode is determined by a leading VarInt
discriminant (tag). The discriminant value can be changed using the
#[packet(tag = ...)] attribute on the variant in question. Discriminant
values are assigned to variants using rules similar to regular enum
discriminants.
use chunkedge_binary::Decode;
#[derive(PartialEq, Debug, Decode)]
struct MyStruct {
first: i32,
second: MyEnum,
}
#[derive(PartialEq, Debug, Decode)]
enum MyEnum {
First, // tag = 0
Second, // tag = 1
#[packet(tag = 25)]
Third, // tag = 25
Fourth, // tag = 26
}
let mut r: &[u8] = &[0, 0, 0, 0, 26];
let value = MyStruct::decode(&mut r).unwrap();
let expected = MyStruct {
first: 0,
second: MyEnum::Fourth,
};
assert_eq!(value, expected);
assert!(r.is_empty());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§
Source§impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>> Decode<'a> for (A, B, C, D)
impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>> Decode<'a> for (A, B, C, D)
Source§impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>> Decode<'a> for (A, B, C, D, E)
impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>> Decode<'a> for (A, B, C, D, E)
Source§impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>> Decode<'a> for (A, B, C, D, E, F)
impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>> Decode<'a> for (A, B, C, D, E, F)
Source§impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G)
impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G)
Source§impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H)
impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H)
Source§impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>, I: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H, I)
impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>, I: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H, I)
Source§impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>, I: Decode<'a>, J: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H, I, J)
impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>, I: Decode<'a>, J: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H, I, J)
Source§impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>, I: Decode<'a>, J: Decode<'a>, K: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H, I, J, K)
impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>, I: Decode<'a>, J: Decode<'a>, K: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H, I, J, K)
Source§impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>, I: Decode<'a>, J: Decode<'a>, K: Decode<'a>, L: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H, I, J, K, L)
impl<'a, A: Decode<'a>, B: Decode<'a>, C: Decode<'a>, D: Decode<'a>, E: Decode<'a>, F: Decode<'a>, G: Decode<'a>, H: Decode<'a>, I: Decode<'a>, J: Decode<'a>, K: Decode<'a>, L: Decode<'a>> Decode<'a> for (A, B, C, D, E, F, G, H, I, J, K, L)
Source§impl<'a, const N: usize> Decode<'a> for &'a [u8; N]
References to fixed-length arrays are not length prefixed.
impl<'a, const N: usize> Decode<'a> for &'a [u8; N]
References to fixed-length arrays are not length prefixed.
Implementors§
impl Decode<'_> for VariableBitSet
impl Decode<'_> for ByteAngle
impl Decode<'_> for TextComponent
impl Decode<'_> for VarInt
impl Decode<'_> for VarLong
impl<'a> Decode<'a> for IDSet
impl<'a> Decode<'a> for RawBytes<'a>
impl<'a, T: Decode<'a> + Encode + Clone + Debug + PartialEq> Decode<'a> for IdOr<T>
impl<'a, T: Decode<'a>, const MAX_LEN: usize> Decode<'a> for Bounded<Box<[T]>, MAX_LEN>
impl<'a, T: Decode<'a>, const MAX_LEN: usize> Decode<'a> for Bounded<Vec<T>, MAX_LEN>
impl<'a, T: Decode<'a>, const N: usize> Decode<'a> for FixedArray<T, N>
impl<'a, const MAX_BYTES: usize> Decode<'a> for Bounded<RawBytes<'a>, MAX_BYTES>
Raises a decoding error if the remainder of the input is larger than
MAX_BYTES.