macro_rules! wrapped_to_primitive {
($(impl From<$from:ty> for $to:ty);* $(;)?) => {
$(
impl From<$from> for $to {
fn from(x: $from) -> $to {
x.0.into()
}
}
)*
}
}
macro_rules! primitive_to_wrapped {
($(impl From<$from:ty> for $to:path);* $(;)?) => {
$(
impl From<$from> for $to {
fn from(x: $from) -> $to {
$to(x.into())
}
}
)*
}
}
macro_rules! wrapped_to_wrapped {
($(impl From<$from:ty> for $to:ident);* $(;)?) => {
$(
impl From<$from> for $to {
fn from(x: $from) -> $to {
$to(x.0.into())
}
}
)*
}
}
macro_rules! new_integer {
(
$(
$(#[$($attr:tt)*])*
pub struct $IntegerType:ident($underlying:ident);)*
) => {
$(
$(#[$($attr)*])*
#[repr(transparent)]
#[derive(Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct $IntegerType($underlying);
$crate::newtype::new_integer!(@__from, $IntegerType, $underlying);
)*
};
(@__from, $IntegerType:ident, u8) => {
$crate::newtype::wrapped_to_primitive!{
impl From<$IntegerType> for u8;
impl From<$IntegerType> for i16;
impl From<$IntegerType> for u16;
impl From<$IntegerType> for i32;
impl From<$IntegerType> for u32;
impl From<$IntegerType> for i64;
impl From<$IntegerType> for u64;
impl From<$IntegerType> for i128;
impl From<$IntegerType> for u128;
impl From<$IntegerType> for usize;
impl From<$IntegerType> for ::core::sync::atomic::AtomicU8;
impl From<$IntegerType> for f32;
impl From<$IntegerType> for f64;
}
$crate::newtype::primitive_to_wrapped!{
impl From<u8> for $IntegerType;
}
};
(@__from, $IntegerType:ident, u16) => {
$crate::newtype::wrapped_to_primitive!{
impl From<$IntegerType> for u16;
impl From<$IntegerType> for i32;
impl From<$IntegerType> for u32;
impl From<$IntegerType> for i64;
impl From<$IntegerType> for u64;
impl From<$IntegerType> for i128;
impl From<$IntegerType> for u128;
impl From<$IntegerType> for usize;
impl From<$IntegerType> for ::core::sync::atomic::AtomicU16;
impl From<$IntegerType> for f32;
impl From<$IntegerType> for f64;
}
$crate::newtype::primitive_to_wrapped!{
impl From<u8> for $IntegerType;
impl From<u16> for $IntegerType;
}
};
(@__from, $IntegerType:ident, i32) => {
$crate::newtype::wrapped_to_primitive!{
impl From<$IntegerType> for i32;
impl From<$IntegerType> for i64;
impl From<$IntegerType> for u64;
impl From<$IntegerType> for i128;
impl From<$IntegerType> for u128;
impl From<$IntegerType> for ::core::sync::atomic::AtomicI32;
impl From<$IntegerType> for f64;
}
$crate::newtype::primitive_to_wrapped!{
impl From<i8> for $IntegerType;
impl From<i16> for $IntegerType;
}
};
(@__from, $IntegerType:ident, u32) => {
$crate::newtype::wrapped_to_primitive!{
impl From<$IntegerType> for u32;
impl From<$IntegerType> for i64;
impl From<$IntegerType> for u64;
impl From<$IntegerType> for i128;
impl From<$IntegerType> for u128;
impl From<$IntegerType> for ::core::sync::atomic::AtomicU32;
impl From<$IntegerType> for f64;
}
};
}
pub(crate) use new_integer;
pub(crate) use primitive_to_wrapped;
pub(crate) use wrapped_to_primitive;
pub(crate) use wrapped_to_wrapped;