#tuple #meta-programming #variadic

macro typle

Generic tuple bounds and transformations

49 releases (9 breaking)

0.10.1 Apr 16, 2024
0.9.14 Apr 17, 2024
0.9.7 Mar 30, 2024
0.7.9 Dec 31, 2023
0.2.5 Nov 27, 2023

#2441 in Rust patterns

Download history 18/week @ 2024-01-08 10/week @ 2024-01-22 7/week @ 2024-01-29 152/week @ 2024-02-12 136/week @ 2024-02-19 70/week @ 2024-02-26 301/week @ 2024-03-04 404/week @ 2024-03-11 225/week @ 2024-03-18 475/week @ 2024-03-25 607/week @ 2024-04-01 252/week @ 2024-04-08 1064/week @ 2024-04-15 8/week @ 2024-04-22

1,964 downloads per month
Used in hefty

MIT license

170KB
3K SLoC

typle

The typle crate provides the ability to constrain generic arguments to be tuples and supports manipulation of the tuple components.

For example, to define a function to zip a pair of tuples into a tuple of pairs:

#[typle(Tuple for 0..=12)]
pub fn zip<A: Tuple, B: Tuple>(
    a: A,
    b: B,
) -> typle_for!(i in .. => (A<{i}>, B<{i}>))
{
    typle_for!(i in .. => (a[[i]], b[[i]]))
}

The types A and B are generic but are constrained to be tuples. The tuples can have 0 to 12 components of any (sized) type, but both tuples must have the same length.

The typle_for! macro loops over an index returning a new tuple with the specified components. For the function return type it creates a type tuple: ((A<0>, B<0>), (A<1>, B<1>),...). In the function body it creates a value tuple: ((a.0, b.0), (a.1, b.1),...).

assert_eq!(
    zip(("LHR", "FCO", "ZRH"), (51.5, 41.8, 47.5)),
    (("LHR", 51.5), ("FCO", 41.8), ("ZRH", 47.5))
);
assert_eq!(
    zip((2.0, "test"), (Some(9u8), ('a', 'b'))),
    ((2.0, Some(9u8)), ("test", ('a', 'b')))
);
assert_eq!(
    zip((), ()),
    ()
);

A common use of typle is to implement a trait for tuples of multiple lengths. Compared to using declarative macros, the typle code looks more Rust-like and provides access to individual components and their position.

For example the Hash implementation for tuples simply hashes each component of the tuple in order.

Using typle this can be written as:

#[typle(Tuple for 1..=12)]
impl<T> Hash for T
where
    T: Tuple,  // `T` must be a tuple with 1-12 components.
    T<_>: Hash,  // Each component must implement `Hash`.
    T<{T::LEN - 1}>: ?Sized,  // The last component may be unsized.
{
    #[inline]
    fn hash<S: Hasher>(&self, state: &mut S) {
        for typle_index!(i) in 0..T::LEN {
            self[[i]].hash(state);
        }
    }
}

Compare this to the current implementation in the standard library:

macro_rules! impl_hash_tuple {
    ( $($name:ident)+) => (
        impl<$($name: Hash),+> Hash for ($($name,)+) where last_type!($($name,)+): ?Sized {
            #[allow(non_snake_case)]
            #[inline]
            fn hash<S: Hasher>(&self, state: &mut S) {
                let ($(ref $name,)+) = *self;
                $($name.hash(state);)+
            }
        }
    );
}

macro_rules! last_type {
    ($a:ident,) => { $a };
    ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
}

impl_hash_tuple! { T }
impl_hash_tuple! { T B }
impl_hash_tuple! { T B C }
impl_hash_tuple! { T B C D }
impl_hash_tuple! { T B C D E }
impl_hash_tuple! { T B C D E F }
impl_hash_tuple! { T B C D E F G }
impl_hash_tuple! { T B C D E F G H }
impl_hash_tuple! { T B C D E F G H I }
impl_hash_tuple! { T B C D E F G H I J }
impl_hash_tuple! { T B C D E F G H I J K }
impl_hash_tuple! { T B C D E F G H I J K L }

See the crate documentation for more examples.

Dependencies

~325–780KB
~19K SLoC