diff --git a/CHANGELOG.md b/CHANGELOG.md index 53b14972..91468004 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - \[[#311](https://github.com/rust-vmm/vm-memory/pull/311)\] Allow compiling without the ReadVolatile and WriteVolatile implementations - \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\] `GuestRegionContainer`, a generic container of `GuestMemoryRegion`s, generalizing `GuestMemoryMmap` (which is now a type alias for `GuestRegionContainer`). +- \[[#338](https://github.com/rust-vmm/vm-memory/pull/338)\] Make `GuestMemoryAtomic` always implement `Clone`. +- \[[#338](https://github.com/rust-vmm/vm-memory/pull/338)\] Make `GuestAddressSpace` a subtrait of `Clone`. ### Changed diff --git a/src/atomic.rs b/src/atomic.rs index 22697d05..87a2c1e3 100644 --- a/src/atomic.rs +++ b/src/atomic.rs @@ -24,7 +24,7 @@ use crate::{GuestAddressSpace, GuestMemory}; /// readers will not be blocked because the copies they retrieved will be collected once /// no one can access them anymore. Under the assumption that updates to the memory map /// are rare, this allows a very efficient implementation of the `memory()` method. -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct GuestMemoryAtomic { // GuestAddressSpace, which we want to implement, is basically a drop-in // replacement for &M. Therefore, we need to pass to devices the `GuestMemoryAtomic` @@ -75,6 +75,14 @@ impl GuestMemoryAtomic { } } +impl Clone for GuestMemoryAtomic { + fn clone(&self) -> Self { + Self { + inner: self.inner.clone(), + } + } +} + impl GuestAddressSpace for GuestMemoryAtomic { type T = GuestMemoryLoadGuard; type M = M; @@ -196,6 +204,15 @@ mod tests { assert_eq!(mem3.num_regions(), 2); assert!(mem3.find_region(GuestAddress(0x1000)).is_some()); assert!(mem3.find_region(GuestAddress(0x10000)).is_none()); + + let gm2 = gm.clone(); + let mem4 = gm2.memory(); + for region in mem4.iter() { + assert_eq!(region.len(), region_size as GuestUsize); + } + assert_eq!(mem4.num_regions(), 2); + assert!(mem4.find_region(GuestAddress(0x1000)).is_some()); + assert!(mem4.find_region(GuestAddress(0x10000)).is_none()); } #[test] diff --git a/src/guest_memory.rs b/src/guest_memory.rs index 39e4f10a..62d0c531 100644 --- a/src/guest_memory.rs +++ b/src/guest_memory.rs @@ -219,7 +219,7 @@ impl FileOffset { /// # } /// # } /// ``` -pub trait GuestAddressSpace { +pub trait GuestAddressSpace: Clone { /// The type that will be used to access guest memory. type M: GuestMemory;