//! Implementation for `arr!` macro. use super::ArrayLength; use core::ops::Add; use typenum::U1; /// Helper trait for `arr!` macro pub trait AddLength>: ArrayLength { /// Resulting length type Output: ArrayLength; } impl AddLength for N1 where N1: ArrayLength + Add, N2: ArrayLength, >::Output: ArrayLength, { type Output = >::Output; } /// Helper type for `arr!` macro pub type Inc = >::Output; #[doc(hidden)] #[macro_export] macro_rules! arr_impl { ($T:ty; $N:ty, [$($x:expr),*], []) => ({ unsafe { $crate::transmute::<_, $crate::GenericArray<$T, $N>>([$($x),*]) } }); ($T:ty; $N:ty, [], [$x1:expr]) => ( arr_impl!($T; $crate::arr::Inc<$T, $N>, [$x1 as $T], []) ); ($T:ty; $N:ty, [], [$x1:expr, $($x:expr),+]) => ( arr_impl!($T; $crate::arr::Inc<$T, $N>, [$x1 as $T], [$($x),+]) ); ($T:ty; $N:ty, [$($y:expr),+], [$x1:expr]) => ( arr_impl!($T; $crate::arr::Inc<$T, $N>, [$($y),+, $x1 as $T], []) ); ($T:ty; $N:ty, [$($y:expr),+], [$x1:expr, $($x:expr),+]) => ( arr_impl!($T; $crate::arr::Inc<$T, $N>, [$($y),+, $x1 as $T], [$($x),+]) ); } /// Macro allowing for easy generation of Generic Arrays. /// Example: `let test = arr![u32; 1, 2, 3];` #[macro_export] macro_rules! arr { ($T:ty; $(,)*) => ({ unsafe { $crate::transmute::<[$T; 0], $crate::GenericArray<$T, $crate::typenum::U0>>([]) } }); ($T:ty; $($x:expr),* $(,)*) => ( arr_impl!($T; $crate::typenum::U0, [], [$($x),*]) ); ($($x:expr,)+) => (arr![$($x),*]); () => ("""Macro requires a type, e.g. `let array = arr![u32; 1, 2, 3];`") }