Skip to content

Commit 091a329

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 <[email protected]>
1 parent 14de7aa commit 091a329

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
a default implementation, based on linear search.
1717
- \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\]: Provide a marker trait, `GuestMemoryRegionBytes`, which enables a default implementation of `Bytes<MemoryRegionAddress>`
1818
for a `GuestMemoryRegion` if implemented.
19-
19+
- \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\]: Adjust error types returned from `GuestMemoryMmap::from_ranges[_with_files]`
20+
and `GuestRegionMmap::from_range` to be separate from the error type returned by `GuestRegionCollection` functions.
21+
Change return type of `GuestRegionMmap::new` from `Result` to `Option`.
2022
- \[#324](https:////github.com/rust-vmm/vm-memory/pull/324)\] `GuestMemoryRegion::bitmap()` now returns a `BitmapSlice`. Accessing the full bitmap is now possible only if the type of the memory region is know, for example with `MmapRegion::bitmap()`.
2123

2224
### Removed

src/mmap/mod.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ 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
74+
.0
75+
.checked_add(mapping.size() as u64)
76+
.map(|_| Self {
77+
mapping,
78+
guest_base,
79+
})
7980
}
8081
}
8182

@@ -94,7 +95,7 @@ impl<B: NewBitmap> GuestRegionMmap<B> {
9495
}
9596
.map_err(Error::MmapRegion)?;
9697

97-
Self::new(region, addr)
98+
Self::new(region, addr).ok_or(Error::InvalidGuestRegion)
9899
}
99100
}
100101

@@ -110,7 +111,7 @@ impl<B: NewBitmap> GuestRegionMmap<B> {
110111
let range = MmapRange::new_unix(size, file, addr);
111112

112113
let region = MmapRegion::from_range(range).map_err(Error::MmapRegion)?;
113-
Self::new(region, addr)
114+
Self::new(region, addr).ok_or(Error::InvalidGuestRegion)
114115
}
115116
}
116117

0 commit comments

Comments
 (0)