|
41 | 41 | //! via pointers, references, or slices returned by methods of `GuestMemory`,`GuestMemoryRegion`,
|
42 | 42 | //! `VolatileSlice`, `VolatileRef`, or `VolatileArrayRef`.
|
43 | 43 |
|
| 44 | +use std::any::Any; |
44 | 45 | use std::convert::From;
|
45 | 46 | use std::fmt::{self, Display};
|
46 | 47 | use std::fs::File;
|
@@ -400,33 +401,48 @@ pub trait GuestAddressSpace {
|
400 | 401 | /// to access memory through this address space. The object provides
|
401 | 402 | /// a consistent snapshot of the memory map.
|
402 | 403 | fn memory(&self) -> Self::T;
|
| 404 | + |
| 405 | + /// Cast `self` to a dynamic trait object of `std::any::Any`. |
| 406 | + fn as_any(&self) -> &dyn Any; |
403 | 407 | }
|
404 | 408 |
|
405 |
| -impl<M: GuestMemory> GuestAddressSpace for &M { |
| 409 | +impl<M: GuestMemory + 'static> GuestAddressSpace for &M { |
406 | 410 | type M = M;
|
407 | 411 | type T = Self;
|
408 | 412 |
|
409 | 413 | fn memory(&self) -> Self {
|
410 | 414 | self
|
411 | 415 | }
|
| 416 | + |
| 417 | + fn as_any(&self) -> &dyn Any { |
| 418 | + *self |
| 419 | + } |
412 | 420 | }
|
413 | 421 |
|
414 |
| -impl<M: GuestMemory> GuestAddressSpace for Rc<M> { |
| 422 | +impl<M: GuestMemory + 'static> GuestAddressSpace for Rc<M> { |
415 | 423 | type M = M;
|
416 | 424 | type T = Self;
|
417 | 425 |
|
418 | 426 | fn memory(&self) -> Self {
|
419 | 427 | self.clone()
|
420 | 428 | }
|
| 429 | + |
| 430 | + fn as_any(&self) -> &dyn Any { |
| 431 | + self |
| 432 | + } |
421 | 433 | }
|
422 | 434 |
|
423 |
| -impl<M: GuestMemory> GuestAddressSpace for Arc<M> { |
| 435 | +impl<M: GuestMemory + 'static> GuestAddressSpace for Arc<M> { |
424 | 436 | type M = M;
|
425 | 437 | type T = Self;
|
426 | 438 |
|
427 | 439 | fn memory(&self) -> Self {
|
428 | 440 | self.clone()
|
429 | 441 | }
|
| 442 | + |
| 443 | + fn as_any(&self) -> &dyn Any { |
| 444 | + self |
| 445 | + } |
430 | 446 | }
|
431 | 447 |
|
432 | 448 | /// Lifetime generic associated iterators. The actual iterator type is defined through associated
|
@@ -1256,4 +1272,14 @@ mod tests {
|
1256 | 1272 | let r = mem.find_region(addr).unwrap();
|
1257 | 1273 | assert_eq!(r.is_hugetlbfs(), None);
|
1258 | 1274 | }
|
| 1275 | + |
| 1276 | + #[cfg(feature = "backend-mmap")] |
| 1277 | + #[test] |
| 1278 | + fn test_as_any() { |
| 1279 | + let addr = GuestAddress(0x1000); |
| 1280 | + let mem = &GuestMemoryMmap::from_ranges(&[(addr, 0x1000)]).unwrap(); |
| 1281 | + |
| 1282 | + assert!(mem.as_any().downcast_ref::<GuestMemoryMmap>().is_some()); |
| 1283 | + assert!(mem.as_any().downcast_ref::<String>().is_none()); |
| 1284 | + } |
1259 | 1285 | }
|
0 commit comments