1use std::ops::{AddAssign, Neg, Shl, Shr};
2use std::convert::{TryFrom, From, TryInto};
3use std::fmt::Display;
4
5pub trait IntegerBaseFunctions {
7 const IS_SIGNED: bool;
8 fn get_absolute_uint(self: &Self) -> u64;
9 fn is_val_negative(self: &Self) -> bool;
10 fn switch_sign_if_possible(self: &Self) -> Self;
11 fn get_zero() -> Self;
12}
13macro_rules! impl_integer_signed {
14 ($type:ty) => {
15 impl IntegerBaseFunctions for $type {
16 const IS_SIGNED: bool = true;
17 fn get_absolute_uint(self: &Self) -> u64 {
18 if *self < 0 { self.clone().neg() as u64 }
19 else { self.clone() as u64 }
20 }
21 fn is_val_negative(self: &Self) -> bool { *self < 0 }
22 fn switch_sign_if_possible(self: &Self) -> Self { return self.clone().neg() }
23 fn get_zero() -> Self { 0 as $type }
24 }
25} }
26macro_rules! impl_integer_unsigned {
27 ($type:ty) => {
28 impl IntegerBaseFunctions for $type {
29 const IS_SIGNED: bool = false;
30 fn get_absolute_uint(self: &Self) -> u64 { self.clone() as u64 }
31 fn is_val_negative(self: &Self) -> bool { false }
32 fn switch_sign_if_possible(self: &Self) -> Self { return self.clone() }
33 fn get_zero() -> Self { 0 as $type }
34 }
35} }
36impl_integer_signed!(i8);
37impl_integer_signed!(i16);
38impl_integer_signed!(i32);
39impl_integer_signed!(i64);
40impl_integer_unsigned!(u8);
41impl_integer_unsigned!(u16);
42impl_integer_unsigned!(u32);
43impl_integer_unsigned!(u64);
44
45#[derive(Debug, Clone)]
47pub struct Biseri {
48 cur_cache_u8: u16,
49 sub_byte_counter: u8,
50 data_cache: Vec<u8>,
51 final_total_bits: u64,
52}
53
54fn bits_for_and(x: u8) -> u16 {
55 u16::MAX >> (u16::BITS as u8 - x)
56}
57
58#[allow(dead_code)]
59pub trait BiserdiTraitVarBitSize : Sized {
60 fn bit_serialize(self: &Self, total_bits: u64, biseri: &mut Biseri) -> Option<u64>;
61 fn bit_deserialize(version_id: u16, total_bits: u64, bides: &mut Bides) -> Option<(Self, u64)>;
62}
63#[allow(dead_code)]
64pub trait BiserdiTrait: Sized {
65 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64>;
66 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)>;
67}
68
69pub struct BiserSizes {
70 pub total_bits: u64,
71 pub total_bytes: u64
72}
73#[allow(dead_code)]
74impl Biseri {
75 pub fn new() -> Biseri {
76 Biseri { cur_cache_u8: 0, data_cache: Vec::new(), sub_byte_counter: 0, final_total_bits: 0 }
77 }
78
79 pub fn data_size_bytes(&self) -> u64 {
80 self.data_cache.len() as u64
81 }
82 pub fn get_data_ref(&self) -> &Vec<u8> {
83 &self.data_cache
84 }
85 pub fn get_data(&self) -> Vec<u8> {
86 self.data_cache.clone()
87 }
88
89 fn add_data_base_u8(&mut self, cur_u8: &u8, total_bits: u64) -> u64 {
90 if total_bits > 0 {
91 let cur_u16 = cur_u8.clone() as u16;
92 let shift_by = self.sub_byte_counter & 7;
93
94 let cur_bit_size = std::cmp::min(8, total_bits as u8);
95 let cur_u16 = (cur_u16 & (bits_for_and(cur_bit_size))) << shift_by;
97 self.cur_cache_u8 += cur_u16;
98
99 self.sub_byte_counter += cur_bit_size;
100 let total_bits = total_bits - cur_bit_size as u64;
101
102 if self.sub_byte_counter >= 8 {
103 self.sub_byte_counter -= 8;
104 let u8_to_add = (self.cur_cache_u8 & 0xFF) as u8;
105 self.data_cache.push(u8_to_add);
106 self.cur_cache_u8 >>= 8;
107 }
108 total_bits
109 }
110 else { 0 }
111 }
112
113 pub fn add_data(&mut self, cur_data: &Vec<u8>, total_bits: u64) -> Option<u64> {
114 let mut cur_total_bits = total_bits;
122 for cu8 in cur_data.iter() {
123 cur_total_bits = self.add_data_base_u8(cu8, cur_total_bits);
124 }
125 Some(total_bits)
126 }
127
128 pub fn add_biseri_data(&mut self, data: &Biseri) -> Option<u64> {
129 self.add_data(&data.data_cache, data.final_total_bits)
130 }
133 pub fn finish_add_data(&mut self) -> Option<BiserSizes> {
134 if self.final_total_bits > 0 {
135 None
136 }
137 else {
138 let total_bits = ((self.data_cache.len() as u64) << 3) + self.sub_byte_counter as u64;
139 if self.sub_byte_counter > 0 {
140 let u8_to_add = (self.cur_cache_u8 & 0xFF) as u8;
141 self.data_cache.push(u8_to_add);
142 }
143
144 self.final_total_bits = total_bits;
145 Some(BiserSizes{total_bits, total_bytes: self.data_cache.len() as u64})
146 }
147 }
148}
149
150#[derive(Debug, Clone)]
151pub struct Bides {
152 cur_read_pos: u64,
153 sub_byte_counter: u8,
154 pub data_cache: Vec<u8>,
155}
156#[allow(dead_code)]
157impl Bides {
158 pub fn new() -> Bides { Bides { cur_read_pos: 0, sub_byte_counter: 0, data_cache: Vec::new() } }
159 pub fn from_vec(data: &Vec<u8>) -> Bides {
160 Bides{sub_byte_counter: 0, data_cache: data.clone(), cur_read_pos: 0}
161 }
162 pub fn from_biseri(biseri: &Biseri) -> Bides {
163 Bides{sub_byte_counter: 0, data_cache: biseri.get_data().clone(), cur_read_pos: 0}
164 }
165
166 pub fn append_data(&mut self, data: &Vec<u8>) {
167 self.data_cache.extend(data);
168 }
169
170 pub fn reset_position(&mut self) {
171 self.sub_byte_counter = 0;
172 self.cur_read_pos = 0;
173 }
174 pub fn decode_data_base_u8(&mut self, total_bits: u64) -> Option<(u8, u64)> {
175 if self.cur_read_pos as usize >= self.data_cache.len() { return None }
176 let mut cur_u16: u16 = self.data_cache[self.cur_read_pos as usize] as u16;
177 if (self.cur_read_pos + 1 < self.data_cache.len() as u64) && (self.sub_byte_counter > 0) {
178 cur_u16 += (self.data_cache[(self.cur_read_pos + 1) as usize] as u16) << 8;
179 }
180
181 let cur_used_bits = std::cmp::min(total_bits as u8, 8);
182
183 let d = ((cur_u16 >> self.sub_byte_counter) & bits_for_and(cur_used_bits)) as u8;
186
187 self.sub_byte_counter += cur_used_bits;
188 if self.sub_byte_counter >= 8 {
189 self.sub_byte_counter -= 8;
190 self.cur_read_pos += 1;
191 }
192
193 Some((d, total_bits - cur_used_bits as u64))
194 }
195
196 pub fn decode_data(&mut self, total_bits: u64, expected_bytes: u32) -> Option<Vec<u8>> {
197 if self.cur_read_pos >= self.data_cache.len() as u64 {
198 return None;
199 }
200 let mut cur_total_bits = total_bits;
201 let mut dv = Vec::new();
202 while cur_total_bits > 0 {
203 let d;
204 (d, cur_total_bits) = match self.decode_data_base_u8(cur_total_bits) {
205 Some(d) => d, None => { return None; }
206 };
207 dv.push(d);
208 }
209 let num_add = expected_bytes - dv.len() as u32;
210 for _ in 0..num_add { dv.push(0); }
211 Some(dv)
212 }
213
214 pub fn skip_bits(&mut self, bits: u64) {
215 let bits_total_pos = bits + self.sub_byte_counter as u64;
216 self.sub_byte_counter = (bits_total_pos.clone() & 7) as u8;
217 self.cur_read_pos += bits_total_pos >> 3;
218 }
219}
220
221macro_rules! impl_biserdi_var_bitsize_trait {
222 ($type:ty, $num_bytes: expr) => {
223 impl BiserdiTraitVarBitSize for $type {
224 fn bit_serialize(self: &Self, total_bits: u64, biseri: &mut Biseri) -> Option<u64> {
225 if Self::IS_SIGNED { self.is_val_negative().bit_serialize(biseri)?; }
226 let v = self.get_absolute_uint();
227 let vv = &v.to_le_bytes().to_vec();
228 let bits = biseri.add_data(vv, total_bits)?;
229
230 let bits_with_sign = if Self::IS_SIGNED { bits+1 } else { bits };
231 Some(bits_with_sign)
232 }
233 fn bit_deserialize(version_id: u16, total_bits: u64, bides: &mut Bides) -> Option<(Self, u64)> {
234 let is_neg = if Self::IS_SIGNED {
235 bool::bit_deserialize(version_id, bides)?.0 }
236 else { false };
237
238 let mut v = Self::from_le_bytes(
239 bides.decode_data(total_bits, $num_bytes)?.try_into().ok()?);
240 if is_neg { v = v.switch_sign_if_possible() }
241
242 let bits_with_sign = if Self::IS_SIGNED { total_bits + 1 } else { total_bits };
243 Some((v, bits_with_sign))
244 }
245 }
246 };
247}
248macro_rules! impl_biserdi {
249 ($type:ty, $num_bits: expr) => {
250 impl BiserdiTrait for $type {
251 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
252 Some(biseri.add_data(&self.clone().to_le_bytes().to_vec(), ($num_bits))?)
253 }
254 fn bit_deserialize(_version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
255 Some((Self::from_le_bytes(bides.decode_data(
256 ($num_bits), std::cmp::max((($num_bits)>>3),1))?.try_into().ok()?),
257 $num_bits))
258 }
259 }
260 };
261}
262
263impl BiserdiTrait for bool {
265 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
266 let val = if *self { 1_u8 } else { 0_u8 };
267 biseri.add_data_base_u8(&val, 1);
268 Some(1)
269 }
270 fn bit_deserialize(_version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
271 let vec = bides.decode_data(1, 1)?;
272 Some((if vec[0] == 0 { false } else { true }, 1))
273 }
274}
275impl_biserdi_var_bitsize_trait!(u8, u8::BITS>>3);
276impl_biserdi_var_bitsize_trait!(u16, u16::BITS>>3);
277impl_biserdi_var_bitsize_trait!(u32, u32::BITS>>3);
278impl_biserdi_var_bitsize_trait!(u64, u64::BITS>>3);
279impl_biserdi_var_bitsize_trait!(i8, i8::BITS>>3);
280impl_biserdi_var_bitsize_trait!(i16, i16::BITS>>3);
281impl_biserdi_var_bitsize_trait!(i32, i32::BITS>>3);
282impl_biserdi_var_bitsize_trait!(i64, i64::BITS>>3);
283impl_biserdi!(f32, 32);
284impl_biserdi!(f64, 64);
285
286
287#[derive(Clone, Debug, PartialEq)]
289pub struct BitisOption<T> {
290 pub val: Option<T>
291}
292impl<T> BiserdiTrait for BitisOption<T> where T: BiserdiTrait + Default {
293 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
294 let mut size = 1;
295 match &self.val {
296 None => { false.bit_serialize(biseri)?; },
297 Some(v) => {
298 true.bit_serialize(biseri)?;
299 size += v.bit_serialize(biseri)?;
300 }
301 }
302 Some(size)
303 }
304 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
305 let mut size = 1;
306
307 let (is_set, _) = bool::bit_deserialize(version_id, bides)?;
308 let v = if is_set {
309 let vv = T::bit_deserialize(version_id, bides)?;
310 size += vv.1.clone();
311 Some(vv.0)
312 }
313 else { None };
314
315 Some((BitisOption{val: v}, size))
316 }
317}
318impl<T, TT> From<Option<TT>> for BitisOption<T> where T: BiserdiTrait + Default, TT: Into<T> {
319 fn from(value: Option<TT>) -> Self {
320 match value {
321 Some(v) => Self { val: Some(v.into()) },
322 None => Self { val: None }
323 }
324 }
325}
326impl<T> From<T> for BitisOption<T> where T: BiserdiTrait + Default {
327 fn from(val: T) -> Self {
328 Self{ val: Some(val) }
329 } }
330impl<T> std::fmt::Display for BitisOption<T> where T: BiserdiTrait + Default + Display {
336 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
337 match &self.val {
338 None => { write!(f, "None [opt]") },
339 Some(v) => { write!(f, "{} [opt]", v) }
340 }
341 } }
342impl<T> Default for BitisOption<T> where T: BiserdiTrait + Default {
343 fn default() -> Self { Self{val: None} }
344}
345
346#[derive(Debug, Clone, PartialEq, Eq, Copy)]
348pub struct FixedArray<T: Default, const N: usize> { pub val: [T; N] }
349impl<T, const N: usize> BiserdiTrait for FixedArray<T, N> where T: BiserdiTrait + Default + Copy {
350 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
351 let mut s = 0;
352 for i in 0..N { s += self.val[i].bit_serialize(biseri)?; }
353 Some(s)
354 }
355 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
356 let mut v: [T; N] = [T::default(); N];
357 let mut bits = 0;
358 let mut cur_bits;
359 for i in 0..N { (v[i], cur_bits) = T::bit_deserialize(version_id, bides)?; bits += cur_bits; }
360 Some((Self{ val: v}, bits))
361 }
362}
363impl<T, const N: usize> BiserdiTraitVarBitSize for FixedArray<T, N> where T: BiserdiTraitVarBitSize + Default + Copy {
364 fn bit_serialize(self: &Self, total_bits_per_unit: u64, biseri: &mut Biseri) -> Option<u64> {
365 let mut s = 0;
366 for i in 0..N {
367 s += self.val[i].bit_serialize(total_bits_per_unit, biseri)?;
368 }
369 Some(s)
370 }
371 fn bit_deserialize(version_id: u16, total_bits_per_unit: u64, bides: &mut Bides) -> Option<(Self, u64)> {
372 let mut v = [T::default(); N];
373 let mut bits = 0;
374 let mut cur_bits;
375 for i in 0..N {
376 (v[i], cur_bits) = T::bit_deserialize(version_id, total_bits_per_unit, bides)?; bits += cur_bits; }
377 Some((Self{ val: v}, bits))
378 }
379}
380impl<T: Sized + Copy + BiserdiTrait + Default, const N: usize> From<[T; N]> for FixedArray<T, N> {
381 fn from(val: [T;N]) -> Self { Self{ val: val.clone()} } }
382impl<T: Sized + Copy + Display + Default, const N: usize> std::fmt::Display for FixedArray<T, N> {
383 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
384 let das: Vec<String> = self.val.iter().map(|v| format!("{}", v)).collect();
385 write!(f, "[{}]", das.join(", "))
386 } }
387impl<T: Sized + Copy + BiserdiTrait + Default, const N: usize> Default for FixedArray<T, N> {
388 fn default() -> Self {
389 Self{val: [T::default(); N]}
390 }
391}
392
393pub struct DynArray<T, const DYNSIZEBITS: u8> { pub val: Vec<T> }
395impl<T, const DYNSIZEBITS: u8> BiserdiTrait for DynArray<T, DYNSIZEBITS> where T: BiserdiTrait + Default + Copy {
396 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
397 let mut s = 0;
398 s += DynInteger::<u32, DYNSIZEBITS>::new(self.val.len() as u32).bit_serialize(biseri)?;
399 for d in self.val.iter() { s += d.bit_serialize(biseri)?; };
400 Some(s)
401 }
402 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
403 let mut s = 0;
404 let (v, cs) =
405 DynInteger::<u32, DYNSIZEBITS>::bit_deserialize(version_id, bides)?;
406 let mut data = Vec::with_capacity(cs as usize);
407 for _ci in 0..v.val {
408 let (vi, si) = T::bit_deserialize(version_id, bides)?;
409 s += si;
410 data.push(vi);
411 }
412 Some((Self{ val: data }, s+cs))
413 }
414}
415impl<T: Sized + Copy + BiserdiTraitVarBitSize, const DYNSIZEBITS: u8, const N: usize> From<[T; N]> for DynArray<T, DYNSIZEBITS> {
416 fn from(val: [T;N]) -> Self {
417 DynArray{ val:Vec::from(val)}
418 } }
419impl<T: Sized + Copy + BiserdiTraitVarBitSize, const DYNSIZEBITS: u8> From<Vec<T>> for DynArray<T, DYNSIZEBITS> {
420 fn from(val: Vec<T>) -> Self {
421 DynArray{ val: val.clone()}
422 } }
423impl<T: Sized + Copy + BiserdiTraitVarBitSize + Display, const DYNSIZEBITS: u8> std::fmt::Display for DynArray<T, DYNSIZEBITS> {
424 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
425 let das: Vec<String> = self.val.iter().map(|v| v.to_string()).collect();
426 write!(f, "[{} |dynbits:{}]", das.join(", "), DYNSIZEBITS)
427 } }
428impl<T: Sized + Copy + BiserdiTraitVarBitSize, const DYNSIZEBITS: u8> Default for DynArray<T, DYNSIZEBITS> {
429 fn default() -> Self {
430 Self{val: Vec::new()}
431 }
432}
433
434#[derive(Debug, Clone, PartialEq, Eq, Default, Copy)]
436pub struct VarWithGivenBitSize<T: Sized + Copy + BiserdiTraitVarBitSize + Default, const NUM_BITS: u64> {
437 pub val: T
438}
439impl<T: Sized + Copy + BiserdiTraitVarBitSize + Default, const NUM_BITS: u64> VarWithGivenBitSize<T, NUM_BITS> {
440 pub fn new(v: T) -> Self { VarWithGivenBitSize {val: v} }
441}
442impl<T: Sized + Copy + BiserdiTraitVarBitSize + Default, const NUM_BITS: u64> From<T> for VarWithGivenBitSize<T, NUM_BITS> {
443 fn from(val: T) -> Self {
444 VarWithGivenBitSize::<T, NUM_BITS>::new(val)
445 }
446}
447impl<T: Sized + Copy + BiserdiTraitVarBitSize + Display + Default, const NUM_BITS: u64> std::fmt::Display for VarWithGivenBitSize<T, NUM_BITS> {
453 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
454 write!(f, "{} |bits:{}", self.val, NUM_BITS)
455} }
456impl<T: Sized + Copy + BiserdiTraitVarBitSize + Default, const NUM_BITS: u64> BiserdiTrait for VarWithGivenBitSize<T, NUM_BITS> {
457 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
458 self.val.bit_serialize(NUM_BITS, biseri)
459 }
460 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
461 let v = T::bit_deserialize(version_id, NUM_BITS, bides)?;
462 Some((Self{val: v.0}, v.1))
463 }
464}
465
466#[derive(Debug, Clone, PartialEq, Eq, Default, Copy)]
467pub struct DynInteger<
468 T: Sized + Copy + BiserdiTraitVarBitSize + AddAssign + Shl<Output = T> + Shr + Ord + PartialEq + IntegerBaseFunctions + Default, const N: u8> {
470 pub val: T
471}
472impl<T: Display + Sized + Copy + BiserdiTraitVarBitSize + AddAssign + Shl<Output = T> + Shr + Ord + PartialEq + TryFrom<u64>
473 + IntegerBaseFunctions + Default, const N: u8> DynInteger<T, N> {
474 const DYN_SIZE: u8 = N;
475 pub fn new(v: T) -> Self{
476 DynInteger{val: v}
477 }
478}
479impl<T: Display + Sized + Copy + BiserdiTraitVarBitSize + AddAssign + Shl<Output = T> + Shr + Ord + PartialEq + TryFrom<u64>
480 + IntegerBaseFunctions + Default, const N: u8> BiserdiTrait for DynInteger<T, N> {
481 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
482 let mut bit_size: u64 = 1;
485 let mut val_work = self.val.get_absolute_uint();
486
487 if T::IS_SIGNED { self.val.is_val_negative().bit_serialize(biseri)?; bit_size +=1; }
488
489 (val_work != 0).bit_serialize(biseri);
490 while val_work > 0 {
491 val_work.bit_serialize(u64::from(Self::DYN_SIZE), biseri)?;
493 val_work >>= Self::DYN_SIZE;
494 bit_size += (Self::DYN_SIZE + 1) as u64;
495 let further_data = val_work > 0;
496 further_data.bit_serialize(biseri);
497 }
498 Some(bit_size)
499 }
500 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
501 let mut cur_shift: u64 = 0;
502 let mut v: u64 = 0;
503 let mut negative_sign = false;
504
505 let mut bit_size = 1;
506 if T::IS_SIGNED {
507 negative_sign = bool::bit_deserialize(version_id, bides)?.0; bit_size += 1; }
508 let mut further_data = bool::bit_deserialize(version_id, bides)?.0;
509 while further_data {
510 let vt = u64::bit_deserialize(version_id, Self::DYN_SIZE as u64, bides)?;
511 bit_size += vt.1 + 1;
512 v += vt.0 << cur_shift;
513 cur_shift += u64::from(Self::DYN_SIZE);
514 further_data = bool::bit_deserialize(version_id, bides)?.0;
515 }
516 let mut vv= T::try_from(v).ok()?;
517 if negative_sign {
518 vv = vv.switch_sign_if_possible();
519 }
520 Some((Self{val: vv}, bit_size))
521 }
522}
523impl<T: Display + Sized + Copy + BiserdiTraitVarBitSize + AddAssign + Shl<Output = T> + Shr + Ord + PartialEq + TryFrom<u64>
524 + IntegerBaseFunctions + Default, const N: u8> Display for DynInteger<T, N> {
525 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
526 write!(f, "{} |dynbits:{}]", self.val, N)
527 } }
528
529
530#[derive(Debug, Clone, PartialEq, Default)]
532pub enum FixPrecisionVal {
533 #[default]
534 Overflow,
535 Value(f64),
536 Underflow
537}
538#[derive(Debug, Clone, PartialEq, Default)]
539pub struct FixPrecisionMinMax<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> {
540 pub val: FixPrecisionVal
541}
542impl<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> FixPrecisionMinMax<NUM_BITS, MIN_IVALUE, MAX_IVALUE> {
543 const MIN_VALUE: f64 = MIN_IVALUE as f64;
544 const MAX_VALUE: f64 = MAX_IVALUE as f64;
545 const RANGE_VALUE: f64 = Self::MAX_VALUE - Self::MIN_VALUE;
546 const MAX_INT_VALUE_FOR_BITS: u64 = (1_u64<<NUM_BITS) - 1_u64;
547 const MAX_VALUE_FOR_BITS: f64 = (Self::MAX_INT_VALUE_FOR_BITS - 1_u64) as f64;
548
549 pub fn new(val: f64) -> Self {
550 if val > Self::MAX_VALUE { FixPrecisionMinMax { val: FixPrecisionVal::Overflow } }
551 else if val < Self::MIN_VALUE { FixPrecisionMinMax { val: FixPrecisionVal::Underflow } }
552 else { FixPrecisionMinMax { val: FixPrecisionVal::Value(val) } }
553 }
554}
555impl<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> BiserdiTrait for FixPrecisionMinMax<NUM_BITS, MIN_IVALUE, MAX_IVALUE> {
556 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
557 let v = match self.val {
558 FixPrecisionVal::Value(v) =>
560 ((v - Self::MIN_VALUE) / Self::RANGE_VALUE * Self::MAX_VALUE_FOR_BITS + 1.0) as u64,
561 FixPrecisionVal::Underflow => 0,
562 FixPrecisionVal::Overflow => Self::MAX_INT_VALUE_FOR_BITS
563 };
564 v.bit_serialize(NUM_BITS as u64, biseri)
565 }
566 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
567 let (v, bits) = u64::bit_deserialize(version_id, NUM_BITS as u64, bides)?;
569 let vv = if v == 0 { FixPrecisionVal::Underflow }
570 else if v == Self::MAX_INT_VALUE_FOR_BITS { FixPrecisionVal::Overflow }
571 else {
572 FixPrecisionVal::Value(((v-1) as f64) / Self::MAX_VALUE_FOR_BITS
573 * Self::RANGE_VALUE + Self::MIN_VALUE)
574 };
575 Some((Self{val: vv}, bits))
576 }
577}
578impl<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> Display for FixPrecisionMinMax<NUM_BITS, MIN_IVALUE, MAX_IVALUE> {
579 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
580 match self.val {
581 FixPrecisionVal::Overflow => write!(f, "Overflow |dynbits:{}", NUM_BITS),
582 FixPrecisionVal::Underflow => write!(f, "Underflow |dynbits:{}", NUM_BITS),
583 FixPrecisionVal::Value(v) => write!(f, "{} |dynbits:{}", v, NUM_BITS)
584 }
585 } }
586impl<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> From<f32> for FixPrecisionMinMax<NUM_BITS, MIN_IVALUE, MAX_IVALUE> {
587 fn from(value: f32) -> Self { Self::new(value.into()) }
588}
589impl<const NUM_BITS: u8, const MIN_IVALUE: i64, const MAX_IVALUE: i64> From<f64> for FixPrecisionMinMax<NUM_BITS, MIN_IVALUE, MAX_IVALUE> {
590 fn from(value: f64) -> Self { Self::new(value.into()) }
591}
592
593#[derive(Debug, Clone, PartialEq)]
595pub struct Binary<const DYNSIZEBITS: u8> {
596 pub val: Vec<u8>
597}
598impl<const DYNSIZEBITS: u8> Binary<DYNSIZEBITS> {
599 pub fn new(data: Vec<u8>) -> Self {
600 Self{ val: data }
601 }
602 pub fn empty() -> Self {
603 Self{val: Vec::new()}
604 }
605}
606impl<const DYNSIZEBITS: u8> BiserdiTrait for Binary<DYNSIZEBITS> {
607 fn bit_serialize(self: &Self, biseri: &mut Biseri) -> Option<u64> {
608 let mut s = 0;
609 s += DynInteger::<u32, DYNSIZEBITS>::new(self.val.len() as u32).bit_serialize(biseri)?;
610 for d in self.val.iter() { s += d.bit_serialize(8, biseri)?; };
611 Some(s)
612 }
613 fn bit_deserialize(version_id: u16, bides: &mut Bides) -> Option<(Self, u64)> {
614 let mut s = 0;
615 let (v, cs) =
616 DynInteger::<u32, DYNSIZEBITS>::bit_deserialize(version_id, bides)?;
617 let mut data = Vec::with_capacity(cs as usize);
618 for _ci in 0..v.val {
619 let (vi, si) = u8::bit_deserialize(version_id, 8, bides)?;
620 s += si;
621 data.push(vi);
622 }
623 Some((Self{ val: data }, s+cs))
624 }
625}
626impl<const N: u8> std::fmt::Display for Binary< N> {
627 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
628 let hex = self.val.iter().map(|b| format!("{:02x}", b).to_string()).collect::<Vec<String>>().join(" ");
629 write!(f, "{:x?} |dynbits:{}", hex, N)
630 }
631}
632
633#[cfg(test)]
634mod bitis_base_serialization_deserialization {
635 use rstest::rstest;
636 use crate::lib_impl::berde::{Bides, Biseri};
637 use super::*;
638
639 #[rstest]
640 fn add_one_u8() {
641 let mut b = Biseri::new();
642 let d = 0b10101010_u8;
643
644 b.add_data_base_u8(&d, 8);
645
646 assert_eq!(b.clone().data_cache.len(), 1);
647 assert_eq!(b.sub_byte_counter, 0);
648
649 let r = b.finish_add_data().unwrap();
650 assert_eq!((r.total_bits, r.total_bytes), (8, 1));
651 }
652
653 #[rstest]
654 #[case::ot_uint4(4, 1, 4, 1)]
655 #[case::tt_uint4(4, 2, 8, 1)]
656 #[case::ot_uint6(6, 1, 6, 1)]
657 #[case::tt_uint6(6, 2, 12, 2)]
658 #[case::trt_uint6(6, 3, 18, 3)]
659 #[case::ft_uint6(6, 4, 24, 3)]
660 fn add_one_var(#[case] bitsize: u64, #[case] repeated: u8, #[case] final_bitsize: u64, #[case] num_bytes: u64) {
661 let mut b = Biseri::new();
662 let d = 0b10101010_u8;
663
664 for _i in 0..repeated {
665 b.add_data_base_u8(&d, bitsize);
666 }
667
668 let r = b.finish_add_data().unwrap();
669 assert_eq!((r.total_bits, r.total_bytes), (final_bitsize, num_bytes));
670 }
671
672 #[rstest]
673 #[case::ok(5, 4, 5)]
674 #[case::expected_overflow(0b10011, 4, 3)]
675 fn serialize_and_deserialize_base(#[case] val_in: u8, #[case] bitsize: u64, #[case] val_out: u8) {
676 let mut ser = Biseri::new();
677
678 ser.add_data_base_u8(&val_in, bitsize);
679 ser.finish_add_data();
680
681 let mut des = Bides::from_biseri(&ser);
682
683 assert_eq!(des.data_cache, ser.data_cache);
684
685 let r = des.decode_data_base_u8(bitsize);
686 assert!(r.is_some());
687 let (dd, bs) = r.unwrap();
688 assert_eq!(bs, 0);
689
690 println!("val_in: {val_in} vs. dd: {dd} (expected: {val_out}");
691 assert_eq!(val_out, dd);
692 }
693
694 #[rstest]
695 #[case::ok(3*256+5, 16, 3*256+5)]
696 #[case::ok(3*256+5, 12, 3*256+5)]
697 #[case::ok(3*256+5, 9, 1*256+5)]
698 fn serialize_and_deserialize_u16_single(#[case] val_in: u16, #[case] bitsize: u64, #[case] val_out: u16) {
699 let val_in_vec = val_in.to_le_bytes().clone();
700
701 let mut ser = Biseri::new();
702
703 let mut total_size = bitsize;
704 val_in_vec.clone().iter().for_each(|x| {
705 total_size = ser.add_data_base_u8(&x, bitsize);
706 });
707 ser.finish_add_data();
708
709 println!("ser.cache: {:?}", ser.data_cache);
710
711 assert_eq!(ser.data_cache.len(), 2);
712
713 let mut des = Bides::from_biseri(&ser);
714
715 assert_eq!(des.data_cache, ser.data_cache);
716
717 let mut dd = Vec::new();
718 let mut total_size = bitsize;
719 while total_size > 0 {
720 let ddd;
721 let r = des.decode_data_base_u8(total_size);
722 assert!(r.is_some());
723 (ddd, total_size) = r.unwrap();
724 dd.push(ddd);
725 };
726
727 let ddv = u16::from_le_bytes(dd.clone().try_into().unwrap());
728 println!("val_in: {val_in} ({val_in_vec:?}) vs. ddv: {ddv:?} ({dd:?}) (expected: {val_out})");
729 assert_eq!(val_out, ddv);
730 }
731
732 fn add_two_u16_fixed(ser: &mut Biseri, bits: u64) -> BiserSizes {
733 let d: u16 = 3;
734
735 ser.add_data(&d.to_le_bytes().to_vec(), bits);
736 ser.add_data(&d.to_le_bytes().to_vec(), bits);
737 ser.finish_add_data().unwrap()
738 }
739 #[rstest]
740 fn serialize_u16_fixed_full() {
741 let mut ser = Biseri::new();
742
743 let r = add_two_u16_fixed(&mut ser, 16);
744 let (lbits, lbytes) = (r.total_bits, r.total_bytes);
745
746 assert_eq!(ser.data_cache.len(), 4);
747 assert_eq!(lbytes, 4);
748 assert_eq!(lbits, 2 * 16);
749
750 assert_eq!(ser.data_cache[0], 3);
751 assert_eq!(ser.data_cache[1], 0);
752 assert_eq!(ser.data_cache[2], 3);
753 assert_eq!(ser.data_cache[3], 0);
754 }
755
756 #[rstest]
757 fn serialize_u16_fixed_12b() {
758 let mut ser = Biseri::new();
759
760 let r = add_two_u16_fixed(&mut ser, 12);
761 let (lbits, lbytes) = (r.total_bits, r.total_bytes);
762
763 assert_eq!(ser.data_cache.len(), 3);
764 assert_eq!(lbytes, 3);
765 assert_eq!(lbits, 2 * 12);
766
767 assert_eq!(ser.data_cache[0], 3);
768 assert_eq!(ser.data_cache[1], 3 << 4);
769 assert_eq!(ser.data_cache[2], 0);
770 }
771
772 #[rstest]
773 #[case::bitsize_16(16)]
774 #[case::bitsize_14(14)]
775 #[case::bitsize_12(12)]
776 fn ser_and_deserialize_u16_fixed(#[case] bits: u64) {
777 let mut ser = Biseri::new();
778
779 let _ = add_two_u16_fixed(&mut ser, bits);
780
781 let mut des = Bides::from_biseri(&ser);
782
783 assert_eq!(des.data_cache, ser.data_cache);
784
785 let d1 = des.decode_data(bits, 2);
786 assert!(d1.is_some());
787 let d2 = des.decode_data(bits, 2);
788 assert!(d2.is_some());
789
790 let d1 = d1.unwrap();
791 let d2 = d2.unwrap();
792
793 assert_eq!(d1[0], 3);
794 assert_eq!(d1[1], 0);
795 assert_eq!(d2[0], 3);
796 assert_eq!(d2[1], 0);
797 }
798
799 #[rstest]
800 fn ser_and_deserialize_i16_fixed() {
801 let mut ser = Biseri::new();
802
803 let v: i8 = -11;
804 v.bit_serialize(5, &mut ser);
805
806 let r = ser.finish_add_data().unwrap();
807 let (bits, bytes) = (r.total_bits, r.total_bytes);
808
809 println!("bits: {}, bytes: {}", bits, bytes);
810
811 let mut des = Bides::from_biseri(&ser);
812
813 let vv = i8::bit_deserialize(1,5, &mut des);
814 println!("v: {}, vv: {:?}", v, vv);
815
816 assert!(vv.is_some());
817
818 let vv = vv.unwrap();
819 println!("bits_des: {}", vv.1);
820 assert_eq!(v, vv.0);
821 }
822
823 #[rstest]
824 fn de_and_serialize_various_unsigned() {
825 let mut ser = Biseri::new();
827 let v1: u8 = 5;
828 v1.bit_serialize(6, &mut ser);
829 let v2: u16 = 15;
831 v2.bit_serialize(14, &mut ser);
832 let v3: u32 = 55;
834 v3.bit_serialize(22, &mut ser);
835 let r = ser.finish_add_data().unwrap();
837 let (bits, bytes) = (r.total_bits, r.total_bytes);
838
839 println!("bits: {}, bytes: {}", bits, bytes);
840
841 let mut des = Bides::from_biseri(&ser);
843 let vo1 = u8::bit_deserialize(1,6, &mut des);
844 let vo2 = u16::bit_deserialize(1,14, &mut des);
846 let vo3 = u32::bit_deserialize(1,22, &mut des);
848 println!("v1: {}, v2: {}, v3: {} vs vo1: {:?}, vo2: {:?}, vo3: {:?}", v1, v2, v3, vo1, vo2, vo3);
851
852 assert!(vo1.is_some());
854 assert_eq!(v1, vo1.unwrap().0);
855
856 assert!(vo2.is_some());
857 assert_eq!(v2, vo2.unwrap().0);
858
859 assert!(vo3.is_some());
860 assert_eq!(v3, vo3.unwrap().0);
861 }
862
863 #[rstest]
864 fn de_and_serialize_various_float() {
865 let mut ser = Biseri::new();
867 let v1: f32 = 56.78;
868 v1.bit_serialize(&mut ser);
869 let v2: u8 = 5;
871 v2.bit_serialize(5, &mut ser);
872 let v3: bool = true;
874 v3.bit_serialize(&mut ser);
875 let v4: bool = false;
877 v4.bit_serialize(&mut ser);
878 v1.bit_serialize(&mut ser);
880 let r = ser.finish_add_data().unwrap();
882 let (bits, bytes) = (r.total_bits, r.total_bytes);
883
884 println!("bits: {}, bytes: {}", bits, bytes);
885
886 let mut des = Bides::from_biseri(&ser);
888 let vo1 = f32::bit_deserialize(1, &mut des);
889 let vo2 = u8::bit_deserialize(1,5, &mut des);
890 let vo3 = bool::bit_deserialize(1,&mut des);
891 let vo4 = bool::bit_deserialize(1,&mut des);
892 let vo5 = f32::bit_deserialize(1,&mut des);
893 println!("vo1: {:?}, vo2: {:?}, vo3: {:?}, vo4: {:?}, vo4: {:?}", vo1, vo2, vo3, vo4, vo5);
900
901 assert!(vo1.is_some());
903 assert_eq!(v1, vo1.unwrap().0);
904
905 assert!(vo2.is_some());
906 assert_eq!(v2, vo2.unwrap().0);
907
908 assert!(vo3.is_some());
909 assert_eq!(v3, vo3.unwrap().0);
910
911 assert!(vo4.is_some());
912 assert_eq!(v4, vo4.unwrap().0);
913
914 assert!(vo5.is_some());
915 assert_eq!(v1, vo5.unwrap().0);
916 }
917
918 #[rstest]
919 fn serialize_and_deserialize_array_uint() {
920 let mut ser = Biseri::new();
921
922 let v: FixedArray<VarWithGivenBitSize<u16, 5>, 4> = [11.into(), 12.into(), 22.into(), 23.into()].into();
923 v.bit_serialize(&mut ser);
924 let r = ser.finish_add_data().unwrap();
925 let (bits, bytes) = (r.total_bits, r.total_bytes);
926
927 println!("bits: {}, bytes: {}", bits, bytes);
928
929 let mut des = Bides::from_biseri(&ser);
930 let vv = FixedArray::<VarWithGivenBitSize<u16, 5>, 4>::bit_deserialize(1,&mut des);
931
932 assert!(vv.is_some());
933 let vv = vv.unwrap().0;
934
935 assert_eq!(v.val[0], vv.val[0]);
936 assert_eq!(v.val[1], vv.val[1]);
937 assert_eq!(v.val[2], vv.val[2]);
938 assert_eq!(v.val[3], vv.val[3]);
939 }
940
941 #[rstest]
942 fn serialize_and_deserialize_array_bool() {
943 let mut ser = Biseri::new();
944
945 let v= FixedArray::from([true, true, false, true]);
946 v.bit_serialize(&mut ser);
947 let r = ser.finish_add_data().unwrap();
948 let (bits, bytes) = (r.total_bits, r.total_bytes);
949 println!("bits: {}, bytes: {}", bits, bytes);
950
951 let mut des = Bides::from_biseri(&ser);
952 let vv = FixedArray::<bool, 4>::bit_deserialize(1,&mut des);
953
954 assert!(vv.is_some());
955 let vv = vv.unwrap().0;
956
957 assert_eq!(v.val[0], vv.val[0]);
958 assert_eq!(v.val[1], vv.val[1]);
959 assert_eq!(v.val[2], vv.val[2]);
960 assert_eq!(v.val[3], vv.val[3]);
961 }
962 #[rstest]
963 fn serialize_and_deserialize_array_f64() {
964 let mut ser = Biseri::new();
965
966 let v = FixedArray::from([1.1, 1.2, 22.34, 123456.78]);
967 v.bit_serialize(&mut ser);
968 let r = ser.finish_add_data().unwrap();
969 let (bits, bytes) = (r.total_bits, r.total_bytes);
970 println!("bits: {}, bytes: {}", bits, bytes);
971
972 let mut des = Bides::from_biseri(&ser);
973 let vv = FixedArray::<f64, 4>::bit_deserialize(1,&mut des);
974
975 assert!(vv.is_some());
976 let vv = vv.unwrap().0;
977
978 assert_eq!(v.val[0], vv.val[0]);
979 assert_eq!(v.val[1], vv.val[1]);
980 assert_eq!(v.val[2], vv.val[2]);
981 assert_eq!(v.val[3], vv.val[3]);
982 }
983
984 #[rstest]
985 #[case::val_0(0, 1, 1)]
986 #[case::val_1(1, 5, 1)]
987 #[case::val_10(10, 9, 2)]
988 fn serialize_dyn_int_u32_3(#[case] val: u32, #[case] ex_bits: u64, #[case] ex_bytes: u64) {
989 let mut ser = Biseri::new();
996
997 let v = DynInteger::<u32, 3>::new(val);
998 v.bit_serialize(&mut ser);
999 let r = ser.finish_add_data().unwrap();
1000 let (bits, bytes) = (r.total_bits, r.total_bytes);
1001 println!("bits: {}, bytes: {}", bits, bytes);
1002
1003 assert_eq!(bits, ex_bits);
1004 assert_eq!(bytes, ex_bytes);
1005 }
1006
1007 #[rstest]
1008 #[case::val_0(0, 2, 1)]
1009 #[case::val_1(1, 6, 1)]
1010 #[case::val_10(10, 10, 2)]
1011 fn serialize_dyn_int_i32_3(#[case] val: i32, #[case] ex_bits: u64, #[case] ex_bytes: u64) {
1012 let mut ser = Biseri::new();
1019
1020 let v = DynInteger::<i32, 3>::new(val);
1021 v.bit_serialize(&mut ser);
1022 let r = ser.finish_add_data().unwrap();
1023 let (bits, bytes) = (r.total_bits, r.total_bytes);
1024 println!("bits: {}, bytes: {}", bits, bytes);
1025
1026 assert_eq!(bits, ex_bits);
1027 assert_eq!(bytes, ex_bytes);
1028 }
1029
1030 #[rstest]
1031 #[case::val_0(0)]
1032 #[case::val_1(1)]
1033 #[case::val_10(10)]
1034 #[case::val_m1(-1)]
1035 #[case::val_m1111(-1111)]
1036 fn ser_and_deserialize_dyn_int_i32_3(#[case] val: i32) {
1037 let mut ser = Biseri::new();
1042
1043 let v = DynInteger::<i32, 3>::new(val);
1044 v.bit_serialize(&mut ser);
1045 let r = ser.finish_add_data().unwrap();
1046 let (bits, bytes) = (r.total_bits, r.total_bytes);
1047 println!("bits: {}, bytes: {}", bits, bytes);
1048
1049 let mut der = Bides::from_biseri(&ser);
1050
1051 let dv = DynInteger::<i32, 3>::bit_deserialize(1, &mut der);
1052 assert!(dv.is_some());
1053
1054 let dv = dv.unwrap();
1055 assert_eq!(val, dv.0.val);
1056 }
1057
1058 #[rstest]
1059 fn ser_and_deserialize_fixed_int() {
1060 let mut ser = Biseri::new();
1061
1062 let v = VarWithGivenBitSize::<u32, 20>::new(1111);
1063 v.bit_serialize(&mut ser);
1064
1065 let r = ser.finish_add_data().unwrap();
1066 let (bits, bytes) = (r.total_bits, r.total_bytes);
1067 println!("bits: {}, bytes: {}", bits, bytes);
1068
1069 let mut der = Bides::from_biseri(&ser);
1070 let vv = VarWithGivenBitSize::<u32, 20>::bit_deserialize(1, &mut der);
1071
1072 println!("v: {:?}, vv: {:?}", v, vv);
1073 assert!(vv.is_some());
1074 let vv = vv.unwrap().0;
1075
1076 assert_eq!(v.val, vv.val);
1077 }
1078
1079 #[rstest]
1080 fn ser_and_deserialize_fixed_int_not_enough_data() {
1081 let mut ser = Biseri::new();
1082
1083 let v = VarWithGivenBitSize::<u32, 20>::new(1111);
1084 v.bit_serialize(&mut ser);
1085
1086 let r = ser.finish_add_data().unwrap();
1087 let (bits, bytes) = (r.total_bits, r.total_bytes);
1088 println!("bits: {}, bytes: {}", bits, bytes);
1089
1090 let mut der = Bides::from_biseri(&ser);
1091 der.data_cache.truncate(1);
1092 let vv = VarWithGivenBitSize::<u32, 20>::bit_deserialize(1, &mut der);
1093
1094 println!("v: {:?}, vv: {:?}", v, vv);
1095 assert!(vv.is_none());
1096 }
1097
1098 #[rstest]
1099 fn ser_and_deserialize_fixed_precision_1() {
1100 let mut ser = Biseri::new();
1101
1102 let v = FixPrecisionMinMax::<20, -50, 50>::new(12.3456);
1103 v.bit_serialize(&mut ser);
1104
1105 let r = ser.finish_add_data().unwrap();
1106 let (bits, bytes) = (r.total_bits, r.total_bytes);
1107 println!("bits: {}, bytes: {}", bits, bytes);
1108
1109 let mut der = Bides::from_biseri(&ser);
1110 let vv = FixPrecisionMinMax::<20, -50, 50>::bit_deserialize(1, &mut der);
1111
1112 println!("v: {:?}, vv: {:?}", v, vv);
1113 assert!(vv.is_some());
1114 let vv = vv.unwrap().0;
1115
1116 let eps = 1e-1;
1117 match v.val {
1118 FixPrecisionVal::Value(fpv) => {
1119 match vv.val {
1120 FixPrecisionVal::Value(fpvv) => assert!((fpv - fpvv).abs() < eps),
1121 _ => assert!(false)
1122 }
1123 },
1124 _ => assert!(false)
1125 }
1126 }
1127 #[rstest]
1128 fn ser_and_deserialize_fixed_precision_2() {
1129 let mut ser = Biseri::new();
1130
1131 let v = FixPrecisionMinMax::<20, -50, 50>::new(-12.3456);
1132 v.bit_serialize(&mut ser);
1133
1134 let r = ser.finish_add_data().unwrap();
1135 let (bits, bytes) = (r.total_bits, r.total_bytes);
1136 println!("bits: {}, bytes: {}", bits, bytes);
1137
1138 let mut der = Bides::from_biseri(&ser);
1139 let vv = FixPrecisionMinMax::<20, -50, 50>::bit_deserialize(1, &mut der);
1140
1141 println!("v: {:?}, vv: {:?}", v, vv);
1142 assert!(vv.is_some());
1143 let vv = vv.unwrap().0;
1144
1145 let eps = 1e-1;
1146 match v.val {
1147 FixPrecisionVal::Value(fpv) => {
1148 match vv.val {
1149 FixPrecisionVal::Value(fpvv) => assert!((fpv - fpvv).abs() < eps),
1150 _ => assert!(false)
1151 }
1152 },
1153 _ => assert!(false)
1154 }
1155 }
1156 #[rstest]
1157 fn ser_and_deserialize_fixed_precision_under() {
1158 let mut ser = Biseri::new();
1159
1160 let v = FixPrecisionMinMax::<10, -50, 50>::new(-60.0);
1161 v.bit_serialize(&mut ser);
1162
1163 let r = ser.finish_add_data().unwrap();
1164 let (bits, bytes) = (r.total_bits, r.total_bytes);
1165 println!("bits: {}, bytes: {}", bits, bytes);
1166
1167 let mut der = Bides::from_biseri(&ser);
1168 let vv = FixPrecisionMinMax::<10, -50, 50>::bit_deserialize(1, &mut der);
1169
1170 println!("v: {:?}, vv: {:?}", v, vv);
1171 assert!(vv.is_some());
1172 let vv = vv.unwrap().0;
1173
1174 match vv.val {
1175 FixPrecisionVal::Underflow => assert!(true),
1176 _ => assert!(false),
1177 }
1178 }
1179 #[rstest]
1180 fn ser_and_deserialize_fixed_precision_over() {
1181 let mut ser = Biseri::new();
1182
1183 let v = FixPrecisionMinMax::<10, -50, 50>::new(60.0);
1184 v.bit_serialize(&mut ser);
1185
1186 let r = ser.finish_add_data().unwrap();
1187 let (bits, bytes) = (r.total_bits, r.total_bytes);
1188 println!("bits: {}, bytes: {}", bits, bytes);
1189
1190 let mut der = Bides::from_biseri(&ser);
1191 let vv = FixPrecisionMinMax::<10, -50, 50>::bit_deserialize(1, &mut der);
1192
1193 println!("v: {:?}, vv: {:?}", v, vv);
1194 assert!(vv.is_some());
1195 let vv = vv.unwrap().0;
1196
1197 match vv.val {
1198 FixPrecisionVal::Overflow => assert!(true),
1199 _ => assert!(false)
1200 }
1201 }
1202}
1203