Skip to content

Commit 1d15af0

Browse files
alexandruaglauralt
authored andcommitted
bitmap: remove Copy bound from BitmapSlice
`BitmapSlice` now only has a `Clone` trait bound (instead of `Clone` + `Copy`), to accomodate for the fact that certain implementations may use reference counted smart pointers for example, which don't implement `Copy`. Types that do implement `Copy` effectively work just as before, since the `clone` calls don't change anything for them. Signed-off-by: Alexandru Agache <[email protected]>
1 parent 7baf866 commit 1d15af0

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

src/bitmap/backend/slice.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ impl<B> BaseSlice<B> {
2727

2828
impl<'a, B> WithBitmapSlice<'a> for BaseSlice<B>
2929
where
30-
B: Copy + Deref,
30+
B: Clone + Deref,
3131
B::Target: Bitmap,
3232
{
3333
type S = Self;
3434
}
3535

3636
impl<B> BitmapSlice for BaseSlice<B>
3737
where
38-
B: Copy + Deref,
38+
B: Clone + Deref,
3939
B::Target: Bitmap,
4040
{
4141
}
4242

4343
impl<B> Bitmap for BaseSlice<B>
4444
where
45-
B: Copy + Deref,
45+
B: Clone + Deref,
4646
B::Target: Bitmap,
4747
{
4848
/// Mark the memory range specified by the given `offset` (relative to the base offset of

src/bitmap/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ pub trait WithBitmapSlice<'a> {
2424

2525
/// Trait used to represent that a `BitmapSlice` is a `Bitmap` itself, but also satisfies the
2626
/// restriction that slices created from it have the same type as `Self`.
27-
pub trait BitmapSlice:
28-
Bitmap + Clone + Copy + Debug + for<'a> WithBitmapSlice<'a, S = Self>
29-
{
30-
}
27+
pub trait BitmapSlice: Bitmap + Clone + Debug + for<'a> WithBitmapSlice<'a, S = Self> {}
3128

3229
/// Common bitmap operations. Using Higher-Rank Trait Bounds (HRTBs) to effectively define
3330
/// an associated type that has a lifetime parameter, without tagging the `Bitmap` trait with

src/volatile_memory.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,11 @@ impl<'a, B: BitmapSlice> VolatileSlice<'a, B> {
345345
/// assert_eq!(8, start.len());
346346
/// assert_eq!(24, end.len());
347347
/// ```
348-
pub fn split_at(self, mid: usize) -> Result<(Self, Self)> {
348+
pub fn split_at(&self, mid: usize) -> Result<(Self, Self)> {
349349
let end = self.offset(mid)?;
350350
let start = unsafe {
351351
// safe because self.offset() already checked the bounds
352-
VolatileSlice::with_bitmap(self.addr, mid, self.bitmap)
352+
VolatileSlice::with_bitmap(self.addr, mid, self.bitmap.clone())
353353
};
354354

355355
Ok((start, end))
@@ -360,7 +360,7 @@ impl<'a, B: BitmapSlice> VolatileSlice<'a, B> {
360360
///
361361
/// The returned subslice is a copy of this slice with the address increased by `offset` bytes
362362
/// and the size set to `count` bytes.
363-
pub fn subslice(self, offset: usize, count: usize) -> Result<Self> {
363+
pub fn subslice(&self, offset: usize, count: usize) -> Result<Self> {
364364
let mem_end = compute_offset(offset, count)?;
365365
if mem_end > self.len() {
366366
return Err(Error::OutOfBounds { addr: mem_end });
@@ -381,7 +381,7 @@ impl<'a, B: BitmapSlice> VolatileSlice<'a, B> {
381381
///
382382
/// The returned subslice is a copy of this slice with the address increased by `count` bytes
383383
/// and the size reduced by `count` bytes.
384-
pub fn offset(self, count: usize) -> Result<VolatileSlice<'a, B>> {
384+
pub fn offset(&self, count: usize) -> Result<VolatileSlice<'a, B>> {
385385
let new_addr = (self.addr as usize)
386386
.checked_add(count)
387387
.ok_or(Error::Overflow {
@@ -953,7 +953,7 @@ where
953953
}
954954

955955
/// Returns a pointer to the underlying memory.
956-
pub fn as_ptr(self) -> *mut u8 {
956+
pub fn as_ptr(&self) -> *mut u8 {
957957
self.addr as *mut u8
958958
}
959959

@@ -968,7 +968,7 @@ where
968968
/// let v_ref = unsafe { VolatileRef::<u32>::new(0 as *mut _) };
969969
/// assert_eq!(v_ref.len(), size_of::<u32>() as usize);
970970
/// ```
971-
pub fn len(self) -> usize {
971+
pub fn len(&self) -> usize {
972972
size_of::<T>()
973973
}
974974

@@ -979,14 +979,14 @@ where
979979

980980
/// Does a volatile write of the value `v` to the address of this ref.
981981
#[inline(always)]
982-
pub fn store(self, v: T) {
982+
pub fn store(&self, v: T) {
983983
unsafe { write_volatile(self.addr, Packed::<T>(v)) };
984984
self.bitmap.mark_dirty(0, size_of::<T>())
985985
}
986986

987987
/// Does a volatile read of the value at the address of this ref.
988988
#[inline(always)]
989-
pub fn load(self) -> T {
989+
pub fn load(&self) -> T {
990990
// For the purposes of demonstrating why read_volatile is necessary, try replacing the code
991991
// in this function with the commented code below and running `cargo test --release`.
992992
// unsafe { *(self.addr as *const T) }
@@ -995,8 +995,10 @@ where
995995

996996
/// Converts this to a [`VolatileSlice`](struct.VolatileSlice.html) with the same size and
997997
/// address.
998-
pub fn to_slice(self) -> VolatileSlice<'a, B> {
999-
unsafe { VolatileSlice::with_bitmap(self.addr as *mut u8, size_of::<T>(), self.bitmap) }
998+
pub fn to_slice(&self) -> VolatileSlice<'a, B> {
999+
unsafe {
1000+
VolatileSlice::with_bitmap(self.addr as *mut u8, size_of::<T>(), self.bitmap.clone())
1001+
}
10001002
}
10011003
}
10021004

@@ -1122,7 +1124,11 @@ where
11221124
/// Converts this to a `VolatileSlice` with the same size and address.
11231125
pub fn to_slice(&self) -> VolatileSlice<'a, B> {
11241126
unsafe {
1125-
VolatileSlice::with_bitmap(self.addr, self.nelem * self.element_size(), self.bitmap)
1127+
VolatileSlice::with_bitmap(
1128+
self.addr,
1129+
self.nelem * self.element_size(),
1130+
self.bitmap.clone(),
1131+
)
11261132
}
11271133
}
11281134

0 commit comments

Comments
 (0)