@@ -16,140 +16,140 @@ use nb;
16
16
17
17
/// Trait to check if two entities are bitwise subset of another.
18
18
pub trait BitSubset {
19
- /// Check that every '1' bit is a '1' on the right hand side.
20
- fn is_subset_of ( & self , rhs : & Self ) -> bool ;
19
+ /// Check that every '1' bit is a '1' on the right hand side.
20
+ fn is_subset_of ( & self , rhs : & Self ) -> bool ;
21
21
}
22
22
23
23
/// Blanket implementation of [`BitSubset`] for all arrays of a type implementing [`BitOr`]
24
24
impl < T : Copy + Eq + BitOr < Output = T > > BitSubset for [ T ] {
25
- fn is_subset_of ( & self , rhs : & Self ) -> bool {
26
- if self . len ( ) > rhs. len ( ) {
27
- false
28
- } else {
29
- self . iter ( ) . zip ( rhs. iter ( ) ) . all ( |( a, b) | ( * a | * b) == * b)
30
- }
31
- }
25
+ fn is_subset_of ( & self , rhs : & Self ) -> bool {
26
+ if self . len ( ) > rhs. len ( ) {
27
+ false
28
+ } else {
29
+ self . iter ( ) . zip ( rhs. iter ( ) ) . all ( |( a, b) | ( * a | * b) == * b)
30
+ }
31
+ }
32
32
}
33
33
34
34
/// An address denotes the read/write address of a single word.
35
35
#[ derive( Default , Copy , Clone , Debug , PartialOrd , PartialEq , Eq , Ord ) ]
36
36
pub struct Address ( pub u32 ) ;
37
37
38
38
impl Add < usize > for Address {
39
- type Output = Self ;
39
+ type Output = Self ;
40
40
41
- fn add ( self , rhs : usize ) -> Self :: Output {
42
- Address ( self . 0 + rhs as u32 )
43
- }
41
+ fn add ( self , rhs : usize ) -> Self :: Output {
42
+ Address ( self . 0 + rhs as u32 )
43
+ }
44
44
}
45
45
46
46
impl Add < Address > for Address {
47
- type Output = Self ;
47
+ type Output = Self ;
48
48
49
- fn add ( self , rhs : Address ) -> Self :: Output {
50
- Address ( self . 0 + rhs. 0 )
51
- }
49
+ fn add ( self , rhs : Address ) -> Self :: Output {
50
+ Address ( self . 0 + rhs. 0 )
51
+ }
52
52
}
53
53
54
54
impl Sub < Address > for Address {
55
- type Output = Self ;
55
+ type Output = Self ;
56
56
57
- fn sub ( self , rhs : Address ) -> Self :: Output {
58
- Address ( self . 0 - rhs. 0 )
59
- }
57
+ fn sub ( self , rhs : Address ) -> Self :: Output {
58
+ Address ( self . 0 - rhs. 0 )
59
+ }
60
60
}
61
61
62
62
/// A region denotes a contiguous piece of memory between two addresses.
63
63
pub trait Region {
64
- /// Check if `address` is contained in the region of `Self`
65
- fn contains ( & self , address : Address ) -> bool ;
64
+ /// Check if `address` is contained in the region of `Self`
65
+ fn contains ( & self , address : Address ) -> bool ;
66
66
}
67
67
68
68
/// Iterator producing block-region pairs, where each memory block maps to each
69
69
/// region.
70
70
pub struct OverlapIterator < ' a , R , I >
71
71
where
72
- R : Region ,
73
- I : Iterator < Item = R > ,
72
+ R : Region ,
73
+ I : Iterator < Item = R > ,
74
74
{
75
- memory : & ' a [ u8 ] ,
76
- regions : I ,
77
- base_address : Address ,
75
+ memory : & ' a [ u8 ] ,
76
+ regions : I ,
77
+ base_address : Address ,
78
78
}
79
79
80
80
/// Trait allowing us to automatically add an `overlaps` function to all iterators over [`Region`]
81
81
pub trait IterableByOverlaps < ' a , R , I >
82
82
where
83
- R : Region ,
84
- I : Iterator < Item = R > ,
83
+ R : Region ,
84
+ I : Iterator < Item = R > ,
85
85
{
86
- /// Obtain an [`OverlapIterator`] over a subslice of `memory` that overlaps with the region in `self`
87
- fn overlaps ( self , memory : & ' a [ u8 ] , base_address : Address ) -> OverlapIterator < R , I > ;
86
+ /// Obtain an [`OverlapIterator`] over a subslice of `memory` that overlaps with the region in `self`
87
+ fn overlaps ( self , memory : & ' a [ u8 ] , base_address : Address ) -> OverlapIterator < R , I > ;
88
88
}
89
89
90
90
impl < ' a , R , I > Iterator for OverlapIterator < ' a , R , I >
91
91
where
92
- R : Region ,
93
- I : Iterator < Item = R > ,
92
+ R : Region ,
93
+ I : Iterator < Item = R > ,
94
94
{
95
- type Item = ( & ' a [ u8 ] , R , Address ) ;
96
-
97
- fn next ( & mut self ) -> Option < Self :: Item > {
98
- while let Some ( region) = self . regions . next ( ) {
99
- // TODO: This might be possible to do in a smarter way?
100
- let mut block_range = ( 0 ..self . memory . len ( ) )
101
- . skip_while ( |index| !region. contains ( self . base_address + Address ( * index as u32 ) ) )
102
- . take_while ( |index| region. contains ( self . base_address + Address ( * index as u32 ) ) ) ;
103
- if let Some ( start) = block_range. next ( ) {
104
- let end = block_range. last ( ) . unwrap_or ( start) + 1 ;
105
- return Some ( (
106
- & self . memory [ start..end] ,
107
- region,
108
- self . base_address + Address ( start as u32 ) ,
109
- ) ) ;
110
- }
111
- }
112
- None
113
- }
95
+ type Item = ( & ' a [ u8 ] , R , Address ) ;
96
+
97
+ fn next ( & mut self ) -> Option < Self :: Item > {
98
+ while let Some ( region) = self . regions . next ( ) {
99
+ // TODO: This might be possible to do in a smarter way?
100
+ let mut block_range = ( 0 ..self . memory . len ( ) )
101
+ . skip_while ( |index| !region. contains ( self . base_address + Address ( * index as u32 ) ) )
102
+ . take_while ( |index| region. contains ( self . base_address + Address ( * index as u32 ) ) ) ;
103
+ if let Some ( start) = block_range. next ( ) {
104
+ let end = block_range. last ( ) . unwrap_or ( start) + 1 ;
105
+ return Some ( (
106
+ & self . memory [ start..end] ,
107
+ region,
108
+ self . base_address + Address ( start as u32 ) ,
109
+ ) ) ;
110
+ }
111
+ }
112
+ None
113
+ }
114
114
}
115
115
116
116
/// Blanket implementation for all types implementing [`Iterator`] over [`Regions`]
117
117
impl < ' a , R , I > IterableByOverlaps < ' a , R , I > for I
118
118
where
119
- R : Region ,
120
- I : Iterator < Item = R > ,
119
+ R : Region ,
120
+ I : Iterator < Item = R > ,
121
121
{
122
- fn overlaps ( self , memory : & ' a [ u8 ] , base_address : Address ) -> OverlapIterator < R , I > {
123
- OverlapIterator {
124
- memory,
125
- regions : self ,
126
- base_address,
127
- }
128
- }
122
+ fn overlaps ( self , memory : & ' a [ u8 ] , base_address : Address ) -> OverlapIterator < R , I > {
123
+ OverlapIterator {
124
+ memory,
125
+ regions : self ,
126
+ base_address,
127
+ }
128
+ }
129
129
}
130
130
131
131
/// Storage trait
132
132
pub trait ReadWrite {
133
- /// An enumeration of storage errors
134
- type Error ;
135
-
136
- /// Read a slice of data from the storage peripheral, starting the read
137
- /// operation at the given address, and reading until end address
138
- /// (`self.range().1`) or buffer length, whichever comes first.
139
- fn try_read ( & mut self , address : Address , bytes : & mut [ u8 ] ) -> nb:: Result < ( ) , Self :: Error > ;
140
-
141
- /// Write a slice of data to the storage peripheral, starting the write
142
- /// operation at the given address.
143
- fn try_write ( & mut self , address : Address , bytes : & [ u8 ] ) -> nb:: Result < ( ) , Self :: Error > ;
144
-
145
- /// The range of possible addresses within the peripheral.
146
- ///
147
- /// (start_addr, end_addr)
148
- fn range ( & self ) -> ( Address , Address ) ;
149
-
150
- /// Erase the given storage range, clearing all data within `[from..to]`.
151
- ///
152
- /// This should return an error if the range is not aligned to a proper
153
- /// erase resolution
154
- fn try_erase ( & mut self , from : Address , to : Address ) -> nb:: Result < ( ) , Self :: Error > ;
133
+ /// An enumeration of storage errors
134
+ type Error ;
135
+
136
+ /// Read a slice of data from the storage peripheral, starting the read
137
+ /// operation at the given address, and reading until end address
138
+ /// (`self.range().1`) or buffer length, whichever comes first.
139
+ fn try_read ( & mut self , address : Address , bytes : & mut [ u8 ] ) -> nb:: Result < ( ) , Self :: Error > ;
140
+
141
+ /// Write a slice of data to the storage peripheral, starting the write
142
+ /// operation at the given address.
143
+ fn try_write ( & mut self , address : Address , bytes : & [ u8 ] ) -> nb:: Result < ( ) , Self :: Error > ;
144
+
145
+ /// The range of possible addresses within the peripheral.
146
+ ///
147
+ /// (start_addr, end_addr)
148
+ fn range ( & self ) -> ( Address , Address ) ;
149
+
150
+ /// Erase the given storage range, clearing all data within `[from..to]`.
151
+ ///
152
+ /// This should return an error if the range is not aligned to a proper
153
+ /// erase resolution
154
+ fn try_erase ( & mut self , from : Address , to : Address ) -> nb:: Result < ( ) , Self :: Error > ;
155
155
}
0 commit comments