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