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