Skip to content

Commit 5b5d321

Browse files
committed
storage: add StorageIterate
This allows iteration over entries within a a storage instance.
1 parent 553823d commit 5b5d321

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/storage.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::any::Any;
2+
use core::ffi::CStr;
23
use core::fmt::{self, Debug};
34

45
use serde::de::DeserializeOwned;
@@ -132,6 +133,23 @@ where
132133
}
133134
}
134135

136+
pub trait StorageIterate {
137+
type Error: Debug;
138+
type Entry: StorageEntry;
139+
type Entries<'a>: Iterator<Item = Result<Self::Entry, Self::Error>>
140+
where
141+
Self: 'a;
142+
143+
fn entries<'a>(&'a self) -> Result<Self::Entries<'a>, Self::Error>;
144+
}
145+
146+
pub trait StorageEntry {
147+
fn name_cstr(&self) -> &CStr;
148+
fn name(&self) -> Option<&str> {
149+
self.name_cstr().to_str().ok()
150+
}
151+
}
152+
135153
#[derive(Debug)]
136154
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
137155
pub enum StorageError<R, S> {
@@ -264,6 +282,21 @@ where
264282
}
265283
}
266284

285+
impl<const N: usize, R, S> StorageIterate for StorageImpl<N, R, S>
286+
where
287+
S: SerDe,
288+
R: StorageIterate,
289+
{
290+
type Error = R::Error;
291+
type Entry = R::Entry;
292+
type Entries<'a> = R::Entries<'a>
293+
where Self: 'a;
294+
295+
fn entries<'a>(&'a self) -> Result<Self::Entries<'a>, Self::Error> {
296+
self.raw_storage.entries()
297+
}
298+
}
299+
267300
struct Entry<'a> {
268301
name: &'a str,
269302
value: &'a dyn Any,

0 commit comments

Comments
 (0)