From 868329539613b40b48565c6bf11fd4920beaacee Mon Sep 17 00:00:00 2001 From: Mica White Date: Thu, 21 May 2026 18:50:06 -0400 Subject: Array tuple --- src/lib.rs | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index b7b58ba..4ef5359 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,8 @@ use varihappy_macros::{ type_to_field_access, }; +pub use varihappy_macros::array_tuple; + pub trait Tuple { const LEN: usize; @@ -29,14 +31,21 @@ pub trait Tuple1: Tuple { fn into_rest(self) -> Self::Rest; } -pub trait ArrayTupleOrEmpty: Tuple {} +pub trait ArrayTuple: Tuple { + fn fill(item: T) -> Self + where + Self: Sized, + T: Copy; +} -pub trait ArrayTuple: Tuple { +pub trait NonEmptyArrayTuple: Tuple { type Item; -} -impl ArrayTupleOrEmpty for () {} -impl> ArrayTupleOrEmpty for T {} + fn fill(item: Self::Item) -> Self + where + Self: Sized, + Self::Item: Copy; +} impl Tuple for () { type AsRef<'a> @@ -57,6 +66,25 @@ impl Tuple for () { fn rev(self) -> Self::Reversed {} } +impl ArrayTuple for () { + fn fill(_item: T) -> Self + where + Self: Sized, + T: Copy, + { + } +} + +impl> ArrayTuple for T { + fn fill(item: I) -> Self + where + Self: Sized, + I: Copy, + { + ::fill(item) + } +} + macro_rules! tuple_apply { (($($letter: ident,)*), $other: ident) => { $other!(($($letter,)*)) @@ -192,16 +220,20 @@ macro_rules! tuple_trait { }; } -macro_rules! t { - ($_t:tt) => { - T +macro_rules! replace { + ($_t:tt, $new: tt) => { + $new }; } macro_rules! impl_array_tuple_inner { (($($letter: ident,)*)) => { - impl ArrayTuple for ($(t!($letter),)*) { + impl NonEmptyArrayTuple for ($(replace!($letter, T),)*) { type Item = T; + + fn fill(item: Self::Item) -> Self where T: Copy { + ($(replace!($letter, item),)*) + } } }; } -- cgit v1.3.1