Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Secret<T> {

/// A serializable type that contains a secret.
///
/// This abstraction enables [expose_secret] to be used to serialize both `Secret<T>`, `&Secret<T>`
/// and `Option<Secret<T>>`.
/// This abstraction enables [expose_secret] to be used to serialize both `Secret<T>`, `&Secret<T>`,
/// `Option<Secret<T>>` and `Vec<Secret<T>>`.
pub trait SerializableSecret<T> {
type Exposed<'a>: Serialize
where
Expand Down Expand Up @@ -47,6 +47,15 @@ impl<T: Serialize> SerializableSecret<T> for Option<Secret<T>> {
}
}

#[cfg(feature = "std")]
impl<T: Serialize> SerializableSecret<T> for Vec<Secret<T>> where for<'a> Vec<&'a T>: Serialize {
type Exposed<'a> = Vec<&'a T> where T: 'a;

fn expose_via(&self, expose: impl Fn(&Secret<T>) -> &T) -> Self::Exposed<'_> {
self.iter().map(expose).collect()
}
}

/// Exposes a [Secret] for serialization.
///
/// For general-purpose secret exposing see [Secret::expose_secret].
Expand Down Expand Up @@ -97,6 +106,8 @@ mod tests {
one: Secret<String>,
#[serde(serialize_with = "expose_secret")]
two: Option<Secret<String>>,
#[serde(serialize_with = "expose_secret")]
three: Vec<Secret<String>>,
}

let to_serialize: Test = Faker.fake();
Expand Down
Loading