@@ -28,10 +28,27 @@ impl<R: Read> Decoder<'static, BufReader<R>> {
2828 /// is no need to do that yourself. If what you have already implements
2929 /// `BufRead` - a `&[u8]`, say - use [`Decoder::with_buffer()`] instead and
3030 /// save the second layer of buffering.
31+ ///
32+ /// Use [`Self::try_new()`] to recover the reader if initialization fails.
3133 pub fn new ( reader : R ) -> io:: Result < Self > {
32- let buffer_size = zstd_safe:: DCtx :: in_size ( ) ;
34+ Self :: try_new ( reader) . map_err ( |( _, error) | error)
35+ }
3336
34- Self :: with_buffer ( BufReader :: with_capacity ( buffer_size, reader) )
37+ /// Creates a new decoder, returning the original reader on error.
38+ ///
39+ /// Like [`Self::new()`], this wraps the reader in a `BufReader` on
40+ /// success. On error, it returns the original `R`, without buffering.
41+ /// Construction does not read from or consume the reader.
42+ pub fn try_new ( reader : R ) -> Result < Self , ( R , io:: Error ) > {
43+ let decoder = match raw:: Decoder :: new ( ) {
44+ Ok ( decoder) => decoder,
45+ Err ( error) => return Err ( ( reader, error) ) ,
46+ } ;
47+ let reader =
48+ BufReader :: with_capacity ( zstd_safe:: DCtx :: in_size ( ) , reader) ;
49+ Ok ( Self {
50+ reader : zio:: Reader :: new ( reader, decoder) ,
51+ } )
3552 }
3653}
3754
@@ -41,14 +58,44 @@ impl<R: BufRead> Decoder<'static, R> {
4158 /// Unlike [`Decoder::new()`], this adds no buffering of its own, so give
4259 /// it something with a reasonable buffer already - `new` uses zstd's
4360 /// preferred input size for exactly that reason.
61+ ///
62+ /// Use [`Self::try_with_buffer()`] to recover the reader if initialization
63+ /// fails.
4464 pub fn with_buffer ( reader : R ) -> io:: Result < Self > {
45- Self :: with_dictionary ( reader, & [ ] )
65+ Self :: try_with_buffer ( reader) . map_err ( | ( _ , error ) | error )
4666 }
67+
68+ /// Creates a decoder around a `BufRead`, returning the reader on error.
69+ ///
70+ /// Like [`Self::with_buffer()`], this adds no buffering of its own.
71+ /// Construction does not read from or consume the reader.
72+ pub fn try_with_buffer ( reader : R ) -> Result < Self , ( R , io:: Error ) > {
73+ Self :: try_with_dictionary ( reader, & [ ] )
74+ }
75+
4776 /// Creates a new decoder, using an existing dictionary.
4877 ///
4978 /// The dictionary must be the same as the one used during compression.
79+ ///
80+ /// Use [`Self::try_with_dictionary()`] to recover the reader if
81+ /// initialization fails.
5082 pub fn with_dictionary ( reader : R , dictionary : & [ u8 ] ) -> io:: Result < Self > {
51- let decoder = raw:: Decoder :: with_dictionary ( dictionary) ?;
83+ Self :: try_with_dictionary ( reader, dictionary)
84+ . map_err ( |( _, error) | error)
85+ }
86+
87+ /// Creates a decoder using a dictionary, returning the reader on error.
88+ ///
89+ /// The dictionary must be the same as the one used during compression.
90+ /// Construction does not read from or consume the reader.
91+ pub fn try_with_dictionary (
92+ reader : R ,
93+ dictionary : & [ u8 ] ,
94+ ) -> Result < Self , ( R , io:: Error ) > {
95+ let decoder = match raw:: Decoder :: with_dictionary ( dictionary) {
96+ Ok ( decoder) => decoder,
97+ Err ( error) => return Err ( ( reader, error) ) ,
98+ } ;
5299 let reader = zio:: Reader :: new ( reader, decoder) ;
53100
54101 Ok ( Decoder { reader } )
@@ -80,14 +127,37 @@ impl<'a, R: BufRead> Decoder<'a, R> {
80127 /// Creates a new decoder, using an existing `DecoderDictionary`.
81128 ///
82129 /// The dictionary must be the same as the one used during compression.
130+ ///
131+ /// Use [`Self::try_with_prepared_dictionary()`] to recover the reader if
132+ /// initialization fails.
83133 pub fn with_prepared_dictionary < ' b > (
84134 reader : R ,
85135 dictionary : & ' a DecoderDictionary < ' b > ,
86136 ) -> io:: Result < Self >
87137 where
88138 ' b : ' a ,
89139 {
90- let decoder = raw:: Decoder :: with_prepared_dictionary ( dictionary) ?;
140+ Self :: try_with_prepared_dictionary ( reader, dictionary)
141+ . map_err ( |( _, error) | error)
142+ }
143+
144+ /// Creates a decoder using a prepared dictionary, returning the reader
145+ /// on error.
146+ ///
147+ /// The dictionary must be the same as the one used during compression.
148+ /// Construction does not read from or consume the reader.
149+ pub fn try_with_prepared_dictionary < ' b > (
150+ reader : R ,
151+ dictionary : & ' a DecoderDictionary < ' b > ,
152+ ) -> Result < Self , ( R , io:: Error ) >
153+ where
154+ ' b : ' a ,
155+ {
156+ let decoder = match raw:: Decoder :: with_prepared_dictionary ( dictionary)
157+ {
158+ Ok ( decoder) => decoder,
159+ Err ( error) => return Err ( ( reader, error) ) ,
160+ } ;
91161 let reader = zio:: Reader :: new ( reader, decoder) ;
92162
93163 Ok ( Decoder { reader } )
@@ -96,14 +166,36 @@ impl<'a, R: BufRead> Decoder<'a, R> {
96166 /// Creates a new decoder, using a ref prefix.
97167 ///
98168 /// The prefix must be the same as the one used during compression.
169+ ///
170+ /// Use [`Self::try_with_ref_prefix()`] to recover the reader if
171+ /// initialization fails.
99172 pub fn with_ref_prefix < ' b > (
100173 reader : R ,
101174 ref_prefix : & ' b [ u8 ] ,
102175 ) -> io:: Result < Self >
103176 where
104177 ' b : ' a ,
105178 {
106- let decoder = raw:: Decoder :: with_ref_prefix ( ref_prefix) ?;
179+ Self :: try_with_ref_prefix ( reader, ref_prefix)
180+ . map_err ( |( _, error) | error)
181+ }
182+
183+ /// Creates a decoder using a reference prefix, returning the reader on
184+ /// error.
185+ ///
186+ /// The prefix must be the same as the one used during compression.
187+ /// Construction does not read from or consume the reader.
188+ pub fn try_with_ref_prefix < ' b > (
189+ reader : R ,
190+ ref_prefix : & ' b [ u8 ] ,
191+ ) -> Result < Self , ( R , io:: Error ) >
192+ where
193+ ' b : ' a ,
194+ {
195+ let decoder = match raw:: Decoder :: with_ref_prefix ( ref_prefix) {
196+ Ok ( decoder) => decoder,
197+ Err ( error) => return Err ( ( reader, error) ) ,
198+ } ;
107199 let reader = zio:: Reader :: new ( reader, decoder) ;
108200
109201 Ok ( Decoder { reader } )
0 commit comments