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