-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into flexible-serde-expose-secret
- Loading branch information
Showing
6 changed files
with
160 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use crate::Secret; | ||
|
||
use core::fmt; | ||
use std::error::Error; | ||
|
||
impl<E: Error> fmt::Display for Secret<E> { | ||
#[inline] | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
<Self as fmt::Debug>::fmt(self, f) | ||
} | ||
} | ||
|
||
impl<E: Error> Error for Secret<E> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use crate::Secret; | ||
|
||
use fake::Dummy; | ||
use rand::Rng; | ||
|
||
impl<T: Dummy<U>, U> Dummy<U> for Secret<T> { | ||
#[inline] | ||
fn dummy_with_rng<R: Rng + ?Sized>(config: &U, rng: &mut R) -> Self { | ||
Secret(T::dummy_with_rng(config, rng)) | ||
} | ||
|
||
#[inline] | ||
fn dummy(config: &U) -> Self { | ||
Secret(T::dummy(config)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::Secret; | ||
|
||
use core::ops; | ||
|
||
macro_rules! ops { | ||
{ ($type:tt, $trait:ident, $method:ident), $($tt:tt)* } => { | ||
ops!(($type, $trait, $method)); | ||
ops!($($tt)*); | ||
}; | ||
{ (binary, $trait:ident, $method:ident)} => { | ||
impl<T, U> ops::$trait<Secret<U>> for Secret<T> | ||
where | ||
T: ops::$trait<U>, | ||
{ | ||
type Output = Secret<T::Output>; | ||
#[inline] | ||
fn $method(self, rhs: Secret<U>) -> Self::Output { | ||
Secret(self.0.$method(rhs.0)) | ||
} | ||
} | ||
}; | ||
{ (assign, $trait:ident, $method:ident)} => { | ||
impl<T, U> ops::$trait<Secret<U>> for Secret<T> | ||
where | ||
T: ops::$trait<U>, | ||
{ | ||
#[inline] | ||
fn $method(&mut self, rhs: Secret<U>) { | ||
self.0.$method(rhs.0) | ||
} | ||
} | ||
}; | ||
{ (unary, $trait:ident, $method:ident)} => { | ||
impl<T> ops::$trait for Secret<T> | ||
where | ||
T: ops::$trait, | ||
{ | ||
type Output = Secret<T::Output>; | ||
#[inline] | ||
fn $method(self) -> Self::Output { | ||
Secret(self.0.$method()) | ||
} | ||
} | ||
}; | ||
() => () | ||
} | ||
|
||
ops! { | ||
(binary, Add, add), | ||
(assign, AddAssign, add_assign), | ||
(binary, BitAnd, bitand), | ||
(assign, BitAndAssign, bitand_assign), | ||
(binary, BitOr, bitor), | ||
(assign, BitOrAssign, bitor_assign), | ||
(binary, BitXor, bitxor), | ||
(assign, BitXorAssign, bitxor_assign), | ||
(binary, Div, div), | ||
(assign, DivAssign, div_assign), | ||
(binary, Mul, mul), | ||
(assign, MulAssign, mul_assign), | ||
(binary, Rem, rem), | ||
(assign, RemAssign, rem_assign), | ||
(binary, Shl, shl), | ||
(assign, ShlAssign, shl_assign), | ||
(binary, Shr, shr), | ||
(assign, ShrAssign, shr_assign), | ||
(binary, Sub, sub), | ||
(assign, SubAssign, sub_assign), | ||
(unary, Neg, neg), | ||
(unary, Not, not), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::Secret; | ||
|
||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
/// *This API requires the following crate features to be activated: `serde`* | ||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Secret<T> { | ||
#[inline] | ||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { | ||
T::deserialize(deserializer).map(Self) | ||
} | ||
} | ||
|
||
/// Exposes a [Secret] for serialization. | ||
/// | ||
/// For general-purpose secret exposing see [Secret::expose_secret]. | ||
/// | ||
/// See [module level documentation][crate] for usage example. | ||
/// | ||
/// *This API requires the following crate features to be activated: `serde`* | ||
#[inline] | ||
pub fn expose_secret<S: Serializer, T: Serialize>( | ||
secret: &Secret<T>, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> { | ||
secret.expose_secret().serialize(serializer) | ||
} |