Skip to content

Commit

Permalink
storage: add StorageIterate
Browse files Browse the repository at this point in the history
This allows iteration over entries within a a storage instance.
  • Loading branch information
codyps authored and codysch committed Apr 11, 2023
1 parent 553823d commit 3f1ebe2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::any::Any;
use core::ffi::CStr;
use core::fmt::{self, Debug};

use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -132,6 +133,23 @@ where
}
}

pub trait StorageIterate {
type Error: Debug;
type Entry: StorageEntry;
type Entries<'a>: Iterator<Item = Result<Self::Entry, Self::Error>>
where
Self: 'a;

fn entries(&self) -> Result<Self::Entries<'_>, Self::Error>;
}

pub trait StorageEntry {
fn name_cstr(&self) -> &CStr;
fn name(&self) -> Option<&str> {
self.name_cstr().to_str().ok()
}
}

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum StorageError<R, S> {
Expand Down Expand Up @@ -264,6 +282,21 @@ where
}
}

impl<const N: usize, R, S> StorageIterate for StorageImpl<N, R, S>
where
S: SerDe,
R: StorageIterate,
{
type Error = R::Error;
type Entry = R::Entry;
type Entries<'a> = R::Entries<'a>
where Self: 'a;

fn entries<'a>(&'a self) -> Result<Self::Entries<'a>, Self::Error> {
self.raw_storage.entries()
}
}

struct Entry<'a> {
name: &'a str,
value: &'a dyn Any,
Expand Down

0 comments on commit 3f1ebe2

Please # to comment.