steel_utils/serial/
read.rs1use std::{
2 array,
3 io::{Cursor, Error, Read, Result},
4 str::FromStr,
5};
6
7use uuid::Uuid;
8
9use crate::{
10 Identifier,
11 codec::VarInt,
12 serial::{PrefixedRead, ReadFrom},
13};
14
15impl ReadFrom for bool {
16 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
17 let byte = u8::read(data)?;
18 Ok(byte == 1)
19 }
20}
21
22impl ReadFrom for u8 {
23 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
24 let mut buf = [0; size_of::<Self>()];
25 data.read_exact(&mut buf)?;
26 Ok(Self::from_be_bytes(buf))
27 }
28}
29
30impl ReadFrom for u16 {
31 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
32 let mut buf = [0; size_of::<Self>()];
33 data.read_exact(&mut buf)?;
34 Ok(Self::from_be_bytes(buf))
35 }
36}
37
38impl ReadFrom for u32 {
39 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
40 let mut buf = [0; size_of::<Self>()];
41 data.read_exact(&mut buf)?;
42 Ok(Self::from_be_bytes(buf))
43 }
44}
45
46impl ReadFrom for u64 {
47 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
48 let mut buf = [0; size_of::<Self>()];
49 data.read_exact(&mut buf)?;
50 Ok(Self::from_be_bytes(buf))
51 }
52}
53
54impl ReadFrom for i8 {
55 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
56 let mut buf = [0; size_of::<Self>()];
57 data.read_exact(&mut buf)?;
58 Ok(Self::from_be_bytes(buf))
59 }
60}
61
62impl ReadFrom for i16 {
63 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
64 let mut buf = [0; size_of::<Self>()];
65 data.read_exact(&mut buf)?;
66 Ok(Self::from_be_bytes(buf))
67 }
68}
69
70impl ReadFrom for i32 {
71 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
72 let mut buf = [0; size_of::<Self>()];
73 data.read_exact(&mut buf)?;
74 Ok(Self::from_be_bytes(buf))
75 }
76}
77
78impl ReadFrom for i64 {
79 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
80 let mut buf = [0; size_of::<Self>()];
81 data.read_exact(&mut buf)?;
82 Ok(Self::from_be_bytes(buf))
83 }
84}
85
86impl ReadFrom for f32 {
87 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
88 let mut buf = [0; size_of::<Self>()];
89 data.read_exact(&mut buf)?;
90 Ok(Self::from_be_bytes(buf))
91 }
92}
93
94impl ReadFrom for f64 {
95 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
96 let mut buf = [0; size_of::<Self>()];
97 data.read_exact(&mut buf)?;
98 Ok(Self::from_be_bytes(buf))
99 }
100}
101
102impl<T: ReadFrom> ReadFrom for Option<T> {
103 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
104 if bool::read(data)? {
105 Ok(Some(T::read(data)?))
106 } else {
107 Ok(None)
108 }
109 }
110}
111
112impl<T: ReadFrom, const N: usize> ReadFrom for [T; N] {
113 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
114 array::try_from_fn(|_| T::read(data))
115 }
116}
117
118impl ReadFrom for Uuid {
119 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
120 let most_significant_bits = u64::read(data)?;
121 let least_significant_bits = u64::read(data)?;
122
123 Ok(Uuid::from_u64_pair(
124 most_significant_bits,
125 least_significant_bits,
126 ))
127 }
128}
129
130impl ReadFrom for Identifier {
131 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
132 Identifier::from_str(&String::read_prefixed::<VarInt>(data)?).map_err(Error::other)
133 }
134}