@@ -142,18 +142,9 @@ pub trait GuestMemoryRegion: Bytes<MemoryRegionAddress, E = GuestMemoryError> {
142
142
}
143
143
}
144
144
145
- /// Errors that can occur when dealing with [`GuestRegion `]s, or collections thereof
145
+ /// Errors that can occur when dealing with [`GuestRegionCollection `]s
146
146
#[ derive( Debug , thiserror:: Error ) ]
147
- pub enum GuestRegionError {
148
- /// Adding the guest base address to the length of the underlying mapping resulted
149
- /// in an overflow.
150
- #[ error( "Adding the guest base address to the length of the underlying mapping resulted in an overflow" ) ]
151
- #[ cfg( feature = "backend-mmap" ) ]
152
- InvalidGuestRegion ,
153
- /// Error creating a `MmapRegion` object.
154
- #[ error( "{0}" ) ]
155
- #[ cfg( feature = "backend-mmap" ) ]
156
- MmapRegion ( crate :: mmap:: MmapRegionError ) ,
147
+ pub enum GuestRegionCollectionError {
157
148
/// No memory region found.
158
149
#[ error( "No memory region found" ) ]
159
150
NoMemoryRegion ,
@@ -203,7 +194,9 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
203
194
/// * `regions` - The vector of regions.
204
195
/// The regions shouldn't overlap, and they should be sorted
205
196
/// by the starting address.
206
- pub fn from_regions ( mut regions : Vec < R > ) -> std:: result:: Result < Self , GuestRegionError > {
197
+ pub fn from_regions (
198
+ mut regions : Vec < R > ,
199
+ ) -> std:: result:: Result < Self , GuestRegionCollectionError > {
207
200
Self :: from_arc_regions ( regions. drain ( ..) . map ( Arc :: new) . collect ( ) )
208
201
}
209
202
@@ -219,21 +212,23 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
219
212
/// * `regions` - The vector of `Arc` regions.
220
213
/// The regions shouldn't overlap and they should be sorted
221
214
/// by the starting address.
222
- pub fn from_arc_regions ( regions : Vec < Arc < R > > ) -> std:: result:: Result < Self , GuestRegionError > {
215
+ pub fn from_arc_regions (
216
+ regions : Vec < Arc < R > > ,
217
+ ) -> std:: result:: Result < Self , GuestRegionCollectionError > {
223
218
if regions. is_empty ( ) {
224
- return Err ( GuestRegionError :: NoMemoryRegion ) ;
219
+ return Err ( GuestRegionCollectionError :: NoMemoryRegion ) ;
225
220
}
226
221
227
222
for window in regions. windows ( 2 ) {
228
223
let prev = & window[ 0 ] ;
229
224
let next = & window[ 1 ] ;
230
225
231
226
if prev. start_addr ( ) > next. start_addr ( ) {
232
- return Err ( GuestRegionError :: UnsortedMemoryRegions ) ;
227
+ return Err ( GuestRegionCollectionError :: UnsortedMemoryRegions ) ;
233
228
}
234
229
235
230
if prev. last_addr ( ) >= next. start_addr ( ) {
236
- return Err ( GuestRegionError :: MemoryRegionOverlap ) ;
231
+ return Err ( GuestRegionCollectionError :: MemoryRegionOverlap ) ;
237
232
}
238
233
}
239
234
@@ -247,7 +242,7 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
247
242
pub fn insert_region (
248
243
& self ,
249
244
region : Arc < R > ,
250
- ) -> std:: result:: Result < GuestRegionCollection < R > , GuestRegionError > {
245
+ ) -> std:: result:: Result < GuestRegionCollection < R > , GuestRegionCollectionError > {
251
246
let mut regions = self . regions . clone ( ) ;
252
247
regions. push ( region) ;
253
248
regions. sort_by_key ( |x| x. start_addr ( ) ) ;
@@ -265,7 +260,7 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
265
260
& self ,
266
261
base : GuestAddress ,
267
262
size : GuestUsize ,
268
- ) -> std:: result:: Result < ( GuestRegionCollection < R > , Arc < R > ) , GuestRegionError > {
263
+ ) -> std:: result:: Result < ( GuestRegionCollection < R > , Arc < R > ) , GuestRegionCollectionError > {
269
264
if let Ok ( region_index) = self . regions . binary_search_by_key ( & base, |x| x. start_addr ( ) ) {
270
265
if self . regions . get ( region_index) . unwrap ( ) . len ( ) == size {
271
266
let mut regions = self . regions . clone ( ) ;
@@ -274,7 +269,7 @@ impl<R: GuestMemoryRegion> GuestRegionCollection<R> {
274
269
}
275
270
}
276
271
277
- Err ( GuestRegionError :: NoMemoryRegion )
272
+ Err ( GuestRegionCollectionError :: NoMemoryRegion )
278
273
}
279
274
}
280
275
@@ -452,7 +447,7 @@ impl<R: GuestMemoryRegionBytes> Bytes<MemoryRegionAddress> for R {
452
447
453
448
#[ cfg( test) ]
454
449
pub ( crate ) mod tests {
455
- use crate :: region:: { GuestMemoryRegionBytes , GuestRegionError } ;
450
+ use crate :: region:: { GuestMemoryRegionBytes , GuestRegionCollectionError } ;
456
451
use crate :: {
457
452
Address , GuestAddress , GuestMemory , GuestMemoryRegion , GuestRegionCollection , GuestUsize ,
458
453
} ;
@@ -483,7 +478,7 @@ pub(crate) mod tests {
483
478
pub ( crate ) type Collection = GuestRegionCollection < MockRegion > ;
484
479
485
480
fn check_guest_memory_mmap (
486
- maybe_guest_mem : Result < Collection , GuestRegionError > ,
481
+ maybe_guest_mem : Result < Collection , GuestRegionCollectionError > ,
487
482
expected_regions_summary : & [ ( GuestAddress , u64 ) ] ,
488
483
) {
489
484
assert ! ( maybe_guest_mem. is_ok( ) ) ;
@@ -510,7 +505,7 @@ pub(crate) mod tests {
510
505
511
506
pub ( crate ) fn new_guest_memory_collection_from_regions (
512
507
regions_summary : & [ ( GuestAddress , u64 ) ] ,
513
- ) -> Result < Collection , GuestRegionError > {
508
+ ) -> Result < Collection , GuestRegionCollectionError > {
514
509
Collection :: from_regions (
515
510
regions_summary
516
511
. iter ( )
@@ -521,7 +516,7 @@ pub(crate) mod tests {
521
516
522
517
fn new_guest_memory_collection_from_arc_regions (
523
518
regions_summary : & [ ( GuestAddress , u64 ) ] ,
524
- ) -> Result < Collection , GuestRegionError > {
519
+ ) -> Result < Collection , GuestRegionCollectionError > {
525
520
Collection :: from_arc_regions (
526
521
regions_summary
527
522
. iter ( )
@@ -536,11 +531,11 @@ pub(crate) mod tests {
536
531
537
532
assert ! ( matches!(
538
533
new_guest_memory_collection_from_regions( & regions_summary) . unwrap_err( ) ,
539
- GuestRegionError :: NoMemoryRegion
534
+ GuestRegionCollectionError :: NoMemoryRegion
540
535
) ) ;
541
536
assert ! ( matches!(
542
537
new_guest_memory_collection_from_arc_regions( & regions_summary) . unwrap_err( ) ,
543
- GuestRegionError :: NoMemoryRegion
538
+ GuestRegionCollectionError :: NoMemoryRegion
544
539
) ) ;
545
540
}
546
541
@@ -550,11 +545,11 @@ pub(crate) mod tests {
550
545
551
546
assert ! ( matches!(
552
547
new_guest_memory_collection_from_regions( & regions_summary) . unwrap_err( ) ,
553
- GuestRegionError :: MemoryRegionOverlap
548
+ GuestRegionCollectionError :: MemoryRegionOverlap
554
549
) ) ;
555
550
assert ! ( matches!(
556
551
new_guest_memory_collection_from_arc_regions( & regions_summary) . unwrap_err( ) ,
557
- GuestRegionError :: MemoryRegionOverlap
552
+ GuestRegionCollectionError :: MemoryRegionOverlap
558
553
) ) ;
559
554
}
560
555
@@ -564,11 +559,11 @@ pub(crate) mod tests {
564
559
565
560
assert ! ( matches!(
566
561
new_guest_memory_collection_from_regions( & regions_summary) . unwrap_err( ) ,
567
- GuestRegionError :: UnsortedMemoryRegions
562
+ GuestRegionCollectionError :: UnsortedMemoryRegions
568
563
) ) ;
569
564
assert ! ( matches!(
570
565
new_guest_memory_collection_from_arc_regions( & regions_summary) . unwrap_err( ) ,
571
- GuestRegionError :: UnsortedMemoryRegions
566
+ GuestRegionCollectionError :: UnsortedMemoryRegions
572
567
) ) ;
573
568
}
574
569
0 commit comments