4
4
//! Contains a generic implementation of `BitmapSlice`.
5
5
6
6
use std:: fmt:: { self , Debug } ;
7
+ use std:: ops:: Deref ;
7
8
8
9
use crate :: bitmap:: { Bitmap , BitmapSlice , WithBitmapSlice } ;
9
10
10
11
/// 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 ,
13
15
base_offset : usize ,
14
16
}
15
17
16
- impl < ' a , B : Bitmap + ' a > RefSlice < ' a , B > {
18
+ impl < B > BaseSlice < B > {
17
19
/// 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,
21
23
base_offset : offset,
22
24
}
23
25
}
24
26
}
25
27
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
+ {
27
33
type S = Self ;
28
34
}
29
35
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
+ }
31
42
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
+ {
33
48
/// Mark the memory range specified by the given `offset` (relative to the base offset of
34
49
/// the slice) and `len` as dirtied.
35
50
fn mark_dirty ( & self , offset : usize , len : usize ) {
@@ -48,31 +63,32 @@ impl<'a, B: Bitmap> Bitmap for RefSlice<'a, B> {
48
63
49
64
/// Create a new `BitmapSlice` starting from the specified `offset` into the current slice.
50
65
fn slice_at ( & self , offset : usize ) -> Self {
51
- RefSlice {
52
- inner : self . inner ,
66
+ BaseSlice {
67
+ inner : self . inner . clone ( ) ,
53
68
base_offset : self . base_offset . wrapping_add ( offset) ,
54
69
}
55
70
}
56
71
}
57
72
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 > {
70
74
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
71
75
// Dummy impl for now.
72
76
write ! ( f, "(bitmap slice)" )
73
77
}
74
78
}
75
79
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
+
76
92
#[ cfg( test) ]
77
93
mod tests {
78
94
use super :: * ;
0 commit comments