@@ -43,6 +43,24 @@ pub enum Error {
43
43
SeekStart ( io:: Error ) ,
44
44
}
45
45
46
+ impl PartialEq for Error {
47
+ fn eq ( & self , other : & Self ) -> bool {
48
+ match ( self , other) {
49
+ // error.kind should be enough to assert equallity because each error
50
+ // has the kind field set and the OS error numbers can be converted
51
+ // to ErrorKind through `sys::decode_error_kind`.
52
+ ( Error :: Mmap ( left) , Error :: Mmap ( right) )
53
+ | ( Error :: SeekEnd ( left) , Error :: SeekEnd ( right) )
54
+ | ( Error :: SeekStart ( left) , Error :: SeekStart ( right) ) => left. kind ( ) == right. kind ( ) ,
55
+ ( Error :: InvalidOffsetLength , Error :: InvalidOffsetLength ) => true ,
56
+ ( Error :: InvalidPointer , Error :: InvalidPointer ) => true ,
57
+ ( Error :: MapFixed , Error :: MapFixed ) => true ,
58
+ ( Error :: MappingOverlap , Error :: MappingOverlap ) => true ,
59
+ ( Error :: MappingPastEof , Error :: MappingPastEof ) => true ,
60
+ _ => false ,
61
+ }
62
+ }
63
+ }
46
64
impl fmt:: Display for Error {
47
65
fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> fmt:: Result {
48
66
match self {
@@ -467,20 +485,12 @@ mod tests {
467
485
468
486
type MmapRegion = super :: MmapRegion < ( ) > ;
469
487
470
- // Adding a helper method to extract the errno within an Error::Mmap(e), or return a
471
- // distinctive value when the error is represented by another variant.
472
- impl Error {
473
- pub fn raw_os_error ( & self ) -> i32 {
474
- match self {
475
- Error :: Mmap ( e) => e. raw_os_error ( ) . unwrap ( ) ,
476
- _ => std:: i32:: MIN ,
477
- }
478
- }
479
- }
480
-
481
488
#[ test]
482
489
fn test_mmap_region_new ( ) {
483
- assert ! ( MmapRegion :: new( 0 ) . is_err( ) ) ;
490
+ assert_eq ! (
491
+ MmapRegion :: new( 0 ) . unwrap_err( ) ,
492
+ Error :: Mmap ( std:: io:: Error :: from_raw_os_error( libc:: EINVAL ) )
493
+ ) ;
484
494
485
495
let size = 4096 ;
486
496
@@ -496,7 +506,10 @@ mod tests {
496
506
497
507
#[ test]
498
508
fn test_mmap_region_set_hugetlbfs ( ) {
499
- assert ! ( MmapRegion :: new( 0 ) . is_err( ) ) ;
509
+ assert_eq ! (
510
+ MmapRegion :: new( 0 ) . unwrap_err( ) ,
511
+ Error :: Mmap ( std:: io:: Error :: from_raw_os_error( libc:: EINVAL ) )
512
+ ) ;
500
513
501
514
let size = 4096 ;
502
515
@@ -567,7 +580,7 @@ mod tests {
567
580
prot,
568
581
flags,
569
582
) ;
570
- assert_eq ! ( format! ( "{:?}" , r. unwrap_err( ) ) , " InvalidOffsetLength" ) ;
583
+ assert_eq ! ( r. unwrap_err( ) , Error :: InvalidOffsetLength ) ;
571
584
572
585
// Offset + size is greater than the size of the file (which is 0 at this point).
573
586
let r = MmapRegion :: build (
@@ -576,7 +589,7 @@ mod tests {
576
589
prot,
577
590
flags,
578
591
) ;
579
- assert_eq ! ( format! ( "{:?}" , r. unwrap_err( ) ) , " MappingPastEof" ) ;
592
+ assert_eq ! ( r. unwrap_err( ) , Error :: MappingPastEof ) ;
580
593
581
594
// MAP_FIXED was specified among the flags.
582
595
let r = MmapRegion :: build (
@@ -585,7 +598,7 @@ mod tests {
585
598
prot,
586
599
flags | libc:: MAP_FIXED ,
587
600
) ;
588
- assert_eq ! ( format! ( "{:?}" , r. unwrap_err( ) ) , " MapFixed" ) ;
601
+ assert_eq ! ( r. unwrap_err( ) , Error :: MapFixed ) ;
589
602
590
603
// Let's resize the file.
591
604
assert_eq ! ( unsafe { libc:: ftruncate( a. as_raw_fd( ) , 1024 * 10 ) } , 0 ) ;
@@ -597,7 +610,10 @@ mod tests {
597
610
prot,
598
611
flags,
599
612
) ;
600
- assert_eq ! ( r. unwrap_err( ) . raw_os_error( ) , libc:: EINVAL ) ;
613
+ assert_eq ! (
614
+ r. unwrap_err( ) ,
615
+ Error :: Mmap ( std:: io:: Error :: from_raw_os_error( libc:: EINVAL ) )
616
+ ) ;
601
617
602
618
// The build should be successful now.
603
619
let r =
@@ -629,7 +645,7 @@ mod tests {
629
645
let flags = libc:: MAP_NORESERVE | libc:: MAP_PRIVATE ;
630
646
631
647
let r = unsafe { MmapRegion :: build_raw ( ( addr + 1 ) as * mut u8 , size, prot, flags) } ;
632
- assert_eq ! ( format! ( "{:?}" , r. unwrap_err( ) ) , " InvalidPointer" ) ;
648
+ assert_eq ! ( r. unwrap_err( ) , Error :: InvalidPointer ) ;
633
649
634
650
let r = unsafe { MmapRegion :: build_raw ( addr as * mut u8 , size, prot, flags) . unwrap ( ) } ;
635
651
0 commit comments