tuplities_len/lib.rs
1//! A trait for getting the length of tuples at compile time.
2//!
3//! This crate provides the `TupleLen` trait, which allows getting the length
4//! of a tuple as a compile-time `typenum::Unsigned` type.
5
6#![no_std]
7
8/// A trait for getting the compile-time length of a tuple.
9///
10/// This trait provides the length of a tuple as an associated type `Len`
11/// that implements `typenum::Unsigned`.
12///
13/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
14#[tuplities_derive::impl_len]
15pub trait TupleLen {
16 /// The length of the tuple as a `typenum::Unsigned` type.
17 type Len: typenum::Unsigned;
18}
19
20/// A marker trait for empty tuples (size 0).
21///
22/// This trait is implemented for the unit type `()` and can be used
23/// for type-level programming to identify empty tuples.
24///
25/// # Examples
26///
27/// ```
28/// use tuplities_len::{UnitTuple, TupleLen};
29/// use typenum::U0;
30///
31/// fn is_empty<T: UnitTuple>(_tuple: T) {
32/// // This function only accepts empty tuples
33/// }
34///
35/// is_empty(()); // This works
36/// // is_empty((1,)); // This would not compile
37/// ```
38///
39/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
40pub trait UnitTuple: TupleLen<Len = typenum::U0> {}
41
42impl UnitTuple for () {}
43
44/// A marker trait for single-element tuples (size 1).
45///
46/// This trait is implemented for single-element tuples `(T,)` and can be used
47/// for type-level programming to identify single-element tuples.
48///
49/// # Examples
50///
51/// ```
52/// use tuplities_len::{SingletonTuple, TupleLen};
53/// use typenum::U1;
54///
55/// fn is_single<T: SingletonTuple>(_tuple: T) {
56/// // This function only accepts single-element tuples
57/// }
58///
59/// is_single((42,)); // This works
60/// // is_single((1, 2)); // This would not compile
61/// // is_single(()); // This would not compile
62/// ```
63///
64/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
65pub trait SingletonTuple: TupleLen<Len = typenum::U1> {}
66
67impl<T> SingletonTuple for (T,) {}
68
69/// A marker trait for two-element tuples (size 2).
70///
71/// This trait is implemented for two-element tuples `(T1, T2)` and can be used
72/// for type-level programming to identify two-element tuples.
73///
74/// # Examples
75///
76/// ```rust
77/// use tuplities_len::{PairTuple, TupleLen};
78/// use typenum::U2;
79/// fn is_pair<T: PairTuple>(_tuple: T) {
80/// // This function only accepts two-element tuples
81/// }
82/// is_pair((1, 2)); // This works
83/// // is_pair((42,)); // This would not compile
84/// // is_pair(()); // This would not compile
85/// ```
86///
87pub trait PairTuple: TupleLen<Len = typenum::U2> {}
88
89impl<T1, T2> PairTuple for (T1, T2) {}