]> git.proxmox.com Git - rustc.git/blob - src/vendor/syn-0.11.11/src/aster/where_predicate.rs
New upstream version 1.27.1+dfsg1
[rustc.git] / src / vendor / syn-0.11.11 / src / aster / where_predicate.rs
1 use {Ident, Lifetime, LifetimeDef, Ty, TyParamBound, WhereBoundPredicate, WherePredicate,
2 WhereRegionPredicate};
3 use aster::invoke::{Invoke, Identity};
4 use aster::lifetime::{IntoLifetime, IntoLifetimeDef, LifetimeDefBuilder};
5 use aster::path::IntoPath;
6 use aster::ty::TyBuilder;
7 use aster::ty_param::{TyParamBoundBuilder, PolyTraitRefBuilder, TraitTyParamBoundBuilder};
8
9 // ////////////////////////////////////////////////////////////////////////////
10
11 pub struct WherePredicateBuilder<F = Identity> {
12 callback: F,
13 }
14
15 impl WherePredicateBuilder {
16 pub fn new() -> Self {
17 WherePredicateBuilder::with_callback(Identity)
18 }
19 }
20
21 impl<F> WherePredicateBuilder<F>
22 where F: Invoke<WherePredicate>
23 {
24 pub fn with_callback(callback: F) -> Self {
25 WherePredicateBuilder { callback: callback }
26 }
27
28 pub fn bound(self) -> TyBuilder<Self> {
29 TyBuilder::with_callback(self)
30 }
31
32 pub fn lifetime<L>(self, lifetime: L) -> WhereRegionPredicateBuilder<F>
33 where L: IntoLifetime
34 {
35 WhereRegionPredicateBuilder {
36 callback: self.callback,
37 lifetime: lifetime.into_lifetime(),
38 bounds: Vec::new(),
39 }
40 }
41 }
42
43 impl<F> Invoke<Ty> for WherePredicateBuilder<F>
44 where F: Invoke<WherePredicate>
45 {
46 type Result = WhereBoundPredicateTyBuilder<F>;
47
48 fn invoke(self, ty: Ty) -> Self::Result {
49 WhereBoundPredicateTyBuilder {
50 callback: self.callback,
51 ty: ty,
52 bound_lifetimes: Vec::new(),
53 }
54 }
55 }
56
57 // ////////////////////////////////////////////////////////////////////////////
58
59 pub struct WhereBoundPredicateBuilder<F> {
60 callback: F,
61 }
62
63 impl<F> Invoke<Ty> for WhereBoundPredicateBuilder<F>
64 where F: Invoke<WherePredicate>
65 {
66 type Result = WhereBoundPredicateTyBuilder<F>;
67
68 fn invoke(self, ty: Ty) -> Self::Result {
69 WhereBoundPredicateTyBuilder {
70 callback: self.callback,
71 ty: ty,
72 bound_lifetimes: Vec::new(),
73 }
74 }
75 }
76
77 // ////////////////////////////////////////////////////////////////////////////
78
79 pub struct WhereBoundPredicateTyBuilder<F> {
80 callback: F,
81 ty: Ty,
82 bound_lifetimes: Vec<LifetimeDef>,
83 }
84
85 impl<F> WhereBoundPredicateTyBuilder<F>
86 where F: Invoke<WherePredicate>
87 {
88 pub fn with_for_lifetime<L>(mut self, lifetime: L) -> Self
89 where L: IntoLifetimeDef
90 {
91 self.bound_lifetimes.push(lifetime.into_lifetime_def());
92 self
93 }
94
95 pub fn for_lifetime<N>(self, name: N) -> LifetimeDefBuilder<Self>
96 where N: Into<Ident>
97 {
98 LifetimeDefBuilder::with_callback(name, self)
99 }
100
101 pub fn with_bound(self, bound: TyParamBound) -> WhereBoundPredicateTyBoundsBuilder<F> {
102 WhereBoundPredicateTyBoundsBuilder {
103 callback: self.callback,
104 ty: self.ty,
105 bound_lifetimes: self.bound_lifetimes,
106 bounds: vec![bound],
107 }
108 }
109
110 pub fn bound(self) -> TyParamBoundBuilder<WhereBoundPredicateTyBoundsBuilder<F>> {
111 let builder = WhereBoundPredicateTyBoundsBuilder {
112 callback: self.callback,
113 ty: self.ty,
114 bound_lifetimes: self.bound_lifetimes,
115 bounds: vec![],
116 };
117 TyParamBoundBuilder::with_callback(builder)
118 }
119
120 pub fn trait_<P>
121 (self,
122 path: P)
123 -> PolyTraitRefBuilder<TraitTyParamBoundBuilder<WhereBoundPredicateTyBoundsBuilder<F>>>
124 where P: IntoPath
125 {
126 self.bound().trait_(path)
127 }
128
129 pub fn lifetime<L>(self, lifetime: L) -> WhereBoundPredicateTyBoundsBuilder<F>
130 where L: IntoLifetime
131 {
132 self.bound().lifetime(lifetime)
133 }
134 }
135
136 impl<F> Invoke<LifetimeDef> for WhereBoundPredicateTyBuilder<F>
137 where F: Invoke<WherePredicate>
138 {
139 type Result = Self;
140
141 fn invoke(self, lifetime: LifetimeDef) -> Self {
142 self.with_for_lifetime(lifetime)
143 }
144 }
145
146 impl<F> Invoke<TyParamBound> for WhereBoundPredicateTyBuilder<F>
147 where F: Invoke<WherePredicate>
148 {
149 type Result = WhereBoundPredicateTyBoundsBuilder<F>;
150
151 fn invoke(self, bound: TyParamBound) -> Self::Result {
152 self.with_bound(bound)
153 }
154 }
155
156 // ////////////////////////////////////////////////////////////////////////////
157
158 pub struct WhereBoundPredicateTyBoundsBuilder<F> {
159 callback: F,
160 ty: Ty,
161 bound_lifetimes: Vec<LifetimeDef>,
162 bounds: Vec<TyParamBound>,
163 }
164
165 impl<F> WhereBoundPredicateTyBoundsBuilder<F>
166 where F: Invoke<WherePredicate>
167 {
168 pub fn with_for_lifetime<L>(mut self, lifetime: L) -> Self
169 where L: IntoLifetimeDef
170 {
171 self.bound_lifetimes.push(lifetime.into_lifetime_def());
172 self
173 }
174
175 pub fn for_lifetime<N>(self, name: N) -> LifetimeDefBuilder<Self>
176 where N: Into<Ident>
177 {
178 LifetimeDefBuilder::with_callback(name, self)
179 }
180
181 pub fn with_bound(mut self, bound: TyParamBound) -> Self {
182 self.bounds.push(bound);
183 self
184 }
185
186 pub fn bound(self) -> TyParamBoundBuilder<Self> {
187 TyParamBoundBuilder::with_callback(self)
188 }
189
190 pub fn trait_<P>(self, path: P) -> PolyTraitRefBuilder<TraitTyParamBoundBuilder<Self>>
191 where P: IntoPath
192 {
193 self.bound().trait_(path)
194 }
195
196 pub fn lifetime<L>(self, lifetime: L) -> Self
197 where L: IntoLifetime
198 {
199 self.bound().lifetime(lifetime)
200 }
201
202 pub fn build(self) -> F::Result {
203 let predicate = WhereBoundPredicate {
204 bound_lifetimes: self.bound_lifetimes,
205 bounded_ty: self.ty,
206 bounds: self.bounds,
207 };
208
209 self.callback.invoke(WherePredicate::BoundPredicate(predicate))
210 }
211 }
212
213 impl<F> Invoke<LifetimeDef> for WhereBoundPredicateTyBoundsBuilder<F>
214 where F: Invoke<WherePredicate>
215 {
216 type Result = Self;
217
218 fn invoke(self, lifetime: LifetimeDef) -> Self {
219 self.with_for_lifetime(lifetime)
220 }
221 }
222
223 impl<F> Invoke<TyParamBound> for WhereBoundPredicateTyBoundsBuilder<F>
224 where F: Invoke<WherePredicate>
225 {
226 type Result = Self;
227
228 fn invoke(self, bound: TyParamBound) -> Self {
229 self.with_bound(bound)
230 }
231 }
232
233 // ////////////////////////////////////////////////////////////////////////////
234
235 pub struct WhereRegionPredicateBuilder<F> {
236 callback: F,
237 lifetime: Lifetime,
238 bounds: Vec<Lifetime>,
239 }
240
241 impl<F> WhereRegionPredicateBuilder<F>
242 where F: Invoke<WherePredicate>
243 {
244 pub fn bound<L>(mut self, lifetime: L) -> Self
245 where L: IntoLifetime
246 {
247 self.bounds.push(lifetime.into_lifetime());
248 self
249 }
250
251 pub fn build(self) -> F::Result {
252 let predicate = WhereRegionPredicate {
253 lifetime: self.lifetime,
254 bounds: self.bounds,
255 };
256
257 self.callback.invoke(WherePredicate::RegionPredicate(predicate))
258 }
259 }