Skip to main content

typed_quote/tokens/
iter_tokens.rs

1use super::*;
2
3impl<I: IntoIterator<Item: IntoTokens>> sealed::IntoTokens for IterTokens<I> {}
4impl<I: IntoIterator<Item: IntoTokens>> IntoTokens for IterTokens<I> {
5    crate::impl_into_tokens!(|self, ts| {
6        self.0.into_iter().for_each(|s| (s, &mut *ts).into_st());
7    });
8}
9
10impl<I: IntoIterator<Item: IntoTokens>, S: crate::Span> sealed::IntoTokens
11    for IterTokensWithDefaultSpan<I, S>
12{
13}
14impl<I: IntoIterator<Item: IntoTokens + WithSpan>, S: crate::Span> IntoTokens
15    for IterTokensWithDefaultSpan<I, S>
16{
17    crate::impl_into_tokens!(
18        #[proxy]
19        |self| IterTokens(self.0.into_iter().map(|s| s.with_default_span(self.1)))
20    );
21}
22
23impl<I: IntoIterator<Item: IntoTokens + WithSpan>, S: crate::Span> sealed::IntoTokens
24    for IterTokensWithReplacedSpan<I, S>
25{
26}
27impl<I: IntoIterator<Item: IntoTokens + WithSpan>, S: crate::Span> IntoTokens
28    for IterTokensWithReplacedSpan<I, S>
29{
30    crate::impl_into_tokens!(
31        #[proxy]
32        |self| IterTokens(self.0.into_iter().map(|s| s.with_replaced_span(self.1)))
33    );
34}
35
36impl<I: IntoIterator<Item: IntoTokens> + Clone> sealed::ToTokens for IterTokens<I> {}
37impl<I: IntoIterator<Item: IntoTokens> + Clone> ToTokens for IterTokens<I> {
38    crate::impl_to_tokens!(
39        #[proxy]
40        |self| Self::clone(self)
41    );
42}
43
44impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone, S: crate::Span> sealed::ToTokens
45    for IterTokensWithDefaultSpan<I, S>
46{
47}
48impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone, S: crate::Span> ToTokens
49    for IterTokensWithDefaultSpan<I, S>
50{
51    crate::impl_to_tokens!(
52        #[proxy]
53        |self| Self::clone(self)
54    );
55}
56
57impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone, S: crate::Span> sealed::ToTokens
58    for IterTokensWithReplacedSpan<I, S>
59{
60}
61impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone, S: crate::Span> ToTokens
62    for IterTokensWithReplacedSpan<I, S>
63{
64    crate::impl_to_tokens!(
65        #[proxy]
66        |self| Self::clone(self)
67    );
68}
69
70impl<I: IntoIterator<Item: IntoTokens + WithSpan>> sealed::WithSpan for IterTokens<I> {}
71impl<I: IntoIterator<Item: IntoTokens + WithSpan>> WithSpan for IterTokens<I> {
72    type WithDefaultSpan<S: crate::Span> = IterTokensWithDefaultSpan<I, S>;
73
74    fn with_default_span<S: crate::Span>(self, span: S) -> Self::WithDefaultSpan<S> {
75        IterTokensWithDefaultSpan(self.0, span)
76    }
77
78    type WithReplacedSpan<S: crate::Span> = IterTokensWithReplacedSpan<I, S>;
79
80    fn with_replaced_span<S: crate::Span>(self, span: S) -> Self::WithReplacedSpan<S> {
81        IterTokensWithReplacedSpan(self.0, span)
82    }
83}
84
85impl<I: IntoIterator<Item: IntoTokens + WithSpan>, SO: crate::Span> sealed::WithSpan
86    for IterTokensWithDefaultSpan<I, SO>
87{
88}
89impl<I: IntoIterator<Item: IntoTokens + WithSpan>, SO: crate::Span> WithSpan
90    for IterTokensWithDefaultSpan<I, SO>
91{
92    type WithDefaultSpan<S: crate::Span> = Self;
93
94    fn with_default_span<S: crate::Span>(self, _: S) -> Self::WithDefaultSpan<S> {
95        self
96    }
97
98    type WithReplacedSpan<S: crate::Span> = IterTokensWithReplacedSpan<I, S>;
99
100    fn with_replaced_span<S: crate::Span>(self, span: S) -> Self::WithReplacedSpan<S> {
101        IterTokensWithReplacedSpan(self.0, span)
102    }
103}
104
105impl<I: IntoIterator<Item: IntoTokens + WithSpan>, SO: crate::Span> sealed::WithSpan
106    for IterTokensWithReplacedSpan<I, SO>
107{
108}
109impl<I: IntoIterator<Item: IntoTokens + WithSpan>, SO: crate::Span> WithSpan
110    for IterTokensWithReplacedSpan<I, SO>
111{
112    type WithDefaultSpan<S: crate::Span> = Self;
113
114    fn with_default_span<S: crate::Span>(self, _: S) -> Self::WithDefaultSpan<S> {
115        self
116    }
117
118    type WithReplacedSpan<S: crate::Span> = IterTokensWithReplacedSpan<I, S>;
119
120    fn with_replaced_span<S: crate::Span>(self, span: S) -> Self::WithReplacedSpan<S> {
121        IterTokensWithReplacedSpan(self.0, span)
122    }
123}
124
125impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone> sealed::RefWithSpan for IterTokens<I> {}
126impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone> RefWithSpan for IterTokens<I> {
127    type RefWithDefaultSpan<'a, S: crate::Span>
128        = IterTokensWithDefaultSpan<clone_into_iter::CloneIntoIter<'a, I>, S>
129    where
130        Self: 'a;
131
132    fn ref_with_default_span<S: crate::Span>(&self, span: S) -> Self::RefWithDefaultSpan<'_, S> {
133        IterTokensWithDefaultSpan(clone_into_iter::CloneIntoIter(&self.0), span)
134    }
135
136    type RefWithReplacedSpan<'a, S: crate::Span>
137        = IterTokensWithReplacedSpan<clone_into_iter::CloneIntoIter<'a, I>, S>
138    where
139        Self: 'a;
140
141    fn ref_with_replaced_span<S: crate::Span>(&self, span: S) -> Self::RefWithReplacedSpan<'_, S> {
142        IterTokensWithReplacedSpan(clone_into_iter::CloneIntoIter(&self.0), span)
143    }
144}
145
146impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone, SO: crate::Span> sealed::RefWithSpan
147    for IterTokensWithDefaultSpan<I, SO>
148{
149}
150impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone, SO: crate::Span> RefWithSpan
151    for IterTokensWithDefaultSpan<I, SO>
152{
153    type RefWithDefaultSpan<'a, S: crate::Span>
154        = &'a Self
155    where
156        Self: 'a;
157
158    fn ref_with_default_span<S: crate::Span>(&self, _: S) -> Self::RefWithDefaultSpan<'_, S> {
159        self
160    }
161
162    type RefWithReplacedSpan<'a, S: crate::Span>
163        = IterTokensWithReplacedSpan<clone_into_iter::CloneIntoIter<'a, I>, S>
164    where
165        Self: 'a;
166
167    fn ref_with_replaced_span<S: crate::Span>(&self, span: S) -> Self::RefWithReplacedSpan<'_, S> {
168        IterTokensWithReplacedSpan(clone_into_iter::CloneIntoIter(&self.0), span)
169    }
170}
171
172impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone, SO: crate::Span> sealed::RefWithSpan
173    for IterTokensWithReplacedSpan<I, SO>
174{
175}
176impl<I: IntoIterator<Item: IntoTokens + WithSpan> + Clone, SO: crate::Span> RefWithSpan
177    for IterTokensWithReplacedSpan<I, SO>
178{
179    type RefWithDefaultSpan<'a, S: crate::Span>
180        = &'a Self
181    where
182        Self: 'a;
183
184    fn ref_with_default_span<S: crate::Span>(&self, _: S) -> Self::RefWithDefaultSpan<'_, S> {
185        self
186    }
187
188    type RefWithReplacedSpan<'a, S: crate::Span>
189        = IterTokensWithReplacedSpan<clone_into_iter::CloneIntoIter<'a, I>, S>
190    where
191        Self: 'a;
192
193    fn ref_with_replaced_span<S: crate::Span>(&self, span: S) -> Self::RefWithReplacedSpan<'_, S> {
194        IterTokensWithReplacedSpan(clone_into_iter::CloneIntoIter(&self.0), span)
195    }
196}
197
198mod clone_into_iter {
199    #[derive(Debug)]
200    pub struct CloneIntoIter<'a, I: Clone + IntoIterator>(pub(super) &'a I);
201
202    impl<'a, I: Clone + IntoIterator> Copy for CloneIntoIter<'a, I> {}
203    impl<'a, I: Clone + IntoIterator> Clone for CloneIntoIter<'a, I> {
204        fn clone(&self) -> Self {
205            *self
206        }
207    }
208
209    impl<'a, I: Clone + IntoIterator> IntoIterator for CloneIntoIter<'a, I> {
210        type Item = I::Item;
211
212        type IntoIter = I::IntoIter;
213
214        fn into_iter(self) -> Self::IntoIter {
215            I::into_iter(self.0.clone())
216        }
217    }
218}