David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 2 | use proc_macro2::TokenStream; |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 3 | use punctuated::Punctuated; |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 4 | #[cfg(feature = "extra-traits")] |
| 5 | use std::hash::{Hash, Hasher}; |
| 6 | #[cfg(feature = "extra-traits")] |
David Tolnay | c43b44e | 2017-12-30 23:55:54 -0500 | [diff] [blame] | 7 | use tt::TokenStreamHelper; |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 8 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 9 | ast_enum_of_structs! { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 10 | /// The possible types that a Rust value could have. |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 11 | /// |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 12 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 13 | /// feature.* |
| 14 | /// |
David Tolnay | 614a014 | 2018-01-07 10:25:43 -0800 | [diff] [blame] | 15 | /// # Syntax tree enum |
| 16 | /// |
| 17 | /// This type is a [syntax tree enum]. |
| 18 | /// |
| 19 | /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 20 | pub enum Type { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 21 | /// A dynamically sized slice type: `[T]`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 22 | /// |
| 23 | /// *This type is available if Syn is built with the `"derive"` or |
| 24 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 25 | pub Slice(TypeSlice { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 26 | pub bracket_token: token::Bracket, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 27 | pub elem: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 28 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 29 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 30 | /// A fixed size array type: `[T; n]`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 31 | /// |
| 32 | /// *This type is available if Syn is built with the `"derive"` or |
| 33 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 34 | pub Array(TypeArray { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 35 | pub bracket_token: token::Bracket, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 36 | pub elem: Box<Type>, |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 37 | pub semi_token: Token![;], |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 38 | pub len: Expr, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 39 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 40 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 41 | /// A raw pointer type: `*const T` or `*mut T`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 42 | /// |
| 43 | /// *This type is available if Syn is built with the `"derive"` or |
| 44 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 45 | pub Ptr(TypePtr { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 46 | pub star_token: Token![*], |
| 47 | pub const_token: Option<Token![const]>, |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 48 | pub mutability: Option<Token![mut]>, |
| 49 | pub elem: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 50 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 51 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 52 | /// A reference type: `&'a T` or `&'a mut T`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 53 | /// |
| 54 | /// *This type is available if Syn is built with the `"derive"` or |
| 55 | /// `"full"` feature.* |
David Tolnay | 0a89b4d | 2017-11-13 00:55:45 -0800 | [diff] [blame] | 56 | pub Reference(TypeReference { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 57 | pub and_token: Token![&], |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 58 | pub lifetime: Option<Lifetime>, |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 59 | pub mutability: Option<Token![mut]>, |
| 60 | pub elem: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 61 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 62 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 63 | /// A bare function type: `fn(usize) -> bool`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 64 | /// |
| 65 | /// *This type is available if Syn is built with the `"derive"` or |
| 66 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 67 | pub BareFn(TypeBareFn { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 68 | pub lifetimes: Option<BoundLifetimes>, |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 69 | pub unsafety: Option<Token![unsafe]>, |
| 70 | pub abi: Option<Abi>, |
| 71 | pub fn_token: Token![fn], |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 72 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 73 | pub inputs: Punctuated<BareFnArg, Token![,]>, |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 74 | pub variadic: Option<Token![...]>, |
| 75 | pub output: ReturnType, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 76 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 77 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 78 | /// The never type: `!`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 79 | /// |
| 80 | /// *This type is available if Syn is built with the `"derive"` or |
| 81 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 82 | pub Never(TypeNever { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 83 | pub bang_token: Token![!], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 84 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 85 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 86 | /// A tuple type: `(A, B, C, String)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 87 | /// |
| 88 | /// *This type is available if Syn is built with the `"derive"` or |
| 89 | /// `"full"` feature.* |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 90 | pub Tuple(TypeTuple { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 91 | pub paren_token: token::Paren, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 92 | pub elems: Punctuated<Type, Token![,]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 93 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 94 | |
| 95 | /// A path like `std::slice::Iter`, optionally qualified with a |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 96 | /// self-type as in `<Vec<T> as SomeTrait>::Associated`. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 97 | /// |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 98 | /// Type arguments are stored in the Path itself. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 99 | /// |
| 100 | /// *This type is available if Syn is built with the `"derive"` or |
| 101 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 102 | pub Path(TypePath { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 103 | pub qself: Option<QSelf>, |
| 104 | pub path: Path, |
| 105 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 106 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 107 | /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a |
| 108 | /// trait or a lifetime. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 109 | /// |
| 110 | /// *This type is available if Syn is built with the `"derive"` or |
| 111 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 112 | pub TraitObject(TypeTraitObject { |
David Tolnay | e45b59f | 2017-12-25 18:44:49 -0500 | [diff] [blame] | 113 | pub dyn_token: Option<Token![dyn]>, |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 114 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 115 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 116 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 117 | /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or |
| 118 | /// a lifetime. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 119 | /// |
| 120 | /// *This type is available if Syn is built with the `"derive"` or |
| 121 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 122 | pub ImplTrait(TypeImplTrait { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 123 | pub impl_token: Token![impl], |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 124 | pub bounds: Punctuated<TypeParamBound, Token![+]>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 125 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 126 | |
| 127 | /// A parenthesized type equivalent to the inner type. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 128 | /// |
| 129 | /// *This type is available if Syn is built with the `"derive"` or |
| 130 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 131 | pub Paren(TypeParen { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 132 | pub paren_token: token::Paren, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 133 | pub elem: Box<Type>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 134 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 135 | |
| 136 | /// A type contained within invisible delimiters. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 137 | /// |
| 138 | /// *This type is available if Syn is built with the `"derive"` or |
| 139 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 140 | pub Group(TypeGroup { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 141 | pub group_token: token::Group, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 142 | pub elem: Box<Type>, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 143 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 144 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 145 | /// Indication that a type should be inferred by the compiler: `_`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 146 | /// |
| 147 | /// *This type is available if Syn is built with the `"derive"` or |
| 148 | /// `"full"` feature.* |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 149 | pub Infer(TypeInfer { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 150 | pub underscore_token: Token![_], |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 151 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 152 | |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 153 | /// A macro in the type position. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 154 | /// |
| 155 | /// *This type is available if Syn is built with the `"derive"` or |
| 156 | /// `"full"` feature.* |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 157 | pub Macro(TypeMacro { |
| 158 | pub mac: Macro, |
| 159 | }), |
David Tolnay | a454c8f | 2018-01-07 01:01:10 -0800 | [diff] [blame] | 160 | |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 161 | /// Tokens in type position not interpreted by Syn. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 162 | /// |
| 163 | /// *This type is available if Syn is built with the `"derive"` or |
| 164 | /// `"full"` feature.* |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 165 | pub Verbatim(TypeVerbatim #manual_extra_traits { |
| 166 | pub tts: TokenStream, |
| 167 | }), |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | #[cfg(feature = "extra-traits")] |
| 172 | impl Eq for TypeVerbatim {} |
| 173 | |
| 174 | #[cfg(feature = "extra-traits")] |
| 175 | impl PartialEq for TypeVerbatim { |
| 176 | fn eq(&self, other: &Self) -> bool { |
| 177 | TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | #[cfg(feature = "extra-traits")] |
| 182 | impl Hash for TypeVerbatim { |
| 183 | fn hash<H>(&self, state: &mut H) |
| 184 | where |
| 185 | H: Hasher, |
| 186 | { |
| 187 | TokenStreamHelper(&self.tts).hash(state); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | ast_struct! { |
David Tolnay | f01e37b | 2018-01-06 23:38:26 -0800 | [diff] [blame] | 192 | /// The binary interface of a function: `extern "C"`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 193 | /// |
| 194 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 195 | /// feature.* |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 196 | pub struct Abi { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 197 | pub extern_token: Token![extern], |
David Tolnay | f01e37b | 2018-01-06 23:38:26 -0800 | [diff] [blame] | 198 | pub name: Option<LitStr>, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | ast_struct! { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 203 | /// An argument in a function type: the `usize` in `fn(usize) -> bool`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 204 | /// |
| 205 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 206 | /// feature.* |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 207 | pub struct BareFnArg { |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 208 | pub name: Option<(BareFnArgName, Token![:])>, |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 209 | pub ty: Type, |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 213 | ast_enum! { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 214 | /// Name of an argument in a function type: the `n` in `fn(n: usize)`. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 215 | /// |
| 216 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 217 | /// feature.* |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 218 | pub enum BareFnArgName { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 219 | /// Argument given a name. |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 220 | Named(Ident), |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 221 | /// Argument not given a name, matched with `_`. |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 222 | Wild(Token![_]), |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 225 | |
| 226 | ast_enum! { |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 227 | /// Return type of a function signature. |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 228 | /// |
| 229 | /// *This type is available if Syn is built with the `"derive"` or `"full"` |
| 230 | /// feature.* |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 231 | pub enum ReturnType { |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 232 | /// Return type is not specified. |
| 233 | /// |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 234 | /// Functions default to `()` and closures default to type inference. |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 235 | Default, |
David Tolnay | 7949ae2 | 2018-01-07 00:14:51 -0800 | [diff] [blame] | 236 | /// A particular type is returned. |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 237 | Type(Token![->], Box<Type>), |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 238 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 239 | } |
| 240 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 241 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 242 | pub mod parsing { |
| 243 | use super::*; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 244 | |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 245 | use parse::{Parse, ParseStream, Result}; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 246 | use path; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 247 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 248 | impl Parse for Type { |
| 249 | fn parse(input: ParseStream) -> Result<Self> { |
| 250 | ambig_ty(input, true) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
David Tolnay | 0047c71 | 2016-12-21 21:59:25 -0500 | [diff] [blame] | 253 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 254 | impl Type { |
Michael Layzell | 9bf2b00 | 2017-06-04 18:49:53 -0400 | [diff] [blame] | 255 | /// In some positions, types may not contain the `+` character, to |
| 256 | /// disambiguate them. For example in the expression `1 as T`, T may not |
| 257 | /// contain a `+` character. |
| 258 | /// |
| 259 | /// This parser does not allow a `+`, while the default parser does. |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 260 | pub fn without_plus(input: ParseStream) -> Result<Self> { |
| 261 | ambig_ty(input, false) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 262 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 263 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 264 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 265 | fn ambig_ty(input: ParseStream, allow_plus: bool) -> Result<Type> { |
| 266 | if input.peek(token::Group) { |
| 267 | return input.parse().map(Type::Group); |
| 268 | } |
| 269 | |
| 270 | let mut lifetimes = None::<BoundLifetimes>; |
| 271 | let mut lookahead = input.lookahead1(); |
| 272 | if lookahead.peek(Token![for]) { |
| 273 | lifetimes = input.parse()?; |
| 274 | lookahead = input.lookahead1(); |
| 275 | if !lookahead.peek(Ident) |
| 276 | && !lookahead.peek(Token![fn]) |
| 277 | && !lookahead.peek(Token![unsafe]) |
| 278 | && !lookahead.peek(Token![extern]) |
| 279 | && !lookahead.peek(Token![super]) |
| 280 | && !lookahead.peek(Token![self]) |
| 281 | && !lookahead.peek(Token![Self]) |
| 282 | && !lookahead.peek(Token![crate]) |
| 283 | { |
| 284 | return Err(lookahead.error()); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if lookahead.peek(token::Paren) { |
| 289 | let content; |
| 290 | let paren_token = parenthesized!(content in input); |
| 291 | if content.is_empty() { |
| 292 | return Ok(Type::Tuple(TypeTuple { |
| 293 | paren_token: paren_token, |
| 294 | elems: Punctuated::new(), |
| 295 | })); |
| 296 | } |
| 297 | if content.peek(Lifetime) { |
| 298 | return Ok(Type::Paren(TypeParen { |
| 299 | paren_token: paren_token, |
| 300 | elem: Box::new(Type::TraitObject(content.parse()?)), |
| 301 | })); |
| 302 | } |
David Tolnay | 5f7a91c | 2018-10-27 22:22:58 -0700 | [diff] [blame] | 303 | if content.peek(Token![?]) { |
| 304 | return Ok(Type::TraitObject(TypeTraitObject { |
| 305 | dyn_token: None, |
| 306 | bounds: { |
| 307 | let mut bounds = Punctuated::new(); |
David Tolnay | 7fd4185 | 2018-10-27 23:04:25 -0700 | [diff] [blame] | 308 | bounds.push_value(TypeParamBound::Trait(TraitBound { |
| 309 | paren_token: Some(paren_token), |
| 310 | ..content.parse()? |
| 311 | })); |
David Tolnay | 5f7a91c | 2018-10-27 22:22:58 -0700 | [diff] [blame] | 312 | while let Some(plus) = input.parse()? { |
| 313 | bounds.push_punct(plus); |
| 314 | bounds.push_value(input.parse()?); |
| 315 | } |
| 316 | bounds |
| 317 | }, |
| 318 | })); |
| 319 | } |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 320 | let first: Type = content.parse()?; |
| 321 | if content.peek(Token![,]) { |
David Tolnay | 4ace1c7 | 2018-10-27 21:49:05 -0700 | [diff] [blame] | 322 | return Ok(Type::Tuple(TypeTuple { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 323 | paren_token: paren_token, |
| 324 | elems: { |
| 325 | let mut elems = Punctuated::new(); |
| 326 | elems.push_value(first); |
| 327 | elems.push_punct(content.parse()?); |
| 328 | let rest: Punctuated<Type, Token![,]> = |
| 329 | content.parse_terminated(Parse::parse)?; |
| 330 | elems.extend(rest); |
| 331 | elems |
| 332 | }, |
David Tolnay | 4ace1c7 | 2018-10-27 21:49:05 -0700 | [diff] [blame] | 333 | })); |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 334 | } |
David Tolnay | 4ace1c7 | 2018-10-27 21:49:05 -0700 | [diff] [blame] | 335 | if allow_plus && input.peek(Token![+]) { |
David Tolnay | 4df5851 | 2018-10-27 22:34:36 -0700 | [diff] [blame] | 336 | loop { |
| 337 | let first = match first { |
| 338 | Type::Path(TypePath { qself: None, path }) => { |
| 339 | TypeParamBound::Trait(TraitBound { |
David Tolnay | 4ace1c7 | 2018-10-27 21:49:05 -0700 | [diff] [blame] | 340 | paren_token: Some(paren_token), |
| 341 | modifier: TraitBoundModifier::None, |
| 342 | lifetimes: None, |
| 343 | path: path, |
David Tolnay | 4df5851 | 2018-10-27 22:34:36 -0700 | [diff] [blame] | 344 | }) |
| 345 | } |
David Tolnay | e614f28 | 2018-10-27 22:50:12 -0700 | [diff] [blame] | 346 | Type::TraitObject(TypeTraitObject { |
| 347 | dyn_token: None, |
| 348 | ref bounds, |
| 349 | }) => { |
David Tolnay | 4df5851 | 2018-10-27 22:34:36 -0700 | [diff] [blame] | 350 | if bounds.len() > 1 || bounds.trailing_punct() { |
| 351 | break; |
| 352 | } |
| 353 | match first { |
| 354 | Type::TraitObject(TypeTraitObject { bounds, .. }) => { |
David Tolnay | 7fd4185 | 2018-10-27 23:04:25 -0700 | [diff] [blame] | 355 | match bounds.into_iter().next().unwrap() { |
| 356 | TypeParamBound::Trait(trait_bound) => { |
| 357 | TypeParamBound::Trait(TraitBound { |
| 358 | paren_token: Some(paren_token), |
| 359 | ..trait_bound |
| 360 | }) |
| 361 | } |
| 362 | other => other, |
| 363 | } |
David Tolnay | 4df5851 | 2018-10-27 22:34:36 -0700 | [diff] [blame] | 364 | } |
| 365 | _ => unreachable!(), |
| 366 | } |
| 367 | } |
| 368 | _ => break, |
| 369 | }; |
| 370 | return Ok(Type::TraitObject(TypeTraitObject { |
| 371 | dyn_token: None, |
| 372 | bounds: { |
| 373 | let mut bounds = Punctuated::new(); |
| 374 | bounds.push_value(first); |
David Tolnay | 4ace1c7 | 2018-10-27 21:49:05 -0700 | [diff] [blame] | 375 | while let Some(plus) = input.parse()? { |
| 376 | bounds.push_punct(plus); |
| 377 | bounds.push_value(input.parse()?); |
| 378 | } |
| 379 | bounds |
| 380 | }, |
| 381 | })); |
| 382 | } |
| 383 | } |
| 384 | Ok(Type::Paren(TypeParen { |
| 385 | paren_token: paren_token, |
| 386 | elem: Box::new(first), |
| 387 | })) |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 388 | } else if lookahead.peek(Token![fn]) |
| 389 | || lookahead.peek(Token![unsafe]) |
| 390 | || lookahead.peek(Token![extern]) && !input.peek2(Token![::]) |
| 391 | { |
| 392 | let mut bare_fn: TypeBareFn = input.parse()?; |
| 393 | bare_fn.lifetimes = lifetimes; |
| 394 | Ok(Type::BareFn(bare_fn)) |
| 395 | } else if lookahead.peek(Ident) |
| 396 | || input.peek(Token![super]) |
| 397 | || input.peek(Token![self]) |
| 398 | || input.peek(Token![Self]) |
| 399 | || input.peek(Token![crate]) |
| 400 | || input.peek(Token![extern]) |
| 401 | || lookahead.peek(Token![::]) |
| 402 | || lookahead.peek(Token![<]) |
| 403 | { |
| 404 | if input.peek(Token![dyn]) { |
| 405 | let mut trait_object: TypeTraitObject = input.parse()?; |
| 406 | if lifetimes.is_some() { |
| 407 | match *trait_object.bounds.iter_mut().next().unwrap() { |
| 408 | TypeParamBound::Trait(ref mut trait_bound) => { |
| 409 | trait_bound.lifetimes = lifetimes; |
| 410 | } |
| 411 | TypeParamBound::Lifetime(_) => unreachable!(), |
| 412 | } |
| 413 | } |
| 414 | return Ok(Type::TraitObject(trait_object)); |
| 415 | } |
| 416 | |
| 417 | let ty: TypePath = input.parse()?; |
| 418 | if ty.qself.is_some() { |
| 419 | return Ok(Type::Path(ty)); |
| 420 | } |
| 421 | |
| 422 | if input.peek(Token![!]) && !input.peek(Token![!=]) { |
| 423 | let mut contains_arguments = false; |
| 424 | for segment in &ty.path.segments { |
| 425 | match segment.arguments { |
| 426 | PathArguments::None => {} |
| 427 | PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => { |
| 428 | contains_arguments = true; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if !contains_arguments { |
| 434 | let bang_token: Token![!] = input.parse()?; |
| 435 | let (delimiter, tts) = mac::parse_delimiter(input)?; |
| 436 | return Ok(Type::Macro(TypeMacro { |
| 437 | mac: Macro { |
| 438 | path: ty.path, |
| 439 | bang_token: bang_token, |
| 440 | delimiter: delimiter, |
| 441 | tts: tts, |
| 442 | }, |
| 443 | })); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if lifetimes.is_some() || allow_plus && input.peek(Token![+]) { |
| 448 | let mut bounds = Punctuated::new(); |
| 449 | bounds.push_value(TypeParamBound::Trait(TraitBound { |
| 450 | paren_token: None, |
| 451 | modifier: TraitBoundModifier::None, |
| 452 | lifetimes: lifetimes, |
| 453 | path: ty.path, |
| 454 | })); |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 455 | if allow_plus { |
| 456 | while input.peek(Token![+]) { |
| 457 | bounds.push_punct(input.parse()?); |
David Tolnay | 1b210d1 | 2018-10-27 22:07:00 -0700 | [diff] [blame] | 458 | if input.peek(Token![>]) { |
| 459 | break; |
| 460 | } |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 461 | bounds.push_value(input.parse()?); |
| 462 | } |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 463 | } |
| 464 | return Ok(Type::TraitObject(TypeTraitObject { |
| 465 | dyn_token: None, |
| 466 | bounds: bounds, |
| 467 | })); |
| 468 | } |
| 469 | |
| 470 | Ok(Type::Path(ty)) |
| 471 | } else if lookahead.peek(token::Bracket) { |
| 472 | let content; |
| 473 | let bracket_token = bracketed!(content in input); |
| 474 | let elem: Type = content.parse()?; |
| 475 | if content.peek(Token![;]) { |
| 476 | Ok(Type::Array(TypeArray { |
| 477 | bracket_token: bracket_token, |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 478 | elem: Box::new(elem), |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 479 | semi_token: content.parse()?, |
David Tolnay | 9389c38 | 2018-08-27 09:13:37 -0700 | [diff] [blame] | 480 | len: content.parse()?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 481 | })) |
| 482 | } else { |
| 483 | Ok(Type::Slice(TypeSlice { |
| 484 | bracket_token: bracket_token, |
| 485 | elem: Box::new(elem), |
| 486 | })) |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 487 | } |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 488 | } else if lookahead.peek(Token![*]) { |
| 489 | input.parse().map(Type::Ptr) |
| 490 | } else if lookahead.peek(Token![&]) { |
| 491 | input.parse().map(Type::Reference) |
| 492 | } else if lookahead.peek(Token![!]) && !input.peek(Token![=]) { |
| 493 | input.parse().map(Type::Never) |
David Tolnay | 2beee04 | 2019-04-03 08:36:59 -0700 | [diff] [blame] | 494 | } else if lookahead.peek(Token![impl]) { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 495 | input.parse().map(Type::ImplTrait) |
| 496 | } else if lookahead.peek(Token![_]) { |
| 497 | input.parse().map(Type::Infer) |
| 498 | } else if lookahead.peek(Lifetime) { |
| 499 | input.parse().map(Type::TraitObject) |
| 500 | } else { |
| 501 | Err(lookahead.error()) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 502 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 503 | } |
David Tolnay | fa94b6f | 2016-10-05 23:26:11 -0700 | [diff] [blame] | 504 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 505 | impl Parse for TypeSlice { |
| 506 | fn parse(input: ParseStream) -> Result<Self> { |
| 507 | let content; |
| 508 | Ok(TypeSlice { |
| 509 | bracket_token: bracketed!(content in input), |
| 510 | elem: content.parse()?, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 511 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 512 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 513 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 514 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 515 | impl Parse for TypeArray { |
| 516 | fn parse(input: ParseStream) -> Result<Self> { |
| 517 | let content; |
| 518 | Ok(TypeArray { |
| 519 | bracket_token: bracketed!(content in input), |
| 520 | elem: content.parse()?, |
| 521 | semi_token: content.parse()?, |
David Tolnay | 9389c38 | 2018-08-27 09:13:37 -0700 | [diff] [blame] | 522 | len: content.parse()?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 523 | }) |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | impl Parse for TypePtr { |
| 528 | fn parse(input: ParseStream) -> Result<Self> { |
| 529 | let star_token: Token![*] = input.parse()?; |
| 530 | |
| 531 | let lookahead = input.lookahead1(); |
| 532 | let (const_token, mutability) = if lookahead.peek(Token![const]) { |
| 533 | (Some(input.parse()?), None) |
| 534 | } else if lookahead.peek(Token![mut]) { |
| 535 | (None, Some(input.parse()?)) |
| 536 | } else { |
| 537 | return Err(lookahead.error()); |
| 538 | }; |
| 539 | |
| 540 | Ok(TypePtr { |
| 541 | star_token: star_token, |
| 542 | const_token: const_token, |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 543 | mutability: mutability, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 544 | elem: Box::new(input.call(Type::without_plus)?), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 545 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 546 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 547 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 548 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 549 | impl Parse for TypeReference { |
| 550 | fn parse(input: ParseStream) -> Result<Self> { |
| 551 | Ok(TypeReference { |
| 552 | and_token: input.parse()?, |
| 553 | lifetime: input.parse()?, |
| 554 | mutability: input.parse()?, |
| 555 | // & binds tighter than +, so we don't allow + here. |
| 556 | elem: Box::new(input.call(Type::without_plus)?), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 557 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 558 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 559 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 560 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 561 | impl Parse for TypeBareFn { |
| 562 | fn parse(input: ParseStream) -> Result<Self> { |
| 563 | let args; |
| 564 | let allow_variadic; |
| 565 | Ok(TypeBareFn { |
| 566 | lifetimes: input.parse()?, |
| 567 | unsafety: input.parse()?, |
| 568 | abi: input.parse()?, |
| 569 | fn_token: input.parse()?, |
| 570 | paren_token: parenthesized!(args in input), |
| 571 | inputs: { |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 572 | let mut inputs = Punctuated::new(); |
| 573 | while !args.is_empty() && !args.peek(Token![...]) { |
| 574 | inputs.push_value(args.parse()?); |
| 575 | if args.is_empty() { |
| 576 | break; |
| 577 | } |
| 578 | inputs.push_punct(args.parse()?); |
| 579 | } |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 580 | allow_variadic = inputs.empty_or_trailing(); |
| 581 | inputs |
| 582 | }, |
| 583 | variadic: { |
| 584 | if allow_variadic && args.peek(Token![...]) { |
| 585 | Some(args.parse()?) |
| 586 | } else { |
| 587 | None |
| 588 | } |
| 589 | }, |
| 590 | output: input.call(ReturnType::without_plus)?, |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 591 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 592 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 593 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 594 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 595 | impl Parse for TypeNever { |
| 596 | fn parse(input: ParseStream) -> Result<Self> { |
| 597 | Ok(TypeNever { |
| 598 | bang_token: input.parse()?, |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 599 | }) |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | impl Parse for TypeInfer { |
| 604 | fn parse(input: ParseStream) -> Result<Self> { |
| 605 | Ok(TypeInfer { |
| 606 | underscore_token: input.parse()?, |
| 607 | }) |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | impl Parse for TypeTuple { |
| 612 | fn parse(input: ParseStream) -> Result<Self> { |
| 613 | let content; |
| 614 | Ok(TypeTuple { |
| 615 | paren_token: parenthesized!(content in input), |
David Tolnay | 60484fe | 2018-08-30 18:43:04 -0700 | [diff] [blame] | 616 | elems: content.parse_terminated(Type::parse)?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 617 | }) |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | impl Parse for TypeMacro { |
| 622 | fn parse(input: ParseStream) -> Result<Self> { |
| 623 | Ok(TypeMacro { |
| 624 | mac: input.parse()?, |
| 625 | }) |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | impl Parse for TypePath { |
| 630 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 631 | let (qself, mut path) = path::parsing::qpath(input, false)?; |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 632 | |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 633 | if path.segments.last().unwrap().value().arguments.is_empty() |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 634 | && input.peek(token::Paren) |
| 635 | { |
| 636 | let args: ParenthesizedGenericArguments = input.parse()?; |
David Tolnay | 6029108 | 2018-08-28 09:54:49 -0700 | [diff] [blame] | 637 | let parenthesized = PathArguments::Parenthesized(args); |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 638 | path.segments.last_mut().unwrap().value_mut().arguments = parenthesized; |
| 639 | } |
| 640 | |
| 641 | Ok(TypePath { |
| 642 | qself: qself, |
| 643 | path: path, |
| 644 | }) |
| 645 | } |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 646 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 647 | |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 648 | impl ReturnType { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 649 | pub fn without_plus(input: ParseStream) -> Result<Self> { |
| 650 | Self::parse(input, false) |
| 651 | } |
Geoffry Song | ac02b18 | 2018-05-19 22:11:31 -0700 | [diff] [blame] | 652 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 653 | pub fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> { |
| 654 | if input.peek(Token![->]) { |
| 655 | let arrow = input.parse()?; |
| 656 | let ty = ambig_ty(input, allow_plus)?; |
| 657 | Ok(ReturnType::Type(arrow, Box::new(ty))) |
| 658 | } else { |
| 659 | Ok(ReturnType::Default) |
| 660 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 661 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 662 | } |
| 663 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 664 | impl Parse for ReturnType { |
| 665 | fn parse(input: ParseStream) -> Result<Self> { |
| 666 | Self::parse(input, true) |
| 667 | } |
| 668 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 669 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 670 | impl Parse for TypeTraitObject { |
| 671 | fn parse(input: ParseStream) -> Result<Self> { |
| 672 | Self::parse(input, true) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 673 | } |
David Tolnay | 0a169d4 | 2017-12-29 17:57:29 -0500 | [diff] [blame] | 674 | } |
| 675 | |
David Tolnay | e39b070 | 2018-01-09 17:57:31 -0800 | [diff] [blame] | 676 | fn at_least_one_type(bounds: &Punctuated<TypeParamBound, Token![+]>) -> bool { |
| 677 | for bound in bounds { |
| 678 | if let TypeParamBound::Trait(_) = *bound { |
| 679 | return true; |
| 680 | } |
| 681 | } |
| 682 | false |
| 683 | } |
| 684 | |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 685 | impl TypeTraitObject { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 686 | pub fn without_plus(input: ParseStream) -> Result<Self> { |
| 687 | Self::parse(input, false) |
| 688 | } |
David Tolnay | 0a169d4 | 2017-12-29 17:57:29 -0500 | [diff] [blame] | 689 | |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 690 | // Only allow multiple trait references if allow_plus is true. |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 691 | pub fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> { |
| 692 | Ok(TypeTraitObject { |
| 693 | dyn_token: input.parse()?, |
| 694 | bounds: { |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 695 | let mut bounds = Punctuated::new(); |
| 696 | if allow_plus { |
| 697 | loop { |
| 698 | bounds.push_value(input.parse()?); |
| 699 | if !input.peek(Token![+]) { |
| 700 | break; |
| 701 | } |
| 702 | bounds.push_punct(input.parse()?); |
David Tolnay | 1b210d1 | 2018-10-27 22:07:00 -0700 | [diff] [blame] | 703 | if input.peek(Token![>]) { |
| 704 | break; |
| 705 | } |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 706 | } |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 707 | } else { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 708 | bounds.push_value(input.parse()?); |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 709 | } |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 710 | // Just lifetimes like `'a + 'b` is not a TraitObject. |
| 711 | if !at_least_one_type(&bounds) { |
| 712 | return Err(input.error("expected at least one type")); |
| 713 | } |
David Tolnay | f2cfd72 | 2017-12-31 18:02:51 -0500 | [diff] [blame] | 714 | bounds |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 715 | }, |
David Tolnay | 7d38c7e | 2017-12-25 22:31:50 -0500 | [diff] [blame] | 716 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 717 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 718 | } |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 719 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 720 | impl Parse for TypeImplTrait { |
| 721 | fn parse(input: ParseStream) -> Result<Self> { |
| 722 | Ok(TypeImplTrait { |
| 723 | impl_token: input.parse()?, |
| 724 | // NOTE: rust-lang/rust#34511 includes discussion about whether |
| 725 | // or not + should be allowed in ImplTrait directly without (). |
David Tolnay | f5ebc19 | 2018-08-30 18:23:46 -0700 | [diff] [blame] | 726 | bounds: { |
| 727 | let mut bounds = Punctuated::new(); |
| 728 | loop { |
| 729 | bounds.push_value(input.parse()?); |
| 730 | if !input.peek(Token![+]) { |
| 731 | break; |
| 732 | } |
| 733 | bounds.push_punct(input.parse()?); |
| 734 | } |
| 735 | bounds |
| 736 | }, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 737 | }) |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame] | 738 | } |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 739 | } |
| 740 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 741 | impl Parse for TypeGroup { |
| 742 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 10951d5 | 2018-08-31 10:27:39 -0700 | [diff] [blame] | 743 | let group = private::parse_group(input)?; |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 744 | Ok(TypeGroup { |
David Tolnay | f57f76f | 2018-08-31 10:23:17 -0700 | [diff] [blame] | 745 | group_token: group.token, |
| 746 | elem: group.content.parse()?, |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 747 | }) |
| 748 | } |
| 749 | } |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame] | 750 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 751 | impl Parse for TypeParen { |
| 752 | fn parse(input: ParseStream) -> Result<Self> { |
| 753 | Self::parse(input, false) |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame] | 754 | } |
David Tolnay | 0a169d4 | 2017-12-29 17:57:29 -0500 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | impl TypeParen { |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 758 | fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> { |
| 759 | let content; |
| 760 | Ok(TypeParen { |
| 761 | paren_token: parenthesized!(content in input), |
| 762 | elem: Box::new(ambig_ty(&content, allow_plus)?), |
Michael Layzell | 92639a5 | 2017-06-01 00:07:44 -0400 | [diff] [blame] | 763 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 764 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 765 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 766 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 767 | impl Parse for BareFnArg { |
| 768 | fn parse(input: ParseStream) -> Result<Self> { |
| 769 | Ok(BareFnArg { |
| 770 | name: { |
| 771 | if (input.peek(Ident) || input.peek(Token![_])) |
| 772 | && !input.peek2(Token![::]) |
| 773 | && input.peek2(Token![:]) |
| 774 | { |
| 775 | let name: BareFnArgName = input.parse()?; |
| 776 | let colon: Token![:] = input.parse()?; |
| 777 | Some((name, colon)) |
| 778 | } else { |
| 779 | None |
| 780 | } |
| 781 | }, |
| 782 | ty: input.parse()?, |
| 783 | }) |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 784 | } |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 785 | } |
| 786 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 787 | impl Parse for BareFnArgName { |
| 788 | fn parse(input: ParseStream) -> Result<Self> { |
| 789 | let lookahead = input.lookahead1(); |
| 790 | if lookahead.peek(Ident) { |
| 791 | input.parse().map(BareFnArgName::Named) |
| 792 | } else if lookahead.peek(Token![_]) { |
| 793 | input.parse().map(BareFnArgName::Wild) |
| 794 | } else { |
| 795 | Err(lookahead.error()) |
| 796 | } |
| 797 | } |
| 798 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 799 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 800 | impl Parse for Abi { |
| 801 | fn parse(input: ParseStream) -> Result<Self> { |
| 802 | Ok(Abi { |
| 803 | extern_token: input.parse()?, |
| 804 | name: input.parse()?, |
| 805 | }) |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | impl Parse for Option<Abi> { |
| 810 | fn parse(input: ParseStream) -> Result<Self> { |
| 811 | if input.peek(Token![extern]) { |
| 812 | input.parse().map(Some) |
| 813 | } else { |
| 814 | Ok(None) |
| 815 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 816 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 817 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 818 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 819 | |
| 820 | #[cfg(feature = "printing")] |
| 821 | mod printing { |
| 822 | use super::*; |
David Tolnay | 6402391 | 2018-08-31 09:51:12 -0700 | [diff] [blame] | 823 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 824 | use proc_macro2::TokenStream; |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 825 | use quote::ToTokens; |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 826 | |
David Tolnay | 6402391 | 2018-08-31 09:51:12 -0700 | [diff] [blame] | 827 | use print::TokensOrDefault; |
| 828 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 829 | impl ToTokens for TypeSlice { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 830 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 831 | self.bracket_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 832 | self.elem.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 833 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 837 | impl ToTokens for TypeArray { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 838 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 839 | self.bracket_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 840 | self.elem.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 841 | self.semi_token.to_tokens(tokens); |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 842 | self.len.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 843 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 847 | impl ToTokens for TypePtr { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 848 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 849 | self.star_token.to_tokens(tokens); |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 850 | match self.mutability { |
David Tolnay | 24237fb | 2017-12-29 02:15:26 -0500 | [diff] [blame] | 851 | Some(ref tok) => tok.to_tokens(tokens), |
| 852 | None => { |
Alex Crichton | 259ee53 | 2017-07-14 06:51:02 -0700 | [diff] [blame] | 853 | TokensOrDefault(&self.const_token).to_tokens(tokens); |
Michael Layzell | 3936ceb | 2017-07-08 00:28:36 -0400 | [diff] [blame] | 854 | } |
| 855 | } |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 856 | self.elem.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
David Tolnay | 0a89b4d | 2017-11-13 00:55:45 -0800 | [diff] [blame] | 860 | impl ToTokens for TypeReference { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 861 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 862 | self.and_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 863 | self.lifetime.to_tokens(tokens); |
David Tolnay | 136aaa3 | 2017-12-29 02:37:36 -0500 | [diff] [blame] | 864 | self.mutability.to_tokens(tokens); |
| 865 | self.elem.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 869 | impl ToTokens for TypeBareFn { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 870 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 871 | self.lifetimes.to_tokens(tokens); |
| 872 | self.unsafety.to_tokens(tokens); |
| 873 | self.abi.to_tokens(tokens); |
| 874 | self.fn_token.to_tokens(tokens); |
| 875 | self.paren_token.surround(tokens, |tokens| { |
| 876 | self.inputs.to_tokens(tokens); |
| 877 | if let Some(ref variadic) = self.variadic { |
| 878 | if !self.inputs.empty_or_trailing() { |
David Tolnay | 7ac699c | 2018-08-24 14:00:58 -0400 | [diff] [blame] | 879 | let span = variadic.spans[0]; |
| 880 | Token.to_tokens(tokens); |
David Tolnay | be7a959 | 2017-12-29 02:39:53 -0500 | [diff] [blame] | 881 | } |
| 882 | variadic.to_tokens(tokens); |
| 883 | } |
| 884 | }); |
| 885 | self.output.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 889 | impl ToTokens for TypeNever { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 890 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 891 | self.bang_token.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
David Tolnay | 0536258 | 2017-12-26 01:33:57 -0500 | [diff] [blame] | 895 | impl ToTokens for TypeTuple { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 896 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 897 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 898 | self.elems.to_tokens(tokens); |
David Tolnay | db40206 | 2018-03-31 22:23:30 +0200 | [diff] [blame] | 899 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 903 | impl ToTokens for TypePath { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 904 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 12f3b6f | 2018-09-01 16:10:53 -0700 | [diff] [blame] | 905 | private::print_path(tokens, &self.qself, &self.path); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 909 | impl ToTokens for TypeTraitObject { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 910 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | e45b59f | 2017-12-25 18:44:49 -0500 | [diff] [blame] | 911 | self.dyn_token.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 912 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 916 | impl ToTokens for TypeImplTrait { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 917 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 918 | self.impl_token.to_tokens(tokens); |
| 919 | self.bounds.to_tokens(tokens); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 923 | impl ToTokens for TypeGroup { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 924 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 925 | self.group_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 926 | self.elem.to_tokens(tokens); |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 927 | }); |
| 928 | } |
| 929 | } |
| 930 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 931 | impl ToTokens for TypeParen { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 932 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 933 | self.paren_token.surround(tokens, |tokens| { |
David Tolnay | eadbda3 | 2017-12-29 02:33:47 -0500 | [diff] [blame] | 934 | self.elem.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 935 | }); |
Alex Crichton | 62a0a59 | 2017-05-22 13:58:53 -0700 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
David Tolnay | fd6bf5c | 2017-11-12 09:41:14 -0800 | [diff] [blame] | 939 | impl ToTokens for TypeInfer { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 940 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 941 | self.underscore_token.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 945 | impl ToTokens for TypeMacro { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 946 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 323279a | 2017-12-29 11:26:32 -0500 | [diff] [blame] | 947 | self.mac.to_tokens(tokens); |
| 948 | } |
| 949 | } |
| 950 | |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 951 | impl ToTokens for TypeVerbatim { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 952 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 2ae520a | 2017-12-29 11:19:50 -0500 | [diff] [blame] | 953 | self.tts.to_tokens(tokens); |
| 954 | } |
| 955 | } |
| 956 | |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 957 | impl ToTokens for ReturnType { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 958 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 959 | match *self { |
David Tolnay | f93b90d | 2017-11-11 19:21:26 -0800 | [diff] [blame] | 960 | ReturnType::Default => {} |
David Tolnay | 4a3f59a | 2017-12-28 21:21:12 -0500 | [diff] [blame] | 961 | ReturnType::Type(ref arrow, ref ty) => { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 962 | arrow.to_tokens(tokens); |
| 963 | ty.to_tokens(tokens); |
| 964 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 969 | impl ToTokens for BareFnArg { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 970 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 971 | if let Some((ref name, ref colon)) = self.name { |
David Tolnay | 62f374c | 2016-10-02 13:37:00 -0700 | [diff] [blame] | 972 | name.to_tokens(tokens); |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 973 | colon.to_tokens(tokens); |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 974 | } |
| 975 | self.ty.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 976 | } |
| 977 | } |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 978 | |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 979 | impl ToTokens for BareFnArgName { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 980 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | 23a15f6 | 2017-08-28 12:34:23 -0700 | [diff] [blame] | 981 | match *self { |
| 982 | BareFnArgName::Named(ref t) => t.to_tokens(tokens), |
| 983 | BareFnArgName::Wild(ref t) => t.to_tokens(tokens), |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 988 | impl ToTokens for Abi { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 989 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Alex Crichton | ccbb45d | 2017-05-23 10:58:24 -0700 | [diff] [blame] | 990 | self.extern_token.to_tokens(tokens); |
David Tolnay | d512576 | 2017-12-29 02:42:17 -0500 | [diff] [blame] | 991 | self.name.to_tokens(tokens); |
David Tolnay | b8d8ef5 | 2016-10-29 14:30:08 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 994 | } |