willow_encoding/
unsigned_int.rs1use crate::error::DecodeError;
2
3use core::mem::size_of;
4
5#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
7pub struct U8BE(u8);
8
9impl From<u8> for U8BE {
10 fn from(value: u8) -> Self {
11 Self(value)
12 }
13}
14
15impl From<U8BE> for u64 {
16 fn from(value: U8BE) -> Self {
17 value.0 as u64
18 }
19}
20
21#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
23pub struct U16BE(u16);
24
25impl From<u16> for U16BE {
26 fn from(value: u16) -> Self {
27 Self(value)
28 }
29}
30
31impl From<U16BE> for u64 {
32 fn from(value: U16BE) -> Self {
33 value.0 as u64
34 }
35}
36
37#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
39pub struct U32BE(u32);
40
41impl From<u32> for U32BE {
42 fn from(value: u32) -> Self {
43 Self(value)
44 }
45}
46
47impl From<U32BE> for u64 {
48 fn from(value: U32BE) -> Self {
49 value.0 as u64
50 }
51}
52
53#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
55pub struct U64BE(u64);
56
57impl From<u64> for U64BE {
58 fn from(value: u64) -> Self {
59 Self(value)
60 }
61}
62
63impl From<U64BE> for u64 {
64 fn from(value: U64BE) -> Self {
65 value.0
66 }
67}
68
69use syncify::syncify;
70use syncify::syncify_replace;
71
72#[syncify(encoding_sync)]
73mod encoding {
74 use super::*;
75
76 #[syncify_replace(use ufotofu::sync::{BulkConsumer, BulkProducer};)]
77 use ufotofu::local_nb::{BulkConsumer, BulkProducer};
78
79 #[syncify_replace(use crate::sync::{Decodable, Encodable};)]
80 use crate::{Decodable, Encodable};
81
82 impl Encodable for U8BE {
83 async fn encode<Consumer>(&self, consumer: &mut Consumer) -> Result<(), Consumer::Error>
84 where
85 Consumer: BulkConsumer<Item = u8>,
86 {
87 let byte = self.0.to_be_bytes();
88 consumer
89 .bulk_consume_full_slice(&byte)
90 .await
91 .map_err(|f| f.reason)?;
92 Ok(())
93 }
94 }
95
96 impl Decodable for U8BE {
97 async fn decode<Producer>(
98 producer: &mut Producer,
99 ) -> Result<Self, DecodeError<Producer::Error>>
100 where
101 Producer: BulkProducer<Item = u8>,
102 Self: Sized,
103 {
104 let mut bytes = [0u8; size_of::<u8>()];
105 producer.bulk_overwrite_full_slice(&mut bytes).await?;
106 Ok(U8BE(u8::from_be_bytes(bytes)))
107 }
108 }
109
110 impl Encodable for U16BE {
111 async fn encode<Consumer>(&self, consumer: &mut Consumer) -> Result<(), Consumer::Error>
112 where
113 Consumer: BulkConsumer<Item = u8>,
114 {
115 let bytes = self.0.to_be_bytes();
116 consumer
117 .bulk_consume_full_slice(&bytes)
118 .await
119 .map_err(|f| f.reason)?;
120 Ok(())
121 }
122 }
123
124 impl Decodable for U16BE {
125 async fn decode<Producer>(
126 producer: &mut Producer,
127 ) -> Result<Self, DecodeError<Producer::Error>>
128 where
129 Producer: BulkProducer<Item = u8>,
130 Self: Sized,
131 {
132 let mut bytes = [0u8; size_of::<u16>()];
133 producer.bulk_overwrite_full_slice(&mut bytes).await?;
134 Ok(U16BE(u16::from_be_bytes(bytes)))
135 }
136 }
137
138 impl Encodable for U32BE {
139 async fn encode<Consumer>(&self, consumer: &mut Consumer) -> Result<(), Consumer::Error>
140 where
141 Consumer: BulkConsumer<Item = u8>,
142 {
143 let bytes = self.0.to_be_bytes();
144 consumer
145 .bulk_consume_full_slice(&bytes)
146 .await
147 .map_err(|f| f.reason)?;
148 Ok(())
149 }
150 }
151
152 impl Decodable for U32BE {
153 async fn decode<Producer>(
154 producer: &mut Producer,
155 ) -> Result<Self, DecodeError<Producer::Error>>
156 where
157 Producer: BulkProducer<Item = u8>,
158 Self: Sized,
159 {
160 let mut bytes = [0u8; size_of::<u32>()];
161 producer.bulk_overwrite_full_slice(&mut bytes).await?;
162 Ok(U32BE(u32::from_be_bytes(bytes)))
163 }
164 }
165
166 impl Encodable for U64BE {
167 async fn encode<Consumer>(&self, consumer: &mut Consumer) -> Result<(), Consumer::Error>
168 where
169 Consumer: BulkConsumer<Item = u8>,
170 {
171 let bytes = self.0.to_be_bytes();
172 consumer
173 .bulk_consume_full_slice(&bytes)
174 .await
175 .map_err(|f| f.reason)?;
176 Ok(())
177 }
178 }
179
180 impl Decodable for U64BE {
181 async fn decode<Producer>(
182 producer: &mut Producer,
183 ) -> Result<Self, DecodeError<Producer::Error>>
184 where
185 Producer: BulkProducer<Item = u8>,
186 Self: Sized,
187 {
188 let mut bytes = [0u8; size_of::<u64>()];
189 producer.bulk_overwrite_full_slice(&mut bytes).await?;
190 Ok(U64BE(u64::from_be_bytes(bytes)))
191 }
192 }
193}