|
| 1 | +//! Adapters to/from `embedded_storage` traits. |
| 2 | +
|
| 3 | +// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`. |
| 4 | +#[cfg(feature = "defmt-03")] |
| 5 | +use defmt_03 as defmt; |
| 6 | + |
| 7 | +use embedded_io::{Error, ErrorKind, Read, Seek, SeekFrom, Write}; |
| 8 | +use embedded_storage::nor_flash::{NorFlashError, NorFlashErrorKind}; |
| 9 | +use embedded_storage::{ReadStorage, Storage}; |
| 10 | + |
| 11 | +/// Adapter from `embedded_storage` traits. |
| 12 | +#[derive(Clone)] |
| 13 | +pub struct FromEmbeddedStorage<T: ?Sized> { |
| 14 | + position: u32, |
| 15 | + inner: T, |
| 16 | +} |
| 17 | + |
| 18 | +impl<T> FromEmbeddedStorage<T> { |
| 19 | + /// Create a new adapter. |
| 20 | + pub fn new(inner: T) -> Self { |
| 21 | + Self { position: 0, inner } |
| 22 | + } |
| 23 | + |
| 24 | + /// Consume the adapter, returning the inner object. |
| 25 | + pub fn into_inner(self) -> T { |
| 26 | + self.inner |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl<T: ?Sized> FromEmbeddedStorage<T> { |
| 31 | + /// Borrow the inner object. |
| 32 | + pub fn inner(&self) -> &T { |
| 33 | + &self.inner |
| 34 | + } |
| 35 | + |
| 36 | + /// Mutably borrow the inner object. |
| 37 | + pub fn inner_mut(&mut self) -> &mut T { |
| 38 | + &mut self.inner |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl<T: ?Sized> embedded_io::ErrorType for FromEmbeddedStorage<T> { |
| 43 | + type Error = StorageIOError; |
| 44 | +} |
| 45 | + |
| 46 | +impl<T: ReadStorage<Error = E> + ?Sized, E: Into<StorageIOError>> embedded_io::Read |
| 47 | + for FromEmbeddedStorage<T> |
| 48 | +{ |
| 49 | + fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 50 | + self.inner.read(self.position, buf).map_err(|e| e.into())?; |
| 51 | + self.position += buf.len() as u32; |
| 52 | + Ok(buf.len()) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<T: Storage<Error = E> + ?Sized, E: Into<StorageIOError>> embedded_io::Write |
| 57 | + for FromEmbeddedStorage<T> |
| 58 | +{ |
| 59 | + fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 60 | + self.inner.write(self.position, buf).map_err(|e| e.into())?; |
| 61 | + self.position += buf.len() as u32; |
| 62 | + Ok(buf.len()) |
| 63 | + } |
| 64 | + |
| 65 | + fn flush(&mut self) -> Result<(), Self::Error> { |
| 66 | + Ok(()) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<T: ReadStorage<Error = E> + ?Sized, E: Into<StorageIOError>> embedded_io::Seek |
| 71 | + for FromEmbeddedStorage<T> |
| 72 | +{ |
| 73 | + fn seek(&mut self, pos: SeekFrom) -> Result<u64, Self::Error> { |
| 74 | + let new_position = match pos { |
| 75 | + SeekFrom::Start(pos) => pos as i64, |
| 76 | + SeekFrom::End(offset) => self.inner.capacity() as i64 + offset, |
| 77 | + SeekFrom::Current(offset) => self.position as i64 + offset, |
| 78 | + }; |
| 79 | + self.position = new_position as u32; |
| 80 | + Ok(self.position as u64) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Adapter to `embedded_storage` traits. |
| 85 | +#[derive(Clone)] |
| 86 | +pub struct ToEmbeddedStorage<T: ?Sized> { |
| 87 | + capacity: usize, |
| 88 | + inner: T, |
| 89 | +} |
| 90 | + |
| 91 | +impl<T: Seek> ToEmbeddedStorage<T> { |
| 92 | + /// Create a new adapter. |
| 93 | + pub fn new(mut inner: T) -> Self { |
| 94 | + let capacity = inner.seek(SeekFrom::End(0)).unwrap() as usize; |
| 95 | + Self { inner, capacity } |
| 96 | + } |
| 97 | + |
| 98 | + /// Consume the adapter, returning the inner object. |
| 99 | + pub fn into_inner(self) -> T { |
| 100 | + self.inner |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl<T: ?Sized> ToEmbeddedStorage<T> { |
| 105 | + /// Borrow the inner object. |
| 106 | + pub fn inner(&self) -> &T { |
| 107 | + &self.inner |
| 108 | + } |
| 109 | + |
| 110 | + /// Mutably borrow the inner object. |
| 111 | + pub fn inner_mut(&mut self) -> &mut T { |
| 112 | + &mut self.inner |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl<T: Read + Seek + ?Sized> ReadStorage for ToEmbeddedStorage<T> { |
| 117 | + type Error = T::Error; |
| 118 | + |
| 119 | + fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { |
| 120 | + self.inner.seek(SeekFrom::Start(offset as u64))?; |
| 121 | + let mut read = 0; |
| 122 | + while read < bytes.len() { |
| 123 | + read += self.inner.read(&mut bytes[read..])?; |
| 124 | + } |
| 125 | + Ok(()) |
| 126 | + } |
| 127 | + |
| 128 | + fn capacity(&self) -> usize { |
| 129 | + self.capacity |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl<T: Read + Write + Seek + ?Sized> Storage for ToEmbeddedStorage<T> { |
| 134 | + fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { |
| 135 | + self.inner.seek(SeekFrom::Start(offset as u64))?; |
| 136 | + let mut written = 0; |
| 137 | + while written < bytes.len() { |
| 138 | + written += self.inner.write(&bytes[written..])?; |
| 139 | + } |
| 140 | + Ok(()) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/// An error type that is implementing embedded_io::Error and that the concret |
| 145 | +/// Storage::Error type should be able to convert to, to allow error compatibility |
| 146 | +/// with the embedded_io layer. |
| 147 | +#[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 148 | +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] |
| 149 | +pub struct StorageIOError { |
| 150 | + kind: ErrorKind, |
| 151 | +} |
| 152 | + |
| 153 | +impl StorageIOError { |
| 154 | + /// Create a new StorageIOError. |
| 155 | + pub fn new(kind: ErrorKind) -> Self { |
| 156 | + Self { kind } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +impl Error for StorageIOError { |
| 161 | + fn kind(&self) -> ErrorKind { |
| 162 | + self.kind |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl<E: NorFlashError> From<E> for StorageIOError { |
| 167 | + fn from(value: E) -> Self { |
| 168 | + match value.kind() { |
| 169 | + NorFlashErrorKind::NotAligned => Self::new(ErrorKind::InvalidInput), |
| 170 | + NorFlashErrorKind::OutOfBounds => Self::new(ErrorKind::InvalidInput), |
| 171 | + _ => Self::new(ErrorKind::Other), |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments