Skip to content

Commit 315a85b

Browse files
secrecy: impl Deserialize for SecretString (#1220)
It needs a special impl since `SecretBox<str>` (which `SecretString` is a type alias for) doesn't meet the `Clone` bound (i.e. `str` doesn't impl `Clone`). Closes #1219
1 parent a0e93f7 commit 315a85b

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

secrecy/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ rust-version = "1.60"
2121
zeroize = { version = "1.6", default-features = false, features = ["alloc"] }
2222

2323
# optional dependencies
24-
serde = { version = "1", optional = true, default-features = false }
24+
serde = { version = "1", optional = true, default-features = false, features = ["alloc"] }
2525

2626
[package.metadata.docs.rs]
2727
all-features = true

secrecy/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,16 @@ where
251251
}
252252
}
253253

254+
#[cfg(feature = "serde")]
255+
impl<'de> Deserialize<'de> for SecretString {
256+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
257+
where
258+
D: de::Deserializer<'de>,
259+
{
260+
String::deserialize(deserializer).map(Into::into)
261+
}
262+
}
263+
254264
#[cfg(feature = "serde")]
255265
impl<T> Serialize for SecretBox<T>
256266
where

0 commit comments

Comments
 (0)