@@ -41,6 +41,30 @@ pub enum Error {
41
41
Mmap ( io:: Error ) ,
42
42
}
43
43
44
+ impl PartialEq for Error {
45
+ fn eq ( & self , other : & Self ) -> bool {
46
+ match ( self , other) {
47
+ ( Error :: Mmap ( left) , Error :: Mmap ( right) ) => {
48
+ match ( left. raw_os_error ( ) , right. raw_os_error ( ) ) {
49
+ // Considering that in this file the only std::io::Error returned is of type
50
+ // OS error, we can leverage raw_os_error for this PartialEq implementation.
51
+ ( Some ( left_os_error) , Some ( right_os_error) ) => left_os_error == right_os_error,
52
+ ( Some ( _) , None ) => false ,
53
+ ( None , Some ( _) ) => false ,
54
+ _ => unreachable ! ( ) ,
55
+ }
56
+ }
57
+ ( Error :: InvalidOffsetLength , Error :: InvalidOffsetLength ) => true ,
58
+ ( Error :: InvalidPointer , Error :: InvalidPointer ) => true ,
59
+ ( Error :: InvalidSize , Error :: InvalidSize ) => true ,
60
+ ( Error :: MapFixed , Error :: MapFixed ) => true ,
61
+ ( Error :: MappingOverlap , Error :: MappingOverlap ) => true ,
62
+ ( Error :: MappingPastEof , Error :: MappingPastEof ) => true ,
63
+ _ => false ,
64
+ }
65
+ }
66
+ }
67
+
44
68
impl fmt:: Display for Error {
45
69
fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> fmt:: Result {
46
70
match self {
@@ -369,20 +393,12 @@ mod tests {
369
393
370
394
type MmapRegion = super :: MmapRegion < ( ) > ;
371
395
372
- // Adding a helper method to extract the errno within an Error::Mmap(e), or return a
373
- // distinctive value when the error is represented by another variant.
374
- impl Error {
375
- pub fn raw_os_error ( & self ) -> i32 {
376
- match self {
377
- Error :: Mmap ( e) => e. raw_os_error ( ) . unwrap ( ) ,
378
- _ => std:: i32:: MIN ,
379
- }
380
- }
381
- }
382
-
383
396
#[ test]
384
397
fn test_mmap_region_new ( ) {
385
- assert ! ( MmapRegion :: new( 0 ) . is_err( ) ) ;
398
+ assert_eq ! (
399
+ MmapRegion :: new( 0 ) . unwrap_err( ) ,
400
+ Error :: Mmap ( std:: io:: Error :: from_raw_os_error( libc:: EINVAL ) )
401
+ ) ;
386
402
387
403
let size = 4096 ;
388
404
@@ -398,7 +414,10 @@ mod tests {
398
414
399
415
#[ test]
400
416
fn test_mmap_region_set_hugetlbfs ( ) {
401
- assert ! ( MmapRegion :: new( 0 ) . is_err( ) ) ;
417
+ assert_eq ! (
418
+ MmapRegion :: new( 0 ) . unwrap_err( ) ,
419
+ Error :: Mmap ( std:: io:: Error :: from_raw_os_error( libc:: EINVAL ) )
420
+ ) ;
402
421
403
422
let size = 4096 ;
404
423
@@ -469,7 +488,7 @@ mod tests {
469
488
prot,
470
489
flags,
471
490
) ;
472
- assert_eq ! ( format! ( "{:?}" , r. unwrap_err( ) ) , " InvalidOffsetLength" ) ;
491
+ assert_eq ! ( r. unwrap_err( ) , Error :: InvalidOffsetLength ) ;
473
492
474
493
// Offset + size is greater than the size of the file (which is 0 at this point).
475
494
let r = MmapRegion :: build (
@@ -478,7 +497,7 @@ mod tests {
478
497
prot,
479
498
flags,
480
499
) ;
481
- assert_eq ! ( format! ( "{:?}" , r. unwrap_err( ) ) , " MappingPastEof" ) ;
500
+ assert_eq ! ( r. unwrap_err( ) , Error :: MappingPastEof ) ;
482
501
483
502
// MAP_FIXED was specified among the flags.
484
503
let r = MmapRegion :: build (
@@ -487,7 +506,7 @@ mod tests {
487
506
prot,
488
507
flags | libc:: MAP_FIXED ,
489
508
) ;
490
- assert_eq ! ( format! ( "{:?}" , r. unwrap_err( ) ) , " MapFixed" ) ;
509
+ assert_eq ! ( r. unwrap_err( ) , Error :: MapFixed ) ;
491
510
492
511
// Let's resize the file.
493
512
assert_eq ! ( unsafe { libc:: ftruncate( a. as_raw_fd( ) , 1024 * 10 ) } , 0 ) ;
@@ -499,7 +518,10 @@ mod tests {
499
518
prot,
500
519
flags,
501
520
) ;
502
- assert_eq ! ( r. unwrap_err( ) . raw_os_error( ) , libc:: EINVAL ) ;
521
+ assert_eq ! (
522
+ r. unwrap_err( ) ,
523
+ Error :: Mmap ( std:: io:: Error :: from_raw_os_error( libc:: EINVAL ) )
524
+ ) ;
503
525
504
526
// The build should be successful now.
505
527
let r =
@@ -520,10 +542,10 @@ mod tests {
520
542
let flags = libc:: MAP_NORESERVE | libc:: MAP_PRIVATE ;
521
543
522
544
let r = unsafe { MmapRegion :: build_raw ( ( addr + 1 ) as * mut u8 , size, prot, flags) } ;
523
- assert_eq ! ( format! ( "{:?}" , r. unwrap_err( ) ) , " InvalidPointer" ) ;
545
+ assert_eq ! ( r. unwrap_err( ) , Error :: InvalidPointer ) ;
524
546
525
547
let r = unsafe { MmapRegion :: build_raw ( addr as * mut u8 , size + 1 , prot, flags) } ;
526
- assert_eq ! ( format! ( "{:?}" , r. unwrap_err( ) ) , " InvalidSize" ) ;
548
+ assert_eq ! ( r. unwrap_err( ) , Error :: InvalidSize ) ;
527
549
528
550
let r = unsafe { MmapRegion :: build_raw ( addr as * mut u8 , size, prot, flags) . unwrap ( ) } ;
529
551
0 commit comments