@@ -221,8 +221,52 @@ impl<I: Iterator<Item = u8>> BitIter<I> {
221
221
Ok ( FailEntropy :: from_byte_array ( ret) )
222
222
}
223
223
224
+ /// Decode a natural number from bits.
225
+ ///
226
+ /// If a bound is specified, then the decoding terminates before trying to
227
+ /// decode a larger number.
228
+ pub fn read_natural ( & mut self , bound : Option < usize > ) -> Result < usize , decode:: Error > {
229
+ decode:: decode_natural ( self , bound)
230
+ }
231
+
232
+ /// Accessor for the number of bits which have been read,
233
+ /// in total, from this iterator
234
+ pub fn n_total_read ( & self ) -> usize {
235
+ self . total_read
236
+ }
237
+
238
+ /// Consumes the bit iterator, checking that there are no remaining
239
+ /// bytes and that any unread bits are zero.
240
+ pub fn close ( mut self ) -> Result < ( ) , CloseError > {
241
+ if let Some ( first_byte) = self . iter . next ( ) {
242
+ return Err ( CloseError :: TrailingBytes { first_byte } ) ;
243
+ }
244
+
245
+ debug_assert ! ( self . read_bits >= 1 ) ;
246
+ debug_assert ! ( self . read_bits <= 8 ) ;
247
+ let n_bits = 8 - self . read_bits ;
248
+ let masked_padding = self . cached_byte & ( ( 1u8 << n_bits) - 1 ) ;
249
+ if masked_padding != 0 {
250
+ Err ( CloseError :: IllegalPadding {
251
+ masked_padding,
252
+ n_bits,
253
+ } )
254
+ } else {
255
+ Ok ( ( ) )
256
+ }
257
+ }
258
+ }
259
+
260
+ /// Functionality for Boolean iterators to decode Simplicity values.
261
+ pub trait ValueDecoder {
224
262
/// Decode a value from bits, based on the given type.
225
- pub fn read_value ( & mut self , ty : & Final ) -> Result < Value , EarlyEndOfStreamError > {
263
+ ///
264
+ /// Return `None` if there are not enough bits.
265
+ fn decode_value ( & mut self , ty : & Final ) -> Result < Value , EarlyEndOfStreamError > ;
266
+ }
267
+
268
+ impl < I : Iterator < Item = bool > > ValueDecoder for I {
269
+ fn decode_value ( & mut self , ty : & Final ) -> Result < Value , EarlyEndOfStreamError > {
226
270
enum State < ' a > {
227
271
ProcessType ( & ' a Final ) ,
228
272
DoSumL ( Arc < Final > ) ,
@@ -237,7 +281,7 @@ impl<I: Iterator<Item = u8>> BitIter<I> {
237
281
State :: ProcessType ( ty) => match ty. bound ( ) {
238
282
types:: CompleteBound :: Unit => result_stack. push ( Value :: unit ( ) ) ,
239
283
types:: CompleteBound :: Sum ( ref l, ref r) => {
240
- if self . read_bit ( ) ? {
284
+ if self . next ( ) . ok_or ( EarlyEndOfStreamError ) ? {
241
285
stack. push ( State :: DoSumR ( Arc :: clone ( l) ) ) ;
242
286
stack. push ( State :: ProcessType ( r) ) ;
243
287
} else {
@@ -269,41 +313,6 @@ impl<I: Iterator<Item = u8>> BitIter<I> {
269
313
debug_assert_eq ! ( result_stack. len( ) , 1 ) ;
270
314
Ok ( result_stack. pop ( ) . unwrap ( ) )
271
315
}
272
-
273
- /// Decode a natural number from bits.
274
- ///
275
- /// If a bound is specified, then the decoding terminates before trying to
276
- /// decode a larger number.
277
- pub fn read_natural ( & mut self , bound : Option < usize > ) -> Result < usize , decode:: Error > {
278
- decode:: decode_natural ( self , bound)
279
- }
280
-
281
- /// Accessor for the number of bits which have been read,
282
- /// in total, from this iterator
283
- pub fn n_total_read ( & self ) -> usize {
284
- self . total_read
285
- }
286
-
287
- /// Consumes the bit iterator, checking that there are no remaining
288
- /// bytes and that any unread bits are zero.
289
- pub fn close ( mut self ) -> Result < ( ) , CloseError > {
290
- if let Some ( first_byte) = self . iter . next ( ) {
291
- return Err ( CloseError :: TrailingBytes { first_byte } ) ;
292
- }
293
-
294
- debug_assert ! ( self . read_bits >= 1 ) ;
295
- debug_assert ! ( self . read_bits <= 8 ) ;
296
- let n_bits = 8 - self . read_bits ;
297
- let masked_padding = self . cached_byte & ( ( 1u8 << n_bits) - 1 ) ;
298
- if masked_padding != 0 {
299
- Err ( CloseError :: IllegalPadding {
300
- masked_padding,
301
- n_bits,
302
- } )
303
- } else {
304
- Ok ( ( ) )
305
- }
306
- }
307
316
}
308
317
309
318
/// Functionality for Boolean iterators to collect their bits or bytes.
0 commit comments