From b4be008ea958a423636c098027881dc1b93b6983 Mon Sep 17 00:00:00 2001 From: Paul Bender Date: Thu, 9 Oct 2025 19:31:43 -0700 Subject: [PATCH] Add defmt support gated behind a feature flag. --- Cargo.toml | 6 ++++++ embedded-storage-async/Cargo.toml | 4 ++++ src/iter.rs | 4 ++-- src/nor_flash.rs | 12 ++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c851083..30b9aa2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,9 @@ documentation = "https://docs.rs/embedded-storage" readme = "README.md" keywords = ["storage"] categories = ["embedded", "hardware-support", "no-std"] + +[features] +defmt = ["dep:defmt"] + +[dependencies] +defmt = { version = "1.0.1", optional = true } diff --git a/embedded-storage-async/Cargo.toml b/embedded-storage-async/Cargo.toml index dcf138f..885b443 100644 --- a/embedded-storage-async/Cargo.toml +++ b/embedded-storage-async/Cargo.toml @@ -17,5 +17,9 @@ readme = "README.md" keywords = ["storage"] categories = ["embedded", "hardware-support", "no-std"] +[features] +defmt = ["dep:defmt", "embedded-storage/defmt"] + [dependencies] embedded-storage = { version = "0.3.1", path = "../" } +defmt = { version = "1.0.1", optional = true } diff --git a/src/iter.rs b/src/iter.rs index 3b5c102..1eb8799 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -19,7 +19,7 @@ where I: Iterator, { /// Obtain an [`OverlapIterator`] over a subslice of `memory` that overlaps with the region in `self` - fn overlaps(self, memory: &'a [u8], base_address: u32) -> OverlapIterator; + fn overlaps(self, memory: &'a [u8], base_address: u32) -> OverlapIterator<'a, R, I>; } impl<'a, R, I> Iterator for OverlapIterator<'a, R, I> @@ -54,7 +54,7 @@ where R: Region, I: Iterator, { - fn overlaps(self, memory: &'a [u8], base_address: u32) -> OverlapIterator { + fn overlaps(self, memory: &'a [u8], base_address: u32) -> OverlapIterator<'a, R, I> { OverlapIterator { memory, regions: self, diff --git a/src/nor_flash.rs b/src/nor_flash.rs index 447bd1a..32807b6 100644 --- a/src/nor_flash.rs +++ b/src/nor_flash.rs @@ -4,11 +4,22 @@ use crate::{iter::IterableByOverlaps, ReadStorage, Region, Storage}; /// /// NOR flash implementations must use an error type implementing this trait. This permits generic /// code to extract a generic error kind. +#[cfg(not(feature = "defmt"))] pub trait NorFlashError: core::fmt::Debug { /// Convert a specific NOR flash error into a generic error kind. fn kind(&self) -> NorFlashErrorKind; } +/// NOR flash errors. +/// +/// NOR flash implementations must use an error type implementing this trait. This permits generic +/// code to extract a generic error kind. +#[cfg(feature = "defmt")] +pub trait NorFlashError: core::fmt::Debug + defmt::Format { + /// Convert a specific NOR flash error into a generic error kind. + fn kind(&self) -> NorFlashErrorKind; +} + impl NorFlashError for core::convert::Infallible { fn kind(&self) -> NorFlashErrorKind { match *self {} @@ -26,6 +37,7 @@ pub trait ErrorType { /// NOR flash implementations must map their error to those generic error kinds through the /// [`NorFlashError`] trait. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[non_exhaustive] pub enum NorFlashErrorKind { /// The arguments are not properly aligned.