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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use std::io::Write;
use std::slice;

use anyhow::ensure;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

use crate::{Decode, Encode};

impl Encode for bool {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_u8(u8::from(*self))?)
    }

    fn encode_slice(slice: &[bool], mut w: impl Write) -> anyhow::Result<()> {
        // SAFETY: Bools have the same layout as u8.
        let bytes = unsafe { slice::from_raw_parts(slice.as_ptr() as *const u8, slice.len()) };
        // Bools are guaranteed to have the correct bit pattern.
        Ok(w.write_all(bytes)?)
    }
}

impl Decode<'_> for bool {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        let n = r.read_u8()?;
        ensure!(n <= 1, "decoded boolean byte is not 0 or 1 (got {n})");
        Ok(n == 1)
    }
}

impl Encode for u8 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_u8(*self)?)
    }

    fn encode_slice(slice: &[u8], mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_all(slice)?)
    }
}

impl Decode<'_> for u8 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        Ok(r.read_u8()?)
    }
}

impl Encode for i8 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_i8(*self)?)
    }

    fn encode_slice(slice: &[i8], mut w: impl Write) -> anyhow::Result<()> {
        // SAFETY: i8 has the same layout as u8.
        let bytes = unsafe { slice::from_raw_parts(slice.as_ptr() as *const u8, slice.len()) };
        Ok(w.write_all(bytes)?)
    }
}

impl Decode<'_> for i8 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        Ok(r.read_i8()?)
    }
}

impl Encode for u16 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_u16::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for u16 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        Ok(r.read_u16::<BigEndian>()?)
    }
}

impl Encode for i16 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_i16::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for i16 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        Ok(r.read_i16::<BigEndian>()?)
    }
}

impl Encode for u32 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_u32::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for u32 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        Ok(r.read_u32::<BigEndian>()?)
    }
}

impl Encode for i32 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_i32::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for i32 {
    fn decode(r: &mut &'_ [u8]) -> anyhow::Result<Self> {
        Ok(r.read_i32::<BigEndian>()?)
    }
}

impl Encode for u64 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_u64::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for u64 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        Ok(r.read_u64::<BigEndian>()?)
    }
}

impl Encode for i64 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_i64::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for i64 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        Ok(r.read_i64::<BigEndian>()?)
    }
}

impl Encode for u128 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_u128::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for u128 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        Ok(r.read_u128::<BigEndian>()?)
    }
}

impl Encode for i128 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        Ok(w.write_i128::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for i128 {
    fn decode(r: &mut &'_ [u8]) -> anyhow::Result<Self> {
        Ok(r.read_i128::<BigEndian>()?)
    }
}

impl Encode for f32 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        ensure!(
            self.is_finite(),
            "attempt to encode non-finite f32 ({})",
            self
        );
        Ok(w.write_f32::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for f32 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        let f = r.read_f32::<BigEndian>()?;
        ensure!(f.is_finite(), "attempt to decode non-finite f32 ({f})");
        Ok(f)
    }
}

impl Encode for f64 {
    fn encode(&self, mut w: impl Write) -> anyhow::Result<()> {
        ensure!(
            self.is_finite(),
            "attempt to encode non-finite f64 ({})",
            self
        );
        Ok(w.write_f64::<BigEndian>(*self)?)
    }
}

impl Decode<'_> for f64 {
    fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
        let f = r.read_f64::<BigEndian>()?;
        ensure!(f.is_finite(), "attempt to decode non-finite f64 ({f})");
        Ok(f)
    }
}