#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! dynamic_newtype {
($(#[$attr:meta])* $vis:vis $name:ident, generic Vec<u8>, $doc:literal, derive: [$($opt:ident),* $(,)?]) => {
$crate::__sg_dynamic_generic_is_shaped!(@bytes);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic Vec<u8>, $doc:literal) => {
$crate::__sg_dynamic_generic_is_shaped!(@bytes);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic Vec<u8>, derive: [$($opt:ident),* $(,)?]) => {
$crate::__sg_dynamic_generic_is_shaped!(@bytes);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic Vec<u8>) => {
$crate::__sg_dynamic_generic_is_shaped!(@bytes);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic String, $doc:literal, derive: [$($opt:ident),* $(,)?]) => {
$crate::__sg_dynamic_generic_is_shaped!(@string);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic String, $doc:literal) => {
$crate::__sg_dynamic_generic_is_shaped!(@string);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic String, derive: [$($opt:ident),* $(,)?]) => {
$crate::__sg_dynamic_generic_is_shaped!(@string);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic String) => {
$crate::__sg_dynamic_generic_is_shaped!(@string);
};
($(#[$attr:meta])* $vis:vis $name:ident, String, $doc:literal, derive: [$($opt:ident),* $(,)?]) => {
$crate::dynamic_newtype!($(#[$attr])* #[doc = $doc] $vis $name, String, derive: [$($opt),*]);
};
($(#[$attr:meta])* $vis:vis $name:ident, String, $doc:literal) => {
$crate::dynamic_newtype!($(#[$attr])* #[doc = $doc] $vis $name, String, derive: []);
};
($(#[$attr:meta])* $vis:vis $name:ident, Vec<u8>, $doc:literal, derive: [$($opt:ident),* $(,)?]) => {
$crate::dynamic_newtype!($(#[$attr])* #[doc = $doc] $vis $name, Vec<u8>, derive: [$($opt),*]);
};
($(#[$attr:meta])* $vis:vis $name:ident, Vec<u8>, $doc:literal) => {
$crate::dynamic_newtype!($(#[$attr])* #[doc = $doc] $vis $name, Vec<u8>, derive: []);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic $inner:ty, $doc:literal, derive: [$($opt:ident),* $(,)?]) => {
$crate::dynamic_newtype!($(#[$attr])* #[doc = $doc] $vis $name, generic $inner, derive: [$($opt),*]);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic $inner:ty, $doc:literal) => {
$crate::dynamic_newtype!($(#[$attr])* #[doc = $doc] $vis $name, generic $inner, derive: []);
};
($(#[$attr:meta])* $vis:vis $name:ident, String) => {
$crate::dynamic_newtype!($(#[$attr])* $vis $name, String, derive: []);
};
($(#[$attr:meta])* $vis:vis $name:ident, String, derive: [$($opt:ident),* $(,)?]) => {
$crate::__sg_newtype_base!(
$(#[$attr])* $vis $name($crate::Dynamic<$crate::__private::String>), derive: [$($opt),*]
);
$crate::__sg_newtype_ct_eq!($name);
$crate::__sg_newtype_len!($name);
$crate::__sg_dynamic_ctor!(@into $name, $crate::__private::String);
impl ::core::convert::From<&str> for $name {
#[inline]
fn from(s: &str) -> Self { Self(<$crate::Dynamic<$crate::__private::String>>::from(s)) }
}
};
($(#[$attr:meta])* $vis:vis $name:ident, Vec<u8>) => {
$crate::dynamic_newtype!($(#[$attr])* $vis $name, Vec<u8>, derive: []);
};
($(#[$attr:meta])* $vis:vis $name:ident, Vec<u8>, derive: [$($opt:ident),* $(,)?]) => {
$crate::__sg_newtype_base!(
$(#[$attr])* $vis $name($crate::Dynamic<$crate::__private::Vec<u8>>), derive: [$($opt),*]
);
$crate::__sg_newtype_ct_eq!($name);
$crate::__sg_newtype_len!($name);
$crate::__sg_dynamic_ctor!(@into $name, $crate::__private::Vec<u8>);
impl ::core::convert::From<&[u8]> for $name {
#[inline]
fn from(s: &[u8]) -> Self { Self(<$crate::Dynamic<$crate::__private::Vec<u8>>>::from(s)) }
}
impl $name {
#[inline]
pub fn new_with<F>(len: usize, f: F) -> Self
where F: ::core::ops::FnOnce(&mut [u8]) {
Self(<$crate::Dynamic<$crate::__private::Vec<u8>>>::new_with(len, f))
}
#[inline]
pub fn try_new_with<F, E>(len: usize, f: F) -> ::core::result::Result<Self, E>
where F: ::core::ops::FnOnce(&mut [u8]) -> ::core::result::Result<(), E> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_new_with(len, f)?
))
}
}
$crate::__sg_if_rand! {
impl $name {
#[inline]
pub fn from_random(len: usize) -> Self { Self(<$crate::Dynamic<$crate::__private::Vec<u8>>>::from_random(len)) }
#[inline]
pub fn from_rng<R>(len: usize, rng: &mut R) -> ::core::result::Result<Self, R::Error>
where
R: $crate::__private::TryRng + $crate::__private::TryCryptoRng,
{
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::from_rng(len, rng)?,
))
}
}
}
$crate::__sg_if_hex! {
impl $name {
#[inline]
pub fn try_from_hex(s: &str) -> ::core::result::Result<Self, $crate::HexError> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_hex(s)?,
))
}
}
impl $crate::ToHex for $name {
#[inline]
fn to_hex(&self) -> $crate::EncodedSecret {
$crate::ToHex::to_hex(&self.0)
}
#[inline]
fn to_hex_upper(&self) -> $crate::EncodedSecret {
$crate::ToHex::to_hex_upper(&self.0)
}
}
}
$crate::__sg_if_base32! {
impl $name {
#[inline]
pub fn try_from_base32(s: &str) -> ::core::result::Result<Self, $crate::Base32Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_base32(s)?,
))
}
}
impl $crate::ToBase32 for $name {
#[inline]
fn to_base32(&self) -> $crate::EncodedSecret {
$crate::ToBase32::to_base32(&self.0)
}
}
}
$crate::__sg_if_base64! {
impl $name {
#[inline]
pub fn try_from_base64url(s: &str) -> ::core::result::Result<Self, $crate::Base64Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_base64url(s)?,
))
}
}
impl $crate::ToBase64Url for $name {
#[inline]
fn to_base64url(&self) -> $crate::EncodedSecret {
$crate::ToBase64Url::to_base64url(&self.0)
}
}
}
$crate::__sg_if_bech32! {
impl $name {
#[inline]
pub fn try_from_bech32(s: &str, expected_hrp: &str)
-> ::core::result::Result<Self, $crate::Bech32Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_bech32(s, expected_hrp)?,
))
}
#[inline]
pub fn try_from_bech32_unchecked(s: &str)
-> ::core::result::Result<Self, $crate::Bech32Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_bech32_unchecked(s)?,
))
}
#[inline]
pub fn try_from_bech32_sized<const C: usize>(s: &str, expected_hrp: &str)
-> ::core::result::Result<Self, $crate::Bech32Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_bech32_sized::<C>(s, expected_hrp)?,
))
}
#[inline]
pub fn try_from_bech32_unchecked_sized<const C: usize>(s: &str)
-> ::core::result::Result<Self, $crate::Bech32Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_bech32_unchecked_sized::<C>(s)?,
))
}
}
impl $crate::ToBech32 for $name {
#[inline]
fn try_to_bech32(
&self,
hrp: &str,
case: $crate::Case,
) -> ::core::result::Result<$crate::EncodedSecret, $crate::Bech32Error> {
$crate::ToBech32::try_to_bech32(&self.0, hrp, case)
}
#[inline]
fn try_to_bech32_sized<const C: usize>(
&self,
hrp: &str,
case: $crate::Case,
) -> ::core::result::Result<$crate::EncodedSecret, $crate::Bech32Error> {
$crate::ToBech32::try_to_bech32_sized::<C>(&self.0, hrp, case)
}
}
}
$crate::__sg_if_bech32m! {
impl $name {
#[inline]
pub fn try_from_bech32m(s: &str, expected_hrp: &str)
-> ::core::result::Result<Self, $crate::Bech32Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_bech32m(s, expected_hrp)?,
))
}
#[inline]
pub fn try_from_bech32m_unchecked(s: &str)
-> ::core::result::Result<Self, $crate::Bech32Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_bech32m_unchecked(s)?,
))
}
#[inline]
pub fn try_from_bech32m_sized<const C: usize>(s: &str, expected_hrp: &str)
-> ::core::result::Result<Self, $crate::Bech32Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_bech32m_sized::<C>(s, expected_hrp)?,
))
}
#[inline]
pub fn try_from_bech32m_unchecked_sized<const C: usize>(s: &str)
-> ::core::result::Result<Self, $crate::Bech32Error> {
::core::result::Result::Ok(Self(
<$crate::Dynamic<$crate::__private::Vec<u8>>>::try_from_bech32m_unchecked_sized::<C>(s)?,
))
}
}
impl $crate::ToBech32m for $name {
#[inline]
fn try_to_bech32m(
&self,
hrp: &str,
case: $crate::Case,
) -> ::core::result::Result<$crate::EncodedSecret, $crate::Bech32Error> {
$crate::ToBech32m::try_to_bech32m(&self.0, hrp, case)
}
#[inline]
fn try_to_bech32m_sized<const C: usize>(
&self,
hrp: &str,
case: $crate::Case,
) -> ::core::result::Result<$crate::EncodedSecret, $crate::Bech32Error> {
$crate::ToBech32m::try_to_bech32m_sized::<C>(&self.0, hrp, case)
}
}
}
$crate::__sg_if_std! {
impl ::std::io::Write for $name {
#[inline]
fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
::std::io::Write::write(&mut self.0, buf)
}
#[inline]
fn flush(&mut self) -> ::std::io::Result<()> { ::std::io::Write::flush(&mut self.0) }
}
impl $name {
#[inline]
pub fn as_reader(&self) -> $crate::DynamicReader<'_> { self.0.as_reader() }
}
}
};
($(#[$attr:meta])* $vis:vis $name:ident, generic $inner:ty) => {
$crate::dynamic_newtype!($(#[$attr])* $vis $name, generic $inner, derive: []);
};
($(#[$attr:meta])* $vis:vis $name:ident, generic $inner:ty, derive: [$($opt:ident),* $(,)?]) => {
$crate::__sg_newtype_base!(
$(#[$attr])* $vis $name($crate::Dynamic<$inner>), derive: [$($opt),*]
);
$crate::__sg_dynamic_ctor!($name, $inner);
};
($(#[$attr:meta])* $vis:vis $name:ident, $($rest:tt)+) => {
::core::compile_error!(::core::concat!(
"dynamic_newtype!: `",
::core::stringify!($($rest)+),
"` is not one of the shaped inner types. `String` and `Vec<u8>` are \
matched as literal tokens, so a type alias (e.g. `type MyStr = String`) \
or a fully-qualified path (e.g. `alloc::vec::Vec<u8>`) does NOT match \
them. If what you wrote IS a `String` or a `Vec<u8>` under another \
spelling, write the literal token to get the full API for that shape — \
do NOT reach for `generic` to silence this, because the macro cannot \
see through a path or an alias either, and you would land on the \
reduced arm with the growable payload and none of the methods that \
handle it safely. `generic <type>` is for an inner type that is \
genuinely neither `String` nor `Vec<u8>`; it gives RevealSecret, \
RevealSecretMut, Debug, Zeroize, and `new`."
));
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __sg_dynamic_generic_is_shaped {
(@bytes) => {
::core::compile_error!(
"dynamic_newtype!: write `Vec<u8>`, not `generic Vec<u8>`. The `generic` \
arm emits only the shape-independent surface, so this spelling withholds \
`SecretLen`, `From<&[u8]>`, `new_with`, `try_new_with`, the encoders, \
`from_random`, and — under `std` — the `std::io::Write` impl, which is \
the one growth path that wipes the buffer it abandons. It is the same \
growable payload with strictly less API and no safe way to grow it. \
`generic` is for inner types that are neither `String` nor `Vec<u8>`."
);
};
(@string) => {
::core::compile_error!(
"dynamic_newtype!: write `String`, not `generic String`. The `generic` arm \
emits only the shape-independent surface, so this spelling withholds \
`SecretLen` and `From<&str>`. It is the same payload with strictly less \
API. `generic` is for inner types that are neither `String` nor \
`Vec<u8>`."
);
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __sg_dynamic_ctor {
(@into $name:ident, $inner:ty) => {
impl $name {
#[inline(always)]
pub fn new(value: impl ::core::convert::Into<$inner>) -> Self {
Self(<$crate::Dynamic<$inner>>::new(value.into()))
}
}
};
($name:ident, $inner:ty) => {
impl $name {
#[inline(always)]
pub fn new<U>(value: U) -> Self
where
U: ::core::convert::Into<$crate::__private::Box<$inner>>,
{
Self(<$crate::Dynamic<$inner>>::new(value))
}
}
};
}