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