1use typenum::{B1, Bit, Sub1, U0, UInt, Unsigned};
12
13use crate::list_traits::{
14 EmptyList, IntoList, List, ListIndex, ListTypeIterator, ListTypeIteratorAdvance,
15 ListTypeProperty,
16};
17use std::ops::{RangeFrom, Sub};
18
19pub trait IntoTupleList {
24 type Output: List;
26 fn into_tuple_list(self) -> Self::Output;
33}
34
35impl<T: IntoList> IntoTupleList for T {
36 type Output = T::Output<()>;
37 fn into_tuple_list(self) -> Self::Output {
41 self.into_list()
42 }
43}
44
45impl<P: ListTypeProperty> ListTypeIteratorAdvance<P> for () {
49 fn advancer<R: List>(_iter: &mut ListTypeIterator<R, P>) -> Option<P::Output> {
51 None
52 }
53}
54
55impl<P: ListTypeProperty, H: 'static, T: ListTypeIteratorAdvance<P>> ListTypeIteratorAdvance<P>
56 for (H, T)
57{
58 fn advancer<R: List>(iter: &mut ListTypeIterator<R, P>) -> Option<P::Output> {
62 iter.advance = T::advancer::<R>;
63 Some(P::property::<(H, T)>())
64 }
65}
66
67impl EmptyList for () {
71 type PushFirst<U: 'static> = (U, ());
72 fn push_first<U: 'static>(self, item: U) -> Self::PushFirst<U> {
74 (item, ())
75 }
76
77 type RootEmpty = Self;
78 fn root_empty() -> Self::RootEmpty {}
80}
81
82impl<H: 'static, T: List> List for (H, T) {
86 type Empty = ();
87 fn empty() -> Self::Empty {}
89
90 type Head = H;
91 fn head(&self) -> &Self::Head {
93 &self.0
94 }
95
96 type Tail = T;
97 fn tail(&self) -> &Self::Tail {
99 &self.1
100 }
101
102 type Push<U: 'static> = (U, Self);
103 fn push<U: 'static>(self, item: U) -> Self::Push<U> {
105 (item, self)
106 }
107
108 type Append<U: List> = <T::Append<U> as List>::Push<H>;
109 fn append<U: List>(self, other: U) -> Self::Append<U> {
113 self.1.append(other).push(self.0)
114 }
115
116 type ReverseOnto<U: List> = T::ReverseOnto<U::Push<H>>;
117 fn reverse_onto<U: List>(self, other: U) -> Self::ReverseOnto<U> {
121 self.1.reverse_onto(other.push(self.0))
122 }
123}
124
125impl ListIndex<RangeFrom<U0>> for () {
126 type Output = ();
127 fn index(&self, _index: RangeFrom<U0>) -> &Self::Output {
129 self
130 }
131}
132
133impl<H: 'static, T: List> ListIndex<RangeFrom<U0>> for (H, T) {
134 type Output = (H, T);
135 fn index(&self, _index: RangeFrom<U0>) -> &Self::Output {
137 self
138 }
139}
140
141impl<H: 'static, T: List, U: Unsigned, B: Bit> ListIndex<RangeFrom<UInt<U, B>>> for (H, T)
142where
143 T: ListIndex<RangeFrom<Sub1<UInt<U, B>>>>,
144 UInt<U, B>: Sub<B1>,
145{
146 type Output = <T as ListIndex<RangeFrom<Sub1<UInt<U, B>>>>>::Output;
147 fn index(&self, index: RangeFrom<UInt<U, B>>) -> &Self::Output {
151 self.tail().index((index.start - B1)..)
152 }
153}
154
155impl<H: 'static, T: List> ListIndex<U0> for (H, T) {
156 type Output = H;
157 fn index(&self, _index: U0) -> &Self::Output {
159 self.head()
160 }
161}
162
163impl<H: 'static, T: List, U: Unsigned, B: Bit> ListIndex<UInt<U, B>> for (H, T)
164where
165 T: ListIndex<Sub1<UInt<U, B>>>,
166 UInt<U, B>: Sub<B1>,
167{
168 type Output = <T as ListIndex<Sub1<UInt<U, B>>>>::Output;
169 fn index(&self, index: UInt<U, B>) -> &Self::Output {
173 self.tail().index(index - B1)
174 }
175}
176
177#[cfg(test)]
180mod tests {
181 use super::*;
182 use crate::list_traits::TypeIdIterator;
183 use std::any::TypeId;
184 use typenum::{U0, U1, U2, U3};
185
186 #[test]
188 fn type_id_iterator() {
189 let ids: [TypeId; 3] = [
190 TypeId::of::<u32>(),
191 TypeId::of::<f64>(),
192 TypeId::of::<&str>(),
193 ];
194 for mut iter in [
195 TypeIdIterator::<(u32, (f64, (&str, ())))>::new(),
196 TypeIdIterator::<(u32, (f64, (&str, ())))>::default(),
197 ] {
198 assert!(iter.by_ref().eq(ids.iter().copied()));
199 assert_eq!(iter.next(), None);
200 }
201 type Canonical = <(u32, f64, &'static str) as crate::IntoCStackList>::Output;
203 let mut canonical = TypeIdIterator::<Canonical>::default();
204 assert!(canonical.by_ref().eq(ids));
205 assert_eq!(canonical.next(), None);
206 assert_eq!(TypeIdIterator::<()>::default().next(), None);
207 assert_eq!(TypeIdIterator::<crate::CNil<()>>::new().next(), None);
208 }
209
210 #[test]
212 fn empty_list() {
213 let () = <()>::empty();
214 }
215
216 #[test]
218 fn into_list() {
219 let () = ().into_tuple_list();
220 assert_eq!((1, 2, 3).into_tuple_list(), (1, (2, (3, ()))));
221 assert_eq!(
222 (1, 2.5, "Hello").into_tuple_list(),
223 (1, (2.5, ("Hello", ())))
224 );
225 }
226
227 #[test]
229 fn push() {
230 assert_eq!(().push_first(1), (1, ()));
231 assert_eq!((1, 2, 3).into_tuple_list().push(4), (4, (1, (2, (3, ())))));
232 }
233
234 #[test]
236 fn append() {
237 assert_eq!(
238 (1, 2, 3)
239 .into_tuple_list()
240 .append((4, 5, 6).into_tuple_list()),
241 (1, (2, (3, (4, (5, (6, ()))))))
242 );
243 }
244
245 #[test]
247 fn reverse() {
248 assert_eq!((1, 2, 3).into_tuple_list().reverse(), (3, (2, (1, ()))));
249 }
250
251 #[test]
253 fn list_length() {
254 assert_eq!(<()>::LENGTH, 0);
255 assert_eq!(<(i32, ())>::LENGTH, 1);
256 assert_eq!(<(i32, (f64, ()))>::LENGTH, 2);
257 }
258
259 #[test]
261 fn list_index() {
262 let list = (1, 2.5, "Hello").into_tuple_list();
263 assert_eq!(*list.index(U0::new()), 1);
264 assert_eq!(*list.index(U1::new()), 2.5);
265 assert_eq!(*list.index(U2::new()), "Hello");
266 }
267
268 #[test]
270 fn list_range_from_index() {
271 let list = (1, 2.5, "Hello").into_tuple_list();
272 assert_eq!(*list.index(U0::new()..), (1, (2.5, ("Hello", ()))));
273 assert_eq!(*list.index(U1::new()..), (2.5, ("Hello", ())));
274 assert_eq!(*list.index(U2::new()..), ("Hello", ()));
275 let () = *list.index(U3::new()..);
276 }
278}