Skip to content

Commit 03a0645

Browse files
[mmap_unix]: Implement PartialEq for Error enum
Implemented PartialEq for Error enum. Considering that from this file the only io error returned are OS error usage of raw_os_error offers anything that is needed. Updated tests to use PartialEq for error cases. Signed-off-by: Alexandru Cihodaru <[email protected]>
1 parent 5bd7138 commit 03a0645

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/mmap_unix.rs

+41-19
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ pub enum Error {
4141
Mmap(io::Error),
4242
}
4343

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+
4468
impl fmt::Display for Error {
4569
fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result {
4670
match self {
@@ -369,20 +393,12 @@ mod tests {
369393

370394
type MmapRegion = super::MmapRegion<()>;
371395

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-
383396
#[test]
384397
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+
);
386402

387403
let size = 4096;
388404

@@ -398,7 +414,10 @@ mod tests {
398414

399415
#[test]
400416
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+
);
402421

403422
let size = 4096;
404423

@@ -469,7 +488,7 @@ mod tests {
469488
prot,
470489
flags,
471490
);
472-
assert_eq!(format!("{:?}", r.unwrap_err()), "InvalidOffsetLength");
491+
assert_eq!(r.unwrap_err(), Error::InvalidOffsetLength);
473492

474493
// Offset + size is greater than the size of the file (which is 0 at this point).
475494
let r = MmapRegion::build(
@@ -478,7 +497,7 @@ mod tests {
478497
prot,
479498
flags,
480499
);
481-
assert_eq!(format!("{:?}", r.unwrap_err()), "MappingPastEof");
500+
assert_eq!(r.unwrap_err(), Error::MappingPastEof);
482501

483502
// MAP_FIXED was specified among the flags.
484503
let r = MmapRegion::build(
@@ -487,7 +506,7 @@ mod tests {
487506
prot,
488507
flags | libc::MAP_FIXED,
489508
);
490-
assert_eq!(format!("{:?}", r.unwrap_err()), "MapFixed");
509+
assert_eq!(r.unwrap_err(), Error::MapFixed);
491510

492511
// Let's resize the file.
493512
assert_eq!(unsafe { libc::ftruncate(a.as_raw_fd(), 1024 * 10) }, 0);
@@ -499,7 +518,10 @@ mod tests {
499518
prot,
500519
flags,
501520
);
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+
);
503525

504526
// The build should be successful now.
505527
let r =
@@ -520,10 +542,10 @@ mod tests {
520542
let flags = libc::MAP_NORESERVE | libc::MAP_PRIVATE;
521543

522544
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);
524546

525547
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);
527549

528550
let r = unsafe { MmapRegion::build_raw(addr as *mut u8, size, prot, flags).unwrap() };
529551

0 commit comments

Comments
 (0)