1- use crate :: Decode ;
2- use brotli:: { enc:: StandardAlloc , BrotliDecompressStream , BrotliResult , BrotliState } ;
3- use compression_core:: util:: PartialBuffer ;
1+ use crate :: DecodeV2 ;
2+ use brotli:: { enc:: StandardAlloc , BrotliDecompressStream , BrotliResult } ;
3+ use compression_core:: util:: { PartialBuffer , WriteBuffer } ;
44use std:: { fmt, io} ;
55
6+ type BrotliState = brotli:: BrotliState < StandardAlloc , StandardAlloc , StandardAlloc > ;
7+
68pub struct BrotliDecoder {
79 // `BrotliState` is very large (over 2kb) which is why we're boxing it.
8- state : Box < BrotliState < StandardAlloc , StandardAlloc , StandardAlloc > > ,
10+ state : Box < BrotliState > ,
911}
1012
1113impl Default for BrotliDecoder {
1214 fn default ( ) -> Self {
1315 Self {
14- state : Box :: new ( BrotliState :: new (
15- StandardAlloc :: default ( ) ,
16- StandardAlloc :: default ( ) ,
17- StandardAlloc :: default ( ) ,
18- ) ) ,
16+ state : Box :: new ( Self :: new_brotli_state ( ) ) ,
1917 }
2018 }
2119}
2220
2321impl BrotliDecoder {
22+ fn new_brotli_state ( ) -> BrotliState {
23+ BrotliState :: new (
24+ StandardAlloc :: default ( ) ,
25+ StandardAlloc :: default ( ) ,
26+ StandardAlloc :: default ( ) ,
27+ )
28+ }
29+
2430 pub fn new ( ) -> Self {
2531 Self :: default ( )
2632 }
2733
2834 fn decode (
2935 & mut self ,
30- input : & mut PartialBuffer < impl AsRef < [ u8 ] > > ,
31- output : & mut PartialBuffer < impl AsRef < [ u8 ] > + AsMut < [ u8 ] > > ,
36+ input : & mut PartialBuffer < & [ u8 ] > ,
37+ output : & mut WriteBuffer < ' _ > ,
3238 ) -> io:: Result < BrotliResult > {
39+ output. initialize_unwritten ( ) ;
40+
3341 let in_buf = input. unwritten ( ) ;
34- let out_buf = output. unwritten_mut ( ) ;
42+ let out_buf = output. unwritten_initialized_mut ( ) ;
3543
3644 let mut input_len = 0 ;
3745 let mut output_len = 0 ;
@@ -57,20 +65,16 @@ impl BrotliDecoder {
5765 }
5866}
5967
60- impl Decode for BrotliDecoder {
68+ impl DecodeV2 for BrotliDecoder {
6169 fn reinit ( & mut self ) -> io:: Result < ( ) > {
62- self . state = Box :: new ( BrotliState :: new (
63- StandardAlloc :: default ( ) ,
64- StandardAlloc :: default ( ) ,
65- StandardAlloc :: default ( ) ,
66- ) ) ;
70+ * self . state = Self :: new_brotli_state ( ) ;
6771 Ok ( ( ) )
6872 }
6973
7074 fn decode (
7175 & mut self ,
72- input : & mut PartialBuffer < impl AsRef < [ u8 ] > > ,
73- output : & mut PartialBuffer < impl AsRef < [ u8 ] > + AsMut < [ u8 ] > > ,
76+ input : & mut PartialBuffer < & [ u8 ] > ,
77+ output : & mut WriteBuffer < ' _ > ,
7478 ) -> io:: Result < bool > {
7579 match self . decode ( input, output) ? {
7680 BrotliResult :: ResultSuccess => Ok ( true ) ,
@@ -79,21 +83,15 @@ impl Decode for BrotliDecoder {
7983 }
8084 }
8185
82- fn flush (
83- & mut self ,
84- output : & mut PartialBuffer < impl AsRef < [ u8 ] > + AsMut < [ u8 ] > > ,
85- ) -> io:: Result < bool > {
86+ fn flush ( & mut self , output : & mut WriteBuffer < ' _ > ) -> io:: Result < bool > {
8687 match self . decode ( & mut PartialBuffer :: new ( & [ ] [ ..] ) , output) ? {
8788 BrotliResult :: ResultSuccess | BrotliResult :: NeedsMoreInput => Ok ( true ) ,
8889 BrotliResult :: NeedsMoreOutput => Ok ( false ) ,
8990 BrotliResult :: ResultFailure => unreachable ! ( ) ,
9091 }
9192 }
9293
93- fn finish (
94- & mut self ,
95- output : & mut PartialBuffer < impl AsRef < [ u8 ] > + AsMut < [ u8 ] > > ,
96- ) -> io:: Result < bool > {
94+ fn finish ( & mut self , output : & mut WriteBuffer < ' _ > ) -> io:: Result < bool > {
9795 match self . decode ( & mut PartialBuffer :: new ( & [ ] [ ..] ) , output) ? {
9896 BrotliResult :: ResultSuccess => Ok ( true ) ,
9997 BrotliResult :: NeedsMoreOutput => Ok ( false ) ,
0 commit comments