1use crate::Decimal;
7
8impl<const D: u8> Decimal<i32, D> {
13 pub const BYTES: usize = 4;
15
16 #[inline(always)]
18 pub const fn to_le_bytes(self) -> [u8; 4] {
19 self.value.to_le_bytes()
20 }
21
22 #[inline(always)]
24 pub const fn to_be_bytes(self) -> [u8; 4] {
25 self.value.to_be_bytes()
26 }
27
28 #[inline(always)]
30 pub const fn to_ne_bytes(self) -> [u8; 4] {
31 self.value.to_ne_bytes()
32 }
33
34 #[inline(always)]
36 pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
37 Self {
38 value: i32::from_le_bytes(bytes),
39 }
40 }
41
42 #[inline(always)]
44 pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
45 Self {
46 value: i32::from_be_bytes(bytes),
47 }
48 }
49
50 #[inline(always)]
52 pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self {
53 Self {
54 value: i32::from_ne_bytes(bytes),
55 }
56 }
57
58 #[inline]
60 pub fn write_le_bytes(&self, buf: &mut [u8]) {
61 buf[..4].copy_from_slice(&self.to_le_bytes());
62 }
63
64 #[inline]
66 pub fn write_be_bytes(&self, buf: &mut [u8]) {
67 buf[..4].copy_from_slice(&self.to_be_bytes());
68 }
69
70 #[inline]
72 pub fn read_le_bytes(buf: &[u8]) -> Self {
73 let bytes: [u8; 4] = buf[..4]
74 .try_into()
75 .expect("buf[..4] always yields a 4-byte slice");
76 Self::from_le_bytes(bytes)
77 }
78
79 #[inline]
81 pub fn read_be_bytes(buf: &[u8]) -> Self {
82 let bytes: [u8; 4] = buf[..4]
83 .try_into()
84 .expect("buf[..4] always yields a 4-byte slice");
85 Self::from_be_bytes(bytes)
86 }
87}
88
89impl<const D: u8> Decimal<i64, D> {
94 pub const BYTES: usize = 8;
96
97 #[inline(always)]
99 pub const fn to_le_bytes(self) -> [u8; 8] {
100 self.value.to_le_bytes()
101 }
102
103 #[inline(always)]
105 pub const fn to_be_bytes(self) -> [u8; 8] {
106 self.value.to_be_bytes()
107 }
108
109 #[inline(always)]
111 pub const fn to_ne_bytes(self) -> [u8; 8] {
112 self.value.to_ne_bytes()
113 }
114
115 #[inline(always)]
117 pub const fn from_le_bytes(bytes: [u8; 8]) -> Self {
118 Self {
119 value: i64::from_le_bytes(bytes),
120 }
121 }
122
123 #[inline(always)]
125 pub const fn from_be_bytes(bytes: [u8; 8]) -> Self {
126 Self {
127 value: i64::from_be_bytes(bytes),
128 }
129 }
130
131 #[inline(always)]
133 pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self {
134 Self {
135 value: i64::from_ne_bytes(bytes),
136 }
137 }
138
139 #[inline]
141 pub fn write_le_bytes(&self, buf: &mut [u8]) {
142 buf[..8].copy_from_slice(&self.to_le_bytes());
143 }
144
145 #[inline]
147 pub fn write_be_bytes(&self, buf: &mut [u8]) {
148 buf[..8].copy_from_slice(&self.to_be_bytes());
149 }
150
151 #[inline]
153 pub fn read_le_bytes(buf: &[u8]) -> Self {
154 let bytes: [u8; 8] = buf[..8]
155 .try_into()
156 .expect("buf[..8] always yields an 8-byte slice");
157 Self::from_le_bytes(bytes)
158 }
159
160 #[inline]
162 pub fn read_be_bytes(buf: &[u8]) -> Self {
163 let bytes: [u8; 8] = buf[..8]
164 .try_into()
165 .expect("buf[..8] always yields an 8-byte slice");
166 Self::from_be_bytes(bytes)
167 }
168}
169
170impl<const D: u8> Decimal<i128, D> {
175 pub const BYTES: usize = 16;
177
178 #[inline(always)]
180 pub const fn to_le_bytes(self) -> [u8; 16] {
181 self.value.to_le_bytes()
182 }
183
184 #[inline(always)]
186 pub const fn to_be_bytes(self) -> [u8; 16] {
187 self.value.to_be_bytes()
188 }
189
190 #[inline(always)]
192 pub const fn to_ne_bytes(self) -> [u8; 16] {
193 self.value.to_ne_bytes()
194 }
195
196 #[inline(always)]
198 pub const fn from_le_bytes(bytes: [u8; 16]) -> Self {
199 Self {
200 value: i128::from_le_bytes(bytes),
201 }
202 }
203
204 #[inline(always)]
206 pub const fn from_be_bytes(bytes: [u8; 16]) -> Self {
207 Self {
208 value: i128::from_be_bytes(bytes),
209 }
210 }
211
212 #[inline(always)]
214 pub const fn from_ne_bytes(bytes: [u8; 16]) -> Self {
215 Self {
216 value: i128::from_ne_bytes(bytes),
217 }
218 }
219
220 #[inline]
222 pub fn write_le_bytes(&self, buf: &mut [u8]) {
223 buf[..16].copy_from_slice(&self.to_le_bytes());
224 }
225
226 #[inline]
228 pub fn write_be_bytes(&self, buf: &mut [u8]) {
229 buf[..16].copy_from_slice(&self.to_be_bytes());
230 }
231
232 #[inline]
234 pub fn read_le_bytes(buf: &[u8]) -> Self {
235 let bytes: [u8; 16] = buf[..16]
236 .try_into()
237 .expect("buf[..16] always yields a 16-byte slice");
238 Self::from_le_bytes(bytes)
239 }
240
241 #[inline]
243 pub fn read_be_bytes(buf: &[u8]) -> Self {
244 let bytes: [u8; 16] = buf[..16]
245 .try_into()
246 .expect("buf[..16] always yields a 16-byte slice");
247 Self::from_be_bytes(bytes)
248 }
249}