Skip to main content

structpack/
lib.rs

1mod field;
2mod helper_impl;
3mod macros;
4pub mod util;
5mod value;
6
7use std::marker::PhantomData;
8
9pub use field::{
10    FloatingField, GenericFloatingField, GenericIntegralField, IntegralField, NumericField,
11    SizedField,
12};
13pub use value::{FloatingValue, IntegralValue, NumericValue};
14
15#[derive(Default)]
16pub struct SizedBuilder<E, F> {
17    fields: E,
18    bit_len: usize,
19    _field: PhantomData<*const F>,
20}
21
22impl<E, F> SizedBuilder<E, F> {
23    pub fn bit_len(&self) -> usize {
24        self.bit_len
25    }
26
27    pub fn byte_len(&self) -> usize {
28        if self.bit_len() == 0 {
29            0
30        } else {
31            (self.bit_len() - 1) / 8 + 1
32        }
33    }
34
35    pub fn build(self) -> E {
36        self.fields
37    }
38}
39
40impl<E, F> Extend<F> for SizedBuilder<E, F>
41where
42    E: Extend<F>,
43    F: SizedField,
44{
45    fn extend<T: IntoIterator<Item = F>>(&mut self, iter: T) {
46        for field in iter {
47            self.bit_len = self.bit_len.max(field.last_bit_exclusive());
48            self.fields.extend([field]);
49        }
50    }
51}