Skip to content

Commit bc0f7c2

Browse files
defunctiojiangliu
authored andcommitted
Deprecate GuestMemory stopgap "iterator" methods
* Deprecates `with_regions` `with_regions_mut` and `map_and_fold`. * Update unit tests Signed-off-by: defunct <[email protected]> Resolves #133
1 parent 7ae7ab3 commit bc0f7c2

File tree

5 files changed

+32
-39
lines changed

5 files changed

+32
-39
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Changelog
2+
## [Unreleased]
3+
4+
### Deprecated
5+
6+
- [[#133]](https://github.com/rust-vmm/vm-memory/issues/8): Deprecate `GuestMemory::with_regions()`,
7+
`GuestMemory::with_regions_mut()`, `GuestMemory::map_and_fold()`
28

39
## [v0.5.0]
410

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 85.6,
2+
"coverage_score": 85.4,
33
"exclude_path": "mmap_windows.rs",
44
"crate_features": "backend-mmap,backend-atomic"
55
}

src/atomic.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ impl<M: GuestMemory> GuestMemoryExclusiveGuard<'_, M> {
143143
mod tests {
144144
use super::*;
145145
use crate::{
146-
GuestAddress, GuestMemory, GuestMemoryMmap, GuestMemoryRegion, GuestMemoryResult,
147-
GuestRegionMmap, GuestUsize, MmapRegion,
146+
GuestAddress, GuestMemory, GuestMemoryMmap, GuestMemoryRegion, GuestRegionMmap, GuestUsize,
147+
MmapRegion,
148148
};
149149

150150
type GuestMemoryMmapAtomic = GuestMemoryAtomic<GuestMemoryMmap>;
@@ -161,16 +161,13 @@ mod tests {
161161
let gm = GuestMemoryMmapAtomic::new(gmm);
162162
let mem = gm.memory();
163163

164-
let res: GuestMemoryResult<()> = mem.with_regions(|_, region| {
164+
for region in mem.iter() {
165165
assert_eq!(region.len(), region_size as GuestUsize);
166-
Ok(())
167-
});
168-
assert!(res.is_ok());
169-
let res: GuestMemoryResult<()> = mem.with_regions_mut(|_, region| {
166+
}
167+
168+
for region in mem.iter() {
170169
iterated_regions.push((region.start_addr(), region.len() as usize));
171-
Ok(())
172-
});
173-
assert!(res.is_ok());
170+
}
174171
assert_eq!(regions, iterated_regions);
175172
assert_eq!(mem.num_regions(), 2);
176173
assert!(mem.find_region(GuestAddress(0x1000)).is_some());
@@ -182,13 +179,9 @@ mod tests {
182179
.eq(iterated_regions.iter().copied()));
183180

184181
let mem2 = mem.into_inner();
185-
let res: GuestMemoryResult<()> = mem2.with_regions(|_, region| {
182+
for region in mem2.iter() {
186183
assert_eq!(region.len(), region_size as GuestUsize);
187-
Ok(())
188-
});
189-
assert!(res.is_ok());
190-
let res: GuestMemoryResult<()> = mem2.with_regions_mut(|_, _| Ok(()));
191-
assert!(res.is_ok());
184+
}
192185
assert_eq!(mem2.num_regions(), 2);
193186
assert!(mem2.find_region(GuestAddress(0x1000)).is_some());
194187
assert!(mem2.find_region(GuestAddress(0x10000)).is_none());
@@ -199,13 +192,9 @@ mod tests {
199192
.eq(iterated_regions.iter().copied()));
200193

201194
let mem3 = mem2.memory();
202-
let res: GuestMemoryResult<()> = mem3.with_regions(|_, region| {
195+
for region in mem3.iter() {
203196
assert_eq!(region.len(), region_size as GuestUsize);
204-
Ok(())
205-
});
206-
assert!(res.is_ok());
207-
let res: GuestMemoryResult<()> = mem3.with_regions_mut(|_, _| Ok(()));
208-
assert!(res.is_ok());
197+
}
209198
assert_eq!(mem3.num_regions(), 2);
210199
assert!(mem3.find_region(GuestAddress(0x1000)).is_some());
211200
assert!(mem3.find_region(GuestAddress(0x10000)).is_none());

src/guest_memory.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ pub trait GuestMemory {
473473
/// Perform the specified action on each region.
474474
///
475475
/// It only walks children of current region and does not step into sub regions.
476+
#[deprecated(since = "0.6.0", note = "Use `.iter()` instead")]
476477
fn with_regions<F, E>(&self, cb: F) -> std::result::Result<(), E>
477478
where
478479
F: Fn(usize, &Self::R) -> std::result::Result<(), E>,
@@ -486,6 +487,7 @@ pub trait GuestMemory {
486487
/// Perform the specified action on each region mutably.
487488
///
488489
/// It only walks children of current region and does not step into sub regions.
490+
#[deprecated(since = "0.6.0", note = "Use `.iter()` instead")]
489491
fn with_regions_mut<F, E>(&self, mut cb: F) -> std::result::Result<(), E>
490492
where
491493
F: FnMut(usize, &Self::R) -> std::result::Result<(), E>,
@@ -554,6 +556,7 @@ pub trait GuestMemory {
554556
/// assert_eq!(3, total_size)
555557
/// # }
556558
/// ```
559+
#[deprecated(since = "0.6.0", note = "Use `.iter()` instead")]
557560
fn map_and_fold<F, G, T>(&self, init: T, mapf: F, foldf: G) -> T
558561
where
559562
F: Fn((usize, &Self::R)) -> T,

src/mmap.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,16 +1196,14 @@ mod tests {
11961196
];
11971197
let mut iterated_regions = Vec::new();
11981198
let gm = GuestMemoryMmap::from_ranges(&regions).unwrap();
1199-
let res: guest_memory::Result<()> = gm.with_regions(|_, region| {
1199+
1200+
for region in gm.iter() {
12001201
assert_eq!(region.len(), region_size as GuestUsize);
1201-
Ok(())
1202-
});
1203-
assert!(res.is_ok());
1204-
let res: guest_memory::Result<()> = gm.with_regions_mut(|_, region| {
1202+
}
1203+
1204+
for region in gm.iter() {
12051205
iterated_regions.push((region.start_addr(), region.len() as usize));
1206-
Ok(())
1207-
});
1208-
assert!(res.is_ok());
1206+
}
12091207
assert_eq!(regions, iterated_regions);
12101208

12111209
assert!(regions
@@ -1228,16 +1226,13 @@ mod tests {
12281226
let gm = Arc::new(GuestMemoryMmap::from_ranges(&regions).unwrap());
12291227
let mem = gm.memory();
12301228

1231-
let res: guest_memory::Result<()> = mem.with_regions(|_, region| {
1229+
for region in mem.iter() {
12321230
assert_eq!(region.len(), region_size as GuestUsize);
1233-
Ok(())
1234-
});
1235-
assert!(res.is_ok());
1236-
let res: guest_memory::Result<()> = mem.with_regions_mut(|_, region| {
1231+
}
1232+
1233+
for region in mem.iter() {
12371234
iterated_regions.push((region.start_addr(), region.len() as usize));
1238-
Ok(())
1239-
});
1240-
assert!(res.is_ok());
1235+
}
12411236
assert_eq!(regions, iterated_regions);
12421237

12431238
assert!(regions

0 commit comments

Comments
 (0)