Skip to content

Commit

Permalink
Merge pull request #61 from liamkinne/cache-defmt
Browse files Browse the repository at this point in the history
Add defmt attributes for caches
  • Loading branch information
diondokter authored Jul 25, 2024
2 parents 855f3cf + 202bb6b commit 6a7bcc5
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Unreleased

- Add `defmt` attributes to cache types.

## 3.0.0 17-07-24

- *Breaking:* Map keys are now always passed by reference. This avoids extra cloning and memory use for bigger keys.
Expand Down
2 changes: 2 additions & 0 deletions src/cache/key_pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub(crate) trait KeyPointersCache<KEY: Key> {
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub(crate) struct CachedKeyPointers<KEY: Eq, const KEYS: usize> {
key_pointers: [Option<(KEY, NonZeroU32)>; KEYS],
}
Expand Down Expand Up @@ -77,6 +78,7 @@ impl<KEY: Key, const KEYS: usize> KeyPointersCache<KEY> for CachedKeyPointers<KE
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub(crate) struct UncachedKeyPointers;

impl<KEY: Key> KeyPointersCache<KEY> for UncachedKeyPointers {
Expand Down
5 changes: 5 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl<KEY: Key, T: PrivateKeyCacheImpl<KEY>> PrivateKeyCacheImpl<KEY> for &mut T
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub(crate) struct DirtTracker {
/// Managed from the library code.
///
Expand Down Expand Up @@ -198,6 +199,7 @@ impl DirtTracker {
/// This type of cache doesn't have to be kept around and may be constructed on every api call.
/// You could simply pass `&mut NoCache::new()` every time.
#[derive(Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct NoCache {
page_states: UncachedPageStates,
page_pointers: UncachedPagePointers,
Expand Down Expand Up @@ -266,6 +268,7 @@ impl<KEY: Key> PrivateKeyCacheImpl<KEY> for NoCache {
///
/// Make sure the page count is correct. If the number is lower than the actual amount, the code will panic at some point.
#[derive(Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct PageStateCache<const PAGE_COUNT: usize> {
dirt_tracker: DirtTracker,
page_states: CachedPageStates<PAGE_COUNT>,
Expand Down Expand Up @@ -339,6 +342,7 @@ impl<KEY: Key, const PAGE_COUNT: usize> PrivateKeyCacheImpl<KEY> for PageStateCa
///
/// Make sure the page count is correct. If the number is lower than the actual amount, the code will panic at some point.
#[derive(Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct PagePointerCache<const PAGE_COUNT: usize> {
dirt_tracker: DirtTracker,
page_states: CachedPageStates<PAGE_COUNT>,
Expand Down Expand Up @@ -417,6 +421,7 @@ impl<KEY: Key, const PAGE_COUNT: usize> PrivateKeyCacheImpl<KEY> for PagePointer
/// the chance of a cache hit.
/// The keys are cached in a fifo and any time its location is updated in cache it's added to the front.
#[derive(Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct KeyPointerCache<const PAGE_COUNT: usize, KEY: Key, const KEYS: usize> {
dirt_tracker: DirtTracker,
page_states: CachedPageStates<PAGE_COUNT>,
Expand Down
2 changes: 2 additions & 0 deletions src/cache/page_pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub(crate) trait PagePointersCache: Debug {

// Use NoneZeroU32 because we never store 0's in here (because of the first page marker)
// and so Option can make use of the niche so we save bytes
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub(crate) struct CachedPagePointers<const PAGE_COUNT: usize> {
after_erased_pointers: [Option<NonZeroU32>; PAGE_COUNT],
after_written_pointers: [Option<NonZeroU32>; PAGE_COUNT],
Expand Down Expand Up @@ -138,6 +139,7 @@ impl<const PAGE_COUNT: usize> PagePointersCache for CachedPagePointers<PAGE_COUN
}

#[derive(Debug, Default)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub(crate) struct UncachedPagePointers;

impl PagePointersCache for UncachedPagePointers {
Expand Down
2 changes: 2 additions & 0 deletions src/cache/page_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) trait PageStatesCache: Debug {
fn invalidate_cache_state(&mut self);
}

#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub(crate) struct CachedPageStates<const PAGE_COUNT: usize> {
pages: [Option<PageState>; PAGE_COUNT],
}
Expand Down Expand Up @@ -55,6 +56,7 @@ impl<const PAGE_COUNT: usize> PageStatesCache for CachedPageStates<PAGE_COUNT> {
}

#[derive(Debug, Default)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub(crate) struct UncachedPageStates;

impl PageStatesCache for UncachedPageStates {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ async fn partial_close_page<S: NorFlash>(

/// The state of a page
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
enum PageState {
/// This page was fully written and has now been sealed
Closed,
Expand Down

0 comments on commit 6a7bcc5

Please sign in to comment.