abi_stable/std_types/tuple.rs
1//! Contains ffi-safe equivalents of tuples up to 4 elements.
2
3#![allow(non_snake_case)]
4
5macro_rules! declare_tuple {
6(
7 struct_attrs[ $(#[$meta: meta])* ]
8
9 into_tuple_attrs[ $(#[$into_tuple_attrs: meta])* ]
10
11 from_tuple_attrs[ $(#[$from_tuple_attrs: meta])* ]
12
13 $tconstr: ident[$( $tparam: ident ),* $(,)? ]
14) => (
15 $(#[$meta])*
16 #[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, StableAbi)]
17 #[repr(C)]
18 pub struct $tconstr< $($tparam,)* > (
19 $(pub $tparam,)*
20 );
21
22 impl_into_rust_repr! {
23 impl[ $($tparam,)* ] Into<( $($tparam,)* )> for $tconstr< $($tparam,)* > {
24 fn(this){
25 let $tconstr($($tparam,)*) = this;
26 ($($tparam,)*)
27 }
28 }
29 }
30
31 impl_from_rust_repr! {
32 impl[ $($tparam,)* ] From<( $($tparam,)* )> for $tconstr< $($tparam,)* > {
33 fn(this){
34 let ($($tparam,)*) = this;
35 $tconstr ( $($tparam),* )
36 }
37 }
38 }
39
40 #[allow(clippy::missing_const_for_fn)]
41 impl< $($tparam,)* > $tconstr<$($tparam,)*>{
42 $(#[$into_tuple_attrs])*
43 #[inline]
44 pub fn into_tuple(self)-> ($($tparam,)*) {
45 let $tconstr($($tparam,)*) = self;
46 ($($tparam,)*)
47 }
48 $(#[$from_tuple_attrs])*
49 #[inline]
50 pub fn from_tuple(from: ($($tparam,)*))-> Self {
51 let ($($tparam,)*) = from;
52 Self($($tparam,)*)
53 }
54 }
55
56)}
57
58declare_tuple! {
59 struct_attrs[
60 /// An ffi safe 1 element tuple.
61 ]
62
63 into_tuple_attrs[
64 /// Converts this Tuple1 to a unary tuple.
65 ///
66 /// # Example
67 ///
68 /// ```rust
69 /// use abi_stable::std_types::*;
70 ///
71 /// assert_eq!(Tuple1(1).into_tuple(), (1,));
72 ///
73 /// ```
74 ]
75
76 from_tuple_attrs[
77 /// Converts a unary tuple to a `Tuple1`.
78 ///
79 /// # Example
80 ///
81 /// ```rust
82 /// use abi_stable::std_types::*;
83 ///
84 /// assert_eq!(Tuple1::from_tuple((1,)), Tuple1(1));
85 ///
86 /// ```
87 ]
88
89 Tuple1[
90 A,
91 ]
92}
93
94declare_tuple! {
95 struct_attrs[
96 /// An ffi safe 2 element tuple.
97 ]
98
99 into_tuple_attrs[
100 /// Converts this Tuple2 to a pair.
101 ///
102 /// # Example
103 ///
104 /// ```rust
105 /// use abi_stable::std_types::*;
106 ///
107 /// assert_eq!(Tuple2(1, 2).into_tuple(), (1, 2));
108 ///
109 /// ```
110 ]
111
112 from_tuple_attrs[
113 /// Converts a pair to a `Tuple2`.
114 ///
115 /// # Example
116 ///
117 /// ```rust
118 /// use abi_stable::std_types::*;
119 ///
120 /// assert_eq!(Tuple2::from_tuple((1, 2)), Tuple2(1, 2));
121 ///
122 /// ```
123 ]
124
125 Tuple2[
126 A,
127 B,
128 ]
129}
130
131declare_tuple! {
132 struct_attrs[
133 /// An ffi safe 3 element tuple.
134 ]
135
136 into_tuple_attrs[
137 /// Converts this Tuple3 to a 3-tuple.
138 ///
139 /// # Example
140 ///
141 /// ```rust
142 /// use abi_stable::std_types::*;
143 ///
144 /// assert_eq!(Tuple3(1, 2, 3).into_tuple(), (1, 2, 3));
145 ///
146 /// ```
147 ]
148
149 from_tuple_attrs[
150 /// Converts a 3-tuple to a `Tuple3`.
151 ///
152 /// # Example
153 ///
154 /// ```rust
155 /// use abi_stable::std_types::*;
156 ///
157 /// assert_eq!(Tuple3::from_tuple((1, 2, 3)), Tuple3(1, 2, 3));
158 ///
159 /// ```
160 ]
161
162 Tuple3[
163 A,
164 B,
165 C,
166 ]
167}
168
169declare_tuple! {
170 struct_attrs[
171 /// An ffi safe 4 element tuple.
172 ]
173
174 into_tuple_attrs[
175 /// Converts this Tuple4 to a 4-tuple.
176 ///
177 /// # Example
178 ///
179 /// ```rust
180 /// use abi_stable::std_types::*;
181 ///
182 /// assert_eq!(Tuple4(1, 2, 3, 4).into_tuple(), (1, 2, 3, 4));
183 ///
184 /// ```
185 ]
186
187 from_tuple_attrs[
188 /// Converts a 4-tuple to a `Tuple4`.
189 ///
190 /// # Example
191 ///
192 /// ```rust
193 /// use abi_stable::std_types::*;
194 ///
195 /// assert_eq!(Tuple4::from_tuple((1, 2, 3, 4)), Tuple4(1, 2, 3, 4));
196 ///
197 /// ```
198 ]
199
200 Tuple4[
201 A,
202 B,
203 C,
204 D,
205 ]
206}
207
208#[cfg(test)]
209mod tests {
210 use super::*;
211
212 #[test]
213 #[allow(clippy::unit_cmp)]
214 fn value_macro() {
215 assert_eq!(rtuple!(), ());
216 assert_eq!(rtuple!(3), Tuple1(3));
217 assert_eq!(rtuple!(3, 5), Tuple2(3, 5));
218 assert_eq!(rtuple!(3, 5, 8), Tuple3(3, 5, 8));
219 assert_eq!(rtuple!(3, 5, 8, 9), Tuple4(3, 5, 8, 9));
220 }
221
222 #[test]
223 #[allow(clippy::let_unit_value)]
224 fn type_macro() {
225 let _: RTuple!() = ();
226 let _: RTuple!(i32) = Tuple1(3);
227 let _: RTuple!(i32, i32,) = Tuple2(3, 5);
228 let _: RTuple!(i32, i32, u32,) = Tuple3(3, 5, 8);
229 let _: RTuple!(i32, i32, u32, u32) = Tuple4(3, 5, 8, 9);
230 }
231}