macro_toolset/string/general/
tuple.rs

1//! Implementations for tuples.
2//!
3//! Since array-or-slice-like types can only carry  elements of the same type,
4//! tuple is the only way to `push` multiple elements with different types.
5
6use super::{StringExtT, StringT};
7use crate::wrapper;
8
9wrapper! {
10    #[derive(Debug, Clone, Copy)]
11    /// Tuple but will not insert separator between elements.
12    pub SeplessTuple<T>(pub T)
13}
14
15macro_rules! impl_for_tuple {
16    ( $( $name:ident )+ ) => {
17        #[allow(non_snake_case)]
18        impl<$($name: StringT),+> StringT for ($($name,)+) {
19            #[inline]
20            fn encode_to_buf(self, string: &mut Vec<u8>) {
21                let ($($name,)+) = self;
22                $(
23                    $name.encode_to_buf(string);
24                )+
25            }
26
27            #[inline]
28            fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
29                let ($($name,)+) = self;
30                $(
31                    $name.encode_to_buf_with_separator(string, separator);
32                )+
33            }
34
35            #[inline]
36            fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
37                let ($($name,)+) = self;
38                $(
39                    $name.encode_to_bytes_buf(string);
40                )+
41            }
42
43            #[inline]
44            fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
45                let ($($name,)+) = self;
46                $(
47                    $name.encode_to_bytes_buf_with_separator(string, separator);
48                )+
49            }
50        }
51
52        #[allow(non_snake_case)]
53        impl<$($name: StringExtT),+> StringExtT for ($($name,)+) {
54            #[inline]
55            fn with_prefix<P: StringExtT + Copy>(self, prefix: P) -> impl StringExtT {
56                let ($($name,)+) = self;
57
58                (
59                    $(
60                        SeplessTuple {
61                            inner: (prefix, $name),
62                        },
63                    )+
64                )
65            }
66
67            #[inline]
68            fn with_suffix<S: StringExtT + Copy>(self, suffix: S) -> impl StringExtT {
69                let ($($name,)+) = self;
70
71                (
72                    $(
73                        SeplessTuple {
74                            inner: ($name, suffix),
75                        },
76                    )+
77                )
78            }
79        }
80
81        #[allow(non_snake_case)]
82        impl<$($name: StringT),+> StringT for SeplessTuple<($($name,)+)> {
83            #[inline]
84            fn encode_to_buf(self, string: &mut Vec<u8>) {
85                self.inner.encode_to_buf(string);
86            }
87
88            #[inline]
89            fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
90                self.inner.encode_to_buf(string);
91                string.extend(separator.as_bytes());
92            }
93
94            #[inline]
95            fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
96                self.inner.encode_to_bytes_buf(string);
97            }
98
99            #[inline]
100            fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
101                self.inner.encode_to_bytes_buf(string);
102                string.extend(separator.as_bytes());
103            }
104        }
105
106        #[allow(non_snake_case)]
107        impl<$($name: StringExtT),+> StringExtT for SeplessTuple<($($name,)+)> {}
108    };
109}
110
111impl_for_tuple!(T1);
112impl_for_tuple!(T1 T2);
113impl_for_tuple!(T1 T2 T3);
114impl_for_tuple!(T1 T2 T3 T4);
115impl_for_tuple!(T1 T2 T3 T4 T5);
116impl_for_tuple!(T1 T2 T3 T4 T5 T6);
117impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7);
118impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8);
119impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9);
120impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10);
121impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11);
122impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12);
123impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13);
124impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14);
125impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
126impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16);
127impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17);
128impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18);
129impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19);
130impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20);
131impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21);
132impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22);
133impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23);
134impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23 T24);
135impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23 T24 T25);
136impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23 T24 T25 T26);
137impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23 T24 T25 T26 T27);
138impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23 T24 T25 T26 T27 T28);
139impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23 T24 T25 T26 T27 T28 T29);
140impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23 T24 T25 T26 T27 T28 T29 T30);
141impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23 T24 T25 T26 T27 T28 T29 T30 T31);
142impl_for_tuple!(T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 T21 T22 T23 T24 T25 T26 T27 T28 T29 T30 T31 T32);