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