1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Core `Encode` and `Decode` traits, plus top-level convenience functions.
use crate::;
/// Writes a value into a [`BufEncoder`].
///
/// # Contract
///
/// - Write fields in declaration order.
/// - No type flags, field names, or padding — just raw bytes.
/// - The byte count produced must be deterministic for a given value.
///
/// Encoding is always infallible. If a value can be constructed, it can be encoded.
///
/// # Deriving
///
/// Enable the `derive` feature and use `#[derive(Encode)]`:
///
/// ```rust,ignore
/// use zerec::Encode;
///
/// #[derive(Encode)]
/// struct Hit {
/// position: [f32; 3],
/// damage: u32,
/// }
/// ```
/// Reads a value from [`BufDecoder`].
///
/// # Contract
///
/// - Read fields in the exact order and sizes that [`Encode`] wrote them.
/// - Return [`DecodeError`] on malformed input; never panic.
/// - Consume exactly as many bytes as [`Encode`] produced.
///
/// # Deriving
///
/// Enable the `derive` feature and use `#[derive(Decode)]`:
///
/// ```rust,ignore
/// use zerec::Decode;
///
/// #[derive(Decode)]
/// struct Hit {
/// position: [f32; 3],
/// damage: u32,
/// }
/// ```
/// Encodes `value` and returns the result as a freshly allocated `Vec<u8>`.
///
/// ```rust
/// use zerec::{Encode, codec::to_bytes};
/// use zerec::encoder::BufEncoder;
///
/// struct Score(u32);
///
/// impl Encode for Score {
/// fn encode(&self, enc: &mut BufEncoder) { self.0.encode(enc); }
/// }
///
/// let bytes = to_bytes(&Score(999));
/// assert_eq!(bytes, 999u32.to_le_bytes());
/// ```
/// Decodes a value from a byte slice.
///
/// Does not require the entire slice to be consumed — use [`BufDecoder`]
/// directly if you need to check for trailing bytes.
///
/// ```rust
/// use zerec::codec::{to_bytes, from_bytes};
///
/// let n: u64 = 0xDEAD_BEEF_CAFE_0001;
/// assert_eq!(from_bytes::<u64>(&to_bytes(&n)).unwrap(), n);
/// ```