tuple_length/
lib.rs

1#![forbid(unsafe_code)]
2#![no_std]
3#![warn(missing_docs)]
4#![deny(missing_docs)]
5
6//! Return the length of a tuple.
7//! # Examples
8//! Usage:
9//! ```
10//! # use tuple_length::TupLen;
11//! assert_eq!(().len(), 0);
12//! assert_eq!((1i8,).len(), 1);
13//! assert_eq!((1u16, 2u64).len(), 2);
14//! assert_eq!((1i8, 2usize, 3i64).len(), 3);
15//! ```
16//! # Supported tuple lengths
17//! Possible length 8, 16, 32, 64.
18//! By default the selected operations are implemented to tuples upto a length of **8 elements**.
19//!
20//! # Features
21//! You can specify the length: features = ["tup_len_16"] or features = ["tup_len_32"] or features = ["tup_len_64"].
22//!
23
24extern crate tuple_macro;
25use tuple_macro::tuple_length;
26
27/// Implements the len method on a tuple.
28pub trait TupLen {
29    /// Returns the length of this tuple.
30    /// # Examples
31    /// Usage:
32    /// ```
33    /// # use tuple_length::TupLen;
34    /// let tuple: (i8, u8, i16, u16, i32, u32, i64, &str) = (-128, 255, -327, 655, -100, 229, -5,
35    /// "rust");
36    /// assert_eq!(tuple.len(), 8);
37    ///
38    /// let tuple = ([2u8, 0u8, 2u8, 1u8], 2021u16, vec!["r", "u", "s", "t"]);
39    /// assert_eq!(tuple.len(), 3);
40    /// ```
41    fn len(&self) -> usize;
42}
43
44impl TupLen for () {
45    #[inline]
46    fn len(&self) -> usize {
47        0usize
48    }
49}
50
51tuple_length!();
52
53#[cfg(test)]
54mod tuple_len_tests {
55    use super::*;
56
57    #[cfg(all(not(feature = "16"), not(feature = "32"), not(feature = "64")))]
58    #[test]
59    fn features_8_ok() {
60        assert_eq!((1i8, 2u8, 3i16, 4u16, 5i32, 6u32, 7i64, 8u64).len(), 8usize);
61    }
62
63    #[cfg(all(not(feature = "32"), not(feature = "64"), feature = "16"))]
64    #[test]
65    fn features_16_ok() {
66        assert_eq!(
67            (
68                1i8, 2u8, 3i16, 4u16, 5i32, 6u32, 7i64, 8u64, 9isize, 10usize, 11i128, 12u128,
69                13i8, 14u8, 15i16, 16u16
70            )
71                .len(),
72            16usize
73        );
74    }
75
76    #[cfg(all(not(feature = "16"), not(feature = "64"), feature = "32"))]
77    #[test]
78    fn features_32_ok() {
79        assert_eq!(
80            (
81                1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8, 9u8, 10u8, 11u8, 12u8, 13u8, 14u8, 15u8,
82                16u8, 17u8, 18u8, 19u8, 20u8, 21u8, 22u8, 23u8, 24u8, 25u8, 26u8, 27u8, 28u8, 29u8,
83                30u8, 31u8, 32u8
84            )
85                .len(),
86            32usize
87        );
88    }
89
90    #[cfg(all(not(feature = "16"), not(feature = "32"), feature = "64"))]
91    #[test]
92    fn features_64_ok() {
93        assert_eq!(
94            (
95                1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8, 9u8, 10u8, 11u8, 12u8, 13u8, 14u8, 15u8,
96                16u8, 17u8, 18u8, 19u8, 20u8, 21u8, 22u8, 23u8, 24u8, 25u8, 26u8, 27u8, 28u8, 29u8,
97                30u8, 31u8, 32u8, 33u8, 34u8, 35u8, 36u8, 37u8, 38u8, 39u8, 40u8, 41u8, 42u8, 43u8,
98                44u8, 45u8, 46u8, 47u8, 48u8, 49u8, 50u8, 51u8, 52u8, 53u8, 54u8, 55u8, 56u8, 57u8,
99                58u8, 59u8, 60u8, 61u8, 62u8, 63u8, 64u8
100            )
101                .len(),
102            64usize
103        );
104    }
105}
106