xet_core_structures/utils/
serialization_utils.rs1use std::io::{Read, Write};
2use std::mem::{size_of, transmute};
3
4use futures::AsyncReadExt;
5use futures::io::AsyncRead;
6
7use crate::merklehash::DataHash;
8
9#[inline]
10pub fn write_hash<W: Write>(writer: &mut W, m: &DataHash) -> Result<(), std::io::Error> {
11 writer.write_all(m.as_bytes())
12}
13
14#[inline]
15pub fn write_u8<W: Write>(writer: &mut W, v: u8) -> Result<(), std::io::Error> {
16 writer.write_all(&v.to_le_bytes())
17}
18
19#[inline]
20pub fn write_u32<W: Write>(writer: &mut W, v: u32) -> Result<(), std::io::Error> {
21 writer.write_all(&v.to_le_bytes())
22}
23
24#[inline]
25pub fn write_u64<W: Write>(writer: &mut W, v: u64) -> Result<(), std::io::Error> {
26 writer.write_all(&v.to_le_bytes())
27}
28
29#[inline]
30pub fn write_bytes<W: Write>(writer: &mut W, vs: &[u8]) -> Result<(), std::io::Error> {
31 writer.write_all(vs)
32}
33
34#[inline]
35pub fn write_u32s<W: Write>(writer: &mut W, vs: &[u32]) -> Result<(), std::io::Error> {
36 for e in vs {
37 write_u32(writer, *e)?;
38 }
39
40 Ok(())
41}
42
43#[inline]
44pub fn write_u64s<W: Write>(writer: &mut W, vs: &[u64]) -> Result<(), std::io::Error> {
45 for e in vs {
46 write_u64(writer, *e)?;
47 }
48
49 Ok(())
50}
51
52#[inline]
53pub fn read_hash<R: Read>(reader: &mut R) -> Result<DataHash, std::io::Error> {
54 let mut m = [0u8; 32];
55 reader.read_exact(&mut m)?; Ok(DataHash::from(unsafe { transmute::<[u8; 32], [u64; 4]>(m) }))
58}
59
60#[inline]
61pub fn read_u8<R: Read>(reader: &mut R) -> Result<u8, std::io::Error> {
62 let mut buf = [0u8; size_of::<u8>()];
63 reader.read_exact(&mut buf[..])?;
64 Ok(u8::from_le_bytes(buf))
65}
66
67#[inline]
68pub fn read_u32<R: Read>(reader: &mut R) -> Result<u32, std::io::Error> {
69 let mut buf = [0u8; size_of::<u32>()];
70 reader.read_exact(&mut buf[..])?;
71 Ok(u32::from_le_bytes(buf))
72}
73
74#[inline]
75pub fn read_u64<R: Read>(reader: &mut R) -> Result<u64, std::io::Error> {
76 let mut buf = [0u8; size_of::<u64>()];
77 reader.read_exact(&mut buf[..])?;
78 Ok(u64::from_le_bytes(buf))
79}
80
81#[inline]
82pub fn read_bytes<R: Read>(reader: &mut R, val: &mut [u8]) -> Result<(), std::io::Error> {
83 reader.read_exact(val)
84}
85
86#[inline]
87pub fn read_u32s<R: Read>(reader: &mut R, vs: &mut [u32]) -> Result<(), std::io::Error> {
88 for e in vs.iter_mut() {
89 *e = read_u32(reader)?;
90 }
91
92 Ok(())
93}
94
95#[inline]
96pub fn read_u64s<R: Read>(reader: &mut R, vs: &mut [u64]) -> Result<(), std::io::Error> {
97 for e in vs.iter_mut() {
98 *e = read_u64(reader)?;
99 }
100
101 Ok(())
102}
103
104#[inline]
106pub async fn read_hash_async<R: AsyncRead + Unpin>(reader: &mut R) -> Result<DataHash, std::io::Error> {
107 let mut m = [0u8; 32];
108 reader.read_exact(&mut m).await?; Ok(DataHash::from(unsafe { transmute::<[u8; 32], [u64; 4]>(m) }))
111}
112
113#[inline]
114pub async fn read_u8_async<R: AsyncRead + Unpin>(reader: &mut R) -> Result<u8, std::io::Error> {
115 let mut buf = [0u8; size_of::<u8>()];
116 reader.read_exact(&mut buf[..]).await?;
117 Ok(u8::from_le_bytes(buf))
118}
119
120#[inline]
121pub async fn read_u32_async<R: AsyncRead + Unpin>(reader: &mut R) -> Result<u32, std::io::Error> {
122 let mut buf = [0u8; size_of::<u32>()];
123 reader.read_exact(&mut buf[..]).await?;
124 Ok(u32::from_le_bytes(buf))
125}
126
127#[inline]
128pub async fn read_u32s_async<R: AsyncRead + Unpin>(reader: &mut R, vs: &mut [u32]) -> Result<(), std::io::Error> {
129 for v in vs.iter_mut() {
130 *v = read_u32_async(reader).await?;
131 }
132 Ok(())
133}
134
135#[inline]
136pub async fn read_u64_async<R: AsyncRead + Unpin>(reader: &mut R) -> Result<u64, std::io::Error> {
137 let mut buf = [0u8; size_of::<u64>()];
138 reader.read_exact(&mut buf[..]).await?;
139 Ok(u64::from_le_bytes(buf))
140}
141
142#[inline]
143pub async fn read_u64s_async<R: AsyncRead + Unpin>(reader: &mut R, vs: &mut [u64]) -> Result<(), std::io::Error> {
144 for v in vs.iter_mut() {
145 *v = read_u64_async(reader).await?;
146 }
147 Ok(())
148}
149
150#[inline]
151pub async fn read_bytes_async<R: AsyncRead + Unpin>(reader: &mut R, val: &mut [u8]) -> Result<(), std::io::Error> {
152 reader.read_exact(val).await
153}