Skip to content

Commit

Permalink
rename headeriter
Browse files Browse the repository at this point in the history
  • Loading branch information
lulf committed Jan 8, 2024
1 parent 25b772f commit 6015241
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
33 changes: 17 additions & 16 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub async fn find_next_free_item_spot<S: NorFlash>(
end_address: u32,
data_length: u32,
) -> Result<Option<u32>, Error<S::Error>> {
let (_, free_item_address) = HeaderIter::new(start_address, end_address)
let (_, free_item_address) = ItemHeaderIter::new(start_address, end_address)
.traverse(flash, |_, _| ControlFlow::Continue(()))
.await?;
if let Some(available) = ItemHeader::available_data_bytes::<S>(end_address - free_item_address)
Expand Down Expand Up @@ -421,29 +421,30 @@ pub async fn is_page_empty<S: NorFlash>(
calculate_page_end_address::<S>(flash_range.clone(), page_index)
- S::WORD_SIZE as u32;

let mut it = HeaderIter::new(page_data_start_address, page_data_end_address);
Ok(it
.traverse(flash, |header, _| match header.crc {
Some(_) => ControlFlow::Break(()),
None => ControlFlow::Continue(()),
})
.await?
.0
.is_none())
Ok(
ItemHeaderIter::new(page_data_start_address, page_data_end_address)
.traverse(flash, |header, _| match header.crc {
Some(_) => ControlFlow::Break(()),
None => ControlFlow::Continue(()),
})
.await?
.0
.is_none(),
)
}
PageState::PartialOpen => Ok(false),
PageState::Open => Ok(true),
}
}

pub struct ItemIter {
header: HeaderIter,
header: ItemHeaderIter,
}

impl ItemIter {
pub fn new(start_address: u32, end_address: u32) -> Self {
Self {
header: HeaderIter::new(start_address, end_address),
header: ItemHeaderIter::new(start_address, end_address),
}
}

Expand All @@ -466,7 +467,6 @@ impl ItemIter {
self.header.current_address
);
data_buffer.replace(buffer);
continue;
}
MaybeItem::Erased(_, buffer) => {
data_buffer.replace(buffer);
Expand All @@ -480,12 +480,12 @@ impl ItemIter {
}
}

pub struct HeaderIter {
pub struct ItemHeaderIter {
current_address: u32,
end_address: u32,
}

impl HeaderIter {
impl ItemHeaderIter {
pub fn new(start_address: u32, end_address: u32) -> Self {
Self {
current_address: start_address,
Expand All @@ -501,7 +501,8 @@ impl HeaderIter {
self.traverse(flash, |_, _| ControlFlow::Break(())).await
}

/// Traverse all headers until the callback determines the next element.
/// Traverse headers until the callback returns `Break`. If the callback returns `Continue`,
/// the element is skipped and traversal continues.
///
/// If the end of the headers is reached, a `None` item header is returned.
pub async fn traverse<S: NorFlash>(
Expand Down
13 changes: 7 additions & 6 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
//! let flash_range = 0x1000..0x3000;
//! // We need to give the crate a buffer to work with.
//! // It must be big enough to serialize the biggest value of your storage type in,
//! // rounded up to to word alignment of the flash.
//! // rounded up to to word alignment of the flash. Some kinds of flash may require
//! // this buffer to be aligned in RAM as well.
//! let mut data_buffer = [0; 100];
//!
//! // We can fetch an item from the flash.
Expand Down Expand Up @@ -397,7 +398,7 @@ pub async fn store_item<I: StorageItem, S: NorFlash>(
}
}

// If we get here, we just freshly partially closed a new page, so this should succeed
// If we get here, we just freshly partially closed a new page, so the next loop iteration should succeed.
recursion_level += 1;
}
}
Expand Down Expand Up @@ -704,7 +705,7 @@ mod tests {
let mut flash = MockFlashBig::default();
let flash_range = 0x000..0x1000;

let mut data_buffer = [0; 128];
let mut data_buffer = AlignedBuf([0; 128]);

let item =
fetch_item::<MockStorageItem, _>(&mut flash, flash_range.clone(), &mut data_buffer, 0)
Expand Down Expand Up @@ -854,7 +855,7 @@ mod tests {
const UPPER_BOUND: u8 = 2;

let mut tiny_flash = MockFlashTiny::default();
let mut data_buffer = [0; 128];
let mut data_buffer = AlignedBuf([0; 128]);

for i in 0..UPPER_BOUND {
let item = MockStorageItem {
Expand Down Expand Up @@ -904,7 +905,7 @@ mod tests {
const UPPER_BOUND: u8 = 67;

let mut big_flash = MockFlashBig::default();
let mut data_buffer = [0; 128];
let mut data_buffer = AlignedBuf([0; 128]);

for i in 0..UPPER_BOUND {
let item = MockStorageItem {
Expand Down Expand Up @@ -952,7 +953,7 @@ mod tests {
#[test]
async fn store_many_items_big() {
let mut flash = mock_flash::MockFlashBase::<4, 1, 4096>::default();
let mut data_buffer = [0; 128];
let mut data_buffer = AlignedBuf([0; 128]);

const LENGHT_PER_KEY: [usize; 24] = [
11, 13, 6, 13, 13, 10, 2, 3, 5, 36, 1, 65, 4, 6, 1, 15, 10, 7, 3, 15, 9, 3, 4, 5,
Expand Down
6 changes: 3 additions & 3 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
//! });
//! ```

use crate::item::{find_next_free_item_spot, is_page_empty, HeaderIter, Item, ItemHeader};
use crate::item::{find_next_free_item_spot, is_page_empty, Item, ItemHeader, ItemHeaderIter};

use super::*;
use embedded_storage_async::nor_flash::MultiwriteNorFlash;
Expand Down Expand Up @@ -373,7 +373,7 @@ impl<'d, S: NorFlash> QueueIterator<'d, S> {
- S::WORD_SIZE as u32;

// Search for the first item with data
let mut it = HeaderIter::new(current_address, page_data_end_address);
let mut it = ItemHeaderIter::new(current_address, page_data_end_address);
loop {
if let (Some(found_item_header), found_item_address) = it
.traverse(self.flash, |header, _| {
Expand Down Expand Up @@ -481,7 +481,7 @@ pub async fn find_max_fit<S: NorFlash>(
let page_data_end_address =
calculate_page_end_address::<S>(flash_range.clone(), current_page) - S::WORD_SIZE as u32;

let next_item_address = HeaderIter::new(page_data_start_address, page_data_end_address)
let next_item_address = ItemHeaderIter::new(page_data_start_address, page_data_end_address)
.traverse(flash, |_, _| ControlFlow::Continue(()))
.await?
.1;
Expand Down

0 comments on commit 6015241

Please sign in to comment.