musli_macros/internals/
apply.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use proc_macro2::TokenStream;
use quote::ToTokens;

pub(crate) trait Apply<T> {
    fn apply(&self, value: &T, tokens: &mut TokenStream);
}

impl<F, T> Apply<T> for F
where
    F: Fn(&T, &mut TokenStream),
{
    fn apply(&self, value: &T, tokens: &mut TokenStream) {
        self(value, tokens)
    }
}

pub(crate) struct IterItem<'a, A, T> {
    apply: A,
    value: &'a T,
}

impl<'a, A, T> ToTokens for IterItem<'a, A, T>
where
    A: Apply<T>,
{
    fn to_tokens(&self, tokens: &mut TokenStream) {
        self.apply.apply(self.value, tokens);
    }
}

pub(crate) struct Iter<'a, I, T> {
    iter: I,
    value: &'a T,
}

impl<'a, I, T> Iterator for Iter<'a, I, T>
where
    I: Iterator<Item: Apply<T>>,
{
    type Item = IterItem<'a, I::Item, T>;

    fn next(&mut self) -> Option<Self::Item> {
        let f = self.iter.next()?;

        Some(IterItem {
            apply: f,
            value: self.value,
        })
    }
}

/// Apply an iterator of functions to a value.
pub(crate) fn iter<I, T>(iter: I, value: &T) -> Iter<'_, I::IntoIter, T>
where
    I: IntoIterator<Item: Apply<T>>,
{
    Iter {
        iter: iter.into_iter(),
        value,
    }
}