embedded_fat/
blockdevice.rs1#[derive(Clone)]
13pub struct Block {
14 pub contents: [u8; Block::LEN],
16}
17
18#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
22#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
23pub struct BlockIdx(pub u32);
24
25#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
28#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
29pub struct BlockCount(pub u32);
30
31pub struct BlockIter {
33 inclusive_end: BlockIdx,
34 current: BlockIdx,
35}
36
37pub trait BlockDevice {
40 type Error: core::fmt::Debug;
42 async fn read(
44 &self,
45 blocks: &mut [Block],
46 start_block_idx: BlockIdx,
47 reason: &str,
48 ) -> Result<(), Self::Error>;
49 async fn write(&self, blocks: &[Block], start_block_idx: BlockIdx) -> Result<(), Self::Error>;
51 async fn num_blocks(&self) -> Result<BlockCount, Self::Error>;
53}
54
55impl Block {
56 pub const LEN: usize = 512;
60
61 pub const LEN_U32: u32 = 512;
63
64 pub fn new() -> Block {
66 Block {
67 contents: [0u8; Self::LEN],
68 }
69 }
70}
71
72impl Default for Block {
73 fn default() -> Self {
74 Self::new()
75 }
76}
77
78impl core::ops::Add<BlockCount> for BlockIdx {
79 type Output = BlockIdx;
80 fn add(self, rhs: BlockCount) -> BlockIdx {
81 BlockIdx(self.0 + rhs.0)
82 }
83}
84
85impl core::ops::AddAssign<BlockCount> for BlockIdx {
86 fn add_assign(&mut self, rhs: BlockCount) {
87 self.0 += rhs.0
88 }
89}
90
91impl core::ops::Add<BlockCount> for BlockCount {
92 type Output = BlockCount;
93 fn add(self, rhs: BlockCount) -> BlockCount {
94 BlockCount(self.0 + rhs.0)
95 }
96}
97
98impl core::ops::AddAssign<BlockCount> for BlockCount {
99 fn add_assign(&mut self, rhs: BlockCount) {
100 self.0 += rhs.0
101 }
102}
103
104impl core::ops::Sub<BlockCount> for BlockIdx {
105 type Output = BlockIdx;
106 fn sub(self, rhs: BlockCount) -> BlockIdx {
107 BlockIdx(self.0 - rhs.0)
108 }
109}
110
111impl core::ops::SubAssign<BlockCount> for BlockIdx {
112 fn sub_assign(&mut self, rhs: BlockCount) {
113 self.0 -= rhs.0
114 }
115}
116
117impl core::ops::Sub<BlockCount> for BlockCount {
118 type Output = BlockCount;
119 fn sub(self, rhs: BlockCount) -> BlockCount {
120 BlockCount(self.0 - rhs.0)
121 }
122}
123
124impl core::ops::SubAssign<BlockCount> for BlockCount {
125 fn sub_assign(&mut self, rhs: BlockCount) {
126 self.0 -= rhs.0
127 }
128}
129
130impl core::ops::Deref for Block {
131 type Target = [u8; 512];
132 fn deref(&self) -> &[u8; 512] {
133 &self.contents
134 }
135}
136
137impl core::ops::DerefMut for Block {
138 fn deref_mut(&mut self) -> &mut [u8; 512] {
139 &mut self.contents
140 }
141}
142
143impl core::fmt::Debug for Block {
144 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
145 writeln!(fmt, "Block:")?;
146 for line in self.contents.chunks(32) {
147 for b in line {
148 write!(fmt, "{:02x}", b)?;
149 }
150 write!(fmt, " ")?;
151 for &b in line {
152 if (0x20..=0x7F).contains(&b) {
153 write!(fmt, "{}", b as char)?;
154 } else {
155 write!(fmt, ".")?;
156 }
157 }
158 writeln!(fmt)?;
159 }
160 Ok(())
161 }
162}
163
164impl BlockIdx {
165 pub fn into_bytes(self) -> u64 {
169 (u64::from(self.0)) * (Block::LEN as u64)
170 }
171
172 pub fn range(self, num: BlockCount) -> BlockIter {
175 BlockIter::new(self, self + BlockCount(num.0))
176 }
177}
178
179impl BlockCount {
180 pub const fn from_bytes(byte_count: u32) -> BlockCount {
191 let mut count = byte_count / Block::LEN_U32;
192 if (count * Block::LEN_U32) != byte_count {
193 count += 1;
194 }
195 BlockCount(count)
196 }
197
198 pub fn offset_bytes(self, offset: u32) -> Self {
201 BlockCount(self.0 + (offset / Block::LEN_U32))
202 }
203}
204
205impl BlockIter {
206 pub const fn new(start: BlockIdx, inclusive_end: BlockIdx) -> BlockIter {
209 BlockIter {
210 inclusive_end,
211 current: start,
212 }
213 }
214}
215
216impl core::iter::Iterator for BlockIter {
217 type Item = BlockIdx;
218 fn next(&mut self) -> Option<Self::Item> {
219 if self.current.0 >= self.inclusive_end.0 {
220 None
221 } else {
222 let this = self.current;
223 self.current += BlockCount(1);
224 Some(this)
225 }
226 }
227}