Skip to content

Commit 883f4f0

Browse files
bonziniShadowCurse
authored andcommitted
atomic: implement Clone by hand
derive(Clone) requires the GuestMemory type parameter of GuestMemoryAtomic to be cloneable as well. However, this is pointless for GuestMemoryAtomic since it is basically an Arc<> wrapper. Make GuestMemoryAtomic<> implement Clone always, just like Arc<>. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 51ea3ec commit 883f4f0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- \[[#311](https://github.com/rust-vmm/vm-memory/pull/311)\] Allow compiling without the ReadVolatile and WriteVolatile implementations
88
- \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\] `GuestRegionContainer`, a generic container of `GuestMemoryRegion`s, generalizing `GuestMemoryMmap` (which
99
is now a type alias for `GuestRegionContainer<GuestRegionMmap>`).
10+
- \[[#338](https://github.com/rust-vmm/vm-memory/pull/338)\] Make `GuestMemoryAtomic` always implement `Clone`.
1011

1112
### Changed
1213

src/atomic.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{GuestAddressSpace, GuestMemory};
2424
/// readers will not be blocked because the copies they retrieved will be collected once
2525
/// no one can access them anymore. Under the assumption that updates to the memory map
2626
/// are rare, this allows a very efficient implementation of the `memory()` method.
27-
#[derive(Clone, Debug)]
27+
#[derive(Debug)]
2828
pub struct GuestMemoryAtomic<M: GuestMemory> {
2929
// GuestAddressSpace<M>, which we want to implement, is basically a drop-in
3030
// replacement for &M. Therefore, we need to pass to devices the `GuestMemoryAtomic`
@@ -75,6 +75,14 @@ impl<M: GuestMemory> GuestMemoryAtomic<M> {
7575
}
7676
}
7777

78+
impl<M: GuestMemory> Clone for GuestMemoryAtomic<M> {
79+
fn clone(&self) -> Self {
80+
Self {
81+
inner: self.inner.clone(),
82+
}
83+
}
84+
}
85+
7886
impl<M: GuestMemory> GuestAddressSpace for GuestMemoryAtomic<M> {
7987
type T = GuestMemoryLoadGuard<M>;
8088
type M = M;
@@ -196,6 +204,15 @@ mod tests {
196204
assert_eq!(mem3.num_regions(), 2);
197205
assert!(mem3.find_region(GuestAddress(0x1000)).is_some());
198206
assert!(mem3.find_region(GuestAddress(0x10000)).is_none());
207+
208+
let gm2 = gm.clone();
209+
let mem4 = gm2.memory();
210+
for region in mem4.iter() {
211+
assert_eq!(region.len(), region_size as GuestUsize);
212+
}
213+
assert_eq!(mem4.num_regions(), 2);
214+
assert!(mem4.find_region(GuestAddress(0x1000)).is_some());
215+
assert!(mem4.find_region(GuestAddress(0x10000)).is_none());
199216
}
200217

201218
#[test]

0 commit comments

Comments
 (0)