macro_rules! define_abbreviations {
(sized $a:ident, $name:ident, $( $b:expr ),+) => {
$( define_abbreviations![@sized $a, $name, $b]; )+
};
(@sized $a:ident, $name:ident, $b:expr) => {
devela::paste!{
#[doc = "Abbreviation of [`" $name $b "`]." ]
pub type [<$a$b>] = [<$name $b>];
}
};
(family $a:ident, $name:ident) => {
devela::paste!{
#[doc = "Abbreviation of [`" $name "`] family." ]
pub type $a = $name;
}
};
}
pub(crate) use define_abbreviations;
macro_rules! define_aliases {
(sized $a:ident, $name:ident, $( $b:expr ),+) => {
$( define_aliases![@sized $a, $name, $b]; )+
};
(@sized $a:ident, $name:ident, $b:expr) => {
devela::paste!{
#[doc = "Alias of [`" $name $b "`]." ]
pub type [<$a$b>] = [<$name $b>];
}
};
(family $a:ident, $name:ident) => {
devela::paste!{
#[doc = "Alias of [`" $name "`] family." ]
pub type $a = $name;
}
};
}
pub(crate) use define_aliases;
macro_rules! impl_from {
(
for: $for:ident + $for_b:literal,
from: @$from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => {
$crate::all::impl_from!(for: $for+$for_b, from: $from+$from_b, arg:$arg, body: $body);
$crate::all::impl_from!(for: $for+$for_b, from: &$from+$from_b, arg:$arg, body: $body);
};
(
for: $for:ident + $for_b:literal,
from: $from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => { devela::paste! {
impl From<[<$from$from_b>]> for [<$for $for_b>] {
#[inline]
fn from($arg: [<$from$from_b>]) -> Self { $body }
}
}};
(
for: $for:ident + $for_b:literal,
from: &$from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => { devela::paste! {
impl From<&[<$from$from_b>]> for [<$for $for_b>] {
#[inline]
fn from($arg: &[<$from$from_b>]) -> Self { $body }
}
impl From<&mut [<$from$from_b>]> for [<$for $for_b>] {
#[inline]
fn from($arg: &mut [<$from$from_b>]) -> Self { $body }
}
}};
(
for: $for:ident,
from: @$from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => {
$crate::all::impl_from!(for: $for, from: $from+$from_b, arg:$arg, body: $body);
$crate::all::impl_from!(for: $for, from: &$from+$from_b, arg:$arg, body: $body);
};
(
for: $for:ident,
from: $from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => { devela::paste! {
impl From<[<$from$from_b>]> for $for {
#[inline]
fn from($arg: [<$from$from_b>]) -> Self { $body }
}
}};
(
for: $for:ident,
from: &$from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => { devela::paste! {
impl From<&[<$from$from_b>]> for $for {
#[inline]
fn from($arg: &[<$from$from_b>]) -> Self { $body }
}
impl From<&mut [<$from$from_b>]> for $for {
#[inline]
fn from($arg: &mut [<$from$from_b>]) -> Self { $body }
}
}};
(try
for: $for:ident + $for_b:literal,
from: @$from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => {
$crate::all::impl_from!(try for: $for+$for_b, from: $from+$from_b, arg:$arg, body: $body);
$crate::all::impl_from!(try for: $for+$for_b, from: &$from+$from_b, arg:$arg, body: $body);
};
(try
for: $for:ident + $for_b:literal,
from: $from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => { devela::paste! {
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
impl TryFrom<[<$from$from_b>]> for [<$for$for_b>] {
type Error = $crate::error::NumeraErrors;
#[inline]
fn try_from($arg: [<$from$from_b>]) -> $crate::error::NumeraResult<[<$for$for_b>]> {
$body
}
}
}};
(try
for: $for:ident + $for_b:literal,
from: &$from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => { devela::paste! {
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
impl TryFrom<&[<$from$from_b>]> for [<$for$for_b>] {
type Error = $crate::error::NumeraErrors;
#[inline]
fn try_from($arg: &[<$from$from_b>]) -> $crate::error::NumeraResult<[<$for$for_b>]> {
$body
}
}
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
impl TryFrom<&mut [<$from$from_b>]> for [<$for$for_b>] {
type Error = $crate::error::NumeraErrors;
#[inline]
fn try_from($arg: &mut [<$from$from_b>]) -> $crate::error::NumeraResult<[<$for$for_b>]> {
$body
}
}
}};
(try
for: $for:ident,
from: @$from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => {
$crate::all::impl_from!(try for: $for, from: $from+$from_b, arg:$arg, body: $body);
$crate::all::impl_from!(try for: $for, from: &$from+$from_b, arg:$arg, body: $body);
};
(try
for: $for:ident,
from: $from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => { devela::paste! {
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
impl TryFrom<[<$from$from_b>]> for $for {
type Error = $crate::error::NumeraErrors;
#[inline]
fn try_from($arg: [<$from$from_b>]) -> $crate::error::NumeraResult<$for> {
$body
}
}
}};
(try
for: $for:ident,
from: &$from:ident + $from_b:literal,
arg: $arg:ident, body: $body:block
) => { devela::paste! {
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
impl TryFrom<&[<$from$from_b>]> for $for {
type Error = $crate::error::NumeraErrors;
#[inline]
fn try_from($arg: &[<$from$from_b>]) -> $crate::error::NumeraResult<$for> {
$body
}
}
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
impl TryFrom<&mut [<$from$from_b>]> for $for {
type Error = $crate::error::NumeraErrors;
#[inline]
fn try_from($arg: &mut [<$from$from_b>]) -> $crate::error::NumeraResult<$for> {
$body
}
}
}};
}
pub(crate) use impl_from;
macro_rules! impl_larger_smaller {
(
$name:ident, $b:expr, $family:ident,
larger: $larger:literal, $larger_b:literal,
smaller: $smaller:literal, $smaller_b:literal
) => {
devela::paste! {
impl [<$name $b>] {
#[devela::codegen::compile($larger)]
pub fn as_larger(&self) -> [<$name $larger_b>] {
[<$name $larger_b>]::from(self)
}
#[devela::codegen::compile($larger)]
pub fn as_larger_or_same(&self) -> [<$name $larger_b>] {
[<$name $larger_b>]::from(self)
}
#[must_use]
#[devela::codegen::compile(not($larger))]
pub fn as_larger_or_same(&self) -> [<$name $b>] {
*self
}
#[devela::codegen::compile($larger)]
pub fn try_as_larger(&self) -> NumeraResult<[<$name $larger_b>]> {
Ok([<$name $larger_b>]::from(self))
}
#[devela::codegen::compile(not($larger))]
pub fn try_as_larger(&self) -> NumeraResult<[<$name $b>]> {
Err($crate::error::NumeraErrors::Conversion)
}
#[devela::codegen::compile($smaller)]
#[cfg(feature = "try_from")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
pub fn as_smaller_or_same(&self) -> $family {
if let Ok(smaller) = [<$name $smaller_b>]::try_from(self) {
$family::[<_ $smaller_b>](smaller)
} else {
$family::[<_ $b>](*self)
}
}
#[devela::codegen::compile(not($smaller))]
#[cfg(feature = "try_from")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
pub fn as_smaller_or_same(&self) -> $family {
$family::[<_ $b>](*self)
}
#[devela::codegen::compile($smaller)]
#[cfg(feature = "try_from")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
pub fn try_as_smaller(&self) -> NumeraResult<[<$name $smaller_b>]> {
[<$name $smaller_b>]::try_from(self)
}
#[devela::codegen::compile(not($smaller))]
#[cfg(feature = "try_from")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "try_from")))]
pub fn try_as_smaller(&self) -> NumeraResult<[<$name $b>]> {
Err($crate::error::NumeraErrors::Conversion)
}
}
}
};
}
pub(crate) use impl_larger_smaller;