@@ -13,6 +13,9 @@ const fn bits_of<T>() -> usize {
13
13
core:: mem:: size_of :: < T > ( ) . saturating_mul ( <u8 >:: BITS as usize )
14
14
}
15
15
16
+ /// Max bits written to [`Reader::write_bits`] during one call
17
+ pub const MAX_BITS_AMT : usize = 128 ;
18
+
16
19
/// Container to use with `to_writer`
17
20
pub struct Writer < W : Write > {
18
21
pub ( crate ) inner : W ,
@@ -43,6 +46,9 @@ impl<W: Write> Writer<W> {
43
46
///
44
47
/// Any leftover bits will be written before `bits`, and non-written bits will
45
48
/// be stored in `self.leftover`.
49
+ ///
50
+ /// # Params
51
+ /// `bits` - Amount of bits that will be written. length must be <= [`MAX_BITS_AMT`].
46
52
#[ inline( never) ]
47
53
pub fn write_bits ( & mut self , bits : & BitSlice < u8 , Msb0 > ) -> Result < ( ) , DekuError > {
48
54
#[ cfg( feature = "logging" ) ]
@@ -66,7 +72,8 @@ impl<W: Write> Writer<W> {
66
72
} ;
67
73
68
74
// one shot impl of BitSlice::read(no read_exact), but for no_std
69
- let mut buf = alloc:: vec![ 0x00 ; bits. len( ) / 8 ] ;
75
+ let mut buf = [ 0 ; MAX_BITS_AMT ] ;
76
+ let buf = & mut buf[ ..bits. len ( ) / 8 ] ;
70
77
let mut count = 0 ;
71
78
bits. chunks_exact ( bits_of :: < u8 > ( ) )
72
79
. zip ( buf. iter_mut ( ) )
@@ -81,7 +88,7 @@ impl<W: Write> Writer<W> {
81
88
bits = unsafe { bits. get_unchecked ( count * bits_of :: < u8 > ( ) ..) } ;
82
89
83
90
self . leftover = bits. to_bitvec ( ) ;
84
- if self . inner . write_all ( & buf) . is_err ( ) {
91
+ if self . inner . write_all ( buf) . is_err ( ) {
85
92
return Err ( DekuError :: Write ) ;
86
93
}
87
94
0 commit comments