macro_rules! __from_primitive {
($tup:ident; $($v:ident)*) => {{
use paste::paste;
paste!{
let ($([< $v:lower >],)*) = $tup;
tuple!($([< $v:lower >]),*)
}
}};
}
macro_rules! __to_primitive {
($tup:ident $(;)?) => { () };
($tup:ident; $($v:ident)+) => {{
use paste::paste;
paste!{
let __to_primitive!(@expand $([< $v:lower >])+) = $tup;
($([< $v:lower >],)+)
}
}};
(@expand $v:ident) => {
Tuple($v, _)
};
(@expand $v:ident $($vs:ident)+) => {
Tuple($v, __to_primitive!(@expand $($vs)+))
};
}
macro_rules! __from_array {
($arr:ident; $($v:ident)*) => {{
use paste::paste;
paste!{
let [$([< $v:lower >],)*] = $arr;
tuple!($([< $v:lower >]),*)
}
}};
}
macro_rules! __to_array {
($tup:ident; $($v:ident)+) => {{
use paste::paste;
paste!{
let __to_primitive!(@expand $([< $v:lower >])+) = $tup;
[ $([< $v:lower >],)+ ]
}
}};
(@expand $v:ident) => {
Tuple($v, _)
};
(@expand $v:ident $($vs:ident)+) => {
Tuple($v, __to_primitive!(@expand $($vs)+))
};
}
#[cfg(not(feature = "any_array"))]
macro_rules! __tuple_array_impl {
($cnt:literal;) => {
impl<T> ToArray<T> for Unit {
type Array = [T; 0];
fn to_array(self) -> Self::Array {
Default::default()
}
}
impl<T> From<[T; 0]> for Unit {
fn from(_: [T; 0]) -> Self {
Unit
}
}
};
($cnt:literal; $($ts:ident)+) => {
impl<T> ToArray<T> for tuple_t!(T; $cnt) {
type Array = [T; $cnt];
fn to_array(self) -> Self::Array {
__to_array!(self; $($ts)*)
}
}
impl<T> From<[T; $cnt]> for tuple_t!(T; $cnt) {
fn from(arr: [T; $cnt]) -> Self {
__from_array!(arr; $($ts)*)
}
}
};
}
macro_rules! __tuple_traits_impl {
($cnt:literal; $($ts:ident)*) => {
impl<$($ts),*> ToPrimitive for tuple_t!($($ts),*)
{
type Primitive = ($($ts,)*);
fn primitive(self) -> Self::Primitive {
__to_primitive!(self; $($ts)*)
}
}
impl<$($ts),*> From<($($ts,)*)> for tuple_t!($($ts),*) {
fn from(prim: ($($ts,)*)) -> Self {
__from_primitive!(prim; $($ts)*)
}
}
#[cfg(not(feature = "any_array"))]
__tuple_array_impl!{ $cnt; $($ts)* }
};
}
#[macro_export]
macro_rules! mapper {
($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr);* $(;)?) => {{
struct __Mapper;
$(
mapper!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
)*
&mut __Mapper
}};
($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr ;)*
$(_ $(=> $ot2:ty)? : $f:ident $(where $($t:tt)*)?)?) => {{
struct __Mapper;
$(
mapper!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
)*
$(
mapper!(@impl _ $(=> $ot2)? : $f $(where $($t)*)?);
)?
&mut __Mapper
}};
(@impl _ : $f:ident $(where $($t:tt)*)?) => {{
impl<T $(: $($t)*)?> Mapper<T> for __Mapper
{
type Output = T;
fn map(&mut self, value: &T) -> Self::Output {
$f(value)
}
}
}};
(@impl _ => $ot:ty : $f:ident $(where $($t:tt)*)?) => {{
impl<T$(: $($t)*)?> Mapper<T> for __Mapper
{
type Output = $ot;
fn map(&mut self, value: &T) -> Self::Output {
$f(value)
}
}
}};
(@impl $x:ident : $it:ty : $e:expr) => {
impl Mapper<$it> for __Mapper {
type Output = $it;
fn map(&mut self, value: &$it) -> Self::Output {
let f = |$x : &$it| -> $it { $e };
f(value)
}
}
};
(@impl $x:ident $(, $lt:lifetime)* : $it:ty : $e:expr) => {
impl<$($lt),*> Mapper<$it> for __Mapper {
type Output = $it;
fn map(&mut self, value: &$it) -> Self::Output {
let f = |$x : &$it| -> $it { $e };
f(value)
}
}
};
(@impl $x:ident : $it:ty => $ot:ty : $e:expr) => {
impl Mapper<$it> for __Mapper {
type Output = $ot;
fn map(&mut self, value: &$it) -> Self::Output {
let f = |$x : &$it| -> $ot { $e };
f(value)
}
}
};
(@impl $x:ident $(, $lt:lifetime)* : $it:ty => $ot:ty : $e:expr) => {
impl<$($lt),*> Mapper<$it> for __Mapper {
type Output = $ot;
fn map(&mut self, value: &$it) -> Self::Output {
let f = |$x : &$it| -> $ot { $e };
f(value)
}
}
};
}
#[macro_export]
macro_rules! mapper_mut {
($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr);* $(;)?) => {{
struct __MapperMut;
$(
mapper_mut!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
)*
&mut __MapperMut
}};
($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr ;)*
$(_ $(=> $ot2:ty)? : $f:ident $(where $($t:tt)*)?)?) => {{
struct __MapperMut;
$(
mapper_mut!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
)*
$(
mapper_mut!(@impl _ $(=> $ot2)? : $f $(where $($t)*)?);
)?
&mut __MapperMut
}};
(@impl _ : $f:ident $(where $($t:tt)*)?) => {{
impl<T $(: $($t)*)?> MapperMut<T> for __MapperMut
{
type Output = T;
fn map_mut(&mut self, value: &mut T) -> Self::Output {
$f(value)
}
}
}};
(@impl _ => $ot:ty : $f:ident $(where $($t:tt)*)?) => {{
impl<T$(: $($t)*)?> MapperMut<T> for __MapperMut
{
type Output = $ot;
fn map_mut(&mut self, value: &mut T) -> Self::Output {
$f(value)
}
}
}};
(@impl $x:ident : $it:ty : $e:expr) => {
impl MapperMut<$it> for __MapperMut {
type Output = $it;
fn map_mut(&mut self, value: &mut $it) -> Self::Output {
let f = |$x : &mut $it| -> $it { $e };
f(value)
}
}
};
(@impl $x:ident $(, $lt:lifetime)* : $it:ty : $e:expr) => {
impl<$($lt),*> MapperMut<$it> for __MapperMut {
type Output = $it;
fn map_mut(&mut self, value: &mut $it) -> Self::Output {
let f = |$x : &mut $it| -> $it { $e };
f(value)
}
}
};
(@impl $x:ident : $it:ty => $ot:ty : $e:expr) => {
impl MapperMut<$it> for __MapperMut {
type Output = $ot;
fn map_mut(&mut self, value: &mut $it) -> Self::Output {
let f = |$x : &mut $it| -> $ot { $e };
f(value)
}
}
};
(@impl $x:ident $(, $lt:lifetime)* : $it:ty => $ot:ty : $e:expr) => {
impl<$($lt),*> MapperMut<$it> for __MapperMut {
type Output = $ot;
fn map_mut(&mut self, value: &mut $it) -> Self::Output {
let f = |$x : &mut $it| -> $ot { $e };
f(value)
}
}
};
}
#[macro_export]
macro_rules! mapper_once {
($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr);* $(;)?) => {{
struct __MapperOnce;
$(
mapper_once!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
)*
&mut __MapperOnce
}};
($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr ;)*
$(_ $(=> $ot2:ty)? : $f:ident $(where $($t:tt)*)?)?) => {{
struct __MapperOnce;
$(
mapper_once!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
)*
$(
mapper_once!(@impl _ $(=> $ot2)? : $f $(where $($t)*)?);
)?
&mut __MapperOnce
}};
(@impl _ : $f:ident $(where $($t:tt)*)?) => {{
impl<T $(: $($t)*)?> MapperOnce<T> for __MapperOnce
{
type Output = T;
fn map_once(&mut self, value: T) -> Self::Output {
$f(value)
}
}
}};
(@impl _ => $ot:ty : $f:ident $(where $($t:tt)*)?) => {{
impl<T$(: $($t)*)?> MapperOnce<T> for __MapperOnce
{
type Output = $ot;
fn map_once(&mut self, value: T) -> Self::Output {
$f(value)
}
}
}};
(@impl $x:ident : $it:ty : $e:expr) => {
impl MapperOnce<$it> for __MapperOnce {
type Output = $it;
fn map_once(&mut self, value: $it) -> Self::Output {
#[allow(unused_mut)]
let f = |mut $x : $it| -> $it { $e };
f(value)
}
}
};
(@impl $x:ident $(, $lt:lifetime)* : $it:ty : $e:expr) => {
impl<$($lt),*> MapperOnce<$it> for __MapperOnce {
type Output = $it;
fn map_once(&mut self, value: $it) -> Self::Output {
#[allow(unused_mut)]
let f = |mut $x : $it| -> $it { $e };
f(value)
}
}
};
(@impl $x:ident : $it:ty => $ot:ty : $e:expr) => {
impl MapperOnce<$it> for __MapperOnce {
type Output = $ot;
fn map_once(&mut self, value: $it) -> Self::Output {
#[allow(unused_mut)]
let f = |mut $x : $it| -> $ot { $e };
f(value)
}
}
};
(@impl $x:ident $(, $lt:lifetime)* : $it:ty => $ot:ty : $e:expr) => {
impl<$($lt),*> MapperOnce<$it> for __MapperOnce {
type Output = $ot;
fn map_once(&mut self, value: $it) -> Self::Output {
#[allow(unused_mut)]
let f = |mut $x : $it| -> $ot { $e };
f(value)
}
}
};
}