pub trait Encode {
// Required method
fn encode(&self, w: impl Write) -> Result<()>;
// Provided method
fn encode_slice(slice: &[Self], w: impl Write) -> Result<()>
where Self: Sized { ... }
}Expand description
The Encode trait allows objects to be written to the Minecraft protocol.
It is the inverse of Decode.
§Deriving
This trait can be implemented automatically for structs and enums by using
the Encode derive macro. All components of the type must
implement Encode. Components are encoded in the order they appear in the
type definition.
For enums, the variant to encode is marked 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::Encode;
#[derive(Encode)]
struct MyStruct<'a> {
first: i32,
second: &'a str,
third: [f64; 3],
}
#[derive(Encode)]
enum MyEnum {
First, // tag = 0
Second, // tag = 1
#[packet(tag = 25)]
Third, // tag = 25
Fourth, // tag = 26
}
let value = MyStruct {
first: 10,
second: "hello",
third: [1.5, 3.14, 2.718],
};
let mut buf = vec![];
value.encode(&mut buf).unwrap();
println!("{buf:?}");Required Methods§
Sourcefn encode(&self, w: impl Write) -> Result<()>
fn encode(&self, w: impl Write) -> Result<()>
Writes this object to the provided writer.
If this type also implements Decode then successful calls to this
function returning Ok(()) must always successfully decode using
the data that was written to the writer. The exact number of bytes
that were originally written must be consumed during the decoding.
Provided Methods§
Sourcefn encode_slice(slice: &[Self], w: impl Write) -> Result<()>where
Self: Sized,
fn encode_slice(slice: &[Self], w: impl Write) -> Result<()>where
Self: Sized,
Like Encode::encode, except that a whole slice of values is encoded.
This method must be semantically equivalent to encoding every element of the slice in sequence with no leading length prefix (which is exactly what the default implementation does), but a more efficient implementation may be used.
This method is important for some types like u8 where the entire slice
can be encoded in a single call to write_all. Because impl
specialization is unavailable in stable Rust at the time of writing,
we must make the slice specialization part of this trait.
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: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode> Encode for (A, B, C, D, E, F)
impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode> Encode for (A, B, C, D, E, F)
Source§impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode> Encode for (A, B, C, D, E, F, G)
impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode> Encode for (A, B, C, D, E, F, G)
Source§impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode> Encode for (A, B, C, D, E, F, G, H)
impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode> Encode for (A, B, C, D, E, F, G, H)
Source§impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode> Encode for (A, B, C, D, E, F, G, H, I)
impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode> Encode for (A, B, C, D, E, F, G, H, I)
Source§impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode, J: Encode> Encode for (A, B, C, D, E, F, G, H, I, J)
impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode, J: Encode> Encode for (A, B, C, D, E, F, G, H, I, J)
Source§impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode, J: Encode, K: Encode> Encode for (A, B, C, D, E, F, G, H, I, J, K)
impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode, J: Encode, K: Encode> Encode for (A, B, C, D, E, F, G, H, I, J, K)
Source§impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode, J: Encode, K: Encode, L: Encode> Encode for (A, B, C, D, E, F, G, H, I, J, K, L)
impl<A: Encode, B: Encode, C: Encode, D: Encode, E: Encode, F: Encode, G: Encode, H: Encode, I: Encode, J: Encode, K: Encode, L: Encode> Encode for (A, B, C, D, E, F, G, H, I, J, K, L)
Source§impl<T: Encode, const N: usize> Encode for [T; N]
Like tuples, fixed-length arrays are encoded and decoded without a VarInt
length prefix.
impl<T: Encode, const N: usize> Encode for [T; N]
Like tuples, fixed-length arrays are encoded and decoded without a VarInt
length prefix.
Implementors§
impl Encode for IDSet
impl Encode for VariableBitSet
impl Encode for ByteAngle
impl Encode for RawBytes<'_>
impl Encode for TextComponent
impl Encode for VarInt
impl Encode for VarLong
impl<T: Encode + Clone + Debug + PartialEq> Encode for IdOr<T>
impl<T: Encode, const MAX_LEN: usize> Encode for Bounded<&[T], MAX_LEN>
impl<T: Encode, const N: usize> Encode for FixedArray<T, N>
impl<const BIT_COUNT: usize, const BYTE_COUNT: usize> Encode for FixedBitSet<BIT_COUNT, BYTE_COUNT>
impl<const MAX_BYTES: usize> Encode for Bounded<RawBytes<'_>, MAX_BYTES>
Raises an encoding error if the inner slice is longer than MAX_BYTES.