@@ -186,25 +186,22 @@ where
186
186
}
187
187
188
188
pub fn item_count ( & self ) -> Result < usize , DecoderError > {
189
- match self . is_list ( ) {
190
- true => match self . count_cache . get ( ) {
191
- Some ( c) => Ok ( c) ,
192
- None => {
193
- let c = self . iter ( ) . count ( ) ;
194
- self . count_cache . set ( Some ( c) ) ;
195
- Ok ( c)
196
- }
197
- } ,
198
- false => Err ( DecoderError :: RlpExpectedToBeList ) ,
189
+ if !self . is_list ( ) {
190
+ return Err ( DecoderError :: RlpExpectedToBeList )
199
191
}
192
+ Ok ( self . count_cache . get ( ) . unwrap_or_else ( || {
193
+ let c = self . iter ( ) . count ( ) ;
194
+ self . count_cache . set ( Some ( c) ) ;
195
+ c
196
+ } ) )
200
197
}
201
198
202
199
pub fn size ( & self ) -> usize {
203
- match self . is_data ( ) {
204
- // TODO: No panic on malformed data, but ideally would Err on no PayloadInfo.
205
- true => BasicDecoder :: payload_info ( self . bytes ) . map ( |b| b. value_len ) . unwrap_or ( 0 ) ,
206
- false => 0 ,
200
+ // TODO: No panic on malformed data, but ideally would Err on no PayloadInfo.
201
+ if !self . is_data ( ) {
202
+ return 0
207
203
}
204
+ BasicDecoder :: payload_info ( self . bytes ) . map ( |b| b. value_len ) . unwrap_or ( 0 )
208
205
}
209
206
210
207
pub fn at ( & ' view self , index : usize ) -> Result < Rlp < ' a > , DecoderError > {
@@ -215,9 +212,10 @@ where
215
212
// move to cached position if its index is less or equal to
216
213
// current search index, otherwise move to beginning of list
217
214
let c = self . offset_cache . get ( ) ;
218
- let ( mut bytes, to_skip) = match c. index <= index {
219
- true => ( Rlp :: consume ( self . bytes , c. offset ) ?, index - c. index ) ,
220
- false => ( self . consume_list_payload ( ) ?, index) ,
215
+ let ( mut bytes, to_skip) = if c. index <= index {
216
+ ( Rlp :: consume ( self . bytes , c. offset ) ?, index - c. index )
217
+ } else {
218
+ ( self . consume_list_payload ( ) ?, index)
221
219
} ;
222
220
223
221
// skip up to x items
@@ -235,17 +233,16 @@ where
235
233
} )
236
234
}
237
235
238
-
239
236
// update the cache
240
- self . offset_cache . set ( OffsetCache :: new ( index, self . bytes . len ( ) - bytes . len ( ) ) ) ;
237
+ self . offset_cache . set ( OffsetCache :: new ( index, new_offset ) ) ;
241
238
242
239
// construct new rlp
243
240
let found = BasicDecoder :: payload_info ( bytes) ?;
244
241
Ok ( Rlp :: new ( & bytes[ 0 ..found. header_len + found. value_len ] ) )
245
242
}
246
243
247
244
pub fn is_null ( & self ) -> bool {
248
- self . bytes . len ( ) == 0
245
+ self . bytes . is_empty ( )
249
246
}
250
247
251
248
pub fn is_empty ( & self ) -> bool {
@@ -325,12 +322,13 @@ where
325
322
326
323
/// consumes slice prefix of length `len`
327
324
fn consume ( bytes : & ' a [ u8 ] , len : usize ) -> Result < & ' a [ u8 ] , DecoderError > {
328
- match bytes. len ( ) >= len {
329
- true => Ok ( & bytes[ len..] ) ,
330
- false => Err ( DecoderError :: RlpIsTooShort {
325
+ if bytes. len ( ) >= len {
326
+ Ok ( & bytes[ len..] )
327
+ } else {
328
+ Err ( DecoderError :: RlpIsTooShort {
331
329
expected : len,
332
330
got : bytes. len ( ) ,
333
- } ) ,
331
+ } )
334
332
}
335
333
}
336
334
}
0 commit comments