#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! dynamic_newtype {
($(#[$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_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)) }
}
impl $name {
#[inline]
pub fn new_with<F>(f: F) -> Self
where F: ::core::ops::FnOnce(&mut $crate::__private::String) {
Self(<$crate::Dynamic<$crate::__private::String>>::new_with(f))
}
}
};
($(#[$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_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>(f: F) -> Self
where F: ::core::ops::FnOnce(&mut $crate::__private::Vec<u8>) {
Self(<$crate::Dynamic<$crate::__private::Vec<u8>>>::new_with(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::__private::String {
$crate::ToHex::to_hex(&self.0)
}
#[inline]
fn to_hex_upper(&self) -> $crate::__private::String {
$crate::ToHex::to_hex_upper(&self.0)
}
#[inline]
fn to_hex_zeroizing(&self) -> $crate::EncodedSecret {
$crate::ToHex::to_hex_zeroizing(&self.0)
}
#[inline]
fn to_hex_upper_zeroizing(&self) -> $crate::EncodedSecret {
$crate::ToHex::to_hex_upper_zeroizing(&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::__private::String {
$crate::ToBase32::to_base32(&self.0)
}
#[inline]
fn to_base32_zeroizing(&self) -> $crate::EncodedSecret {
$crate::ToBase32::to_base32_zeroizing(&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::__private::String {
$crate::ToBase64Url::to_base64url(&self.0)
}
#[inline]
fn to_base64url_zeroizing(&self) -> $crate::EncodedSecret {
$crate::ToBase64Url::to_base64url_zeroizing(&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)?,
))
}
}
impl $crate::ToBech32 for $name {
#[inline]
fn try_to_bech32(
&self,
hrp: &str,
) -> ::core::result::Result<$crate::__private::String, $crate::Bech32Error> {
$crate::ToBech32::try_to_bech32(&self.0, hrp)
}
#[inline]
fn try_to_bech32_zeroizing(
&self,
hrp: &str,
) -> ::core::result::Result<$crate::EncodedSecret, $crate::Bech32Error> {
$crate::ToBech32::try_to_bech32_zeroizing(&self.0, hrp)
}
}
}
$crate::__sg_if_bech32m! {
impl $crate::ToBech32m for $name {
#[inline]
fn try_to_bech32m(
&self,
hrp: &str,
) -> ::core::result::Result<$crate::__private::String, $crate::Bech32Error> {
$crate::ToBech32m::try_to_bech32m(&self.0, hrp)
}
#[inline]
fn try_to_bech32m_zeroizing(
&self,
hrp: &str,
) -> ::core::result::Result<$crate::EncodedSecret, $crate::Bech32Error> {
$crate::ToBech32m::try_to_bech32m_zeroizing(&self.0, hrp)
}
}
}
$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. `std::string::String`) does NOT match \
them. Write `String` or `Vec<u8>` literally to get the full API for \
that shape, or write `generic <type>` to accept the reduced API \
(RevealSecret, RevealSecretMut, Debug, Zeroize, and `new`) on purpose."
));
};
}
#[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))
}
}
};
}