typle
The typle crate provides the ability to constrain generic arguments to 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:
!
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 parameters must have the
same length.
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"), (9u8, ())),
((2.0, 9u8), ("test", ()))
);
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 simple access to individual components.
For example the Hash trait for tuples simply hashes each component of the
tuple in order.
Using typle this can be written as:
Compare this to the current implementation in the standard library:
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
impl_hash_tuple!
See the crate documentation for more examples.