diff --git a/src/serde.rs b/src/serde.rs index 63230e1..56edf9b 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -12,8 +12,8 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Secret { /// A serializable type that contains a secret. /// -/// This abstraction enables [expose_secret] to be used to serialize both `Secret`, `&Secret` -/// and `Option>`. +/// This abstraction enables [expose_secret] to be used to serialize both `Secret`, `&Secret`, +/// `Option>` and `Vec>`. pub trait SerializableSecret { type Exposed<'a>: Serialize where @@ -47,6 +47,15 @@ impl SerializableSecret for Option> { } } +#[cfg(feature = "std")] +impl SerializableSecret for Vec> 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) -> Self::Exposed<'_> { + self.iter().map(expose).collect() + } +} + /// Exposes a [Secret] for serialization. /// /// For general-purpose secret exposing see [Secret::expose_secret]. @@ -97,6 +106,8 @@ mod tests { one: Secret, #[serde(serialize_with = "expose_secret")] two: Option>, + #[serde(serialize_with = "expose_secret")] + three: Vec>, } let to_serialize: Test = Faker.fake();