David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 3 | /// Represents lifetimes and type parameters attached to a declaration |
| 4 | /// of a function, enum, trait, etc. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 5 | #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 6 | pub struct Generics { |
| 7 | pub lifetimes: Vec<LifetimeDef>, |
| 8 | pub ty_params: Vec<TyParam>, |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 9 | pub where_clause: WhereClause, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 10 | } |
| 11 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 12 | impl Generics { |
| 13 | /// Split a type's generics into the pieces required for impl'ing a trait |
| 14 | /// for that type. |
| 15 | /// |
| 16 | /// ``` |
| 17 | /// # extern crate syn; |
| 18 | /// # #[macro_use] |
| 19 | /// # extern crate quote; |
| 20 | /// # fn main() { |
| 21 | /// # let generics: syn::Generics = Default::default(); |
| 22 | /// # let name = syn::Ident::new("MyType"); |
| 23 | /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 24 | /// quote! { |
| 25 | /// impl #impl_generics MyTrait for #name #ty_generics #where_clause { |
| 26 | /// // ... |
| 27 | /// } |
| 28 | /// } |
| 29 | /// # ; |
| 30 | /// # } |
| 31 | /// ``` |
| 32 | pub fn split_for_impl(&self) -> (Generics, Generics, WhereClause) { |
| 33 | // Remove where clause and type parameter defaults. |
| 34 | let impl_generics = Generics { |
| 35 | lifetimes: self.lifetimes.clone(), |
David Tolnay | 2e73736 | 2016-10-05 23:44:15 -0700 | [diff] [blame] | 36 | ty_params: self.ty_params |
| 37 | .iter() |
| 38 | .map(|ty_param| { |
| 39 | TyParam { |
| 40 | ident: ty_param.ident.clone(), |
| 41 | bounds: ty_param.bounds.clone(), |
| 42 | default: None, |
| 43 | } |
| 44 | }) |
| 45 | .collect(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 46 | where_clause: WhereClause::none(), |
| 47 | }; |
| 48 | |
| 49 | // Remove where clause, type parameter defaults, and bounds. |
| 50 | let ty_generics = Generics { |
David Tolnay | 2e73736 | 2016-10-05 23:44:15 -0700 | [diff] [blame] | 51 | lifetimes: self.lifetimes |
| 52 | .iter() |
| 53 | .map(|lifetime_def| { |
| 54 | LifetimeDef { |
| 55 | lifetime: lifetime_def.lifetime.clone(), |
| 56 | bounds: Vec::new(), |
| 57 | } |
| 58 | }) |
| 59 | .collect(), |
| 60 | ty_params: self.ty_params |
| 61 | .iter() |
| 62 | .map(|ty_param| { |
| 63 | TyParam { |
| 64 | ident: ty_param.ident.clone(), |
| 65 | bounds: Vec::new(), |
| 66 | default: None, |
| 67 | } |
| 68 | }) |
| 69 | .collect(), |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 70 | where_clause: WhereClause::none(), |
| 71 | }; |
| 72 | |
| 73 | let where_clause = self.where_clause.clone(); |
| 74 | |
| 75 | (impl_generics, ty_generics, where_clause) |
| 76 | } |
| 77 | } |
| 78 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 79 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 80 | pub struct Lifetime { |
| 81 | pub ident: Ident, |
| 82 | } |
| 83 | |
David Tolnay | 01405f0 | 2016-10-02 09:05:02 -0700 | [diff] [blame] | 84 | impl Lifetime { |
| 85 | pub fn new<T: Into<Ident>>(t: T) -> Self { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 86 | Lifetime { ident: Ident::new(t) } |
David Tolnay | 01405f0 | 2016-10-02 09:05:02 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 90 | /// A lifetime definition, e.g. `'a: 'b+'c+'d` |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 91 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 92 | pub struct LifetimeDef { |
| 93 | pub lifetime: Lifetime, |
| 94 | pub bounds: Vec<Lifetime>, |
| 95 | } |
| 96 | |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 97 | impl LifetimeDef { |
| 98 | pub fn new<T: Into<Ident>>(t: T) -> Self { |
| 99 | LifetimeDef { |
| 100 | lifetime: Lifetime::new(t), |
| 101 | bounds: Vec::new(), |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 106 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 107 | pub struct TyParam { |
| 108 | pub ident: Ident, |
| 109 | pub bounds: Vec<TyParamBound>, |
| 110 | pub default: Option<Ty>, |
| 111 | } |
| 112 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 113 | /// The AST represents all type param bounds as types. |
David Tolnay | aed77b0 | 2016-09-23 20:50:31 -0700 | [diff] [blame] | 114 | /// `typeck::collect::compute_bounds` matches these against |
| 115 | /// the "special" built-in traits (see `middle::lang_items`) and |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 116 | /// detects Copy, Send and Sync. |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 117 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 118 | pub enum TyParamBound { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 119 | Trait(PolyTraitRef, TraitBoundModifier), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 120 | Region(Lifetime), |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 121 | } |
| 122 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 123 | /// A modifier on a bound, currently this is only used for `?Sized`, where the |
| 124 | /// modifier is `Maybe`. Negative bounds should also be handled here. |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 125 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 126 | pub enum TraitBoundModifier { |
| 127 | None, |
| 128 | Maybe, |
| 129 | } |
| 130 | |
David Tolnay | 771ecf4 | 2016-09-23 19:26:37 -0700 | [diff] [blame] | 131 | /// A `where` clause in a definition |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 132 | #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 133 | pub struct WhereClause { |
| 134 | pub predicates: Vec<WherePredicate>, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 135 | } |
| 136 | |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 137 | impl WhereClause { |
| 138 | pub fn none() -> Self { |
David Tolnay | 2e73736 | 2016-10-05 23:44:15 -0700 | [diff] [blame] | 139 | WhereClause { predicates: Vec::new() } |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 143 | /// A single predicate in a `where` clause |
| 144 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 145 | pub enum WherePredicate { |
| 146 | /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c` |
| 147 | BoundPredicate(WhereBoundPredicate), |
| 148 | /// A lifetime predicate, e.g. `'a: 'b+'c` |
| 149 | RegionPredicate(WhereRegionPredicate), |
| 150 | } |
| 151 | |
| 152 | /// A type bound. |
| 153 | /// |
| 154 | /// E.g. `for<'c> Foo: Send+Clone+'c` |
| 155 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 156 | pub struct WhereBoundPredicate { |
| 157 | /// Any lifetimes from a `for` binding |
| 158 | pub bound_lifetimes: Vec<LifetimeDef>, |
| 159 | /// The type being bounded |
| 160 | pub bounded_ty: Ty, |
| 161 | /// Trait and lifetime bounds (`Clone+Send+'static`) |
| 162 | pub bounds: Vec<TyParamBound>, |
| 163 | } |
| 164 | |
| 165 | /// A lifetime predicate. |
| 166 | /// |
| 167 | /// E.g. `'a: 'b+'c` |
| 168 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 169 | pub struct WhereRegionPredicate { |
| 170 | pub lifetime: Lifetime, |
| 171 | pub bounds: Vec<Lifetime>, |
| 172 | } |
| 173 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 174 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 175 | pub mod parsing { |
| 176 | use super::*; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 177 | use ident::parsing::ident; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 178 | use ty::parsing::{ty, poly_trait_ref}; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 179 | |
David Tolnay | 3cf5298 | 2016-10-01 17:11:37 -0700 | [diff] [blame] | 180 | named!(pub generics -> Generics, map!( |
| 181 | alt!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 182 | do_parse!( |
| 183 | punct!("<") >> |
| 184 | lifetimes: separated_list!(punct!(","), lifetime_def) >> |
| 185 | ty_params: opt_vec!(preceded!( |
| 186 | cond!(!lifetimes.is_empty(), punct!(",")), |
| 187 | separated_nonempty_list!(punct!(","), ty_param) |
| 188 | )) >> |
| 189 | punct!(">") >> |
| 190 | (lifetimes, ty_params) |
| 191 | ) |
| 192 | | |
| 193 | epsilon!() => { |_| (Vec::new(), Vec::new()) } |
David Tolnay | 3cf5298 | 2016-10-01 17:11:37 -0700 | [diff] [blame] | 194 | ), |
| 195 | |(lifetimes, ty_params)| Generics { |
| 196 | lifetimes: lifetimes, |
| 197 | ty_params: ty_params, |
| 198 | where_clause: Default::default(), |
| 199 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 200 | )); |
| 201 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 202 | named!(pub lifetime -> Lifetime, preceded!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 203 | punct!("'"), |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 204 | map!(ident, |id| Lifetime { |
| 205 | ident: format!("'{}", id).into(), |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 206 | }) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 207 | )); |
| 208 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 209 | named!(pub lifetime_def -> LifetimeDef, do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 210 | life: lifetime >> |
| 211 | bounds: opt_vec!(preceded!( |
| 212 | punct!(":"), |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 213 | separated_nonempty_list!(punct!("+"), lifetime) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 214 | )) >> |
| 215 | (LifetimeDef { |
| 216 | lifetime: life, |
| 217 | bounds: bounds, |
| 218 | }) |
| 219 | )); |
| 220 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 221 | named!(pub bound_lifetimes -> Vec<LifetimeDef>, opt_vec!(do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 222 | keyword!("for") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 223 | punct!("<") >> |
| 224 | lifetimes: separated_list!(punct!(","), lifetime_def) >> |
| 225 | punct!(">") >> |
| 226 | (lifetimes) |
| 227 | ))); |
| 228 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 229 | named!(ty_param -> TyParam, do_parse!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 230 | id: ident >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 231 | bounds: opt_vec!(preceded!( |
| 232 | punct!(":"), |
| 233 | separated_nonempty_list!(punct!("+"), ty_param_bound) |
| 234 | )) >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 235 | default: option!(preceded!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 236 | punct!("="), |
| 237 | ty |
| 238 | )) >> |
| 239 | (TyParam { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 240 | ident: id, |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 241 | bounds: bounds, |
| 242 | default: default, |
| 243 | }) |
| 244 | )); |
| 245 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 246 | named!(pub ty_param_bound -> TyParamBound, alt!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 247 | preceded!(punct!("?"), poly_trait_ref) => { |
| 248 | |poly| TyParamBound::Trait(poly, TraitBoundModifier::Maybe) |
| 249 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 250 | | |
| 251 | lifetime => { TyParamBound::Region } |
| 252 | | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 253 | poly_trait_ref => { |
| 254 | |poly| TyParamBound::Trait(poly, TraitBoundModifier::None) |
| 255 | } |
| 256 | )); |
| 257 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 258 | named!(pub where_clause -> WhereClause, alt!( |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 259 | do_parse!( |
David Tolnay | 10413f0 | 2016-09-30 09:12:02 -0700 | [diff] [blame] | 260 | keyword!("where") >> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 261 | predicates: separated_nonempty_list!(punct!(","), where_predicate) >> |
| 262 | option!(punct!(",")) >> |
| 263 | (WhereClause { predicates: predicates }) |
| 264 | ) |
| 265 | | |
| 266 | epsilon!() => { |_| Default::default() } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 267 | )); |
| 268 | |
David Tolnay | b5a7b14 | 2016-09-13 22:46:39 -0700 | [diff] [blame] | 269 | named!(where_predicate -> WherePredicate, alt!( |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 270 | do_parse!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 271 | ident: lifetime >> |
| 272 | punct!(":") >> |
| 273 | bounds: separated_nonempty_list!(punct!("+"), lifetime) >> |
| 274 | (WherePredicate::RegionPredicate(WhereRegionPredicate { |
| 275 | lifetime: ident, |
| 276 | bounds: bounds, |
| 277 | })) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 278 | ) |
| 279 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 280 | do_parse!( |
| 281 | bound_lifetimes: bound_lifetimes >> |
| 282 | bounded_ty: ty >> |
| 283 | punct!(":") >> |
| 284 | bounds: separated_nonempty_list!(punct!("+"), ty_param_bound) >> |
| 285 | (WherePredicate::BoundPredicate(WhereBoundPredicate { |
| 286 | bound_lifetimes: bound_lifetimes, |
| 287 | bounded_ty: bounded_ty, |
| 288 | bounds: bounds, |
| 289 | })) |
| 290 | ) |
| 291 | )); |
| 292 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 293 | |
| 294 | #[cfg(feature = "printing")] |
| 295 | mod printing { |
| 296 | use super::*; |
| 297 | use quote::{Tokens, ToTokens}; |
| 298 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 299 | impl ToTokens for Generics { |
| 300 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 301 | let has_lifetimes = !self.lifetimes.is_empty(); |
| 302 | let has_ty_params = !self.ty_params.is_empty(); |
| 303 | if has_lifetimes || has_ty_params { |
| 304 | tokens.append("<"); |
| 305 | tokens.append_separated(&self.lifetimes, ","); |
| 306 | if has_lifetimes && has_ty_params { |
| 307 | tokens.append(","); |
| 308 | } |
| 309 | tokens.append_separated(&self.ty_params, ","); |
| 310 | tokens.append(">"); |
| 311 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 315 | impl ToTokens for Lifetime { |
| 316 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 317 | self.ident.to_tokens(tokens); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | impl ToTokens for LifetimeDef { |
| 322 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 323 | self.lifetime.to_tokens(tokens); |
| 324 | if !self.bounds.is_empty() { |
| 325 | tokens.append(":"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 326 | tokens.append_separated(&self.bounds, "+"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 331 | impl ToTokens for TyParam { |
| 332 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 333 | self.ident.to_tokens(tokens); |
| 334 | if !self.bounds.is_empty() { |
| 335 | tokens.append(":"); |
David Tolnay | 686b59b | 2016-09-04 14:39:50 -0700 | [diff] [blame] | 336 | tokens.append_separated(&self.bounds, "+"); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 337 | } |
| 338 | if let Some(ref default) = self.default { |
| 339 | tokens.append("="); |
| 340 | default.to_tokens(tokens); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 345 | impl ToTokens for TyParamBound { |
| 346 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 347 | match *self { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 348 | TyParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens), |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 349 | TyParamBound::Trait(ref trait_ref, modifier) => { |
| 350 | match modifier { |
| 351 | TraitBoundModifier::None => {} |
| 352 | TraitBoundModifier::Maybe => tokens.append("?"), |
| 353 | } |
| 354 | trait_ref.to_tokens(tokens); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | impl ToTokens for WhereClause { |
| 361 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 362 | if !self.predicates.is_empty() { |
| 363 | tokens.append("where"); |
| 364 | tokens.append_separated(&self.predicates, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 368 | |
| 369 | impl ToTokens for WherePredicate { |
| 370 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 371 | match *self { |
| 372 | WherePredicate::BoundPredicate(ref predicate) => { |
| 373 | predicate.to_tokens(tokens); |
| 374 | } |
| 375 | WherePredicate::RegionPredicate(ref predicate) => { |
| 376 | predicate.to_tokens(tokens); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | impl ToTokens for WhereBoundPredicate { |
| 383 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 384 | if !self.bound_lifetimes.is_empty() { |
| 385 | tokens.append("for"); |
| 386 | tokens.append("<"); |
| 387 | tokens.append_separated(&self.bound_lifetimes, ","); |
| 388 | tokens.append(">"); |
| 389 | } |
| 390 | self.bounded_ty.to_tokens(tokens); |
| 391 | if !self.bounds.is_empty() { |
| 392 | tokens.append(":"); |
| 393 | tokens.append_separated(&self.bounds, "+"); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | impl ToTokens for WhereRegionPredicate { |
| 399 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 400 | self.lifetime.to_tokens(tokens); |
| 401 | if !self.bounds.is_empty() { |
| 402 | tokens.append(":"); |
| 403 | tokens.append_separated(&self.bounds, "+"); |
| 404 | } |
| 405 | } |
| 406 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 407 | } |