From 61aa1a9ea684e32bf1791e966a9a90cd745f3423 Mon Sep 17 00:00:00 2001 From: Bogdan Opanchuk Date: Sun, 8 Dec 2024 10:40:09 -0800 Subject: [PATCH] Add Default impl for Secret --- synedrion/src/tools/secret.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/synedrion/src/tools/secret.rs b/synedrion/src/tools/secret.rs index e3cf0ba..4a200e9 100644 --- a/synedrion/src/tools/secret.rs +++ b/synedrion/src/tools/secret.rs @@ -45,13 +45,19 @@ impl Secret { } pub fn maybe_init_with(ctr: impl FnOnce() -> Option) -> Option { - Some(Self(SecretBox::try_init_with(|| ctr().ok_or(())).ok()?)) + Self::try_init_with(|| ctr().ok_or(())).ok() + } +} + +impl Default for Secret { + fn default() -> Self { + Self::init_with(|| T::default()) } } impl Clone for Secret { fn clone(&self) -> Self { - Self(SecretBox::init_with(|| self.0.expose_secret().clone())) + Self::init_with(|| self.0.expose_secret().clone()) } } @@ -306,13 +312,13 @@ impl<'a, T: Zeroize + Clone + RemAssign<&'a NonZero>> Rem<&'a NonZero> for impl AddAssign<&'a T> + Default> core::iter::Sum for Secret { fn sum>(iter: I) -> Self { - iter.reduce(Add::add).unwrap_or(Secret::init_with(|| T::default())) + iter.reduce(Add::add).unwrap_or(Secret::::default()) } } impl<'b, T: Zeroize + Clone + for<'a> AddAssign<&'a T> + Default> core::iter::Sum<&'b Secret> for Secret { fn sum>>(iter: I) -> Self { - iter.fold(Secret::init_with(|| T::default()), |accum, x| accum + x) + iter.fold(Secret::::default(), |accum, x| accum + x) } }