Skip to content

Commit 80f6dc8

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 4775dd7 commit 80f6dc8

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/mmap_unix.rs

+34-18
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ pub enum Error {
4343
SeekStart(io::Error),
4444
}
4545

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+
}
4664
impl fmt::Display for Error {
4765
fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result {
4866
match self {
@@ -467,20 +485,12 @@ mod tests {
467485

468486
type MmapRegion = super::MmapRegion<()>;
469487

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-
481488
#[test]
482489
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+
);
484494

485495
let size = 4096;
486496

@@ -496,7 +506,10 @@ mod tests {
496506

497507
#[test]
498508
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+
);
500513

501514
let size = 4096;
502515

@@ -567,7 +580,7 @@ mod tests {
567580
prot,
568581
flags,
569582
);
570-
assert_eq!(format!("{:?}", r.unwrap_err()), "InvalidOffsetLength");
583+
assert_eq!(r.unwrap_err(), Error::InvalidOffsetLength);
571584

572585
// Offset + size is greater than the size of the file (which is 0 at this point).
573586
let r = MmapRegion::build(
@@ -576,7 +589,7 @@ mod tests {
576589
prot,
577590
flags,
578591
);
579-
assert_eq!(format!("{:?}", r.unwrap_err()), "MappingPastEof");
592+
assert_eq!(r.unwrap_err(), Error::MappingPastEof);
580593

581594
// MAP_FIXED was specified among the flags.
582595
let r = MmapRegion::build(
@@ -585,7 +598,7 @@ mod tests {
585598
prot,
586599
flags | libc::MAP_FIXED,
587600
);
588-
assert_eq!(format!("{:?}", r.unwrap_err()), "MapFixed");
601+
assert_eq!(r.unwrap_err(), Error::MapFixed);
589602

590603
// Let's resize the file.
591604
assert_eq!(unsafe { libc::ftruncate(a.as_raw_fd(), 1024 * 10) }, 0);
@@ -597,7 +610,10 @@ mod tests {
597610
prot,
598611
flags,
599612
);
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+
);
601617

602618
// The build should be successful now.
603619
let r =
@@ -629,7 +645,7 @@ mod tests {
629645
let flags = libc::MAP_NORESERVE | libc::MAP_PRIVATE;
630646

631647
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);
633649

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

0 commit comments

Comments
 (0)