diff --git a/src/iter.rs b/src/iter.rs index 7878e6e..7fc4118 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -13,13 +13,13 @@ where } /// Trait allowing us to automatically add an `overlaps` function to all iterators over [`Region`] -pub trait IterableByOverlaps<'a, R, I> +pub trait IterableByOverlaps where R: Region, I: Iterator, { /// Obtain an [`OverlapIterator`] over a subslice of `memory` that overlaps with the region in `self` - fn overlaps(self, memory: &'a [u8], base_address: u32) -> OverlapIterator; + fn overlaps(self, memory: &[u8], base_address: u32) -> OverlapIterator; } impl<'a, R, I> Iterator for OverlapIterator<'a, R, I> @@ -32,7 +32,7 @@ where fn next(&mut self) -> Option { let mem_start = self.base_address; let mem_end = self.base_address + self.memory.len() as u32; - while let Some(region) = self.regions.next() { + for region in self.regions.by_ref() { if mem_start < region.end() && mem_end >= region.start() { let addr_start = core::cmp::max(mem_start, region.start()); let addr_end = core::cmp::min(mem_end, region.end()); @@ -46,12 +46,12 @@ where } /// Blanket implementation for all types implementing [`Iterator`] over [`Regions`] -impl<'a, R, I> IterableByOverlaps<'a, R, I> for I +impl IterableByOverlaps for I where R: Region, I: Iterator, { - fn overlaps(self, memory: &'a [u8], base_address: u32) -> OverlapIterator { + fn overlaps(self, memory: &[u8], base_address: u32) -> OverlapIterator { OverlapIterator { memory, regions: self, diff --git a/src/lib.rs b/src/lib.rs index 3070723..2acbd1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,10 +34,8 @@ pub trait ReadStorage { /// Read a slice of data from the storage peripheral, starting the read /// operation at the given address offset, and reading `bytes.len()` bytes. - /// - /// This should throw an error in case `bytes.len()` will be larger than - /// `self.capacity() - offset`. - fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>; + /// This should return how many bytes were read into the `bytes` slice allowing for sub-slices that are exclusively read information, or an error + fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result; /// The capacity of the storage peripheral in bytes. fn capacity(&self) -> usize; diff --git a/src/nor_flash.rs b/src/nor_flash.rs index c20ddba..0bbd0f0 100644 --- a/src/nor_flash.rs +++ b/src/nor_flash.rs @@ -66,7 +66,7 @@ pub trait ReadNorFlash: ErrorType { /// /// Returns an error if the arguments are not aligned or out of bounds. The implementation /// can use the [`check_read`] helper function. - fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>; + fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result; /// The capacity of the peripheral in bytes. fn capacity(&self) -> usize; @@ -156,7 +156,7 @@ impl ErrorType for &mut T { impl ReadNorFlash for &mut T { const READ_SIZE: usize = T::READ_SIZE; - fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { + fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result { T::read(self, offset, bytes) } @@ -214,7 +214,7 @@ impl Region for Page { } } -/// +/// Read-Modify-Write (RMW) storage struct for NOR flash memory. If the NOR flash memory has support for multiple writes to the same memory location use [`RmwMultiwriteNorFlashStorage`](RmwMultiwriteNorFlashStorage). `RmwNorFlashStorage` always needs to erase the page before writing, where as [`RmwMultiwriteNorFlashStorage`](RmwMultiwriteNorFlashStorage) only needs to erase in accordance with the rules specified in the [`MultiwriteNorFlash`](MultiwriteNorFlash) trait. pub struct RmwNorFlashStorage<'a, S> { storage: S, merge_buffer: &'a mut [u8], @@ -240,13 +240,13 @@ where } } -impl<'a, S> ReadStorage for RmwNorFlashStorage<'a, S> +impl ReadStorage for RmwNorFlashStorage<'_, S> where S: ReadNorFlash, { type Error = S::Error; - fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { + fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result { // Nothing special to be done for reads self.storage.read(offset, bytes) } @@ -256,7 +256,7 @@ where } } -impl<'a, S> Storage for RmwNorFlashStorage<'a, S> +impl Storage for RmwNorFlashStorage<'_, S> where S: NorFlash, { @@ -289,7 +289,7 @@ where } } -/// +/// Read-Modify-Write (RMW) storage struct for NOR flash memory that supports multiple writes to the same memory location without necessarily erasing. Rules governing this are specified in the [`MultiwriteNorFlash`](MultiwriteNorFlash) trait. pub struct RmwMultiwriteNorFlashStorage<'a, S> { storage: S, merge_buffer: &'a mut [u8], @@ -315,13 +315,13 @@ where } } -impl<'a, S> ReadStorage for RmwMultiwriteNorFlashStorage<'a, S> +impl ReadStorage for RmwMultiwriteNorFlashStorage<'_, S> where S: ReadNorFlash, { type Error = S::Error; - fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { + fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result { // Nothing special to be done for reads self.storage.read(offset, bytes) } @@ -331,7 +331,7 @@ where } } -impl<'a, S> Storage for RmwMultiwriteNorFlashStorage<'a, S> +impl Storage for RmwMultiwriteNorFlashStorage<'_, S> where S: MultiwriteNorFlash, {