Skip to content

Commit c1f9a01

Browse files
committed
add naive default impl for GuestMemory::num_regions
Similar to find_region(), we can provide an inefficient default impl to make downstream impls of the trait less verbose in scenarios that really do not care (e.g. mock impls for tests). Signed-off-by: Patrick Roy <[email protected]>
1 parent 15606ea commit c1f9a01

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
- \[[#307](https://github.com/rust-vmm/vm-memory/pull/304)\] Move `read_volatile_from`, `read_exact_volatile_from`,
1212
`write_volatile_to` and `write_all_volatile_to` functions from the `GuestMemory` trait to the `Bytes` trait.
13-
- \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\]: Give `GuestMemory::find_region` a default implementation,
14-
based on linear search.
13+
- \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\]: Give `GuestMemory::find_region` and `GuestMemory::num_regions`
14+
a default implementation, based on linear search.
1515

1616
- \[#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()`.
1717

src/guest_memory.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,9 @@ pub trait GuestMemory {
405405
type R: GuestMemoryRegion;
406406

407407
/// Returns the number of regions in the collection.
408-
fn num_regions(&self) -> usize;
408+
fn num_regions(&self) -> usize {
409+
self.iter().count()
410+
}
409411

410412
/// Returns the region containing the specified address or `None`.
411413
fn find_region(&self, addr: GuestAddress) -> Option<&Self::R> {

0 commit comments

Comments
 (0)