Skip to content

Commit

Permalink
refactor(map): clean map iterator api, add docs
Browse files Browse the repository at this point in the history
Signed-off-by: Haobo Gu <[email protected]>
  • Loading branch information
HaoboGu committed Dec 11, 2024
1 parent a05daa2 commit c297631
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ std = []
arrayvec = ["dep:arrayvec"]
alloc = []
heapless = ["dep:heapless"]
# Enable the raw access of items. This feature is NOT bounded by the semver.
raw_access = []
_test = ["dep:futures", "dep:approx", "std", "arrayvec", "alloc", "heapless"]

[lints.rust]
Expand Down
33 changes: 21 additions & 12 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,27 @@ use self::{

use super::*;

/// Iterator which iterates all valid items
/// Iterator which iterates all non-erased & non-corrupted items in the map.
///
/// The iterator will return the (Key, Value) tuple when calling `next()`.
/// If the iterator ends, it will return `None`.
///
/// The following is a simple example of how to use the iterator:
/// ```rust
/// // Create the iterator
/// let mut iterator = get_item_iter(&mut flash, flash_range.clone());
///
/// // Iterate through all items
/// loop {
/// // Suppose the Key and Value types are u8, u32
/// if let Ok(Some(item)) = iterator.next::<u8, u32>(&mut buffer).await {
/// // Do something with the item
/// } else {
/// // Iterator ends
/// break;
/// }
/// }
/// ```
pub struct MapItemIter<'d, S: NorFlash> {
flash: &'d mut S,
flash_range: Range<u32>,
Expand Down Expand Up @@ -182,17 +202,6 @@ impl<'d, S: NorFlash> MapItemIter<'d, S> {
}
}

/// Get an iterator that iterates over all non-erased & non-corrupted items in the map.
pub async fn get_next_item<'d, 'a, K: Key, V: Value<'a>, S: NorFlash>(
data_buffer: &'a mut [u8],
iter: &mut MapItemIter<'d, S>,
) -> Result<Option<(K, V)>, Error<S::Error>>
where
'd: 'a,
{
iter.next(data_buffer).await
}

/// Get an iterator that iterates over all non-erased & non-corrupted items in the map.
pub fn get_item_iter<'d, S: NorFlash>(
flash: &'d mut S,
Expand Down

0 comments on commit c297631

Please sign in to comment.