Skip to content

Commit 805a232

Browse files
committed
Remove GuestMemoryIterator, replace with GAT
Use an implicit generic associated type to realize GuestMemory::iter(). This removes the pre-GAT workaround used here to get an iterator that borrows values with the same lifetime as the `&self` parameter to `.iter()`. Signed-off-by: Patrick Roy <[email protected]>
1 parent 29e165e commit 805a232

File tree

3 files changed

+9
-63
lines changed

3 files changed

+9
-63
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Upcoming version
44

5+
### Changed
6+
7+
- [[#278](https://github.com/rust-vmm/vm-memory/pull/278) Remove `GuestMemoryIterator` trait,
8+
and instead have GuestMemory::iter() return `impl Iterator`.
9+
510
## [v0.15.0]
611

712
### Added

src/guest_memory.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -422,42 +422,6 @@ impl<M: GuestMemory> GuestAddressSpace for Arc<M> {
422422
}
423423
}
424424

425-
/// Lifetime generic associated iterators. The actual iterator type is defined through associated
426-
/// item `Iter`, for example:
427-
///
428-
/// ```
429-
/// # use std::marker::PhantomData;
430-
/// # use vm_memory::guest_memory::GuestMemoryIterator;
431-
/// #
432-
/// // Declare the relevant Region and Memory types
433-
/// struct MyGuestRegion {/* fields omitted */}
434-
/// struct MyGuestMemory {/* fields omitted */}
435-
///
436-
/// // Make an Iterator type to iterate over the Regions
437-
/// # /*
438-
/// struct MyGuestMemoryIter<'a> {/* fields omitted */}
439-
/// # */
440-
/// # struct MyGuestMemoryIter<'a> {
441-
/// # _marker: PhantomData<&'a MyGuestRegion>,
442-
/// # }
443-
/// impl<'a> Iterator for MyGuestMemoryIter<'a> {
444-
/// type Item = &'a MyGuestRegion;
445-
/// fn next(&mut self) -> Option<&'a MyGuestRegion> {
446-
/// // ...
447-
/// # None
448-
/// }
449-
/// }
450-
///
451-
/// // Associate the Iter type with the Memory type
452-
/// impl<'a> GuestMemoryIterator<'a, MyGuestRegion> for MyGuestMemory {
453-
/// type Iter = MyGuestMemoryIter<'a>;
454-
/// }
455-
/// ```
456-
pub trait GuestMemoryIterator<'a, R: 'a> {
457-
/// Type of the `iter` method's return value.
458-
type Iter: Iterator<Item = &'a R>;
459-
}
460-
461425
/// `GuestMemory` represents a container for an *immutable* collection of
462426
/// `GuestMemoryRegion` objects. `GuestMemory` provides the `Bytes<GuestAddress>`
463427
/// trait to hide the details of accessing guest memory by physical address.
@@ -471,9 +435,6 @@ pub trait GuestMemory {
471435
/// Type of objects hosted by the address space.
472436
type R: GuestMemoryRegion;
473437

474-
/// Lifetime generic associated iterators. Usually this is just `Self`.
475-
type I: for<'a> GuestMemoryIterator<'a, Self::R>;
476-
477438
/// Returns the number of regions in the collection.
478439
fn num_regions(&self) -> usize;
479440

@@ -533,7 +494,7 @@ pub trait GuestMemory {
533494
/// assert_eq!(3, total_size)
534495
/// # }
535496
/// ```
536-
fn iter(&self) -> <Self::I as GuestMemoryIterator<Self::R>>::Iter;
497+
fn iter(&self) -> impl Iterator<Item = &Self::R>;
537498

538499
/// Applies two functions, specified as callbacks, on the inner memory regions.
539500
///

src/mmap.rs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ use std::sync::Arc;
2424
use crate::address::Address;
2525
use crate::bitmap::{Bitmap, BS};
2626
use crate::guest_memory::{
27-
self, FileOffset, GuestAddress, GuestMemory, GuestMemoryIterator, GuestMemoryRegion,
28-
GuestUsize, MemoryRegionAddress,
27+
self, FileOffset, GuestAddress, GuestMemory, GuestMemoryRegion, GuestUsize, MemoryRegionAddress,
2928
};
3029
use crate::volatile_memory::{VolatileMemory, VolatileSlice};
3130
use crate::{AtomicAccess, Bytes};
@@ -613,28 +612,9 @@ impl<B: Bitmap> GuestMemoryMmap<B> {
613612
}
614613
}
615614

616-
/// An iterator over the elements of `GuestMemoryMmap`.
617-
///
618-
/// This struct is created by `GuestMemory::iter()`. See its documentation for more.
619-
#[derive(Debug)]
620-
pub struct Iter<'a, B>(std::slice::Iter<'a, Arc<GuestRegionMmap<B>>>);
621-
622-
impl<'a, B> Iterator for Iter<'a, B> {
623-
type Item = &'a GuestRegionMmap<B>;
624-
fn next(&mut self) -> Option<Self::Item> {
625-
self.0.next().map(AsRef::as_ref)
626-
}
627-
}
628-
629-
impl<'a, B: 'a> GuestMemoryIterator<'a, GuestRegionMmap<B>> for GuestMemoryMmap<B> {
630-
type Iter = Iter<'a, B>;
631-
}
632-
633615
impl<B: Bitmap + 'static> GuestMemory for GuestMemoryMmap<B> {
634616
type R = GuestRegionMmap<B>;
635617

636-
type I = Self;
637-
638618
fn num_regions(&self) -> usize {
639619
self.regions.len()
640620
}
@@ -649,8 +629,8 @@ impl<B: Bitmap + 'static> GuestMemory for GuestMemoryMmap<B> {
649629
index.map(|x| self.regions[x].as_ref())
650630
}
651631

652-
fn iter(&self) -> Iter<B> {
653-
Iter(self.regions.iter())
632+
fn iter(&self) -> impl Iterator<Item = &Self::R> {
633+
self.regions.iter().map(AsRef::as_ref)
654634
}
655635
}
656636

0 commit comments

Comments
 (0)