diff --git a/newtype_derive/src/lib.rs b/newtype_derive/src/lib.rs index b322ed9..1d613ec 100644 --- a/newtype_derive/src/lib.rs +++ b/newtype_derive/src/lib.rs @@ -1169,3 +1169,53 @@ macro_rules! NewtypeUpperHex { newtype_fmt! { UpperHex, $name } }; } + +#[macro_export] +macro_rules! NewtypeProduct { + ($arg:tt $(pub)* struct $name:ident(pub $t0:ty);) => { + NewtypeProduct! { $arg struct $name($t0); } + }; + + (() $(pub)* struct $name:ident($t0:ty);) => { + impl ::std::iter::Product<$name> for $name { + fn product(iter: I) -> Self + where I: Iterator { + $name(iter.map(|e| e.0).product::<$t0>()) + } + } + }; + + ((&Self) $(pub)* struct $name:ident($t0:ty);) => { + impl<'a> ::std::iter::Product<&'a $name> for $name { + fn product(iter: I) -> Self + where I: Iterator { + $name(iter.map(|e| &e.0).product::<$t0>()) + } + } + }; +} + +#[macro_export] +macro_rules! NewtypeSum { + ($arg:tt $(pub)* struct $name:ident(pub $t0:ty);) => { + NewtypeSum! { $arg struct $name($t0); } + }; + + (() $(pub)* struct $name:ident($t0:ty);) => { + impl ::std::iter::Sum<$name> for $name { + fn sum(iter: I) -> Self + where I: Iterator { + $name(iter.map(|e| e.0).sum::<$t0>()) + } + } + }; + + ((&Self) $(pub)* struct $name:ident($t0:ty);) => { + impl<'a> ::std::iter::Sum<&'a $name> for $name { + fn sum(iter: I) -> Self + where I: Iterator { + $name(iter.map(|e| &e.0).sum::<$t0>()) + } + } + }; +} diff --git a/newtype_derive/src/std_unstable.rs b/newtype_derive/src/std_unstable.rs index 7feb7b9..4db5e08 100644 --- a/newtype_derive/src/std_unstable.rs +++ b/newtype_derive/src/std_unstable.rs @@ -28,56 +28,6 @@ macro_rules! NewtypeOne { }; } -#[macro_export] -macro_rules! NewtypeProduct { - ($arg:tt $(pub)* struct $name:ident(pub $t0:ty);) => { - NewtypeProduct! { $arg struct $name($t0); } - }; - - (() $(pub)* struct $name:ident($t0:ty);) => { - impl ::std::iter::Product<$name> for $name { - fn product(iter: I) -> Self - where I: Iterator { - $name(iter.map(|e| e.0).product::<$t0>()) - } - } - }; - - ((&Self) $(pub)* struct $name:ident($t0:ty);) => { - impl<'a> ::std::iter::Product<&'a $name> for $name { - fn product(iter: I) -> Self - where I: Iterator { - $name(iter.map(|e| &e.0).product::<$t0>()) - } - } - }; -} - -#[macro_export] -macro_rules! NewtypeSum { - ($arg:tt $(pub)* struct $name:ident(pub $t0:ty);) => { - NewtypeSum! { $arg struct $name($t0); } - }; - - (() $(pub)* struct $name:ident($t0:ty);) => { - impl ::std::iter::Sum<$name> for $name { - fn sum(iter: I) -> Self - where I: Iterator { - $name(iter.map(|e| e.0).sum::<$t0>()) - } - } - }; - - ((&Self) $(pub)* struct $name:ident($t0:ty);) => { - impl<'a> ::std::iter::Sum<&'a $name> for $name { - fn sum(iter: I) -> Self - where I: Iterator { - $name(iter.map(|e| &e.0).sum::<$t0>()) - } - } - }; -} - #[macro_export] macro_rules! NewtypeZero { (() $(pub)* struct $name:ident(pub $_t0:ty);) => {