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
26 changes: 21 additions & 5 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,48 @@ pub trait SerializableSecret<T> {
}

impl<T: Serialize> SerializableSecret<T> for &Secret<T> {
type Exposed<'a> = &'a T where T: 'a, Self: 'a;
type Exposed<'a>
= &'a T
where
T: 'a,
Self: 'a;

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

impl<T: Serialize> SerializableSecret<T> for Secret<T> {
type Exposed<'a> = &'a T where T: 'a;
type Exposed<'a>
= &'a T
where
T: 'a;

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

impl<T: Serialize> SerializableSecret<T> for Option<Secret<T>> {
type Exposed<'a> = Option<&'a T> where T: 'a;
type Exposed<'a>
= Option<&'a T>
where
T: 'a;

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

#[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;
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()
Expand Down
Loading