Skip to content

Commit 81f195b

Browse files
authored
Merge pull request #67 from giuliabee/serializable_vec
make Vec<Secret<T>> serializable
2 parents 0edf3f9 + 2f8d649 commit 81f195b

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/serde.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for Secret<T> {
1212

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

50+
#[cfg(feature = "std")]
51+
impl<T: Serialize> SerializableSecret<T> for Vec<Secret<T>> where for<'a> Vec<&'a T>: Serialize {
52+
type Exposed<'a> = Vec<&'a T> where T: 'a;
53+
54+
fn expose_via(&self, expose: impl Fn(&Secret<T>) -> &T) -> Self::Exposed<'_> {
55+
self.iter().map(expose).collect()
56+
}
57+
}
58+
5059
/// Exposes a [Secret] for serialization.
5160
///
5261
/// For general-purpose secret exposing see [Secret::expose_secret].
@@ -97,6 +106,8 @@ mod tests {
97106
one: Secret<String>,
98107
#[serde(serialize_with = "expose_secret")]
99108
two: Option<Secret<String>>,
109+
#[serde(serialize_with = "expose_secret")]
110+
three: Vec<Secret<String>>,
100111
}
101112

102113
let to_serialize: Test = Faker.fake();

0 commit comments

Comments
 (0)