Skip to content

Commit

Permalink
Merge pull request #12 from lulf/peek-pop-many
Browse files Browse the repository at this point in the history
feat: support peek_many and pop_many
  • Loading branch information
diondokter authored Dec 16, 2023
2 parents 418ea00 + cfe1396 commit b3b0e99
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 113 deletions.
10 changes: 5 additions & 5 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl ItemHeader {
return Err(Error::BufferTooSmall(read_len));
}
if data_address + read_len as u32 > end_address {
return Ok(MaybeItem::Corrupted(self));
return Ok(MaybeItem::Corrupted(self, data_buffer));
}

flash
Expand All @@ -107,7 +107,7 @@ impl ItemHeader {
data_buffer,
}))
} else {
return Ok(MaybeItem::Corrupted(self));
return Ok(MaybeItem::Corrupted(self, data_buffer));
}
}
}
Expand Down Expand Up @@ -283,7 +283,7 @@ pub fn read_items<S: NorFlash, R>(
start_address,
end_address,
|flash, header, address| match header.read_item(flash, data_buffer, address, end_address) {
Ok(MaybeItem::Corrupted(_)) => {
Ok(MaybeItem::Corrupted(_, _)) => {
#[cfg(feature = "defmt")]
defmt::error!(
"Found a corrupted item at {:X}. Skipping...",
Expand Down Expand Up @@ -359,15 +359,15 @@ pub fn find_next_free_item_spot<S: NorFlash>(

#[derive(Debug)]
pub enum MaybeItem<'d> {
Corrupted(ItemHeader),
Corrupted(ItemHeader, &'d mut [u8]),
Erased(ItemHeader),
Present(Item<'d>),
}

impl<'d> MaybeItem<'d> {
pub fn unwrap<E>(self) -> Result<Item<'d>, Error<E>> {
match self {
MaybeItem::Corrupted(_) => Err(Error::Corrupted),
MaybeItem::Corrupted(_, _) => Err(Error::Corrupted),
MaybeItem::Erased(_) => panic!("Cannot unwrap an erased item"),
MaybeItem::Present(item) => Ok(item),
}
Expand Down
2 changes: 1 addition & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A module for storing key-value pairs in flash with minimal erase cycles.
//!
//! When a key-value is stored, it overwrites the any old items with the same key.
//!
//!
//! Make sure to use the same [StorageItem] type on a given range in flash.
//! In theory you could use multiple types if you're careful, but they must at least have the same key definition and format.
//!
Expand Down
Loading

0 comments on commit b3b0e99

Please sign in to comment.