From ce655aa295fbbfdf09f66762e4a4ef58942d6680 Mon Sep 17 00:00:00 2001 From: Lucas Martins Date: Fri, 1 Nov 2024 12:11:11 -0300 Subject: [PATCH] optionally derive defmt --- Cargo.toml | 4 ++++ src/lib.rs | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index c5f8c99db..dfebce1d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,6 +90,9 @@ no_avx2 = [] no_avx512 = [] no_neon = [] +# the defmt feature enables deriving [defmt::Format] on the Hash struct +defmt = ["dep:defmt"] + [package.metadata.docs.rs] # Document the rayon/mmap methods and the Serialize/Deserialize/Zeroize impls on docs.rs. features = ["mmap", "rayon", "serde", "zeroize"] @@ -104,6 +107,7 @@ memmap2 = { version = "0.9", optional = true } rayon-core = { version = "1.12.1", optional = true } serde = { version = "1.0", default-features = false, features = ["derive"], optional = true } zeroize = { version = "1", default-features = false, optional = true } +defmt = { version = "0.3.8", default-features = false, optional = true } [dev-dependencies] hmac = "0.12.0" diff --git a/src/lib.rs b/src/lib.rs index 37c2c0b31..33a82a28b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -365,6 +365,15 @@ impl fmt::Debug for Hash { } } +#[cfg(feature = "defmt")] +impl defmt::Format for Hash { + fn format(&self, fmt: defmt::Formatter) { + for v in self.0.iter() { + defmt::write!(fmt, "{:02x}", v) + } + } +} + /// The error type for [`Hash::from_hex`]. /// /// The `.to_string()` representation of this error currently distinguishes between bad length