#[doc(hidden)]
#[macro_export]
#[cfg(feature = "ct-eq")]
macro_rules! __sg_if_ct_eq { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "ct-eq"))]
macro_rules! __sg_if_ct_eq {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "cloneable")]
macro_rules! __sg_if_cloneable { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "cloneable"))]
macro_rules! __sg_if_cloneable {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "serde-serialize")]
macro_rules! __sg_if_ser { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "serde-serialize"))]
macro_rules! __sg_if_ser {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "serde-deserialize")]
macro_rules! __sg_if_de { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "serde-deserialize"))]
macro_rules! __sg_if_de {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __sg_newtype_base {
($(#[$attr:meta])* $vis:vis $name:ident($wrapper:ty)) => {
$crate::__sg_newtype_base!($(#[$attr])* $vis $name($wrapper), derive: []);
};
($(#[$attr:meta])* $vis:vis $name:ident($wrapper:ty), derive: [$($opt:ident),* $(,)?]) => {
$(#[$attr])*
#[repr(transparent)]
#[must_use = "holds secret material; bind it or chain a method call"]
$vis struct $name($wrapper);
impl ::core::fmt::Debug for $name {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str("[REDACTED]")
}
}
impl $crate::RevealSecret for $name {
type Inner = <$wrapper as $crate::RevealSecret>::Inner;
#[inline(always)]
fn with_secret<F, R>(&self, f: F) -> R
where F: ::core::ops::FnOnce(&Self::Inner) -> R {
$crate::RevealSecret::with_secret(&self.0, f)
}
#[inline(always)]
fn expose_secret(&self) -> &Self::Inner {
$crate::RevealSecret::expose_secret(&self.0)
}
#[inline(always)]
fn into_inner(self) -> Self::Inner
where
Self: Sized,
Self::Inner: Sized + $crate::SentinelValue + $crate::__private::Zeroize,
{
$crate::RevealSecret::into_inner(self.0)
}
}
impl $crate::RevealSecretMut for $name {
#[inline(always)]
fn with_secret_mut<F, R>(&mut self, f: F) -> R
where F: ::core::ops::FnOnce(&mut Self::Inner) -> R {
$crate::RevealSecretMut::with_secret_mut(&mut self.0, f)
}
#[inline(always)]
fn expose_secret_mut(&mut self) -> &mut Self::Inner {
$crate::RevealSecretMut::expose_secret_mut(&mut self.0)
}
}
impl $crate::__private::Zeroize for $name {
#[inline(always)]
fn zeroize(&mut self) { $crate::__private::Zeroize::zeroize(&mut self.0) }
}
impl $crate::__private::ZeroizeOnDrop for $name {}
$(
$crate::__sg_newtype_opt!($opt, $name, $wrapper);
)*
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __sg_newtype_ct_eq {
($name:ident) => {
$crate::__sg_if_ct_eq! {
impl $crate::ConstantTimeEq for $name {
#[inline]
fn ct_eq(&self, other: &Self) -> bool {
$crate::ConstantTimeEq::ct_eq(&self.0, &other.0)
}
}
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __sg_newtype_opt {
(ConstantTimeEq, $name:ident, $wrapper:ty) => {
$crate::__sg_if_ct_eq! {
impl $crate::ConstantTimeEq for $name {
#[inline]
fn ct_eq(&self, other: &Self) -> bool {
$crate::ConstantTimeEq::ct_eq(&self.0, &other.0)
}
}
}
};
(Clone, $name:ident, $wrapper:ty) => {
::core::compile_error!(
"secure_newtype: `derive: [Clone]` is not supported. Cloning a secret \
cannot be forwarded (the wrapper's `Clone` needs `CloneableSecret` on \
the inner type, which downstream crates cannot implement), so a \
generated impl would have to route around the opt-in marker system. \
Write `impl Clone for YourType` by hand if you mean it — the newtype \
is local to your crate, so the decision stays visible in your code."
);
};
(Serialize, $name:ident, $wrapper:ty) => {
::core::compile_error!(
"secure_newtype: `derive: [Serialize]` is not supported. Serializing a \
secret cannot be forwarded (the wrapper's `Serialize` needs \
`SerializableSecret` on the inner type, which downstream crates cannot \
implement), so a generated impl would have to route around the opt-in \
marker system. Write `impl Serialize for YourType` by hand if you mean \
it — serialization exposes the full secret, so make it a visible, \
audited decision in your own code."
);
};
(Deserialize, $name:ident, $wrapper:ty) => {
$crate::__sg_if_de! {
impl<'de> $crate::__private::Deserialize<'de> for $name {
#[inline]
fn deserialize<D>(deserializer: D) -> ::core::result::Result<Self, D::Error>
where D: $crate::__private::Deserializer<'de> {
::core::result::Result::Ok(Self(
$crate::__private::Deserialize::deserialize(deserializer)?,
))
}
}
}
};
(FromWrapper, $name:ident, $wrapper:ty) => {
impl $name {
#[inline(always)]
pub fn from_wrapper(wrapper: $wrapper) -> Self {
Self(wrapper)
}
}
};
(IntoWrapper, $name:ident, $wrapper:ty) => {
impl $name {
#[inline(always)]
pub fn as_wrapper(&self) -> &$wrapper {
&self.0
}
#[inline(always)]
pub fn as_wrapper_mut(&mut self) -> &mut $wrapper {
&mut self.0
}
#[inline(always)]
pub fn into_wrapper(self) -> $wrapper {
self.0
}
}
};
(WrapperAccess, $name:ident, $wrapper:ty) => {
$crate::__sg_newtype_opt!(FromWrapper, $name, $wrapper);
$crate::__sg_newtype_opt!(IntoWrapper, $name, $wrapper);
};
($other:ident, $name:ident, $wrapper:ty) => {
::core::compile_error!(::core::concat!(
"secure_newtype: unknown `derive:` option `",
::core::stringify!($other),
"`. Supported: Deserialize, FromWrapper, IntoWrapper, WrapperAccess, and \
ConstantTimeEq on the `generic` arms only (the shaped arms emit it \
automatically). `Clone` and `Serialize` are deliberately unsupported — \
write them by hand."
));
};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "sealed")]
macro_rules! __sg_if_sealed { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "sealed"))]
macro_rules! __sg_if_sealed {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "rand")]
macro_rules! __sg_if_rand { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "rand"))]
macro_rules! __sg_if_rand {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "alloc")]
macro_rules! __sg_if_alloc { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "alloc"))]
macro_rules! __sg_if_alloc {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "std")]
macro_rules! __sg_if_std { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "std"))]
macro_rules! __sg_if_std {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "encoding-hex")]
macro_rules! __sg_if_hex { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "encoding-hex"))]
macro_rules! __sg_if_hex {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "encoding-bech32")]
macro_rules! __sg_if_bech32 { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "encoding-bech32"))]
macro_rules! __sg_if_bech32 {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __sg_newtype_len {
($name:ident) => {
impl $crate::SecretLen for $name {
#[inline(always)]
fn len(&self) -> usize {
$crate::SecretLen::len(&self.0)
}
#[inline(always)]
fn byte_len(&self) -> usize {
$crate::SecretLen::byte_len(&self.0)
}
}
};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "encoding-base32")]
macro_rules! __sg_if_base32 { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "encoding-base32"))]
macro_rules! __sg_if_base32 {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "encoding-base64")]
macro_rules! __sg_if_base64 { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "encoding-base64"))]
macro_rules! __sg_if_base64 {
($($t:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "encoding-bech32")]
macro_rules! __sg_if_bech32m { ($($t:tt)*) => { $($t)* }; }
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "encoding-bech32"))]
macro_rules! __sg_if_bech32m {
($($t:tt)*) => {};
}