musli_macros/internals/
apply.rs
1use proc_macro2::TokenStream;
2use quote::ToTokens;
3
4pub(crate) trait Apply<T> {
5 fn apply(&self, value: &T, tokens: &mut TokenStream);
6}
7
8impl<F, T> Apply<T> for F
9where
10 F: Fn(&T, &mut TokenStream),
11{
12 fn apply(&self, value: &T, tokens: &mut TokenStream) {
13 self(value, tokens)
14 }
15}
16
17pub(crate) struct IterItem<'a, A, T> {
18 apply: A,
19 value: &'a T,
20}
21
22impl<'a, A, T> ToTokens for IterItem<'a, A, T>
23where
24 A: Apply<T>,
25{
26 fn to_tokens(&self, tokens: &mut TokenStream) {
27 self.apply.apply(self.value, tokens);
28 }
29}
30
31pub(crate) struct Iter<'a, I, T> {
32 iter: I,
33 value: &'a T,
34}
35
36impl<'a, I, T> Iterator for Iter<'a, I, T>
37where
38 I: Iterator<Item: Apply<T>>,
39{
40 type Item = IterItem<'a, I::Item, T>;
41
42 fn next(&mut self) -> Option<Self::Item> {
43 let f = self.iter.next()?;
44
45 Some(IterItem {
46 apply: f,
47 value: self.value,
48 })
49 }
50}
51
52pub(crate) fn iter<I, T>(iter: I, value: &T) -> Iter<'_, I::IntoIter, T>
54where
55 I: IntoIterator<Item: Apply<T>>,
56{
57 Iter {
58 iter: iter.into_iter(),
59 value,
60 }
61}