Skip to content

Commit 7baf866

Browse files
alexandruaglauralt
authored andcommitted
bitmap: add BaseSlice
The `BaseSlice` abstraction represents slices into `B: Bitmap` objects that wrap an inner generic smart pointer to `B`. This allows us to reuse the same slice logic for references, `Arc` objects, and others. As a result, `RefSlice` is now just a type alias for a `BaseSlice` with a particular type parameter. Signed-off-by: Alexandru Agache <[email protected]>
1 parent d560e09 commit 7baf866

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

src/bitmap/backend/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
33

44
mod atomic_bitmap;
5-
mod ref_slice;
5+
mod slice;
66

77
pub use atomic_bitmap::AtomicBitmap;
8-
pub use ref_slice::RefSlice;
8+
pub use slice::RefSlice;

src/bitmap/backend/ref_slice.rs renamed to src/bitmap/backend/slice.rs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,47 @@
44
//! Contains a generic implementation of `BitmapSlice`.
55
66
use std::fmt::{self, Debug};
7+
use std::ops::Deref;
78

89
use crate::bitmap::{Bitmap, BitmapSlice, WithBitmapSlice};
910

1011
/// Represents a slice into a `Bitmap` object, starting at `base_offset`.
11-
pub struct RefSlice<'a, B> {
12-
inner: &'a B,
12+
#[derive(Clone, Copy)]
13+
pub struct BaseSlice<B> {
14+
inner: B,
1315
base_offset: usize,
1416
}
1517

16-
impl<'a, B: Bitmap + 'a> RefSlice<'a, B> {
18+
impl<B> BaseSlice<B> {
1719
/// Create a new `BitmapSlice`, starting at the specified `offset`.
18-
pub fn new(bitmap: &'a B, offset: usize) -> Self {
19-
RefSlice {
20-
inner: bitmap,
20+
pub fn new(inner: B, offset: usize) -> Self {
21+
BaseSlice {
22+
inner,
2123
base_offset: offset,
2224
}
2325
}
2426
}
2527

26-
impl<'a, B: Bitmap> WithBitmapSlice<'a> for RefSlice<'_, B> {
28+
impl<'a, B> WithBitmapSlice<'a> for BaseSlice<B>
29+
where
30+
B: Copy + Deref,
31+
B::Target: Bitmap,
32+
{
2733
type S = Self;
2834
}
2935

30-
impl<B: Bitmap> BitmapSlice for RefSlice<'_, B> {}
36+
impl<B> BitmapSlice for BaseSlice<B>
37+
where
38+
B: Copy + Deref,
39+
B::Target: Bitmap,
40+
{
41+
}
3142

32-
impl<'a, B: Bitmap> Bitmap for RefSlice<'a, B> {
43+
impl<B> Bitmap for BaseSlice<B>
44+
where
45+
B: Copy + Deref,
46+
B::Target: Bitmap,
47+
{
3348
/// Mark the memory range specified by the given `offset` (relative to the base offset of
3449
/// the slice) and `len` as dirtied.
3550
fn mark_dirty(&self, offset: usize, len: usize) {
@@ -48,31 +63,32 @@ impl<'a, B: Bitmap> Bitmap for RefSlice<'a, B> {
4863

4964
/// Create a new `BitmapSlice` starting from the specified `offset` into the current slice.
5065
fn slice_at(&self, offset: usize) -> Self {
51-
RefSlice {
52-
inner: self.inner,
66+
BaseSlice {
67+
inner: self.inner.clone(),
5368
base_offset: self.base_offset.wrapping_add(offset),
5469
}
5570
}
5671
}
5772

58-
impl<'a, B> Clone for RefSlice<'a, B> {
59-
fn clone(&self) -> Self {
60-
RefSlice {
61-
inner: self.inner,
62-
base_offset: self.base_offset,
63-
}
64-
}
65-
}
66-
67-
impl<'a, B> Copy for RefSlice<'a, B> {}
68-
69-
impl<'a, B> Debug for RefSlice<'a, B> {
73+
impl<B> Debug for BaseSlice<B> {
7074
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7175
// Dummy impl for now.
7276
write!(f, "(bitmap slice)")
7377
}
7478
}
7579

80+
impl<B: Default> Default for BaseSlice<B> {
81+
fn default() -> Self {
82+
BaseSlice {
83+
inner: B::default(),
84+
base_offset: 0,
85+
}
86+
}
87+
}
88+
89+
/// A `BitmapSlice` implementation that wraps a reference to a `Bitmap` object.
90+
pub type RefSlice<'a, B> = BaseSlice<&'a B>;
91+
7692
#[cfg(test)]
7793
mod tests {
7894
use super::*;

0 commit comments

Comments
 (0)