From 315a85bc8b9e447ea4059b4dc0f013f68a0a6271 Mon Sep 17 00:00:00 2001 From: "Tony Arcieri (iqlusion)" Date: Thu, 19 Sep 2024 16:16:14 -0600 Subject: [PATCH] secrecy: impl `Deserialize` for `SecretString` (#1220) It needs a special impl since `SecretBox` (which `SecretString` is a type alias for) doesn't meet the `Clone` bound (i.e. `str` doesn't impl `Clone`). Closes #1219 --- secrecy/Cargo.toml | 2 +- secrecy/src/lib.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/secrecy/Cargo.toml b/secrecy/Cargo.toml index 6220ebdd..edb0f87b 100644 --- a/secrecy/Cargo.toml +++ b/secrecy/Cargo.toml @@ -21,7 +21,7 @@ rust-version = "1.60" zeroize = { version = "1.6", default-features = false, features = ["alloc"] } # optional dependencies -serde = { version = "1", optional = true, default-features = false } +serde = { version = "1", optional = true, default-features = false, features = ["alloc"] } [package.metadata.docs.rs] all-features = true diff --git a/secrecy/src/lib.rs b/secrecy/src/lib.rs index 184e45c7..5c714b08 100644 --- a/secrecy/src/lib.rs +++ b/secrecy/src/lib.rs @@ -251,6 +251,16 @@ where } } +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for SecretString { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + String::deserialize(deserializer).map(Into::into) + } +} + #[cfg(feature = "serde")] impl Serialize for SecretBox where