codeq/codec/impls/
u32_impl.rs

1use std::io;
2use std::mem::size_of;
3
4use byteorder::ReadBytesExt;
5use byteorder::WriteBytesExt;
6
7use crate::Decode;
8use crate::Encode;
9use crate::FixedSize;
10
11impl FixedSize for u32 {
12    fn encoded_size() -> usize {
13        size_of::<Self>()
14    }
15}
16
17impl Encode for u32 {
18    fn encode<W: io::Write>(&self, mut w: W) -> Result<usize, io::Error> {
19        w.write_u32::<byteorder::BigEndian>(*self)?;
20        Ok(Self::encoded_size())
21    }
22}
23
24impl Decode for u32 {
25    fn decode<R: io::Read>(mut r: R) -> Result<Self, io::Error> {
26        let v = r.read_u32::<byteorder::BigEndian>()?;
27        Ok(v)
28    }
29}
30
31#[cfg(test)]
32mod tests {
33
34    use crate::testing::test_int_coded;
35
36    #[test]
37    fn test_u32_codec() -> anyhow::Result<()> {
38        test_int_coded(0x12345678u32)
39    }
40}