#[macro_export]
macro_rules! fixed_newtype {
($(#[$attr:meta])* $vis:vis $name:ident, $size:literal, $doc:literal, derive: [$($opt:ident),* $(,)?]) => {
$crate::fixed_newtype!($(#[$attr])* #[doc = $doc] $vis $name, $size, derive: [$($opt),*]);
};
($(#[$attr:meta])* $vis:vis $name:ident, $size:literal, $doc:literal) => {
$crate::fixed_newtype!($(#[$attr])* #[doc = $doc] $vis $name, $size, derive: []);
};
($(#[$attr:meta])* $vis:vis $name:ident, $size:literal) => {
$crate::fixed_newtype!($(#[$attr])* $vis $name, $size, derive: []);
};
($(#[$attr:meta])* $vis:vis $name:ident, $size:literal, derive: [$($opt:ident),* $(,)?]) => {
const _: () = { let _ = [(); $size][0]; };
$crate::__sg_newtype_base!(
$(#[$attr])* $vis $name($crate::Fixed<[u8; $size]>), derive: [$($opt),*]
);
$crate::__sg_newtype_len!($name);
impl $name {
#[inline(always)]
pub const fn new(value: [u8; $size]) -> Self {
Self($crate::Fixed::new(value))
}
#[inline(always)]
pub fn new_with<F>(f: F) -> Self
where F: ::core::ops::FnOnce(&mut [u8; $size]) {
Self($crate::Fixed::new_with(f))
}
}
impl ::core::convert::From<[u8; $size]> for $name {
#[inline(always)]
fn from(value: [u8; $size]) -> Self { Self($crate::Fixed::new(value)) }
}
impl ::core::convert::TryFrom<&[u8]> for $name {
type Error = $crate::FromSliceError;
#[inline]
fn try_from(slice: &[u8]) -> ::core::result::Result<Self, Self::Error> {
::core::result::Result::Ok(Self(
<$crate::Fixed<[u8; $size]> as ::core::convert::TryFrom<&[u8]>>::try_from(slice)?
))
}
}
$crate::__sg_if_rand! {
impl $name {
#[inline]
pub fn from_random() -> Self { Self($crate::Fixed::from_random()) }
#[inline]
pub fn from_rng<R>(rng: &mut R) -> ::core::result::Result<Self, R::Error>
where
R: $crate::__private::TryRng + $crate::__private::TryCryptoRng,
{
::core::result::Result::Ok(Self($crate::Fixed::from_rng(rng)?))
}
}
}
$crate::__sg_if_hex! {
impl $name {
#[inline]
pub fn try_from_hex(hex: &str) -> ::core::result::Result<Self, $crate::HexError> {
::core::result::Result::Ok(Self($crate::Fixed::try_from_hex(hex)?))
}
}
$crate::__sg_if_alloc! {
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::Fixed::try_from_base32(s)?))
}
}
$crate::__sg_if_alloc! {
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::Fixed::try_from_base64url(s)?))
}
}
$crate::__sg_if_alloc! {
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_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::Fixed::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::Fixed::try_from_bech32m_unchecked(s)?))
}
}
$crate::__sg_if_alloc! {
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_bech32! {
$crate::__sg_if_alloc! {
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)
}
}
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::Fixed::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::Fixed::try_from_bech32_unchecked(s)?))
}
}
}
}
};
}