@@ -164,16 +164,9 @@ macro_rules! impl_batch_operations {
164
164
fn del_last_index( & mut self , keychain: KeychainKind ) -> Result <Option <u32 >, Error > {
165
165
let key = MapKey :: LastIndex ( keychain) . as_map_key( ) ;
166
166
let res = self . remove( key) ;
167
- let res = $process_delete!( res) ;
168
-
169
- match res {
170
- None => Ok ( None ) ,
171
- Some ( b) => {
172
- let array: [ u8 ; 4 ] = b. as_ref( ) . try_into( ) . map_err( |_| Error :: InvalidU32Bytes ( b. to_vec( ) ) ) ?;
173
- let val = u32 :: from_be_bytes( array) ;
174
- Ok ( Some ( val) )
175
- }
176
- }
167
+ $process_delete!( res)
168
+ . map( ivec_to_u32)
169
+ . transpose( )
177
170
}
178
171
179
172
fn del_sync_time( & mut self ) -> Result <Option <SyncTime >, Error > {
@@ -347,14 +340,7 @@ impl Database for Tree {
347
340
fn get_last_index ( & self , keychain : KeychainKind ) -> Result < Option < u32 > , Error > {
348
341
let key = MapKey :: LastIndex ( keychain) . as_map_key ( ) ;
349
342
self . get ( key) ?
350
- . map ( |b| -> Result < _ , Error > {
351
- let array: [ u8 ; 4 ] = b
352
- . as_ref ( )
353
- . try_into ( )
354
- . map_err ( |_| Error :: InvalidU32Bytes ( b. to_vec ( ) ) ) ?;
355
- let val = u32:: from_be_bytes ( array) ;
356
- Ok ( val)
357
- } )
343
+ . map ( ivec_to_u32)
358
344
. transpose ( )
359
345
}
360
346
@@ -383,20 +369,22 @@ impl Database for Tree {
383
369
384
370
Some ( new. to_be_bytes ( ) . to_vec ( ) )
385
371
} ) ?
386
- . map_or ( Ok ( 0 ) , |b| -> Result < _ , Error > {
387
- let array: [ u8 ; 4 ] = b
388
- . as_ref ( )
389
- . try_into ( )
390
- . map_err ( |_| Error :: InvalidU32Bytes ( b. to_vec ( ) ) ) ?;
391
- let val = u32:: from_be_bytes ( array) ;
392
- Ok ( val)
393
- } ) ?;
372
+ . map_or ( Ok ( 0 ) , ivec_to_u32) ?;
394
373
395
374
Tree :: flush ( self ) . map ( |_| ( ) ) ?;
396
375
Ok ( last_index)
397
376
}
398
377
}
399
378
379
+ fn ivec_to_u32 ( b : sled:: IVec ) -> Result < u32 , Error > {
380
+ let array: [ u8 ; 4 ] = b
381
+ . as_ref ( )
382
+ . try_into ( )
383
+ . map_err ( |_| Error :: InvalidU32Bytes ( b. to_vec ( ) ) ) ?;
384
+ let val = u32:: from_be_bytes ( array) ;
385
+ Ok ( val)
386
+ }
387
+
400
388
impl BatchDatabase for Tree {
401
389
type Batch = sled:: Batch ;
402
390
0 commit comments