Skip to content

Commit

Permalink
secrecy: impl Deserialize for SecretString (#1220)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tony-iqlusion authored Sep 19, 2024
1 parent a0e93f7 commit 315a85b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion secrecy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions secrecy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ where
}
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for SecretString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
String::deserialize(deserializer).map(Into::into)
}
}

#[cfg(feature = "serde")]
impl<T> Serialize for SecretBox<T>
where
Expand Down

0 comments on commit 315a85b

Please sign in to comment.