David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 2 | use punctuated::{Iter, IterMut, Punctuated}; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 4 | ast_struct! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 5 | /// Lifetimes and type parameters attached to a declaration of a function, |
| 6 | /// enum, trait, etc. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 7 | /// |
| 8 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 9 | /// feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 10 | #[derive(Default)] |
| 11 | pub struct Generics { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 12 | pub lt_token: Option<Token![<]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 13 | pub params: Punctuated<GenericParam, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 14 | pub gt_token: Option<Token![>]>, |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 15 | pub where_clause: Option<WhereClause>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 16 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 17 | } |
| 18 | |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 19 | ast_enum_of_structs! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 20 | /// A generic type parameter, lifetime, or const generic: `T: Into<String>`, |
| 21 | /// `'a: 'b`, `const LEN: usize`. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 22 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 23 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 24 | /// feature.* |
| 25 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 26 | /// # Syntax tree enum |
| 27 | /// |
| 28 | /// This type is a [syntax tree enum]. |
| 29 | /// |
| 30 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
David Tolnay | b8dec88 | 2019-07-20 09:46:14 -0700 | [diff] [blame] | 31 | // |
| 32 | // TODO: change syntax-tree-enum link to an intra rustdoc link, currently |
| 33 | // blocked on https://github.com/rust-lang/rust/issues/62833 |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 34 | pub enum GenericParam { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 35 | /// A generic type parameter: `T: Into<String>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 36 | /// |
| 37 | /// *This type is available if Syn is built with the `"derive"` or |
| 38 | /// `"full"` feature.* |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 39 | pub Type(TypeParam { |
| 40 | pub attrs: Vec<Attribute>, |
| 41 | pub ident: Ident, |
| 42 | pub colon_token: Option<Token![:]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 43 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 44 | pub eq_token: Option<Token![=]>, |
| 45 | pub default: Option<Type>, |
| 46 | }), |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 47 | |
| 48 | /// A lifetime definition: `'a: 'b + 'c + 'd`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 49 | /// |
| 50 | /// *This type is available if Syn is built with the `"derive"` or |
| 51 | /// `"full"` feature.* |
David Tolnay | 517f369 | 2018-01-01 20:17:23 -0800 | [diff] [blame] | 52 | pub Lifetime(LifetimeDef { |
| 53 | pub attrs: Vec<Attribute>, |
| 54 | pub lifetime: Lifetime, |
| 55 | pub colon_token: Option<Token![:]>, |
| 56 | pub bounds: Punctuated<Lifetime, Token![+]>, |
| 57 | }), |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 58 | |
| 59 | /// A const generic parameter: `const LENGTH: usize`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 60 | /// |
| 61 | /// *This type is available if Syn is built with the `"derive"` or |
| 62 | /// `"full"` feature.* |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 63 | pub Const(ConstParam { |
| 64 | pub attrs: Vec<Attribute>, |
| 65 | pub const_token: Token![const], |
| 66 | pub ident: Ident, |
| 67 | pub colon_token: Token![:], |
| 68 | pub ty: Type, |
| 69 | pub eq_token: Option<Token![=]>, |
| 70 | pub default: Option<Expr>, |
| 71 | }), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 75 | impl Generics { |
David Tolnay | ef5199b | 2018-01-17 10:56:03 -0800 | [diff] [blame] | 76 | /// Returns an |
| 77 | /// <code |
| 78 | /// style="padding-right:0;">Iterator<Item = &</code><a |
| 79 | /// href="struct.TypeParam.html"><code |
| 80 | /// style="padding-left:0;padding-right:0;">TypeParam</code></a><code |
| 81 | /// style="padding-left:0;">></code> |
| 82 | /// over the type parameters in `self.params`. |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 83 | pub fn type_params(&self) -> TypeParams { |
| 84 | TypeParams(self.params.iter()) |
| 85 | } |
| 86 | |
David Tolnay | ef5199b | 2018-01-17 10:56:03 -0800 | [diff] [blame] | 87 | /// Returns an |
| 88 | /// <code |
| 89 | /// style="padding-right:0;">Iterator<Item = &mut </code><a |
| 90 | /// href="struct.TypeParam.html"><code |
| 91 | /// style="padding-left:0;padding-right:0;">TypeParam</code></a><code |
| 92 | /// style="padding-left:0;">></code> |
| 93 | /// over the type parameters in `self.params`. |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 94 | pub fn type_params_mut(&mut self) -> TypeParamsMut { |
| 95 | TypeParamsMut(self.params.iter_mut()) |
| 96 | } |
| 97 | |
David Tolnay | ef5199b | 2018-01-17 10:56:03 -0800 | [diff] [blame] | 98 | /// Returns an |
| 99 | /// <code |
| 100 | /// style="padding-right:0;">Iterator<Item = &</code><a |
| 101 | /// href="struct.LifetimeDef.html"><code |
| 102 | /// style="padding-left:0;padding-right:0;">LifetimeDef</code></a><code |
| 103 | /// style="padding-left:0;">></code> |
| 104 | /// over the lifetime parameters in `self.params`. |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 105 | pub fn lifetimes(&self) -> Lifetimes { |
| 106 | Lifetimes(self.params.iter()) |
| 107 | } |
| 108 | |
David Tolnay | ef5199b | 2018-01-17 10:56:03 -0800 | [diff] [blame] | 109 | /// Returns an |
| 110 | /// <code |
| 111 | /// style="padding-right:0;">Iterator<Item = &mut </code><a |
| 112 | /// href="struct.LifetimeDef.html"><code |
| 113 | /// style="padding-left:0;padding-right:0;">LifetimeDef</code></a><code |
| 114 | /// style="padding-left:0;">></code> |
| 115 | /// over the lifetime parameters in `self.params`. |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 116 | pub fn lifetimes_mut(&mut self) -> LifetimesMut { |
| 117 | LifetimesMut(self.params.iter_mut()) |
| 118 | } |
| 119 | |
David Tolnay | ef5199b | 2018-01-17 10:56:03 -0800 | [diff] [blame] | 120 | /// Returns an |
| 121 | /// <code |
| 122 | /// style="padding-right:0;">Iterator<Item = &</code><a |
| 123 | /// href="struct.ConstParam.html"><code |
| 124 | /// style="padding-left:0;padding-right:0;">ConstParam</code></a><code |
| 125 | /// style="padding-left:0;">></code> |
| 126 | /// over the constant parameters in `self.params`. |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 127 | pub fn const_params(&self) -> ConstParams { |
| 128 | ConstParams(self.params.iter()) |
| 129 | } |
| 130 | |
David Tolnay | ef5199b | 2018-01-17 10:56:03 -0800 | [diff] [blame] | 131 | /// Returns an |
| 132 | /// <code |
| 133 | /// style="padding-right:0;">Iterator<Item = &mut </code><a |
| 134 | /// href="struct.ConstParam.html"><code |
| 135 | /// style="padding-left:0;padding-right:0;">ConstParam</code></a><code |
| 136 | /// style="padding-left:0;">></code> |
| 137 | /// over the constant parameters in `self.params`. |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 138 | pub fn const_params_mut(&mut self) -> ConstParamsMut { |
| 139 | ConstParamsMut(self.params.iter_mut()) |
| 140 | } |
David Tolnay | 7f9954d | 2018-04-01 16:52:15 +0200 | [diff] [blame] | 141 | |
| 142 | /// Initializes an empty `where`-clause if there is not one present already. |
| 143 | pub fn make_where_clause(&mut self) -> &mut WhereClause { |
| 144 | // This is Option::get_or_insert_with in Rust 1.20. |
| 145 | if self.where_clause.is_none() { |
| 146 | self.where_clause = Some(WhereClause { |
David Tolnay | d228b33 | 2018-06-27 23:56:05 -0700 | [diff] [blame] | 147 | where_token: <Token![where]>::default(), |
David Tolnay | 7f9954d | 2018-04-01 16:52:15 +0200 | [diff] [blame] | 148 | predicates: Punctuated::new(), |
| 149 | }); |
| 150 | } |
| 151 | match self.where_clause { |
| 152 | Some(ref mut where_clause) => where_clause, |
| 153 | None => unreachable!(), |
| 154 | } |
| 155 | } |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 156 | } |
| 157 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 158 | pub struct TypeParams<'a>(Iter<'a, GenericParam>); |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 159 | |
| 160 | impl<'a> Iterator for TypeParams<'a> { |
| 161 | type Item = &'a TypeParam; |
| 162 | |
| 163 | fn next(&mut self) -> Option<Self::Item> { |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 164 | let next = match self.0.next() { |
| 165 | Some(item) => item, |
| 166 | None => return None, |
| 167 | }; |
| 168 | if let GenericParam::Type(ref type_param) = *next { |
| 169 | Some(type_param) |
| 170 | } else { |
| 171 | self.next() |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 176 | pub struct TypeParamsMut<'a>(IterMut<'a, GenericParam>); |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 177 | |
| 178 | impl<'a> Iterator for TypeParamsMut<'a> { |
| 179 | type Item = &'a mut TypeParam; |
| 180 | |
| 181 | fn next(&mut self) -> Option<Self::Item> { |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 182 | let next = match self.0.next() { |
| 183 | Some(item) => item, |
| 184 | None => return None, |
| 185 | }; |
| 186 | if let GenericParam::Type(ref mut type_param) = *next { |
| 187 | Some(type_param) |
| 188 | } else { |
| 189 | self.next() |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 194 | pub struct Lifetimes<'a>(Iter<'a, GenericParam>); |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 195 | |
| 196 | impl<'a> Iterator for Lifetimes<'a> { |
| 197 | type Item = &'a LifetimeDef; |
| 198 | |
| 199 | fn next(&mut self) -> Option<Self::Item> { |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 200 | let next = match self.0.next() { |
| 201 | Some(item) => item, |
| 202 | None => return None, |
| 203 | }; |
| 204 | if let GenericParam::Lifetime(ref lifetime) = *next { |
| 205 | Some(lifetime) |
| 206 | } else { |
| 207 | self.next() |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 212 | pub struct LifetimesMut<'a>(IterMut<'a, GenericParam>); |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 213 | |
| 214 | impl<'a> Iterator for LifetimesMut<'a> { |
| 215 | type Item = &'a mut LifetimeDef; |
| 216 | |
| 217 | fn next(&mut self) -> Option<Self::Item> { |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 218 | let next = match self.0.next() { |
| 219 | Some(item) => item, |
| 220 | None => return None, |
| 221 | }; |
| 222 | if let GenericParam::Lifetime(ref mut lifetime) = *next { |
| 223 | Some(lifetime) |
| 224 | } else { |
| 225 | self.next() |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 230 | pub struct ConstParams<'a>(Iter<'a, GenericParam>); |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 231 | |
| 232 | impl<'a> Iterator for ConstParams<'a> { |
| 233 | type Item = &'a ConstParam; |
| 234 | |
| 235 | fn next(&mut self) -> Option<Self::Item> { |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 236 | let next = match self.0.next() { |
| 237 | Some(item) => item, |
| 238 | None => return None, |
| 239 | }; |
| 240 | if let GenericParam::Const(ref const_param) = *next { |
| 241 | Some(const_param) |
| 242 | } else { |
| 243 | self.next() |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
David Tolnay | 8095c30 | 2018-03-31 19:34:17 +0200 | [diff] [blame] | 248 | pub struct ConstParamsMut<'a>(IterMut<'a, GenericParam>); |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 249 | |
| 250 | impl<'a> Iterator for ConstParamsMut<'a> { |
| 251 | type Item = &'a mut ConstParam; |
| 252 | |
| 253 | fn next(&mut self) -> Option<Self::Item> { |
Sean Griffin | 8fcca80 | 2018-01-15 15:45:41 -0700 | [diff] [blame] | 254 | let next = match self.0.next() { |
| 255 | Some(item) => item, |
| 256 | None => return None, |
| 257 | }; |
| 258 | if let GenericParam::Const(ref mut const_param) = *next { |
| 259 | Some(const_param) |
| 260 | } else { |
| 261 | self.next() |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 266 | /// Returned by `Generics::split_for_impl`. |
| 267 | /// |
| 268 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 269 | /// feature and the `"printing"` feature.* |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 270 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 271 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 272 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 273 | pub struct ImplGenerics<'a>(&'a Generics); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 274 | |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 275 | /// Returned by `Generics::split_for_impl`. |
| 276 | /// |
| 277 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 278 | /// feature and the `"printing"` feature.* |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 279 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 280 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 281 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 282 | pub struct TypeGenerics<'a>(&'a Generics); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 283 | |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 284 | /// Returned by `TypeGenerics::as_turbofish`. |
| 285 | /// |
| 286 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 287 | /// feature and the `"printing"` feature.* |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 288 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 289 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 290 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 291 | pub struct Turbofish<'a>(&'a Generics); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 292 | |
David Tolnay | e95cc9f | 2017-01-25 15:57:09 -0800 | [diff] [blame] | 293 | #[cfg(feature = "printing")] |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 294 | impl Generics { |
| 295 | /// Split a type's generics into the pieces required for impl'ing a trait |
| 296 | /// for that type. |
| 297 | /// |
David Tolnay | 95989db | 2019-01-01 15:05:57 -0500 | [diff] [blame] | 298 | /// ```edition2018 |
Alex Crichton | f0fea9a | 2018-05-17 11:19:34 -0700 | [diff] [blame] | 299 | /// # use proc_macro2::{Span, Ident}; |
David Tolnay | fd5b117 | 2018-12-31 17:54:36 -0500 | [diff] [blame] | 300 | /// # use quote::quote; |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 301 | /// # |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 302 | /// # fn main() { |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 303 | /// # let generics: syn::Generics = Default::default(); |
| 304 | /// # let name = Ident::new("MyType", Span::call_site()); |
| 305 | /// # |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 306 | /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 307 | /// quote! { |
| 308 | /// impl #impl_generics MyTrait for #name #ty_generics #where_clause { |
| 309 | /// // ... |
| 310 | /// } |
| 311 | /// } |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 312 | /// # ; |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 313 | /// # } |
| 314 | /// ``` |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 315 | /// |
| 316 | /// *This method is available if Syn is built with the `"derive"` or |
| 317 | /// `"full"` feature and the `"printing"` feature.* |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 318 | pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, Option<&WhereClause>) { |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 319 | ( |
| 320 | ImplGenerics(self), |
| 321 | TypeGenerics(self), |
| 322 | self.where_clause.as_ref(), |
| 323 | ) |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
David Tolnay | e95cc9f | 2017-01-25 15:57:09 -0800 | [diff] [blame] | 327 | #[cfg(feature = "printing")] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 328 | impl<'a> TypeGenerics<'a> { |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 329 | /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 330 | /// |
| 331 | /// *This method is available if Syn is built with the `"derive"` or |
| 332 | /// `"full"` feature and the `"printing"` feature.* |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 333 | pub fn as_turbofish(&self) -> Turbofish { |
| 334 | Turbofish(self.0) |
| 335 | } |
| 336 | } |
| 337 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 338 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 339 | /// A set of bound lifetimes: `for<'a, 'b, 'c>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 340 | /// |
| 341 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 342 | /// feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 343 | #[derive(Default)] |
| 344 | pub struct BoundLifetimes { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 345 | pub for_token: Token![for], |
| 346 | pub lt_token: Token![<], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 347 | pub lifetimes: Punctuated<LifetimeDef, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 348 | pub gt_token: Token![>], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 352 | impl LifetimeDef { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 353 | pub fn new(lifetime: Lifetime) -> Self { |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 354 | LifetimeDef { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 355 | attrs: Vec::new(), |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 356 | lifetime: lifetime, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 357 | colon_token: None, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 358 | bounds: Punctuated::new(), |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 363 | impl From<Ident> for TypeParam { |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 364 | fn from(ident: Ident) -> Self { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 365 | TypeParam { |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 366 | attrs: vec![], |
| 367 | ident: ident, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 368 | colon_token: None, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 369 | bounds: Punctuated::new(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 370 | eq_token: None, |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 371 | default: None, |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 376 | ast_enum_of_structs! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 377 | /// A trait or lifetime used as a bound on a type parameter. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 378 | /// |
| 379 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 380 | /// feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 381 | pub enum TypeParamBound { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 382 | pub Trait(TraitBound), |
| 383 | pub Lifetime(Lifetime), |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | ast_struct! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 388 | /// A trait used as a bound on a type parameter. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 389 | /// |
| 390 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 391 | /// feature.* |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 392 | pub struct TraitBound { |
David Tolnay | c1f5d5d | 2018-03-31 22:17:56 +0200 | [diff] [blame] | 393 | pub paren_token: Option<token::Paren>, |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 394 | pub modifier: TraitBoundModifier, |
| 395 | /// The `for<'a>` in `for<'a> Foo<&'a T>` |
| 396 | pub lifetimes: Option<BoundLifetimes>, |
| 397 | /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>` |
| 398 | pub path: Path, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 399 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 402 | ast_enum! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 403 | /// A modifier on a trait bound, currently only used for the `?` in |
| 404 | /// `?Sized`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 405 | /// |
| 406 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 407 | /// feature.* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 408 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 409 | pub enum TraitBoundModifier { |
| 410 | None, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 411 | Maybe(Token![?]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 412 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 415 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 416 | /// A `where` clause in a definition: `where T: Deserialize<'de>, D: |
| 417 | /// 'static`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 418 | /// |
| 419 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 420 | /// feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 421 | pub struct WhereClause { |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 422 | pub where_token: Token![where], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 423 | pub predicates: Punctuated<WherePredicate, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 424 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 427 | ast_enum_of_structs! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 428 | /// A single predicate in a `where` clause: `T: Deserialize<'de>`. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 429 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 430 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 431 | /// feature.* |
| 432 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 433 | /// # Syntax tree enum |
| 434 | /// |
| 435 | /// This type is a [syntax tree enum]. |
| 436 | /// |
| 437 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
David Tolnay | b8dec88 | 2019-07-20 09:46:14 -0700 | [diff] [blame] | 438 | // |
| 439 | // TODO: change syntax-tree-enum link to an intra rustdoc link, currently |
| 440 | // blocked on https://github.com/rust-lang/rust/issues/62833 |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 441 | pub enum WherePredicate { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 442 | /// A type predicate in a `where` clause: `for<'c> Foo<'c>: Trait<'c>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 443 | /// |
| 444 | /// *This type is available if Syn is built with the `"derive"` or |
| 445 | /// `"full"` feature.* |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 446 | pub Type(PredicateType { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 447 | /// Any lifetimes from a `for` binding |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 448 | pub lifetimes: Option<BoundLifetimes>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 449 | /// The type being bounded |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 450 | pub bounded_ty: Type, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 451 | pub colon_token: Token![:], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 452 | /// Trait and lifetime bounds (`Clone+Send+'static`) |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 453 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 454 | }), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 455 | |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 456 | /// A lifetime predicate in a `where` clause: `'a: 'b + 'c`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 457 | /// |
| 458 | /// *This type is available if Syn is built with the `"derive"` or |
| 459 | /// `"full"` feature.* |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 460 | pub Lifetime(PredicateLifetime { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 461 | pub lifetime: Lifetime, |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 462 | pub colon_token: Token![:], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 463 | pub bounds: Punctuated<Lifetime, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 464 | }), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 465 | |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 466 | /// An equality predicate in a `where` clause (unsupported). |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 467 | /// |
| 468 | /// *This type is available if Syn is built with the `"derive"` or |
| 469 | /// `"full"` feature.* |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 470 | pub Eq(PredicateEq { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 471 | pub lhs_ty: Type, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 472 | pub eq_token: Token![=], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 473 | pub rhs_ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 474 | }), |
| 475 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 476 | } |
| 477 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 478 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 479 | pub mod parsing { |
| 480 | use super::*; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 481 | |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 482 | use parse::{Parse, ParseStream, Result}; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 483 | |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 484 | impl Parse for Generics { |
| 485 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 486 | if !input.peek(Token![<]) { |
David Tolnay | 3c29f6e | 2018-10-27 22:47:48 -0700 | [diff] [blame] | 487 | return Ok(Generics::default()); |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 488 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 489 | |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 490 | let lt_token: Token![<] = input.parse()?; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 491 | |
David Tolnay | 3c29f6e | 2018-10-27 22:47:48 -0700 | [diff] [blame] | 492 | let mut params = Punctuated::new(); |
David Tolnay | d502e4b | 2019-05-25 00:20:27 -0700 | [diff] [blame] | 493 | let mut allow_lifetime_param = true; |
| 494 | let mut allow_type_param = true; |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 495 | loop { |
| 496 | if input.peek(Token![>]) { |
| 497 | break; |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 498 | } |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 499 | |
| 500 | let attrs = input.call(Attribute::parse_outer)?; |
| 501 | let lookahead = input.lookahead1(); |
David Tolnay | d502e4b | 2019-05-25 00:20:27 -0700 | [diff] [blame] | 502 | if allow_lifetime_param && lookahead.peek(Lifetime) { |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 503 | params.push_value(GenericParam::Lifetime(LifetimeDef { |
| 504 | attrs: attrs, |
| 505 | ..input.parse()? |
| 506 | })); |
David Tolnay | d502e4b | 2019-05-25 00:20:27 -0700 | [diff] [blame] | 507 | } else if allow_type_param && lookahead.peek(Ident) { |
| 508 | allow_lifetime_param = false; |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 509 | params.push_value(GenericParam::Type(TypeParam { |
| 510 | attrs: attrs, |
| 511 | ..input.parse()? |
| 512 | })); |
David Tolnay | d502e4b | 2019-05-25 00:20:27 -0700 | [diff] [blame] | 513 | } else if lookahead.peek(Token![const]) { |
| 514 | allow_lifetime_param = false; |
| 515 | allow_type_param = false; |
| 516 | params.push_value(GenericParam::Const(ConstParam { |
| 517 | attrs: attrs, |
| 518 | ..input.parse()? |
| 519 | })); |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 520 | } else { |
| 521 | return Err(lookahead.error()); |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 522 | } |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 523 | |
| 524 | if input.peek(Token![>]) { |
| 525 | break; |
| 526 | } |
| 527 | let punct = input.parse()?; |
| 528 | params.push_punct(punct); |
| 529 | } |
| 530 | |
| 531 | let gt_token: Token![>] = input.parse()?; |
| 532 | |
| 533 | Ok(Generics { |
| 534 | lt_token: Some(lt_token), |
| 535 | params: params, |
| 536 | gt_token: Some(gt_token), |
| 537 | where_clause: None, |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 538 | }) |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 542 | impl Parse for GenericParam { |
| 543 | fn parse(input: ParseStream) -> Result<Self> { |
| 544 | let attrs = input.call(Attribute::parse_outer)?; |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 545 | |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 546 | let lookahead = input.lookahead1(); |
| 547 | if lookahead.peek(Ident) { |
| 548 | Ok(GenericParam::Type(TypeParam { |
David Tolnay | 78ee520 | 2017-12-04 22:17:54 -0800 | [diff] [blame] | 549 | attrs: attrs, |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 550 | ..input.parse()? |
| 551 | })) |
| 552 | } else if lookahead.peek(Lifetime) { |
| 553 | Ok(GenericParam::Lifetime(LifetimeDef { |
| 554 | attrs: attrs, |
| 555 | ..input.parse()? |
| 556 | })) |
| 557 | } else if lookahead.peek(Token![const]) { |
| 558 | Ok(GenericParam::Const(ConstParam { |
| 559 | attrs: attrs, |
| 560 | ..input.parse()? |
| 561 | })) |
| 562 | } else { |
| 563 | Err(lookahead.error()) |
| 564 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 565 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 566 | } |
| 567 | |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 568 | impl Parse for LifetimeDef { |
| 569 | fn parse(input: ParseStream) -> Result<Self> { |
| 570 | let has_colon; |
| 571 | Ok(LifetimeDef { |
| 572 | attrs: input.call(Attribute::parse_outer)?, |
| 573 | lifetime: input.parse()?, |
| 574 | colon_token: { |
| 575 | if input.peek(Token![:]) { |
| 576 | has_colon = true; |
| 577 | Some(input.parse()?) |
| 578 | } else { |
| 579 | has_colon = false; |
| 580 | None |
| 581 | } |
| 582 | }, |
| 583 | bounds: { |
| 584 | let mut bounds = Punctuated::new(); |
| 585 | if has_colon { |
| 586 | loop { |
| 587 | if input.peek(Token![,]) || input.peek(Token![>]) { |
| 588 | break; |
| 589 | } |
| 590 | let value = input.parse()?; |
| 591 | bounds.push_value(value); |
| 592 | if !input.peek(Token![+]) { |
| 593 | break; |
| 594 | } |
| 595 | let punct = input.parse()?; |
| 596 | bounds.push_punct(punct); |
| 597 | } |
| 598 | } |
| 599 | bounds |
| 600 | }, |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 601 | }) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 605 | impl Parse for BoundLifetimes { |
| 606 | fn parse(input: ParseStream) -> Result<Self> { |
| 607 | Ok(BoundLifetimes { |
| 608 | for_token: input.parse()?, |
| 609 | lt_token: input.parse()?, |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 610 | lifetimes: { |
| 611 | let mut lifetimes = Punctuated::new(); |
| 612 | while !input.peek(Token![>]) { |
| 613 | lifetimes.push_value(input.parse()?); |
| 614 | if input.peek(Token![>]) { |
| 615 | break; |
| 616 | } |
| 617 | lifetimes.push_punct(input.parse()?); |
| 618 | } |
| 619 | lifetimes |
| 620 | }, |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 621 | gt_token: input.parse()?, |
| 622 | }) |
| 623 | } |
| 624 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 625 | |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 626 | impl Parse for Option<BoundLifetimes> { |
| 627 | fn parse(input: ParseStream) -> Result<Self> { |
| 628 | if input.peek(Token![for]) { |
| 629 | input.parse().map(Some) |
| 630 | } else { |
| 631 | Ok(None) |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | impl Parse for TypeParam { |
| 637 | fn parse(input: ParseStream) -> Result<Self> { |
| 638 | let has_colon; |
| 639 | let has_default; |
| 640 | Ok(TypeParam { |
| 641 | attrs: input.call(Attribute::parse_outer)?, |
| 642 | ident: input.parse()?, |
| 643 | colon_token: { |
| 644 | if input.peek(Token![:]) { |
| 645 | has_colon = true; |
| 646 | Some(input.parse()?) |
| 647 | } else { |
| 648 | has_colon = false; |
| 649 | None |
| 650 | } |
| 651 | }, |
| 652 | bounds: { |
| 653 | let mut bounds = Punctuated::new(); |
| 654 | if has_colon { |
| 655 | loop { |
David Tolnay | e614f28 | 2018-10-27 22:50:12 -0700 | [diff] [blame] | 656 | if input.peek(Token![,]) |
| 657 | || input.peek(Token![>]) |
| 658 | || input.peek(Token![=]) |
| 659 | { |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | let value = input.parse()?; |
| 663 | bounds.push_value(value); |
| 664 | if !input.peek(Token![+]) { |
| 665 | break; |
| 666 | } |
| 667 | let punct = input.parse()?; |
| 668 | bounds.push_punct(punct); |
| 669 | } |
| 670 | } |
| 671 | bounds |
| 672 | }, |
| 673 | eq_token: { |
| 674 | if input.peek(Token![=]) { |
| 675 | has_default = true; |
| 676 | Some(input.parse()?) |
| 677 | } else { |
| 678 | has_default = false; |
| 679 | None |
| 680 | } |
| 681 | }, |
| 682 | default: { |
| 683 | if has_default { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 684 | Some(input.parse()?) |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 685 | } else { |
| 686 | None |
| 687 | } |
| 688 | }, |
| 689 | }) |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | impl Parse for TypeParamBound { |
| 694 | fn parse(input: ParseStream) -> Result<Self> { |
| 695 | if input.peek(Lifetime) { |
| 696 | return input.parse().map(TypeParamBound::Lifetime); |
| 697 | } |
| 698 | |
| 699 | if input.peek(token::Paren) { |
| 700 | let content; |
| 701 | let paren_token = parenthesized!(content in input); |
| 702 | let mut bound: TraitBound = content.parse()?; |
| 703 | bound.paren_token = Some(paren_token); |
| 704 | return Ok(TypeParamBound::Trait(bound)); |
| 705 | } |
| 706 | |
| 707 | input.parse().map(TypeParamBound::Trait) |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | impl Parse for TraitBound { |
| 712 | fn parse(input: ParseStream) -> Result<Self> { |
| 713 | let modifier: TraitBoundModifier = input.parse()?; |
| 714 | let lifetimes: Option<BoundLifetimes> = input.parse()?; |
| 715 | |
| 716 | let mut path: Path = input.parse()?; |
| 717 | if path.segments.last().unwrap().value().arguments.is_empty() |
| 718 | && input.peek(token::Paren) |
| 719 | { |
| 720 | let parenthesized = PathArguments::Parenthesized(input.parse()?); |
| 721 | path.segments.last_mut().unwrap().value_mut().arguments = parenthesized; |
| 722 | } |
| 723 | |
| 724 | Ok(TraitBound { |
| 725 | paren_token: None, |
| 726 | modifier: modifier, |
| 727 | lifetimes: lifetimes, |
| 728 | path: path, |
| 729 | }) |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | impl Parse for TraitBoundModifier { |
| 734 | fn parse(input: ParseStream) -> Result<Self> { |
| 735 | if input.peek(Token![?]) { |
| 736 | input.parse().map(TraitBoundModifier::Maybe) |
| 737 | } else { |
| 738 | Ok(TraitBoundModifier::None) |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | impl Parse for ConstParam { |
| 744 | fn parse(input: ParseStream) -> Result<Self> { |
| 745 | let mut default = None; |
| 746 | Ok(ConstParam { |
| 747 | attrs: input.call(Attribute::parse_outer)?, |
| 748 | const_token: input.parse()?, |
| 749 | ident: input.parse()?, |
| 750 | colon_token: input.parse()?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 751 | ty: input.parse()?, |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 752 | eq_token: { |
| 753 | if input.peek(Token![=]) { |
| 754 | let eq_token = input.parse()?; |
David Tolnay | 9389c38 | 2018-08-27 09:13:37 -0700 | [diff] [blame] | 755 | default = Some(input.parse::<Expr>()?); |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 756 | Some(eq_token) |
| 757 | } else { |
| 758 | None |
| 759 | } |
| 760 | }, |
| 761 | default: default, |
| 762 | }) |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | impl Parse for WhereClause { |
| 767 | fn parse(input: ParseStream) -> Result<Self> { |
| 768 | Ok(WhereClause { |
| 769 | where_token: input.parse()?, |
| 770 | predicates: { |
| 771 | let mut predicates = Punctuated::new(); |
| 772 | loop { |
David Tolnay | 38012de | 2018-09-02 13:32:47 -0700 | [diff] [blame] | 773 | if input.is_empty() |
| 774 | || input.peek(token::Brace) |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 775 | || input.peek(Token![,]) |
| 776 | || input.peek(Token![;]) |
| 777 | || input.peek(Token![:]) && !input.peek(Token![::]) |
| 778 | || input.peek(Token![=]) |
| 779 | { |
| 780 | break; |
| 781 | } |
| 782 | let value = input.parse()?; |
| 783 | predicates.push_value(value); |
| 784 | if !input.peek(Token![,]) { |
| 785 | break; |
| 786 | } |
| 787 | let punct = input.parse()?; |
| 788 | predicates.push_punct(punct); |
| 789 | } |
| 790 | predicates |
| 791 | }, |
| 792 | }) |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | impl Parse for Option<WhereClause> { |
| 797 | fn parse(input: ParseStream) -> Result<Self> { |
| 798 | if input.peek(Token![where]) { |
| 799 | input.parse().map(Some) |
| 800 | } else { |
| 801 | Ok(None) |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | impl Parse for WherePredicate { |
| 807 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 66cb0c4 | 2018-08-31 09:01:30 -0700 | [diff] [blame] | 808 | if input.peek(Lifetime) && input.peek2(Token![:]) { |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 809 | Ok(WherePredicate::Lifetime(PredicateLifetime { |
| 810 | lifetime: input.parse()?, |
| 811 | colon_token: input.parse()?, |
| 812 | bounds: { |
| 813 | let mut bounds = Punctuated::new(); |
| 814 | loop { |
David Tolnay | 1e072a7 | 2019-07-23 19:21:27 -0700 | [diff] [blame] | 815 | if input.is_empty() |
| 816 | || input.peek(token::Brace) |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 817 | || input.peek(Token![,]) |
| 818 | || input.peek(Token![;]) |
| 819 | || input.peek(Token![:]) |
| 820 | || input.peek(Token![=]) |
| 821 | { |
| 822 | break; |
| 823 | } |
| 824 | let value = input.parse()?; |
| 825 | bounds.push_value(value); |
| 826 | if !input.peek(Token![+]) { |
| 827 | break; |
| 828 | } |
| 829 | let punct = input.parse()?; |
| 830 | bounds.push_punct(punct); |
| 831 | } |
| 832 | bounds |
| 833 | }, |
| 834 | })) |
| 835 | } else { |
| 836 | Ok(WherePredicate::Type(PredicateType { |
| 837 | lifetimes: input.parse()?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 838 | bounded_ty: input.parse()?, |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 839 | colon_token: input.parse()?, |
| 840 | bounds: { |
| 841 | let mut bounds = Punctuated::new(); |
| 842 | loop { |
David Tolnay | 1e072a7 | 2019-07-23 19:21:27 -0700 | [diff] [blame] | 843 | if input.is_empty() |
| 844 | || input.peek(token::Brace) |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 845 | || input.peek(Token![,]) |
| 846 | || input.peek(Token![;]) |
| 847 | || input.peek(Token![:]) && !input.peek(Token![::]) |
| 848 | || input.peek(Token![=]) |
| 849 | { |
| 850 | break; |
| 851 | } |
| 852 | let value = input.parse()?; |
| 853 | bounds.push_value(value); |
| 854 | if !input.peek(Token![+]) { |
| 855 | break; |
| 856 | } |
| 857 | let punct = input.parse()?; |
| 858 | bounds.push_punct(punct); |
| 859 | } |
| 860 | bounds |
| 861 | }, |
| 862 | })) |
| 863 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 864 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 865 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 866 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 867 | |
| 868 | #[cfg(feature = "printing")] |
| 869 | mod printing { |
| 870 | use super::*; |
David Tolnay | 6402391 | 2018-08-31 09:51:12 -0700 | [diff] [blame] | 871 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 872 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 873 | use quote::{ToTokens, TokenStreamExt}; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 874 | |
David Tolnay | 6402391 | 2018-08-31 09:51:12 -0700 | [diff] [blame] | 875 | use attr::FilterAttrs; |
| 876 | use print::TokensOrDefault; |
| 877 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 878 | impl ToTokens for Generics { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 879 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 7c6e0f4 | 2018-01-11 20:52:52 -0800 | [diff] [blame] | 880 | if self.params.is_empty() { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 884 | TokensOrDefault(&self.lt_token).to_tokens(tokens); |
David Tolnay | 298570b | 2018-01-11 16:38:36 -0800 | [diff] [blame] | 885 | |
| 886 | // Print lifetimes before types and consts, regardless of their |
| 887 | // order in self.params. |
| 888 | // |
| 889 | // TODO: ordering rules for const parameters vs type parameters have |
| 890 | // not been settled yet. https://github.com/rust-lang/rust/issues/44580 |
| 891 | let mut trailing_or_empty = true; |
| 892 | for param in self.params.pairs() { |
| 893 | if let GenericParam::Lifetime(_) = **param.value() { |
| 894 | param.to_tokens(tokens); |
| 895 | trailing_or_empty = param.punct().is_some(); |
| 896 | } |
| 897 | } |
| 898 | for param in self.params.pairs() { |
| 899 | match **param.value() { |
| 900 | GenericParam::Type(_) | GenericParam::Const(_) => { |
| 901 | if !trailing_or_empty { |
| 902 | <Token![,]>::default().to_tokens(tokens); |
| 903 | trailing_or_empty = true; |
| 904 | } |
| 905 | param.to_tokens(tokens); |
| 906 | } |
| 907 | GenericParam::Lifetime(_) => {} |
| 908 | } |
| 909 | } |
| 910 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 911 | TokensOrDefault(&self.gt_token).to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 915 | impl<'a> ToTokens for ImplGenerics<'a> { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 916 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 7c6e0f4 | 2018-01-11 20:52:52 -0800 | [diff] [blame] | 917 | if self.0.params.is_empty() { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 918 | return; |
| 919 | } |
| 920 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 921 | TokensOrDefault(&self.0.lt_token).to_tokens(tokens); |
David Tolnay | 225aca6 | 2018-01-11 20:51:46 -0800 | [diff] [blame] | 922 | |
| 923 | // Print lifetimes before types and consts, regardless of their |
| 924 | // order in self.params. |
| 925 | // |
| 926 | // TODO: ordering rules for const parameters vs type parameters have |
| 927 | // not been settled yet. https://github.com/rust-lang/rust/issues/44580 |
| 928 | let mut trailing_or_empty = true; |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 929 | for param in self.0.params.pairs() { |
David Tolnay | 225aca6 | 2018-01-11 20:51:46 -0800 | [diff] [blame] | 930 | if let GenericParam::Lifetime(_) = **param.value() { |
| 931 | param.to_tokens(tokens); |
| 932 | trailing_or_empty = param.punct().is_some(); |
| 933 | } |
| 934 | } |
| 935 | for param in self.0.params.pairs() { |
| 936 | if let GenericParam::Lifetime(_) = **param.value() { |
| 937 | continue; |
| 938 | } |
| 939 | if !trailing_or_empty { |
| 940 | <Token![,]>::default().to_tokens(tokens); |
| 941 | trailing_or_empty = true; |
| 942 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 943 | match **param.value() { |
David Tolnay | 225aca6 | 2018-01-11 20:51:46 -0800 | [diff] [blame] | 944 | GenericParam::Lifetime(_) => unreachable!(), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 945 | GenericParam::Type(ref param) => { |
| 946 | // Leave off the type parameter defaults |
| 947 | tokens.append_all(param.attrs.outer()); |
| 948 | param.ident.to_tokens(tokens); |
| 949 | if !param.bounds.is_empty() { |
| 950 | TokensOrDefault(¶m.colon_token).to_tokens(tokens); |
| 951 | param.bounds.to_tokens(tokens); |
| 952 | } |
| 953 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 954 | GenericParam::Const(ref param) => { |
| 955 | // Leave off the const parameter defaults |
| 956 | tokens.append_all(param.attrs.outer()); |
| 957 | param.const_token.to_tokens(tokens); |
| 958 | param.ident.to_tokens(tokens); |
| 959 | param.colon_token.to_tokens(tokens); |
| 960 | param.ty.to_tokens(tokens); |
| 961 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 962 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 963 | param.punct().to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 964 | } |
David Tolnay | 225aca6 | 2018-01-11 20:51:46 -0800 | [diff] [blame] | 965 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 966 | TokensOrDefault(&self.0.gt_token).to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 970 | impl<'a> ToTokens for TypeGenerics<'a> { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 971 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 7c6e0f4 | 2018-01-11 20:52:52 -0800 | [diff] [blame] | 972 | if self.0.params.is_empty() { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 973 | return; |
| 974 | } |
| 975 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 976 | TokensOrDefault(&self.0.lt_token).to_tokens(tokens); |
David Tolnay | 225aca6 | 2018-01-11 20:51:46 -0800 | [diff] [blame] | 977 | |
| 978 | // Print lifetimes before types and consts, regardless of their |
| 979 | // order in self.params. |
| 980 | // |
| 981 | // TODO: ordering rules for const parameters vs type parameters have |
| 982 | // not been settled yet. https://github.com/rust-lang/rust/issues/44580 |
| 983 | let mut trailing_or_empty = true; |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 984 | for param in self.0.params.pairs() { |
David Tolnay | 225aca6 | 2018-01-11 20:51:46 -0800 | [diff] [blame] | 985 | if let GenericParam::Lifetime(ref def) = **param.value() { |
| 986 | // Leave off the lifetime bounds and attributes |
| 987 | def.lifetime.to_tokens(tokens); |
| 988 | param.punct().to_tokens(tokens); |
| 989 | trailing_or_empty = param.punct().is_some(); |
| 990 | } |
| 991 | } |
| 992 | for param in self.0.params.pairs() { |
| 993 | if let GenericParam::Lifetime(_) = **param.value() { |
| 994 | continue; |
| 995 | } |
| 996 | if !trailing_or_empty { |
| 997 | <Token![,]>::default().to_tokens(tokens); |
| 998 | trailing_or_empty = true; |
| 999 | } |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 1000 | match **param.value() { |
David Tolnay | 225aca6 | 2018-01-11 20:51:46 -0800 | [diff] [blame] | 1001 | GenericParam::Lifetime(_) => unreachable!(), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 1002 | GenericParam::Type(ref param) => { |
| 1003 | // Leave off the type parameter defaults |
| 1004 | param.ident.to_tokens(tokens); |
| 1005 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 1006 | GenericParam::Const(ref param) => { |
| 1007 | // Leave off the const parameter defaults |
| 1008 | param.ident.to_tokens(tokens); |
| 1009 | } |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 1010 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 1011 | param.punct().to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1012 | } |
David Tolnay | 225aca6 | 2018-01-11 20:51:46 -0800 | [diff] [blame] | 1013 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 1014 | TokensOrDefault(&self.0.gt_token).to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 1018 | impl<'a> ToTokens for Turbofish<'a> { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1019 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 7c6e0f4 | 2018-01-11 20:52:52 -0800 | [diff] [blame] | 1020 | if !self.0.params.is_empty() { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 1021 | <Token![::]>::default().to_tokens(tokens); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1022 | TypeGenerics(self.0).to_tokens(tokens); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1027 | impl ToTokens for BoundLifetimes { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1028 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1029 | self.for_token.to_tokens(tokens); |
| 1030 | self.lt_token.to_tokens(tokens); |
| 1031 | self.lifetimes.to_tokens(tokens); |
| 1032 | self.gt_token.to_tokens(tokens); |
| 1033 | } |
| 1034 | } |
| 1035 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1036 | impl ToTokens for LifetimeDef { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1037 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 1038 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1039 | self.lifetime.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1040 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 1041 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1042 | self.bounds.to_tokens(tokens); |
| 1043 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 1047 | impl ToTokens for TypeParam { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1048 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 1049 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 1050 | self.ident.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1051 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 1052 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1053 | self.bounds.to_tokens(tokens); |
| 1054 | } |
| 1055 | if self.default.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 1056 | TokensOrDefault(&self.eq_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 1057 | self.default.to_tokens(tokens); |
| 1058 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 1062 | impl ToTokens for TraitBound { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1063 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 1064 | let to_tokens = |tokens: &mut TokenStream| { |
David Tolnay | c1f5d5d | 2018-03-31 22:17:56 +0200 | [diff] [blame] | 1065 | self.modifier.to_tokens(tokens); |
| 1066 | self.lifetimes.to_tokens(tokens); |
| 1067 | self.path.to_tokens(tokens); |
| 1068 | }; |
| 1069 | match self.paren_token { |
David Tolnay | 997c6cb | 2018-03-31 22:49:52 +0200 | [diff] [blame] | 1070 | Some(ref paren) => paren.surround(tokens, to_tokens), |
David Tolnay | c1f5d5d | 2018-03-31 22:17:56 +0200 | [diff] [blame] | 1071 | None => to_tokens(tokens), |
| 1072 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1076 | impl ToTokens for TraitBoundModifier { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1077 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1078 | match *self { |
| 1079 | TraitBoundModifier::None => {} |
| 1080 | TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens), |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 1085 | impl ToTokens for ConstParam { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1086 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 1087 | tokens.append_all(self.attrs.outer()); |
| 1088 | self.const_token.to_tokens(tokens); |
| 1089 | self.ident.to_tokens(tokens); |
| 1090 | self.colon_token.to_tokens(tokens); |
| 1091 | self.ty.to_tokens(tokens); |
| 1092 | if self.default.is_some() { |
David Tolnay | 78ee520 | 2017-12-04 22:17:54 -0800 | [diff] [blame] | 1093 | TokensOrDefault(&self.eq_token).to_tokens(tokens); |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 1094 | self.default.to_tokens(tokens); |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 1099 | impl ToTokens for WhereClause { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1100 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 2a0b02f | 2018-03-09 00:50:45 -0800 | [diff] [blame] | 1101 | if !self.predicates.is_empty() { |
| 1102 | self.where_token.to_tokens(tokens); |
| 1103 | self.predicates.to_tokens(tokens); |
| 1104 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 1107 | |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 1108 | impl ToTokens for PredicateType { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1109 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 1110 | self.lifetimes.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 1111 | self.bounded_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1112 | self.colon_token.to_tokens(tokens); |
| 1113 | self.bounds.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 1117 | impl ToTokens for PredicateLifetime { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1118 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 1119 | self.lifetime.to_tokens(tokens); |
David Tolnay | 1b8e285 | 2018-08-26 08:25:18 -0400 | [diff] [blame] | 1120 | self.colon_token.to_tokens(tokens); |
| 1121 | self.bounds.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 1122 | } |
| 1123 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 1124 | |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 1125 | impl ToTokens for PredicateEq { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 1126 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 1127 | self.lhs_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 1128 | self.eq_token.to_tokens(tokens); |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 1129 | self.rhs_ty.to_tokens(tokens); |
| 1130 | } |
| 1131 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 1132 | } |