Skip to content

Commit 65ada43

Browse files
committed
refactor: Have GuestRegionMmap::new return Option
There is only a single error condition, so we can indicate the error case with an Option. First step towards untangling the region specific errors from the region collection specific errors. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
1 parent dbc363e commit 65ada43

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/mmap/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ impl<B> Deref for GuestRegionMmap<B> {
6767

6868
impl<B: Bitmap> GuestRegionMmap<B> {
6969
/// Create a new memory-mapped memory region for the guest's physical memory.
70-
pub fn new(mapping: MmapRegion<B>, guest_base: GuestAddress) -> result::Result<Self, Error> {
71-
if guest_base.0.checked_add(mapping.size() as u64).is_none() {
72-
return Err(Error::InvalidGuestRegion);
73-
}
74-
75-
Ok(GuestRegionMmap {
76-
mapping,
77-
guest_base,
78-
})
70+
///
71+
/// Returns `None` if guest_base + mapping.len() would overflow.
72+
pub fn new(mapping: MmapRegion<B>, guest_base: GuestAddress) -> Option<Self> {
73+
guest_base.0.checked_add(mapping.size() as u64)
74+
.map(|_| Self { mapping, guest_base })
7975
}
8076
}
8177

@@ -95,6 +91,7 @@ impl<B: NewBitmap> GuestRegionMmap<B> {
9591
.map_err(Error::MmapRegion)?;
9692

9793
Self::new(region, addr)
94+
.ok_or(Error::InvalidGuestRegion)
9895
}
9996
}
10097

@@ -111,6 +108,7 @@ impl<B: NewBitmap> GuestRegionMmap<B> {
111108

112109
let region = MmapRegion::from_range(range).map_err(Error::MmapRegion)?;
113110
Self::new(region, addr)
111+
.ok_or(Error::InvalidGuestRegion)
114112
}
115113
}
116114

0 commit comments

Comments
 (0)