Skip to content

Commit

Permalink
Add Default impl for Secret
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Dec 8, 2024
1 parent a2d30e3 commit 61aa1a9
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions synedrion/src/tools/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ impl<T: Zeroize + Clone> Secret<T> {
}

pub fn maybe_init_with(ctr: impl FnOnce() -> Option<T>) -> Option<Self> {
Some(Self(SecretBox::try_init_with(|| ctr().ok_or(())).ok()?))
Self::try_init_with(|| ctr().ok_or(())).ok()
}
}

impl<T: Zeroize + Clone + Default> Default for Secret<T> {
fn default() -> Self {
Self::init_with(|| T::default())
}
}

impl<T: Zeroize + Clone> Clone for Secret<T> {
fn clone(&self) -> Self {
Self(SecretBox::init_with(|| self.0.expose_secret().clone()))
Self::init_with(|| self.0.expose_secret().clone())
}
}

Expand Down Expand Up @@ -306,13 +312,13 @@ impl<'a, T: Zeroize + Clone + RemAssign<&'a NonZero<T>>> Rem<&'a NonZero<T>> for

impl<T: Zeroize + Clone + for<'a> AddAssign<&'a T> + Default> core::iter::Sum for Secret<T> {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.reduce(Add::add).unwrap_or(Secret::init_with(|| T::default()))
iter.reduce(Add::add).unwrap_or(Secret::<T>::default())
}
}

impl<'b, T: Zeroize + Clone + for<'a> AddAssign<&'a T> + Default> core::iter::Sum<&'b Secret<T>> for Secret<T> {
fn sum<I: Iterator<Item = &'b Secret<T>>>(iter: I) -> Self {
iter.fold(Secret::init_with(|| T::default()), |accum, x| accum + x)
iter.fold(Secret::<T>::default(), |accum, x| accum + x)
}
}

Expand Down

0 comments on commit 61aa1a9

Please sign in to comment.