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::*; |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 10 | use punctuated::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 | |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 80 | /// Returned by `Generics::split_for_impl`. |
| 81 | /// |
| 82 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 83 | /// feature and the `"printing"` feature.* |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 84 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 85 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 86 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 87 | pub struct ImplGenerics<'a>(&'a Generics); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 88 | |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 89 | /// Returned by `Generics::split_for_impl`. |
| 90 | /// |
| 91 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 92 | /// feature and the `"printing"` feature.* |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 93 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 94 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 95 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 96 | pub struct TypeGenerics<'a>(&'a Generics); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 97 | |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 98 | /// Returned by `TypeGenerics::as_turbofish`. |
| 99 | /// |
| 100 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 101 | /// feature and the `"printing"` feature.* |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 102 | #[cfg(feature = "printing")] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 103 | #[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))] |
| 104 | #[cfg_attr(feature = "clone-impls", derive(Clone))] |
Nika Layzell | 6b38b13 | 2017-10-24 23:09:39 -0400 | [diff] [blame] | 105 | pub struct Turbofish<'a>(&'a Generics); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 106 | |
David Tolnay | e95cc9f | 2017-01-25 15:57:09 -0800 | [diff] [blame] | 107 | #[cfg(feature = "printing")] |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 108 | impl Generics { |
| 109 | /// Split a type's generics into the pieces required for impl'ing a trait |
| 110 | /// for that type. |
| 111 | /// |
| 112 | /// ``` |
| 113 | /// # extern crate syn; |
| 114 | /// # #[macro_use] |
| 115 | /// # extern crate quote; |
| 116 | /// # fn main() { |
| 117 | /// # let generics: syn::Generics = Default::default(); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 118 | /// # let name = syn::Ident::from("MyType"); |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 119 | /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 120 | /// quote! { |
| 121 | /// impl #impl_generics MyTrait for #name #ty_generics #where_clause { |
| 122 | /// // ... |
| 123 | /// } |
| 124 | /// } |
| 125 | /// # ; |
| 126 | /// # } |
| 127 | /// ``` |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 128 | /// |
| 129 | /// *This method is available if Syn is built with the `"derive"` or |
| 130 | /// `"full"` feature and the `"printing"` feature.* |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 131 | pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, Option<&WhereClause>) { |
David Tolnay | 61037c6 | 2018-01-05 16:21:03 -0800 | [diff] [blame] | 132 | ( |
| 133 | ImplGenerics(self), |
| 134 | TypeGenerics(self), |
| 135 | self.where_clause.as_ref(), |
| 136 | ) |
David Tolnay | b153dbc | 2016-10-04 23:39:10 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
David Tolnay | e95cc9f | 2017-01-25 15:57:09 -0800 | [diff] [blame] | 140 | #[cfg(feature = "printing")] |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 141 | impl<'a> TypeGenerics<'a> { |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 142 | /// 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] | 143 | /// |
| 144 | /// *This method is available if Syn is built with the `"derive"` or |
| 145 | /// `"full"` feature and the `"printing"` feature.* |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 146 | pub fn as_turbofish(&self) -> Turbofish { |
| 147 | Turbofish(self.0) |
| 148 | } |
| 149 | } |
| 150 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 151 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 152 | /// A set of bound lifetimes: `for<'a, 'b, 'c>`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 153 | /// |
| 154 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 155 | /// feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 156 | #[derive(Default)] |
| 157 | pub struct BoundLifetimes { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 158 | pub for_token: Token![for], |
| 159 | pub lt_token: Token![<], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 160 | pub lifetimes: Punctuated<LifetimeDef, Token![,]>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 161 | pub gt_token: Token![>], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 165 | impl LifetimeDef { |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 166 | pub fn new(lifetime: Lifetime) -> Self { |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 167 | LifetimeDef { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 168 | attrs: Vec::new(), |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 169 | lifetime: lifetime, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 170 | colon_token: None, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 171 | bounds: Punctuated::new(), |
David Tolnay | f9505b5 | 2016-10-02 09:18:52 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 176 | impl From<Ident> for TypeParam { |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 177 | fn from(ident: Ident) -> Self { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 178 | TypeParam { |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 179 | attrs: vec![], |
| 180 | ident: ident, |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 181 | colon_token: None, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 182 | bounds: Punctuated::new(), |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 183 | eq_token: None, |
Ted Driggs | 0547d0d | 2017-04-20 10:00:12 -0700 | [diff] [blame] | 184 | default: None, |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 189 | ast_enum_of_structs! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 190 | /// A trait or lifetime used as a bound on a type parameter. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 191 | /// |
| 192 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 193 | /// feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 194 | pub enum TypeParamBound { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 195 | pub Trait(TraitBound), |
| 196 | pub Lifetime(Lifetime), |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | ast_struct! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 201 | /// A trait used as a bound on a type parameter. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 202 | /// |
| 203 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 204 | /// feature.* |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 205 | pub struct TraitBound { |
| 206 | pub modifier: TraitBoundModifier, |
| 207 | /// The `for<'a>` in `for<'a> Foo<&'a T>` |
| 208 | pub lifetimes: Option<BoundLifetimes>, |
| 209 | /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>` |
| 210 | pub path: Path, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 211 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 214 | ast_enum! { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 215 | /// A modifier on a trait bound, currently only used for the `?` in |
| 216 | /// `?Sized`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 217 | /// |
| 218 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 219 | /// feature.* |
Alex Crichton | 2e0229c | 2017-05-23 09:34:50 -0700 | [diff] [blame] | 220 | #[cfg_attr(feature = "clone-impls", derive(Copy))] |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 221 | pub enum TraitBoundModifier { |
| 222 | None, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 223 | Maybe(Token![?]), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 224 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 227 | ast_struct! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 228 | /// A `where` clause in a definition: `where T: Deserialize<'de>, D: |
| 229 | /// 'static`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 230 | /// |
| 231 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 232 | /// feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 233 | pub struct WhereClause { |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 234 | pub where_token: Token![where], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 235 | pub predicates: Punctuated<WherePredicate, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 236 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 239 | ast_enum_of_structs! { |
David Tolnay | 0565850 | 2018-01-07 09:56:37 -0800 | [diff] [blame] | 240 | /// A single predicate in a `where` clause: `T: Deserialize<'de>`. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 241 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 242 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 243 | /// feature.* |
| 244 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 245 | /// # Syntax tree enum |
| 246 | /// |
| 247 | /// This type is a [syntax tree enum]. |
| 248 | /// |
| 249 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 250 | pub enum WherePredicate { |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 251 | /// 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] | 252 | /// |
| 253 | /// *This type is available if Syn is built with the `"derive"` or |
| 254 | /// `"full"` feature.* |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 255 | pub Type(PredicateType { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 256 | /// Any lifetimes from a `for` binding |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 257 | pub lifetimes: Option<BoundLifetimes>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 258 | /// The type being bounded |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 259 | pub bounded_ty: Type, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 260 | pub colon_token: Token![:], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 261 | /// Trait and lifetime bounds (`Clone+Send+'static`) |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 262 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 263 | }), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 264 | |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 265 | /// A lifetime predicate in a `where` clause: `'a: 'b + 'c`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 266 | /// |
| 267 | /// *This type is available if Syn is built with the `"derive"` or |
| 268 | /// `"full"` feature.* |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 269 | pub Lifetime(PredicateLifetime { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 270 | pub lifetime: Lifetime, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 271 | pub colon_token: Option<Token![:]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 272 | pub bounds: Punctuated<Lifetime, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 273 | }), |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 274 | |
David Tolnay | ed906d1 | 2018-01-07 01:20:29 -0800 | [diff] [blame] | 275 | /// An equality predicate in a `where` clause (unsupported). |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 276 | /// |
| 277 | /// *This type is available if Syn is built with the `"derive"` or |
| 278 | /// `"full"` feature.* |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 279 | pub Eq(PredicateEq { |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 280 | pub lhs_ty: Type, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 281 | pub eq_token: Token![=], |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 282 | pub rhs_ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 283 | }), |
| 284 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 285 | } |
| 286 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 287 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 288 | pub mod parsing { |
| 289 | use super::*; |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 290 | |
David Tolnay | 63e3dee | 2017-06-03 20:13:17 -0700 | [diff] [blame] | 291 | use synom::Synom; |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 292 | use punctuated::Pair; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 293 | |
| 294 | impl Synom for Generics { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 295 | named!(parse -> Self, map!( |
| 296 | alt!( |
| 297 | do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 298 | lt: punct!(<) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 299 | lifetimes: call!(Punctuated::<LifetimeDef, Token![,]>::parse_terminated) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 300 | ty_params: cond!( |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 301 | lifetimes.empty_or_trailing(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 302 | Punctuated::<TypeParam, Token![,]>::parse_terminated |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 303 | ) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 304 | gt: punct!(>) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 305 | (lifetimes, ty_params, Some(lt), Some(gt)) |
| 306 | ) |
| 307 | | |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 308 | epsilon!() => { |_| (Punctuated::new(), None, None, None) } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 309 | ), |
David Tolnay | bc7d7d9 | 2017-06-03 20:54:05 -0700 | [diff] [blame] | 310 | |(lifetimes, ty_params, lt, gt)| Generics { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 311 | lt_token: lt, |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 312 | params: lifetimes.into_pairs() |
| 313 | .map(Pair::into_tuple) |
| 314 | .map(|(life, comma)| Pair::new(GenericParam::Lifetime(life), comma)) |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 315 | .chain(ty_params.unwrap_or_default() |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 316 | .into_pairs() |
| 317 | .map(Pair::into_tuple) |
| 318 | .map(|(ty, comma)| Pair::new(GenericParam::Type(ty), comma))) |
David Tolnay | 660fd1f | 2017-12-31 01:52:57 -0500 | [diff] [blame] | 319 | .collect(), |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 320 | gt_token: gt, |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 321 | where_clause: None, |
Michael Layzell | 416724e | 2017-05-24 21:12:34 -0400 | [diff] [blame] | 322 | } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 323 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 324 | |
| 325 | fn description() -> Option<&'static str> { |
| 326 | Some("generic parameters in declaration") |
| 327 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 330 | impl Synom for LifetimeDef { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 331 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 332 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 333 | life: syn!(Lifetime) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 334 | colon: option!(punct!(:)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 335 | bounds: cond!( |
| 336 | colon.is_some(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 337 | Punctuated::parse_separated_nonempty |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 338 | ) >> |
| 339 | (LifetimeDef { |
| 340 | attrs: attrs, |
| 341 | lifetime: life, |
| 342 | bounds: bounds.unwrap_or_default(), |
David Tolnay | 6af8f1d | 2017-12-27 23:08:43 -0500 | [diff] [blame] | 343 | colon_token: colon, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 344 | }) |
| 345 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 346 | |
| 347 | fn description() -> Option<&'static str> { |
| 348 | Some("lifetime definition") |
| 349 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | impl Synom for BoundLifetimes { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 353 | named!(parse -> Self, do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 354 | for_: keyword!(for) >> |
| 355 | lt: punct!(<) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 356 | lifetimes: call!(Punctuated::parse_terminated) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 357 | gt: punct!(>) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 358 | (BoundLifetimes { |
| 359 | for_token: for_, |
| 360 | lt_token: lt, |
| 361 | gt_token: gt, |
| 362 | lifetimes: lifetimes, |
| 363 | }) |
| 364 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 365 | |
| 366 | fn description() -> Option<&'static str> { |
| 367 | Some("bound lifetimes") |
| 368 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 369 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 370 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 371 | impl Synom for TypeParam { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 372 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 373 | attrs: many0!(Attribute::parse_outer) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 374 | id: syn!(Ident) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 375 | colon: option!(punct!(:)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 376 | bounds: cond!( |
| 377 | colon.is_some(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 378 | Punctuated::parse_separated_nonempty |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 379 | ) >> |
| 380 | default: option!(do_parse!( |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 381 | eq: punct!(=) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 382 | ty: syn!(Type) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 383 | (eq, ty) |
| 384 | )) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 385 | (TypeParam { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 386 | attrs: attrs, |
| 387 | ident: id, |
| 388 | bounds: bounds.unwrap_or_default(), |
| 389 | colon_token: colon, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 390 | eq_token: default.as_ref().map(|d| Token.0)), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 391 | default: default.map(|d| d.1), |
| 392 | }) |
| 393 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 394 | |
| 395 | fn description() -> Option<&'static str> { |
| 396 | Some("type parameter") |
| 397 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 398 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 399 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 400 | impl Synom for TypeParamBound { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 401 | named!(parse -> Self, alt!( |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 402 | syn!(Lifetime) => { TypeParamBound::Lifetime } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 403 | | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 404 | syn!(TraitBound) => { TypeParamBound::Trait } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 405 | | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 406 | parens!(syn!(TraitBound)) => { |bound| TypeParamBound::Trait(bound.1) } |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 407 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 408 | |
| 409 | fn description() -> Option<&'static str> { |
Nika Layzell | c691cb4 | 2017-12-04 13:44:38 -0500 | [diff] [blame] | 410 | Some("type parameter bound") |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 414 | impl Synom for TraitBound { |
| 415 | named!(parse -> Self, do_parse!( |
| 416 | modifier: syn!(TraitBoundModifier) >> |
| 417 | lifetimes: option!(syn!(BoundLifetimes)) >> |
| 418 | mut path: syn!(Path) >> |
| 419 | parenthesized: option!(cond_reduce!( |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 420 | path.segments.last().unwrap().value().arguments.is_empty(), |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 421 | syn!(ParenthesizedGenericArguments) |
| 422 | )) >> |
| 423 | ({ |
| 424 | if let Some(parenthesized) = parenthesized { |
| 425 | let parenthesized = PathArguments::Parenthesized(parenthesized); |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 426 | path.segments.last_mut().unwrap().value_mut().arguments = parenthesized; |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 427 | } |
| 428 | TraitBound { |
| 429 | modifier: modifier, |
| 430 | lifetimes: lifetimes, |
| 431 | path: path, |
| 432 | } |
| 433 | }) |
| 434 | )); |
| 435 | |
| 436 | fn description() -> Option<&'static str> { |
| 437 | Some("trait bound") |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | impl Synom for TraitBoundModifier { |
| 442 | named!(parse -> Self, alt!( |
| 443 | punct!(?) => { TraitBoundModifier::Maybe } |
| 444 | | |
| 445 | epsilon!() => { |_| TraitBoundModifier::None } |
| 446 | )); |
| 447 | |
| 448 | fn description() -> Option<&'static str> { |
| 449 | Some("trait bound modifier") |
| 450 | } |
| 451 | } |
| 452 | |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 453 | impl Synom for ConstParam { |
| 454 | named!(parse -> Self, do_parse!( |
David Tolnay | 2c13645 | 2017-12-27 14:13:32 -0500 | [diff] [blame] | 455 | attrs: many0!(Attribute::parse_outer) >> |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 456 | const_: keyword!(const) >> |
| 457 | ident: syn!(Ident) >> |
| 458 | colon: punct!(:) >> |
| 459 | ty: syn!(Type) >> |
| 460 | eq_def: option!(tuple!(punct!(=), syn!(Expr))) >> |
David Tolnay | 78ee520 | 2017-12-04 22:17:54 -0800 | [diff] [blame] | 461 | ({ |
| 462 | let (eq_token, default) = match eq_def { |
| 463 | Some((eq_token, default)) => (Some(eq_token), Some(default)), |
| 464 | None => (None, None), |
| 465 | }; |
| 466 | ConstParam { |
| 467 | attrs: attrs, |
| 468 | const_token: const_, |
| 469 | ident: ident, |
| 470 | colon_token: colon, |
| 471 | ty: ty, |
| 472 | eq_token: eq_token, |
| 473 | default: default, |
| 474 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 475 | }) |
| 476 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 477 | |
| 478 | fn description() -> Option<&'static str> { |
| 479 | Some("generic `const` parameter") |
| 480 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 481 | } |
| 482 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 483 | impl Synom for WhereClause { |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 484 | named!(parse -> Self, do_parse!( |
| 485 | where_: keyword!(where) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 486 | predicates: call!(Punctuated::parse_terminated) >> |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 487 | (WhereClause { |
| 488 | predicates: predicates, |
| 489 | where_token: where_, |
| 490 | }) |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 491 | )); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 492 | |
| 493 | fn description() -> Option<&'static str> { |
| 494 | Some("where clause") |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | impl Synom for WherePredicate { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 499 | named!(parse -> Self, alt!( |
| 500 | do_parse!( |
| 501 | ident: syn!(Lifetime) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 502 | colon: option!(punct!(:)) >> |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 503 | bounds: cond!( |
| 504 | colon.is_some(), |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 505 | Punctuated::parse_separated |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 506 | ) >> |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 507 | (WherePredicate::Lifetime(PredicateLifetime { |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 508 | lifetime: ident, |
| 509 | bounds: bounds.unwrap_or_default(), |
| 510 | colon_token: colon, |
| 511 | })) |
| 512 | ) |
| 513 | | |
| 514 | do_parse!( |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 515 | lifetimes: option!(syn!(BoundLifetimes)) >> |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 516 | bounded_ty: syn!(Type) >> |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 517 | colon: punct!(:) >> |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 518 | bounds: call!(Punctuated::parse_separated_nonempty) >> |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 519 | (WherePredicate::Type(PredicateType { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 520 | lifetimes: lifetimes, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 521 | bounded_ty: bounded_ty, |
| 522 | bounds: bounds, |
| 523 | colon_token: colon, |
| 524 | })) |
| 525 | ) |
| 526 | )); |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 527 | |
| 528 | fn description() -> Option<&'static str> { |
| 529 | Some("predicate in where clause") |
| 530 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 531 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 532 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 533 | |
| 534 | #[cfg(feature = "printing")] |
| 535 | mod printing { |
| 536 | use super::*; |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 537 | use attr::FilterAttrs; |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 538 | use quote::{ToTokens, Tokens}; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 539 | |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 540 | /// Returns true if the generics object has no lifetimes or ty_params. |
| 541 | fn empty_normal_generics(generics: &Generics) -> bool { |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 542 | generics.params.is_empty() |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 543 | } |
| 544 | |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 545 | impl ToTokens for Generics { |
| 546 | fn to_tokens(&self, tokens: &mut Tokens) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 547 | if empty_normal_generics(self) { |
| 548 | return; |
| 549 | } |
| 550 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 551 | TokensOrDefault(&self.lt_token).to_tokens(tokens); |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 552 | self.params.to_tokens(tokens); |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 553 | TokensOrDefault(&self.gt_token).to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 557 | impl<'a> ToTokens for ImplGenerics<'a> { |
| 558 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 559 | if empty_normal_generics(self.0) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 563 | TokensOrDefault(&self.0.lt_token).to_tokens(tokens); |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 564 | for param in self.0.params.pairs() { |
| 565 | match **param.value() { |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 566 | GenericParam::Lifetime(ref param) => { |
| 567 | param.to_tokens(tokens); |
| 568 | } |
| 569 | GenericParam::Type(ref param) => { |
| 570 | // Leave off the type parameter defaults |
| 571 | tokens.append_all(param.attrs.outer()); |
| 572 | param.ident.to_tokens(tokens); |
| 573 | if !param.bounds.is_empty() { |
| 574 | TokensOrDefault(¶m.colon_token).to_tokens(tokens); |
| 575 | param.bounds.to_tokens(tokens); |
| 576 | } |
| 577 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 578 | GenericParam::Const(ref param) => { |
| 579 | // Leave off the const parameter defaults |
| 580 | tokens.append_all(param.attrs.outer()); |
| 581 | param.const_token.to_tokens(tokens); |
| 582 | param.ident.to_tokens(tokens); |
| 583 | param.colon_token.to_tokens(tokens); |
| 584 | param.ty.to_tokens(tokens); |
| 585 | } |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 586 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 587 | param.punct().to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 588 | } |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 589 | TokensOrDefault(&self.0.gt_token).to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 593 | impl<'a> ToTokens for TypeGenerics<'a> { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 594 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 595 | if empty_normal_generics(self.0) { |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 599 | TokensOrDefault(&self.0.lt_token).to_tokens(tokens); |
David Tolnay | 5608068 | 2018-01-06 14:01:52 -0800 | [diff] [blame] | 600 | for param in self.0.params.pairs() { |
| 601 | match **param.value() { |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 602 | GenericParam::Lifetime(ref param) => { |
| 603 | // Leave off the lifetime bounds and attributes |
| 604 | param.lifetime.to_tokens(tokens); |
| 605 | } |
| 606 | GenericParam::Type(ref param) => { |
| 607 | // Leave off the type parameter defaults |
| 608 | param.ident.to_tokens(tokens); |
| 609 | } |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 610 | GenericParam::Const(ref param) => { |
| 611 | // Leave off the const parameter defaults |
| 612 | param.ident.to_tokens(tokens); |
| 613 | } |
David Tolnay | c2f1aba | 2017-11-12 20:29:22 -0800 | [diff] [blame] | 614 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 615 | param.punct().to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 616 | } |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 617 | TokensOrDefault(&self.0.gt_token).to_tokens(tokens); |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 621 | impl<'a> ToTokens for Turbofish<'a> { |
| 622 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | bb4ca9f | 2017-12-26 12:28:58 -0500 | [diff] [blame] | 623 | if !empty_normal_generics(self.0) { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 624 | <Token![::]>::default().to_tokens(tokens); |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 625 | TypeGenerics(self.0).to_tokens(tokens); |
David Tolnay | c879a50 | 2017-01-25 15:51:32 -0800 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 630 | impl ToTokens for BoundLifetimes { |
| 631 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 632 | self.for_token.to_tokens(tokens); |
| 633 | self.lt_token.to_tokens(tokens); |
| 634 | self.lifetimes.to_tokens(tokens); |
| 635 | self.gt_token.to_tokens(tokens); |
| 636 | } |
| 637 | } |
| 638 | |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 639 | impl ToTokens for LifetimeDef { |
| 640 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 641 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 642 | self.lifetime.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 643 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 644 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 645 | self.bounds.to_tokens(tokens); |
| 646 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 650 | impl ToTokens for TypeParam { |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 651 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | e767892 | 2016-10-13 20:44:03 -0700 | [diff] [blame] | 652 | tokens.append_all(self.attrs.outer()); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 653 | self.ident.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 654 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 655 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 656 | self.bounds.to_tokens(tokens); |
| 657 | } |
| 658 | if self.default.is_some() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 659 | TokensOrDefault(&self.eq_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 660 | self.default.to_tokens(tokens); |
| 661 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 665 | impl ToTokens for TraitBound { |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 666 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 667 | self.modifier.to_tokens(tokens); |
| 668 | self.lifetimes.to_tokens(tokens); |
| 669 | self.path.to_tokens(tokens); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 673 | impl ToTokens for TraitBoundModifier { |
| 674 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 675 | match *self { |
| 676 | TraitBoundModifier::None => {} |
| 677 | TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens), |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 682 | impl ToTokens for ConstParam { |
| 683 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 684 | tokens.append_all(self.attrs.outer()); |
| 685 | self.const_token.to_tokens(tokens); |
| 686 | self.ident.to_tokens(tokens); |
| 687 | self.colon_token.to_tokens(tokens); |
| 688 | self.ty.to_tokens(tokens); |
| 689 | if self.default.is_some() { |
David Tolnay | 78ee520 | 2017-12-04 22:17:54 -0800 | [diff] [blame] | 690 | TokensOrDefault(&self.eq_token).to_tokens(tokens); |
Nika Layzell | f1fdc0b | 2017-12-04 19:58:32 -0500 | [diff] [blame] | 691 | self.default.to_tokens(tokens); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 696 | impl ToTokens for WhereClause { |
| 697 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | ac997dd | 2017-12-27 23:18:22 -0500 | [diff] [blame] | 698 | self.where_token.to_tokens(tokens); |
| 699 | self.predicates.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 702 | |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 703 | impl ToTokens for PredicateType { |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 704 | fn to_tokens(&self, tokens: &mut Tokens) { |
David Tolnay | 40fb8ce | 2018-01-02 10:53:46 -0800 | [diff] [blame] | 705 | self.lifetimes.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 706 | self.bounded_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 707 | self.colon_token.to_tokens(tokens); |
| 708 | self.bounds.to_tokens(tokens); |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 712 | impl ToTokens for PredicateLifetime { |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 713 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 714 | self.lifetime.to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 715 | if !self.bounds.is_empty() { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 716 | TokensOrDefault(&self.colon_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 717 | self.bounds.to_tokens(tokens); |
| 718 | } |
David Tolnay | 8ef9304 | 2016-09-04 14:08:40 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 721 | |
David Tolnay | d4add85 | 2018-01-01 20:13:24 -0800 | [diff] [blame] | 722 | impl ToTokens for PredicateEq { |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 723 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 724 | self.lhs_ty.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 725 | self.eq_token.to_tokens(tokens); |
David Tolnay | f8e0883 | 2017-01-23 00:04:32 -0800 | [diff] [blame] | 726 | self.rhs_ty.to_tokens(tokens); |
| 727 | } |
| 728 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 729 | } |