Skip to content

Commit

Permalink
Force everyone to use &mut impl Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
diondokter committed Mar 18, 2024
1 parent 067b99d commit 02a8374
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 238 deletions.
3 changes: 1 addition & 2 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ pub(crate) use page_states::PageStatesCache;
/// Trait implemented by all cache types
#[allow(private_bounds)]
pub trait CacheImpl: PrivateCacheImpl {}
impl<T: CacheImpl> CacheImpl for &mut T {}

/// Trait implemented by all cache types that know about keys
#[allow(private_bounds)]
pub trait KeyCacheImpl<KEY: Eq>: CacheImpl + PrivateKeyCacheImpl<KEY> {}
impl<KEY: Eq, T: KeyCacheImpl<KEY>> KeyCacheImpl<KEY> for &mut T {}

pub(crate) trait Invalidate {
fn invalidate_cache_state(&mut self);
Expand Down
16 changes: 8 additions & 8 deletions src/cache/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod queue_tests {
);
}

async fn run_test(mut cache: impl CacheImpl) -> FlashStatsResult {
async fn run_test(cache: &mut impl CacheImpl) -> FlashStatsResult {
let mut flash =
mock_flash::MockFlashBase::<NUM_PAGES, 1, 256>::new(WriteCountCheck::Twice, None, true);
const FLASH_RANGE: Range<u32> = 0x00..0x400;
Expand All @@ -69,11 +69,11 @@ mod queue_tests {
let data = vec![i as u8; i % 20 + 1];

println!("PUSH");
push(&mut flash, FLASH_RANGE, &mut cache, &data, true)
push(&mut flash, FLASH_RANGE, cache, &data, true)
.await
.unwrap();
assert_eq!(
peek(&mut flash, FLASH_RANGE, &mut cache, &mut data_buffer)
peek(&mut flash, FLASH_RANGE, cache, &mut data_buffer)
.await
.unwrap()
.unwrap(),
Expand All @@ -82,7 +82,7 @@ mod queue_tests {
);
println!("POP");
assert_eq!(
pop(&mut flash, FLASH_RANGE, &mut cache, &mut data_buffer)
pop(&mut flash, FLASH_RANGE, cache, &mut data_buffer)
.await
.unwrap()
.unwrap(),
Expand All @@ -91,7 +91,7 @@ mod queue_tests {
);
println!("PEEK");
assert_eq!(
peek(&mut flash, FLASH_RANGE, &mut cache, &mut data_buffer)
peek(&mut flash, FLASH_RANGE, cache, &mut data_buffer)
.await
.unwrap(),
None,
Expand Down Expand Up @@ -257,7 +257,7 @@ mod map_tests {
}
}

async fn run_test(mut cache: impl KeyCacheImpl<u8>) -> FlashStatsResult {
async fn run_test(cache: &mut impl KeyCacheImpl<u8>) -> FlashStatsResult {
let mut flash =
mock_flash::MockFlashBase::<NUM_PAGES, 1, 256>::new(WriteCountCheck::Twice, None, true);
const FLASH_RANGE: Range<u32> = 0x00..0x400;
Expand All @@ -276,7 +276,7 @@ mod map_tests {
value: vec![i as u8; LENGHT_PER_KEY[i]],
};

store_item::<_, _>(&mut flash, FLASH_RANGE, &mut cache, &mut data_buffer, &item)
store_item::<_, _>(&mut flash, FLASH_RANGE, cache, &mut data_buffer, &item)
.await
.unwrap();
}
Expand All @@ -285,7 +285,7 @@ mod map_tests {
let item = fetch_item::<MockStorageItem, _>(
&mut flash,
FLASH_RANGE,
&mut cache,
cache,
&mut data_buffer,
i as u8,
)
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use core::{
};
use embedded_storage_async::nor_flash::NorFlash;

use crate::cache::NoCache;

pub mod cache;
mod item;
pub mod map;
Expand Down Expand Up @@ -50,15 +48,16 @@ impl<const SIZE: usize> DerefMut for AlignedBuf<SIZE> {
async fn try_general_repair<S: NorFlash>(
flash: &mut S,
flash_range: Range<u32>,
cache: &mut impl PrivateCacheImpl,
) -> Result<(), BasicError<S::Error>> {
// Loop through the pages and get their state. If one returns the corrupted error,
// the page is likely half-erased. Fix for that is to re-erase again to hopefully finish the job.
for page_index in get_pages::<S>(flash_range.clone(), 0) {
if matches!(
get_page_state(flash, flash_range.clone(), &mut NoCache::new(), page_index).await,
get_page_state(flash, flash_range.clone(), cache, page_index).await,
Err(BasicError::Corrupted { .. })
) {
open_page(flash, flash_range.clone(), &mut NoCache::new(), page_index).await?;
open_page(flash, flash_range.clone(), cache, page_index).await?;
}
}

Expand Down
Loading

0 comments on commit 02a8374

Please sign in to comment.