@@ -16,40 +16,6 @@ pub struct Block {
16
16
pub contents : [ u8 ; Block :: LEN ] ,
17
17
}
18
18
19
- /// The linear numeric address of a block (or sector).
20
- ///
21
- /// The first block on a disk gets `BlockIdx(0)` (which usually contains the
22
- /// Master Boot Record).
23
- #[ cfg_attr( feature = "defmt-log" , derive( defmt:: Format ) ) ]
24
- #[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
25
- pub struct BlockIdx ( pub u32 ) ;
26
-
27
- /// The a number of blocks (or sectors).
28
- ///
29
- /// Add this to a `BlockIdx` to get an actual address on disk.
30
- #[ cfg_attr( feature = "defmt-log" , derive( defmt:: Format ) ) ]
31
- #[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
32
- pub struct BlockCount ( pub u32 ) ;
33
-
34
- /// An iterator returned from `Block::range`.
35
- pub struct BlockIter {
36
- inclusive_end : BlockIdx ,
37
- current : BlockIdx ,
38
- }
39
-
40
- /// A block device - a device which can read and write blocks (or
41
- /// sectors). Only supports devices which are <= 2 TiB in size.
42
- pub trait BlockDevice {
43
- /// The errors that the `BlockDevice` can return. Must be debug formattable.
44
- type Error : core:: fmt:: Debug ;
45
- /// Read one or more blocks, starting at the given block index.
46
- fn read ( & self , blocks : & mut [ Block ] , start_block_idx : BlockIdx ) -> Result < ( ) , Self :: Error > ;
47
- /// Write one or more blocks, starting at the given block index.
48
- fn write ( & self , blocks : & [ Block ] , start_block_idx : BlockIdx ) -> Result < ( ) , Self :: Error > ;
49
- /// Determine how many blocks this device can hold.
50
- fn num_blocks ( & self ) -> Result < BlockCount , Self :: Error > ;
51
- }
52
-
53
19
impl Block {
54
20
/// All our blocks are a fixed length of 512 bytes. We do not support
55
21
/// 'Advanced Format' Hard Drives with 4 KiB blocks, nor weird old
@@ -67,64 +33,6 @@ impl Block {
67
33
}
68
34
}
69
35
70
- impl Default for Block {
71
- fn default ( ) -> Self {
72
- Self :: new ( )
73
- }
74
- }
75
-
76
- impl core:: ops:: Add < BlockCount > for BlockIdx {
77
- type Output = BlockIdx ;
78
- fn add ( self , rhs : BlockCount ) -> BlockIdx {
79
- BlockIdx ( self . 0 + rhs. 0 )
80
- }
81
- }
82
-
83
- impl core:: ops:: AddAssign < BlockCount > for BlockIdx {
84
- fn add_assign ( & mut self , rhs : BlockCount ) {
85
- self . 0 += rhs. 0
86
- }
87
- }
88
-
89
- impl core:: ops:: Add < BlockCount > for BlockCount {
90
- type Output = BlockCount ;
91
- fn add ( self , rhs : BlockCount ) -> BlockCount {
92
- BlockCount ( self . 0 + rhs. 0 )
93
- }
94
- }
95
-
96
- impl core:: ops:: AddAssign < BlockCount > for BlockCount {
97
- fn add_assign ( & mut self , rhs : BlockCount ) {
98
- self . 0 += rhs. 0
99
- }
100
- }
101
-
102
- impl core:: ops:: Sub < BlockCount > for BlockIdx {
103
- type Output = BlockIdx ;
104
- fn sub ( self , rhs : BlockCount ) -> BlockIdx {
105
- BlockIdx ( self . 0 - rhs. 0 )
106
- }
107
- }
108
-
109
- impl core:: ops:: SubAssign < BlockCount > for BlockIdx {
110
- fn sub_assign ( & mut self , rhs : BlockCount ) {
111
- self . 0 -= rhs. 0
112
- }
113
- }
114
-
115
- impl core:: ops:: Sub < BlockCount > for BlockCount {
116
- type Output = BlockCount ;
117
- fn sub ( self , rhs : BlockCount ) -> BlockCount {
118
- BlockCount ( self . 0 - rhs. 0 )
119
- }
120
- }
121
-
122
- impl core:: ops:: SubAssign < BlockCount > for BlockCount {
123
- fn sub_assign ( & mut self , rhs : BlockCount ) {
124
- self . 0 -= rhs. 0
125
- }
126
- }
127
-
128
36
impl core:: ops:: Deref for Block {
129
37
type Target = [ u8 ; 512 ] ;
130
38
fn deref ( & self ) -> & [ u8 ; 512 ] {
@@ -159,6 +67,33 @@ impl core::fmt::Debug for Block {
159
67
}
160
68
}
161
69
70
+ impl Default for Block {
71
+ fn default ( ) -> Self {
72
+ Self :: new ( )
73
+ }
74
+ }
75
+
76
+ /// A block device - a device which can read and write blocks (or
77
+ /// sectors). Only supports devices which are <= 2 TiB in size.
78
+ pub trait BlockDevice {
79
+ /// The errors that the `BlockDevice` can return. Must be debug formattable.
80
+ type Error : core:: fmt:: Debug ;
81
+ /// Read one or more blocks, starting at the given block index.
82
+ fn read ( & self , blocks : & mut [ Block ] , start_block_idx : BlockIdx ) -> Result < ( ) , Self :: Error > ;
83
+ /// Write one or more blocks, starting at the given block index.
84
+ fn write ( & self , blocks : & [ Block ] , start_block_idx : BlockIdx ) -> Result < ( ) , Self :: Error > ;
85
+ /// Determine how many blocks this device can hold.
86
+ fn num_blocks ( & self ) -> Result < BlockCount , Self :: Error > ;
87
+ }
88
+
89
+ /// The linear numeric address of a block (or sector).
90
+ ///
91
+ /// The first block on a disk gets `BlockIdx(0)` (which usually contains the
92
+ /// Master Boot Record).
93
+ #[ cfg_attr( feature = "defmt-log" , derive( defmt:: Format ) ) ]
94
+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
95
+ pub struct BlockIdx ( pub u32 ) ;
96
+
162
97
impl BlockIdx {
163
98
/// Convert a block index into a 64-bit byte offset from the start of the
164
99
/// volume. Useful if your underlying block device actually works in
@@ -174,6 +109,65 @@ impl BlockIdx {
174
109
}
175
110
}
176
111
112
+ impl core:: ops:: Add < BlockCount > for BlockIdx {
113
+ type Output = BlockIdx ;
114
+ fn add ( self , rhs : BlockCount ) -> BlockIdx {
115
+ BlockIdx ( self . 0 + rhs. 0 )
116
+ }
117
+ }
118
+
119
+ impl core:: ops:: AddAssign < BlockCount > for BlockIdx {
120
+ fn add_assign ( & mut self , rhs : BlockCount ) {
121
+ self . 0 += rhs. 0
122
+ }
123
+ }
124
+
125
+ impl core:: ops:: Sub < BlockCount > for BlockIdx {
126
+ type Output = BlockIdx ;
127
+ fn sub ( self , rhs : BlockCount ) -> BlockIdx {
128
+ BlockIdx ( self . 0 - rhs. 0 )
129
+ }
130
+ }
131
+
132
+ impl core:: ops:: SubAssign < BlockCount > for BlockIdx {
133
+ fn sub_assign ( & mut self , rhs : BlockCount ) {
134
+ self . 0 -= rhs. 0
135
+ }
136
+ }
137
+
138
+ /// The a number of blocks (or sectors).
139
+ ///
140
+ /// Add this to a `BlockIdx` to get an actual address on disk.
141
+ #[ cfg_attr( feature = "defmt-log" , derive( defmt:: Format ) ) ]
142
+ #[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
143
+ pub struct BlockCount ( pub u32 ) ;
144
+
145
+ impl core:: ops:: Add < BlockCount > for BlockCount {
146
+ type Output = BlockCount ;
147
+ fn add ( self , rhs : BlockCount ) -> BlockCount {
148
+ BlockCount ( self . 0 + rhs. 0 )
149
+ }
150
+ }
151
+
152
+ impl core:: ops:: AddAssign < BlockCount > for BlockCount {
153
+ fn add_assign ( & mut self , rhs : BlockCount ) {
154
+ self . 0 += rhs. 0
155
+ }
156
+ }
157
+
158
+ impl core:: ops:: Sub < BlockCount > for BlockCount {
159
+ type Output = BlockCount ;
160
+ fn sub ( self , rhs : BlockCount ) -> BlockCount {
161
+ BlockCount ( self . 0 - rhs. 0 )
162
+ }
163
+ }
164
+
165
+ impl core:: ops:: SubAssign < BlockCount > for BlockCount {
166
+ fn sub_assign ( & mut self , rhs : BlockCount ) {
167
+ self . 0 -= rhs. 0
168
+ }
169
+ }
170
+
177
171
impl BlockCount {
178
172
/// How many blocks are required to hold this many bytes.
179
173
///
@@ -200,6 +194,12 @@ impl BlockCount {
200
194
}
201
195
}
202
196
197
+ /// An iterator returned from `Block::range`.
198
+ pub struct BlockIter {
199
+ inclusive_end : BlockIdx ,
200
+ current : BlockIdx ,
201
+ }
202
+
203
203
impl BlockIter {
204
204
/// Create a new `BlockIter`, from the given start block, through (and
205
205
/// including) the given end block.
0 commit comments