blob: b15b42be18cb6a73b252eedec24c97246fbe9608 [file] [log] [blame]
David Tolnayf60f4262017-12-28 19:17:58 -05001#[cfg(feature = "fold")]
2pub mod fold {
David Tolnay4b4c4b62018-01-06 13:48:05 -08003 use fold::Fold;
David Tolnaycc0f0372017-12-28 19:11:04 -05004 use proc_macro2::Span;
David Tolnay94d2b792018-04-29 12:26:10 -07005 use punctuated::{Pair, Punctuated};
David Tolnayf60f4262017-12-28 19:17:58 -05006
7 pub trait FoldHelper {
8 type Item;
David Tolnay61037c62018-01-05 16:21:03 -08009 fn lift<F>(self, f: F) -> Self
10 where
11 F: FnMut(Self::Item) -> Self::Item;
David Tolnayf60f4262017-12-28 19:17:58 -050012 }
13
14 impl<T> FoldHelper for Vec<T> {
15 type Item = T;
David Tolnay61037c62018-01-05 16:21:03 -080016 fn lift<F>(self, f: F) -> Self
17 where
18 F: FnMut(Self::Item) -> Self::Item,
19 {
David Tolnayf60f4262017-12-28 19:17:58 -050020 self.into_iter().map(f).collect()
21 }
22 }
23
David Tolnayf2cfd722017-12-31 18:02:51 -050024 impl<T, U> FoldHelper for Punctuated<T, U> {
David Tolnayf60f4262017-12-28 19:17:58 -050025 type Item = T;
David Tolnay61037c62018-01-05 16:21:03 -080026 fn lift<F>(self, mut f: F) -> Self
27 where
28 F: FnMut(Self::Item) -> Self::Item,
29 {
David Tolnay56080682018-01-06 14:01:52 -080030 self.into_pairs()
31 .map(Pair::into_tuple)
32 .map(|(t, u)| Pair::new(f(t), u))
David Tolnay660fd1f2017-12-31 01:52:57 -050033 .collect()
David Tolnayf60f4262017-12-28 19:17:58 -050034 }
35 }
David Tolnaycc0f0372017-12-28 19:11:04 -050036
David Tolnay4b4c4b62018-01-06 13:48:05 -080037 pub fn tokens_helper<F: Fold + ?Sized, S: Spans>(folder: &mut F, spans: &S) -> S {
David Tolnaycc0f0372017-12-28 19:11:04 -050038 spans.fold(folder)
39 }
40
41 pub trait Spans {
David Tolnay4b4c4b62018-01-06 13:48:05 -080042 fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self;
David Tolnaycc0f0372017-12-28 19:11:04 -050043 }
44
45 impl Spans for Span {
David Tolnay4b4c4b62018-01-06 13:48:05 -080046 fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self {
David Tolnaycc0f0372017-12-28 19:11:04 -050047 folder.fold_span(*self)
48 }
49 }
50
51 impl Spans for [Span; 1] {
David Tolnay4b4c4b62018-01-06 13:48:05 -080052 fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self {
David Tolnaycc0f0372017-12-28 19:11:04 -050053 [folder.fold_span(self[0])]
54 }
55 }
56
57 impl Spans for [Span; 2] {
David Tolnay4b4c4b62018-01-06 13:48:05 -080058 fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self {
David Tolnaycc0f0372017-12-28 19:11:04 -050059 [folder.fold_span(self[0]), folder.fold_span(self[1])]
60 }
61 }
62
63 impl Spans for [Span; 3] {
David Tolnay4b4c4b62018-01-06 13:48:05 -080064 fn fold<F: Fold + ?Sized>(&self, folder: &mut F) -> Self {
David Tolnay61037c62018-01-05 16:21:03 -080065 [
66 folder.fold_span(self[0]),
67 folder.fold_span(self[1]),
68 folder.fold_span(self[2]),
69 ]
David Tolnaycc0f0372017-12-28 19:11:04 -050070 }
71 }
72}
73
74#[cfg(feature = "visit")]
75pub mod visit {
76 use proc_macro2::Span;
David Tolnay4b4c4b62018-01-06 13:48:05 -080077 use visit::Visit;
David Tolnaycc0f0372017-12-28 19:11:04 -050078
David Tolnay94d2b792018-04-29 12:26:10 -070079 pub fn tokens_helper<'ast, V: Visit<'ast> + ?Sized, S: Spans>(visitor: &mut V, spans: &'ast S) {
David Tolnaycc0f0372017-12-28 19:11:04 -050080 spans.visit(visitor);
81 }
82
83 pub trait Spans {
David Tolnay4b4c4b62018-01-06 13:48:05 -080084 fn visit<'ast, V: Visit<'ast> + ?Sized>(&'ast self, visitor: &mut V);
David Tolnaycc0f0372017-12-28 19:11:04 -050085 }
86
87 impl Spans for Span {
David Tolnay4b4c4b62018-01-06 13:48:05 -080088 fn visit<'ast, V: Visit<'ast> + ?Sized>(&'ast self, visitor: &mut V) {
David Tolnaycc0f0372017-12-28 19:11:04 -050089 visitor.visit_span(self);
90 }
91 }
92
93 impl Spans for [Span; 1] {
David Tolnay4b4c4b62018-01-06 13:48:05 -080094 fn visit<'ast, V: Visit<'ast> + ?Sized>(&'ast self, visitor: &mut V) {
David Tolnaycc0f0372017-12-28 19:11:04 -050095 visitor.visit_span(&self[0]);
96 }
97 }
98
99 impl Spans for [Span; 2] {
David Tolnay4b4c4b62018-01-06 13:48:05 -0800100 fn visit<'ast, V: Visit<'ast> + ?Sized>(&'ast self, visitor: &mut V) {
David Tolnaycc0f0372017-12-28 19:11:04 -0500101 visitor.visit_span(&self[0]);
102 visitor.visit_span(&self[1]);
103 }
104 }
105
106 impl Spans for [Span; 3] {
David Tolnay4b4c4b62018-01-06 13:48:05 -0800107 fn visit<'ast, V: Visit<'ast> + ?Sized>(&'ast self, visitor: &mut V) {
David Tolnaycc0f0372017-12-28 19:11:04 -0500108 visitor.visit_span(&self[0]);
109 visitor.visit_span(&self[1]);
110 visitor.visit_span(&self[2]);
111 }
112 }
113}
114
David Tolnay9df02c42018-01-06 13:52:48 -0800115#[cfg(feature = "visit-mut")]
David Tolnaycc0f0372017-12-28 19:11:04 -0500116pub mod visit_mut {
117 use proc_macro2::Span;
David Tolnay4b4c4b62018-01-06 13:48:05 -0800118 use visit_mut::VisitMut;
David Tolnaycc0f0372017-12-28 19:11:04 -0500119
David Tolnay4b4c4b62018-01-06 13:48:05 -0800120 pub fn tokens_helper<V: VisitMut + ?Sized, S: Spans>(visitor: &mut V, spans: &mut S) {
David Tolnaycc0f0372017-12-28 19:11:04 -0500121 spans.visit_mut(visitor);
122 }
123
124 pub trait Spans {
David Tolnay4b4c4b62018-01-06 13:48:05 -0800125 fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V);
David Tolnaycc0f0372017-12-28 19:11:04 -0500126 }
127
128 impl Spans for Span {
David Tolnay4b4c4b62018-01-06 13:48:05 -0800129 fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V) {
David Tolnaycc0f0372017-12-28 19:11:04 -0500130 visitor.visit_span_mut(self);
131 }
132 }
133
134 impl Spans for [Span; 1] {
David Tolnay4b4c4b62018-01-06 13:48:05 -0800135 fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V) {
David Tolnaycc0f0372017-12-28 19:11:04 -0500136 visitor.visit_span_mut(&mut self[0]);
137 }
138 }
139
140 impl Spans for [Span; 2] {
David Tolnay4b4c4b62018-01-06 13:48:05 -0800141 fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V) {
David Tolnaycc0f0372017-12-28 19:11:04 -0500142 visitor.visit_span_mut(&mut self[0]);
143 visitor.visit_span_mut(&mut self[1]);
144 }
145 }
146
147 impl Spans for [Span; 3] {
David Tolnay4b4c4b62018-01-06 13:48:05 -0800148 fn visit_mut<V: VisitMut + ?Sized>(&mut self, visitor: &mut V) {
David Tolnaycc0f0372017-12-28 19:11:04 -0500149 visitor.visit_span_mut(&mut self[0]);
150 visitor.visit_span_mut(&mut self[1]);
151 visitor.visit_span_mut(&mut self[2]);
152 }
153 }
David Tolnayf60f4262017-12-28 19:17:58 -0500154}