Skip to content

Commit 595a12c

Browse files
committed
Implement nor flash traits for &mut
1 parent 387acbe commit 595a12c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

embedded-storage-async/src/nor_flash.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,32 @@ pub trait NorFlash: ReadNorFlash {
4848
/// can use the [`check_write`] helper function.
4949
async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>;
5050
}
51+
52+
impl<T: ErrorType> ErrorType for &mut T {
53+
type Error = T::Error;
54+
}
55+
56+
impl<T: ReadNorFlash> ReadNorFlash for &mut T {
57+
const READ_SIZE: usize = T::READ_SIZE;
58+
59+
async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
60+
T::read(self, offset, bytes)
61+
}
62+
63+
fn capacity(&self) -> usize {
64+
T::capacity(self)
65+
}
66+
}
67+
68+
impl<T: NorFlash> NorFlash for &mut T {
69+
const WRITE_SIZE: usize = T::WRITE_SIZE;
70+
const ERASE_SIZE: usize = T::ERASE_SIZE;
71+
72+
async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
73+
T::erase(self, from, to)
74+
}
75+
76+
async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
77+
T::write(self, offset, bytes)
78+
}
79+
}

src/nor_flash.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,35 @@ fn check_slice<T: ReadNorFlash>(
149149
Ok(())
150150
}
151151

152+
impl<T: ErrorType> ErrorType for &mut T {
153+
type Error = T::Error;
154+
}
155+
156+
impl<T: ReadNorFlash> ReadNorFlash for &mut T {
157+
const READ_SIZE: usize = T::READ_SIZE;
158+
159+
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
160+
T::read(self, offset, bytes)
161+
}
162+
163+
fn capacity(&self) -> usize {
164+
T::capacity(self)
165+
}
166+
}
167+
168+
impl<T: NorFlash> NorFlash for &mut T {
169+
const WRITE_SIZE: usize = T::WRITE_SIZE;
170+
const ERASE_SIZE: usize = T::ERASE_SIZE;
171+
172+
fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> {
173+
T::erase(self, from, to)
174+
}
175+
176+
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
177+
T::write(self, offset, bytes)
178+
}
179+
}
180+
152181
/// Marker trait for NorFlash relaxing the restrictions on `write`.
153182
///
154183
/// Writes to the same word twice are now allowed. The result is the logical AND of the

0 commit comments

Comments
 (0)