      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
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154

    #[cfg(feature = "fold")]
    pub mod fold {
        use crate::fold::Fold;
        use crate::punctuated::{Pair, Punctuated};
        use proc_macro2::Span;

        pub trait FoldHelper {
            type Item;
            fn lift<F>(self, f: F) -> Self
            where
                F: FnMut(Self::Item) -> Self::Item;
        }

        impl<T> FoldHelper for Vec<T> {
            type Item = T;
            fn lift<F>(self, f: F) -> Self
            where
                F: FnMut(Self::Item) -> Self::Item,
            {
                self.into_iter().map(f).collect()
            }
        }

        impl<T, U> FoldHelper for Punctuated<T, U> {
            type Item = T;
            fn lift<F>(self, mut f: F) -> Self
            where
                F: FnMut(Self::Item) -> Self::Item,
            {
                self.into_pairs()
                    .map(Pair::into_tuple)
                    .map(|(t, u)| Pair::new(f(t), u))
                    .collect()
            }
        }

        pub fn tokens_helper<F: Fold + ?Sized, S: Spans>(folder: &mut F, spans: &S) -> S {
            spans.fold(folder)
        }

        pub trait Spans {
            fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self;
        }

        impl Spans for Span {
            fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self {
                folder.fold_span(*self)
            }
        }

        impl Spans for [Span; 1] {
            fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self {
                [folder.fold_span(self[0])]
            }
        }

        impl Spans for [Span; 2] {
            fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self {
                [folder.fold_span(self[0]), folder.fold_span(self[1])]
            }
        }

        impl Spans for [Span; 3] {
            fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self {
                [
                    folder.fold_span(self[0]),
                    folder.fold_span(self[1]),
                    folder.fold_span(self[2]),
                ]
            }
        }
    }

    #[cfg(feature = "visit")]
    pub mod visit {
        use crate::visit::Visit;
        use proc_macro2::Span;

        pub fn tokens_helper<'ast, V: Visit<'ast> + ?Sized, S: Spans>(visitor: &mut V, spans: &S) {
            spans.visit(visitor);
        }

        pub trait Spans {
            fn visit<'ast, V: Visit<'ast> + ?Sized>(&self, visitor: &mut V);
        }

        impl Spans for Span {
            fn visit<'ast, V: Visit<'ast> + ?Sized>(&self, visitor: &mut V) {
                visitor.visit_span(self);
            }
        }

        impl Spans for [Span; 1] {
            fn visit<'ast, V: Visit<'ast> + ?Sized>(&self, visitor: &mut V) {
                visitor.visit_span(&self[0]);
            }
        }

        impl Spans for [Span; 2] {
            fn visit<'ast, V: Visit<'ast> + ?Sized>(&self, visitor: &mut V) {
                visitor.visit_span(&self[0]);
                visitor.visit_span(&self[1]);
            }
        }

        impl Spans for [Span; 3] {
            fn visit<'ast, V: Visit<'ast> + ?Sized>(&self, visitor: &mut V) {
                visitor.visit_span(&self[0]);
                visitor.visit_span(&self[1]);
                visitor.visit_span(&self[2]);
            }
        }
    }

    #[cfg(feature = "visit-mut")]
    pub mod visit_mut {
        use crate::visit_mut::VisitMut;
        use proc_macro2::Span;

        pub fn tokens_helper<V: VisitMut + ?Sized, S: Spans>(visitor: &mut V, spans: &mut S) {
            spans.visit_mut(visitor);
        }

        pub trait Spans {
            fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V);
        }

        impl Spans for Span {
            fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V) {
                visitor.visit_span_mut(self);
            }
        }

        impl Spans for [Span; 1] {
            fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V) {
                visitor.visit_span_mut(&mut self[0]);
            }
        }

        impl Spans for [Span; 2] {
            fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V) {
                visitor.visit_span_mut(&mut self[0]);
                visitor.visit_span_mut(&mut self[1]);
            }
        }

        impl Spans for [Span; 3] {
            fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V) {
                visitor.visit_span_mut(&mut self[0]);
                visitor.visit_span_mut(&mut self[1]);
                visitor.visit_span_mut(&mut self[2]);
            }
        }
    }