Skip to content

Added usize to read, elided lifetimes, and docs #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -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<R, I>
where
R: Region,
I: Iterator<Item = R>,
{
/// 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<R, I>;
fn overlaps(self, memory: &[u8], base_address: u32) -> OverlapIterator<R, I>;
}

impl<'a, R, I> Iterator for OverlapIterator<'a, R, I>
@@ -32,7 +32,7 @@ where
fn next(&mut self) -> Option<Self::Item> {
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<R, I> IterableByOverlaps<R, I> for I
where
R: Region,
I: Iterator<Item = R>,
{
fn overlaps(self, memory: &'a [u8], base_address: u32) -> OverlapIterator<R, I> {
fn overlaps(self, memory: &[u8], base_address: u32) -> OverlapIterator<R, I> {
OverlapIterator {
memory,
regions: self,
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<usize, Self::Error>;

/// The capacity of the storage peripheral in bytes.
fn capacity(&self) -> usize;
20 changes: 10 additions & 10 deletions src/nor_flash.rs
Original file line number Diff line number Diff line change
@@ -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<usize, Self::Error>;

/// The capacity of the peripheral in bytes.
fn capacity(&self) -> usize;
@@ -156,7 +156,7 @@ impl<T: ErrorType> ErrorType for &mut T {
impl<T: ReadNorFlash> 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<usize, Self::Error> {
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<S> 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<usize, Self::Error> {
// 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<S> 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<S> 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<usize, Self::Error> {
// 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<S> Storage for RmwMultiwriteNorFlashStorage<'_, S>
where
S: MultiwriteNorFlash,
{