@@ -169,18 +169,9 @@ pub trait GuestMemoryRegion: Bytes<MemoryRegionAddress, E = GuestMemoryError> {
169
169
}
170
170
}
171
171
172
- /// Errors that can occur when dealing with [`GuestRegion `]s, or collections thereof
172
+ /// Errors that can occur when dealing with [`GuestRegionCollection `]s
173
173
#[ derive( Debug , thiserror:: Error ) ]
174
- pub enum GuestRegionError {
175
- /// Adding the guest base address to the length of the underlying mapping resulted
176
- /// in an overflow.
177
- #[ error( "Adding the guest base address to the length of the underlying mapping resulted in an overflow" ) ]
178
- #[ cfg( feature = "backend-mmap" ) ]
179
- InvalidGuestRegion ,
180
- /// Error creating a `MmapRegion` object.
181
- #[ error( "{0}" ) ]
182
- #[ cfg( feature = "backend-mmap" ) ]
183
- MmapRegion ( crate :: mmap:: MmapRegionError ) ,
174
+ pub enum GuestRegionCollectionError {
184
175
/// No memory region found.
185
176
#[ error( "No memory region found" ) ]
186
177
NoMemoryRegion ,
@@ -230,7 +221,9 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
230
221
/// * `regions` - The vector of regions.
231
222
/// The regions shouldn't overlap, and they should be sorted
232
223
/// by the starting address.
233
- pub fn from_regions ( mut regions : Vec < R > ) -> std:: result:: Result < Self , GuestRegionError > {
224
+ pub fn from_regions (
225
+ mut regions : Vec < R > ,
226
+ ) -> std:: result:: Result < Self , GuestRegionCollectionError > {
234
227
Self :: from_arc_regions ( regions. drain ( ..) . map ( Arc :: new) . collect ( ) )
235
228
}
236
229
@@ -246,21 +239,23 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
246
239
/// * `regions` - The vector of `Arc` regions.
247
240
/// The regions shouldn't overlap and they should be sorted
248
241
/// by the starting address.
249
- pub fn from_arc_regions ( regions : Vec < Arc < R > > ) -> std:: result:: Result < Self , GuestRegionError > {
242
+ pub fn from_arc_regions (
243
+ regions : Vec < Arc < R > > ,
244
+ ) -> std:: result:: Result < Self , GuestRegionCollectionError > {
250
245
if regions. is_empty ( ) {
251
- return Err ( GuestRegionError :: NoMemoryRegion ) ;
246
+ return Err ( GuestRegionCollectionError :: NoMemoryRegion ) ;
252
247
}
253
248
254
249
for window in regions. windows ( 2 ) {
255
250
let prev = & window[ 0 ] ;
256
251
let next = & window[ 1 ] ;
257
252
258
253
if prev. start_addr ( ) > next. start_addr ( ) {
259
- return Err ( GuestRegionError :: UnsortedMemoryRegions ) ;
254
+ return Err ( GuestRegionCollectionError :: UnsortedMemoryRegions ) ;
260
255
}
261
256
262
257
if prev. last_addr ( ) >= next. start_addr ( ) {
263
- return Err ( GuestRegionError :: MemoryRegionOverlap ) ;
258
+ return Err ( GuestRegionCollectionError :: MemoryRegionOverlap ) ;
264
259
}
265
260
}
266
261
@@ -274,7 +269,7 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
274
269
pub fn insert_region (
275
270
& self ,
276
271
region : Arc < R > ,
277
- ) -> std:: result:: Result < GuestRegionCollection < R > , GuestRegionError > {
272
+ ) -> std:: result:: Result < GuestRegionCollection < R > , GuestRegionCollectionError > {
278
273
let mut regions = self . regions . clone ( ) ;
279
274
regions. push ( region) ;
280
275
regions. sort_by_key ( |x| x. start_addr ( ) ) ;
@@ -292,7 +287,7 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
292
287
& self ,
293
288
base : GuestAddress ,
294
289
size : GuestUsize ,
295
- ) -> std:: result:: Result < ( GuestRegionCollection < R > , Arc < R > ) , GuestRegionError > {
290
+ ) -> std:: result:: Result < ( GuestRegionCollection < R > , Arc < R > ) , GuestRegionCollectionError > {
296
291
if let Ok ( region_index) = self . regions . binary_search_by_key ( & base, |x| x. start_addr ( ) ) {
297
292
if self . regions . get ( region_index) . unwrap ( ) . len ( ) == size {
298
293
let mut regions = self . regions . clone ( ) ;
@@ -301,7 +296,7 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
301
296
}
302
297
}
303
298
304
- Err ( GuestRegionError :: NoMemoryRegion )
299
+ Err ( GuestRegionCollectionError :: NoMemoryRegion )
305
300
}
306
301
}
307
302
@@ -479,7 +474,7 @@ impl<R: GuestMemoryRegionBytes> Bytes<MemoryRegionAddress> for R {
479
474
480
475
#[ cfg( test) ]
481
476
pub ( crate ) mod tests {
482
- use crate :: region:: { GuestMemoryRegionBytes , GuestRegionError } ;
477
+ use crate :: region:: { GuestMemoryRegionBytes , GuestRegionCollectionError } ;
483
478
use crate :: {
484
479
Address , GuestAddress , GuestMemory , GuestMemoryRegion , GuestRegionCollection , GuestUsize ,
485
480
} ;
@@ -510,7 +505,7 @@ pub(crate) mod tests {
510
505
pub ( crate ) type Collection = GuestRegionCollection < MockRegion > ;
511
506
512
507
fn check_guest_memory_mmap (
513
- maybe_guest_mem : Result < Collection , GuestRegionError > ,
508
+ maybe_guest_mem : Result < Collection , GuestRegionCollectionError > ,
514
509
expected_regions_summary : & [ ( GuestAddress , u64 ) ] ,
515
510
) {
516
511
assert ! ( maybe_guest_mem. is_ok( ) ) ;
@@ -537,7 +532,7 @@ pub(crate) mod tests {
537
532
538
533
pub ( crate ) fn new_guest_memory_collection_from_regions (
539
534
regions_summary : & [ ( GuestAddress , u64 ) ] ,
540
- ) -> Result < Collection , GuestRegionError > {
535
+ ) -> Result < Collection , GuestRegionCollectionError > {
541
536
Collection :: from_regions (
542
537
regions_summary
543
538
. iter ( )
@@ -548,7 +543,7 @@ pub(crate) mod tests {
548
543
549
544
fn new_guest_memory_collection_from_arc_regions (
550
545
regions_summary : & [ ( GuestAddress , u64 ) ] ,
551
- ) -> Result < Collection , GuestRegionError > {
546
+ ) -> Result < Collection , GuestRegionCollectionError > {
552
547
Collection :: from_arc_regions (
553
548
regions_summary
554
549
. iter ( )
@@ -563,11 +558,11 @@ pub(crate) mod tests {
563
558
564
559
assert ! ( matches!(
565
560
new_guest_memory_collection_from_regions( & regions_summary) . unwrap_err( ) ,
566
- GuestRegionError :: NoMemoryRegion
561
+ GuestRegionCollectionError :: NoMemoryRegion
567
562
) ) ;
568
563
assert ! ( matches!(
569
564
new_guest_memory_collection_from_arc_regions( & regions_summary) . unwrap_err( ) ,
570
- GuestRegionError :: NoMemoryRegion
565
+ GuestRegionCollectionError :: NoMemoryRegion
571
566
) ) ;
572
567
}
573
568
@@ -577,11 +572,11 @@ pub(crate) mod tests {
577
572
578
573
assert ! ( matches!(
579
574
new_guest_memory_collection_from_regions( & regions_summary) . unwrap_err( ) ,
580
- GuestRegionError :: MemoryRegionOverlap
575
+ GuestRegionCollectionError :: MemoryRegionOverlap
581
576
) ) ;
582
577
assert ! ( matches!(
583
578
new_guest_memory_collection_from_arc_regions( & regions_summary) . unwrap_err( ) ,
584
- GuestRegionError :: MemoryRegionOverlap
579
+ GuestRegionCollectionError :: MemoryRegionOverlap
585
580
) ) ;
586
581
}
587
582
@@ -591,11 +586,11 @@ pub(crate) mod tests {
591
586
592
587
assert ! ( matches!(
593
588
new_guest_memory_collection_from_regions( & regions_summary) . unwrap_err( ) ,
594
- GuestRegionError :: UnsortedMemoryRegions
589
+ GuestRegionCollectionError :: UnsortedMemoryRegions
595
590
) ) ;
596
591
assert ! ( matches!(
597
592
new_guest_memory_collection_from_arc_regions( & regions_summary) . unwrap_err( ) ,
598
- GuestRegionError :: UnsortedMemoryRegions
593
+ GuestRegionCollectionError :: UnsortedMemoryRegions
599
594
) ) ;
600
595
}
601
596
0 commit comments