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