]>
git.proxmox.com Git - rustc.git/blob - src/vendor/syn-0.12.15/src/gen_helper.rs
1 // Copyright 2018 Syn Developers
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
9 #[cfg(feature = "fold")]
11 use punctuated
::{Pair, Punctuated}
;
13 use proc_macro2
::Span
;
15 pub trait FoldHelper
{
17 fn lift
<F
>(self, f
: F
) -> Self
19 F
: FnMut(Self::Item
) -> Self::Item
;
22 impl<T
> FoldHelper
for Vec
<T
> {
24 fn lift
<F
>(self, f
: F
) -> Self
26 F
: FnMut(Self::Item
) -> Self::Item
,
28 self.into_iter().map(f
).collect()
32 impl<T
, U
> FoldHelper
for Punctuated
<T
, U
> {
34 fn lift
<F
>(self, mut f
: F
) -> Self
36 F
: FnMut(Self::Item
) -> Self::Item
,
39 .map(Pair
::into_tuple
)
40 .map(|(t
, u
)| Pair
::new(f(t
), u
))
45 pub fn tokens_helper
<F
: Fold
+ ?Sized
, S
: Spans
>(folder
: &mut F
, spans
: &S
) -> S
{
50 fn fold
<F
: Fold
+ ?Sized
>(&self, folder
: &mut F
) -> Self;
54 fn fold
<F
: Fold
+ ?Sized
>(&self, folder
: &mut F
) -> Self {
55 folder
.fold_span(*self)
59 impl Spans
for [Span
; 1] {
60 fn fold
<F
: Fold
+ ?Sized
>(&self, folder
: &mut F
) -> Self {
61 [folder
.fold_span(self[0])]
65 impl Spans
for [Span
; 2] {
66 fn fold
<F
: Fold
+ ?Sized
>(&self, folder
: &mut F
) -> Self {
67 [folder
.fold_span(self[0]), folder
.fold_span(self[1])]
71 impl Spans
for [Span
; 3] {
72 fn fold
<F
: Fold
+ ?Sized
>(&self, folder
: &mut F
) -> Self {
74 folder
.fold_span(self[0]),
75 folder
.fold_span(self[1]),
76 folder
.fold_span(self[2]),
82 #[cfg(feature = "visit")]
84 use proc_macro2
::Span
;
87 pub fn tokens_helper
<'ast
, V
: Visit
<'ast
> + ?Sized
, S
: Spans
>(
95 fn visit
<'ast
, V
: Visit
<'ast
> + ?Sized
>(&'ast
self, visitor
: &mut V
);
99 fn visit
<'ast
, V
: Visit
<'ast
> + ?Sized
>(&'ast
self, visitor
: &mut V
) {
100 visitor
.visit_span(self);
104 impl Spans
for [Span
; 1] {
105 fn visit
<'ast
, V
: Visit
<'ast
> + ?Sized
>(&'ast
self, visitor
: &mut V
) {
106 visitor
.visit_span(&self[0]);
110 impl Spans
for [Span
; 2] {
111 fn visit
<'ast
, V
: Visit
<'ast
> + ?Sized
>(&'ast
self, visitor
: &mut V
) {
112 visitor
.visit_span(&self[0]);
113 visitor
.visit_span(&self[1]);
117 impl Spans
for [Span
; 3] {
118 fn visit
<'ast
, V
: Visit
<'ast
> + ?Sized
>(&'ast
self, visitor
: &mut V
) {
119 visitor
.visit_span(&self[0]);
120 visitor
.visit_span(&self[1]);
121 visitor
.visit_span(&self[2]);
126 #[cfg(feature = "visit-mut")]
128 use proc_macro2
::Span
;
129 use visit_mut
::VisitMut
;
131 pub fn tokens_helper
<V
: VisitMut
+ ?Sized
, S
: Spans
>(visitor
: &mut V
, spans
: &mut S
) {
132 spans
.visit_mut(visitor
);
136 fn visit_mut
<V
: VisitMut
+ ?Sized
>(&mut self, visitor
: &mut V
);
139 impl Spans
for Span
{
140 fn visit_mut
<V
: VisitMut
+ ?Sized
>(&mut self, visitor
: &mut V
) {
141 visitor
.visit_span_mut(self);
145 impl Spans
for [Span
; 1] {
146 fn visit_mut
<V
: VisitMut
+ ?Sized
>(&mut self, visitor
: &mut V
) {
147 visitor
.visit_span_mut(&mut self[0]);
151 impl Spans
for [Span
; 2] {
152 fn visit_mut
<V
: VisitMut
+ ?Sized
>(&mut self, visitor
: &mut V
) {
153 visitor
.visit_span_mut(&mut self[0]);
154 visitor
.visit_span_mut(&mut self[1]);
158 impl Spans
for [Span
; 3] {
159 fn visit_mut
<V
: VisitMut
+ ?Sized
>(&mut self, visitor
: &mut V
) {
160 visitor
.visit_span_mut(&mut self[0]);
161 visitor
.visit_span_mut(&mut self[1]);
162 visitor
.visit_span_mut(&mut self[2]);