@@ -51,6 +51,18 @@ impl std::fmt::Display for LruCacheCorrupted {
51
51
52
52
impl std:: error:: Error for LruCacheCorrupted { }
53
53
54
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
55
+ pub enum FlushError < E > {
56
+ LruCacheCorrupted ,
57
+ FlushError ( E ) ,
58
+ }
59
+
60
+ impl < E > From < E > for FlushError < E > {
61
+ fn from ( e : E ) -> Self {
62
+ FlushError :: FlushError ( e)
63
+ }
64
+ }
65
+
54
66
/// LRU cache
55
67
pub struct LruCache < K , V > {
56
68
capacity : usize ,
@@ -197,14 +209,12 @@ impl<K: Eq + std::hash::Hash + Clone, V: Copy> LruCache<K, V> {
197
209
pub fn flush < E > (
198
210
& mut self ,
199
211
mut f : impl FnMut ( & K , V ) -> Result < ( ) , E > ,
200
- ) -> Result < Result < ( ) , E > , LruCacheCorrupted > {
212
+ ) -> Result < ( ) , FlushError < E > > {
201
213
for node in self . order . iter_mut ( ) . filter ( |n| n. dirty ) {
202
- match f ( & node. key , node. value ) {
203
- Ok ( ( ) ) => node. dirty = false ,
204
- Err ( e) => return Ok ( Err ( e) ) ,
205
- }
214
+ f ( & node. key , node. value ) ?;
215
+ node. dirty = false ;
206
216
}
207
- Ok ( Ok ( ( ) ) )
217
+ Ok ( ( ) )
208
218
}
209
219
210
220
/// Helper function to remove a node from the linked list (by index)
@@ -338,8 +348,7 @@ mod tests {
338
348
flushed. push ( ( * k, v) ) ;
339
349
Ok :: < ( ) , ( ) > ( ( ) )
340
350
} )
341
- . expect ( "cache corrupted" )
342
- . expect ( "flush failed" ) ;
351
+ . expect ( "cache corrupted or flush failed" ) ;
343
352
344
353
assert_eq ! ( flushed, vec![ ( 1 , 1 ) ] ) ;
345
354
@@ -352,8 +361,7 @@ mod tests {
352
361
flushed. push ( ( * k, v) ) ;
353
362
Ok :: < ( ) , ( ) > ( ( ) )
354
363
} )
355
- . expect ( "cache corrupted" )
356
- . expect ( "flush failed" ) ;
364
+ . expect ( "cache corrupted or flush failed" ) ;
357
365
358
366
flushed. sort ( ) ;
359
367
assert_eq ! ( flushed, vec![ ( 1 , 3 ) , ( 2 , 2 ) ] ) ;
@@ -386,8 +394,7 @@ mod tests {
386
394
flushed. push ( ( * k, v) ) ;
387
395
Ok :: < ( ) , ( ) > ( ( ) )
388
396
} )
389
- . expect ( "cache corrupted" )
390
- . expect ( "flush failed" ) ;
397
+ . expect ( "cache corrupted or flush failed" ) ;
391
398
392
399
flushed. sort ( ) ;
393
400
assert_eq ! ( flushed, [ ( 2 , 2 ) , ( 3 , 3 ) ] ) ;
@@ -442,8 +449,7 @@ mod tests {
442
449
flushed. push ( ( * k, v) ) ;
443
450
Ok :: < ( ) , ( ) > ( ( ) )
444
451
} )
445
- . expect ( "cache corrupted" )
446
- . expect ( "flush failed" ) ;
452
+ . expect ( "cache corrupted or flush failed" ) ;
447
453
448
454
assert_eq ! ( flushed, vec![ ( 1 , 1 ) ] ) ;
449
455
@@ -455,8 +461,7 @@ mod tests {
455
461
flushed. push ( ( * k, v) ) ;
456
462
Ok :: < ( ) , ( ) > ( ( ) )
457
463
} )
458
- . expect ( "cache corrupted" )
459
- . expect ( "flush failed" ) ;
464
+ . expect ( "cache corrupted or flush failed" ) ;
460
465
461
466
assert_eq ! ( flushed, vec![ ( 2 , 2 ) ] ) ;
462
467
}
@@ -548,7 +553,7 @@ mod property_tests {
548
553
CacheOp :: Insert ( k, v) => { cache. insert( k, v) . expect( "cache corrupted" ) ; }
549
554
CacheOp :: Get ( k) => { cache. get( & k) . expect( "cache corrupted" ) ; }
550
555
CacheOp :: InsertClean ( k, v) => { cache. insert_clean( k, v) . expect( "cache corrupted" ) ; }
551
- CacheOp :: Flush => { cache. flush( |_, _| Ok :: <( ) , ( ) >( ( ) ) ) . expect( "cache corrupted" ) . expect ( " flush failed") ; }
556
+ CacheOp :: Flush => { cache. flush( |_, _| Ok :: <( ) , ( ) >( ( ) ) ) . expect( "cache corrupted or flush failed" ) ; }
552
557
}
553
558
}
554
559
}
@@ -572,7 +577,7 @@ mod property_tests {
572
577
CacheOp :: Insert ( k, v) => { cache. insert( k, v) . expect( "cache corrupted" ) ; }
573
578
CacheOp :: Get ( k) => { cache. get( & k) . expect( "cache corrupted" ) ; }
574
579
CacheOp :: InsertClean ( k, v) => { cache. insert_clean( k, v) . expect( "cache corrupted" ) ; }
575
- CacheOp :: Flush => { cache. flush( |_, _| Ok :: <( ) , ( ) >( ( ) ) ) . expect( "cache corrupted" ) . expect ( " flush failed") ; }
580
+ CacheOp :: Flush => { cache. flush( |_, _| Ok :: <( ) , ( ) >( ( ) ) ) . expect( "cache corrupted or flush failed" ) ; }
576
581
}
577
582
// Verify linked list integrity
578
583
if !cache. order. is_empty( ) {
@@ -617,7 +622,7 @@ mod property_tests {
617
622
cache. flush( |k, v| {
618
623
flushed. push( ( * k, v) ) ;
619
624
Ok :: <( ) , ( ) >( ( ) )
620
- } ) . expect( "cache corrupted" ) . expect ( " flush failed") ;
625
+ } ) . expect( "cache corrupted or flush failed" ) ;
621
626
simple. flush( |k, v| {
622
627
simple_flushed. push( ( * k, v) ) ;
623
628
Ok :: <( ) , ( ) >( ( ) )
0 commit comments