From 5f637a98743c2bb79639db4cde4a34fc82c44b0d Mon Sep 17 00:00:00 2001 From: Cody P Schafer Date: Fri, 31 Mar 2023 16:43:45 -0400 Subject: [PATCH 1/2] storage: add StorageIterate This allows iteration over entries within a a storage instance. --- src/storage.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/storage.rs b/src/storage.rs index 6546269..ddc665b 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,4 +1,5 @@ use core::any::Any; +use core::ffi::CStr; use core::fmt::{self, Debug}; use serde::de::DeserializeOwned; @@ -132,6 +133,23 @@ where } } +pub trait StorageIterate { + type Error: Debug; + type Entry: StorageEntry; + type Entries<'a>: Iterator> + where + Self: 'a; + + fn entries(&self) -> Result, 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 { @@ -264,6 +282,21 @@ where } } +impl StorageIterate for StorageImpl +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::Error> { + self.raw_storage.entries() + } +} + struct Entry<'a> { name: &'a str, value: &'a dyn Any, From 40ad062540270f7af48c92d76c39043430d1df81 Mon Sep 17 00:00:00 2001 From: Cody P Schafer Date: Wed, 10 May 2023 21:12:26 -0400 Subject: [PATCH 2/2] remove cstr --- src/storage.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/storage.rs b/src/storage.rs index ddc665b..d2e8629 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,5 +1,4 @@ use core::any::Any; -use core::ffi::CStr; use core::fmt::{self, Debug}; use serde::de::DeserializeOwned; @@ -144,10 +143,7 @@ pub trait StorageIterate { } pub trait StorageEntry { - fn name_cstr(&self) -> &CStr; - fn name(&self) -> Option<&str> { - self.name_cstr().to_str().ok() - } + fn name(&self) -> &str; } #[derive(Debug)]