blob: 90529687c1c2c1c69aeb004a81462a2043a81b5e [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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 Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
David Tolnay2ae520a2017-12-29 11:19:50 -050010use proc_macro2::TokenStream;
David Tolnay94d2b792018-04-29 12:26:10 -070011use punctuated::Punctuated;
David Tolnay2ae520a2017-12-29 11:19:50 -050012#[cfg(feature = "extra-traits")]
13use std::hash::{Hash, Hasher};
14#[cfg(feature = "extra-traits")]
David Tolnayc43b44e2017-12-30 23:55:54 -050015use tt::TokenStreamHelper;
David Tolnayb79ee962016-09-04 09:39:20 -070016
Alex Crichton62a0a592017-05-22 13:58:53 -070017ast_enum_of_structs! {
David Tolnay7949ae22018-01-07 00:14:51 -080018 /// The possible types that a Rust value could have.
David Tolnay614a0142018-01-07 10:25:43 -080019 ///
David Tolnay461d98e2018-01-07 11:07:19 -080020 /// *This type is available if Syn is built with the `"derive"` or `"full"`
21 /// feature.*
22 ///
David Tolnay614a0142018-01-07 10:25:43 -080023 /// # Syntax tree enum
24 ///
25 /// This type is a [syntax tree enum].
26 ///
27 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayfd6bf5c2017-11-12 09:41:14 -080028 pub enum Type {
David Tolnay7949ae22018-01-07 00:14:51 -080029 /// A dynamically sized slice type: `[T]`.
David Tolnay461d98e2018-01-07 11:07:19 -080030 ///
31 /// *This type is available if Syn is built with the `"derive"` or
32 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080033 pub Slice(TypeSlice {
David Tolnay32954ef2017-12-26 22:43:16 -050034 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050035 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070036 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080037
David Tolnay7949ae22018-01-07 00:14:51 -080038 /// A fixed size array type: `[T; n]`.
David Tolnay461d98e2018-01-07 11:07:19 -080039 ///
40 /// *This type is available if Syn is built with the `"derive"` or
41 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080042 pub Array(TypeArray {
David Tolnay32954ef2017-12-26 22:43:16 -050043 pub bracket_token: token::Bracket,
David Tolnayeadbda32017-12-29 02:33:47 -050044 pub elem: Box<Type>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080045 pub semi_token: Token![;],
David Tolnayeadbda32017-12-29 02:33:47 -050046 pub len: Expr,
Alex Crichton62a0a592017-05-22 13:58:53 -070047 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080048
David Tolnay7949ae22018-01-07 00:14:51 -080049 /// A raw pointer type: `*const T` or `*mut T`.
David Tolnay461d98e2018-01-07 11:07:19 -080050 ///
51 /// *This type is available if Syn is built with the `"derive"` or
52 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080053 pub Ptr(TypePtr {
David Tolnayf8db7ba2017-11-11 22:52:16 -080054 pub star_token: Token![*],
55 pub const_token: Option<Token![const]>,
David Tolnay136aaa32017-12-29 02:37:36 -050056 pub mutability: Option<Token![mut]>,
57 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070058 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080059
David Tolnay7949ae22018-01-07 00:14:51 -080060 /// A reference type: `&'a T` or `&'a mut T`.
David Tolnay461d98e2018-01-07 11:07:19 -080061 ///
62 /// *This type is available if Syn is built with the `"derive"` or
63 /// `"full"` feature.*
David Tolnay0a89b4d2017-11-13 00:55:45 -080064 pub Reference(TypeReference {
David Tolnayf8db7ba2017-11-11 22:52:16 -080065 pub and_token: Token![&],
Alex Crichton62a0a592017-05-22 13:58:53 -070066 pub lifetime: Option<Lifetime>,
David Tolnay136aaa32017-12-29 02:37:36 -050067 pub mutability: Option<Token![mut]>,
68 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -070069 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080070
David Tolnay7949ae22018-01-07 00:14:51 -080071 /// A bare function type: `fn(usize) -> bool`.
David Tolnay461d98e2018-01-07 11:07:19 -080072 ///
73 /// *This type is available if Syn is built with the `"derive"` or
74 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080075 pub BareFn(TypeBareFn {
David Tolnaya7d69fc2018-08-26 13:30:24 -040076 pub lifetimes: Option<BoundLifetimes>,
David Tolnaybe7a9592017-12-29 02:39:53 -050077 pub unsafety: Option<Token![unsafe]>,
78 pub abi: Option<Abi>,
79 pub fn_token: Token![fn],
David Tolnaybe7a9592017-12-29 02:39:53 -050080 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -050081 pub inputs: Punctuated<BareFnArg, Token![,]>,
David Tolnaybe7a9592017-12-29 02:39:53 -050082 pub variadic: Option<Token![...]>,
83 pub output: ReturnType,
Alex Crichton62a0a592017-05-22 13:58:53 -070084 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080085
David Tolnay7949ae22018-01-07 00:14:51 -080086 /// The never type: `!`.
David Tolnay461d98e2018-01-07 11:07:19 -080087 ///
88 /// *This type is available if Syn is built with the `"derive"` or
89 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -080090 pub Never(TypeNever {
David Tolnayf8db7ba2017-11-11 22:52:16 -080091 pub bang_token: Token![!],
Alex Crichtonccbb45d2017-05-23 10:58:24 -070092 }),
David Tolnaya454c8f2018-01-07 01:01:10 -080093
David Tolnay7949ae22018-01-07 00:14:51 -080094 /// A tuple type: `(A, B, C, String)`.
David Tolnay461d98e2018-01-07 11:07:19 -080095 ///
96 /// *This type is available if Syn is built with the `"derive"` or
97 /// `"full"` feature.*
David Tolnay05362582017-12-26 01:33:57 -050098 pub Tuple(TypeTuple {
David Tolnay32954ef2017-12-26 22:43:16 -050099 pub paren_token: token::Paren,
David Tolnayf2cfd722017-12-31 18:02:51 -0500100 pub elems: Punctuated<Type, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700101 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800102
103 /// A path like `std::slice::Iter`, optionally qualified with a
David Tolnay7949ae22018-01-07 00:14:51 -0800104 /// self-type as in `<Vec<T> as SomeTrait>::Associated`.
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 ///
David Tolnay7949ae22018-01-07 00:14:51 -0800106 /// Type arguments are stored in the Path itself.
David Tolnay461d98e2018-01-07 11:07:19 -0800107 ///
108 /// *This type is available if Syn is built with the `"derive"` or
109 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800110 pub Path(TypePath {
Alex Crichton62a0a592017-05-22 13:58:53 -0700111 pub qself: Option<QSelf>,
112 pub path: Path,
113 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800114
David Tolnay7949ae22018-01-07 00:14:51 -0800115 /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a
116 /// trait or a lifetime.
David Tolnay461d98e2018-01-07 11:07:19 -0800117 ///
118 /// *This type is available if Syn is built with the `"derive"` or
119 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800120 pub TraitObject(TypeTraitObject {
David Tolnaye45b59f2017-12-25 18:44:49 -0500121 pub dyn_token: Option<Token![dyn]>,
David Tolnayf2cfd722017-12-31 18:02:51 -0500122 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700123 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800124
David Tolnay7949ae22018-01-07 00:14:51 -0800125 /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or
126 /// a lifetime.
David Tolnay461d98e2018-01-07 11:07:19 -0800127 ///
128 /// *This type is available if Syn is built with the `"derive"` or
129 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800130 pub ImplTrait(TypeImplTrait {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800131 pub impl_token: Token![impl],
David Tolnayf2cfd722017-12-31 18:02:51 -0500132 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700133 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800134
135 /// A parenthesized type equivalent to the inner type.
David Tolnay461d98e2018-01-07 11:07:19 -0800136 ///
137 /// *This type is available if Syn is built with the `"derive"` or
138 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800139 pub Paren(TypeParen {
David Tolnay32954ef2017-12-26 22:43:16 -0500140 pub paren_token: token::Paren,
David Tolnayeadbda32017-12-29 02:33:47 -0500141 pub elem: Box<Type>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700142 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800143
144 /// A type contained within invisible delimiters.
David Tolnay461d98e2018-01-07 11:07:19 -0800145 ///
146 /// *This type is available if Syn is built with the `"derive"` or
147 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800148 pub Group(TypeGroup {
David Tolnay32954ef2017-12-26 22:43:16 -0500149 pub group_token: token::Group,
David Tolnayeadbda32017-12-29 02:33:47 -0500150 pub elem: Box<Type>,
Michael Layzell93c36282017-06-04 20:43:14 -0400151 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800152
David Tolnay7949ae22018-01-07 00:14:51 -0800153 /// Indication that a type should be inferred by the compiler: `_`.
David Tolnay461d98e2018-01-07 11:07:19 -0800154 ///
155 /// *This type is available if Syn is built with the `"derive"` or
156 /// `"full"` feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800157 pub Infer(TypeInfer {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800158 pub underscore_token: Token![_],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700159 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800160
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 /// A macro in the type position.
David Tolnay461d98e2018-01-07 11:07:19 -0800162 ///
163 /// *This type is available if Syn is built with the `"derive"` or
164 /// `"full"` feature.*
David Tolnay323279a2017-12-29 11:26:32 -0500165 pub Macro(TypeMacro {
166 pub mac: Macro,
167 }),
David Tolnaya454c8f2018-01-07 01:01:10 -0800168
David Tolnay7949ae22018-01-07 00:14:51 -0800169 /// Tokens in type position not interpreted by Syn.
David Tolnay461d98e2018-01-07 11:07:19 -0800170 ///
171 /// *This type is available if Syn is built with the `"derive"` or
172 /// `"full"` feature.*
David Tolnay2ae520a2017-12-29 11:19:50 -0500173 pub Verbatim(TypeVerbatim #manual_extra_traits {
174 pub tts: TokenStream,
175 }),
176 }
177}
178
179#[cfg(feature = "extra-traits")]
180impl Eq for TypeVerbatim {}
181
182#[cfg(feature = "extra-traits")]
183impl PartialEq for TypeVerbatim {
184 fn eq(&self, other: &Self) -> bool {
185 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
186 }
187}
188
189#[cfg(feature = "extra-traits")]
190impl Hash for TypeVerbatim {
191 fn hash<H>(&self, state: &mut H)
192 where
193 H: Hasher,
194 {
195 TokenStreamHelper(&self.tts).hash(state);
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 }
197}
198
199ast_struct! {
David Tolnayf01e37b2018-01-06 23:38:26 -0800200 /// The binary interface of a function: `extern "C"`.
David Tolnay461d98e2018-01-07 11:07:19 -0800201 ///
202 /// *This type is available if Syn is built with the `"derive"` or `"full"`
203 /// feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700204 pub struct Abi {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800205 pub extern_token: Token![extern],
David Tolnayf01e37b2018-01-06 23:38:26 -0800206 pub name: Option<LitStr>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700207 }
208}
209
210ast_struct! {
David Tolnay7949ae22018-01-07 00:14:51 -0800211 /// An argument in a function type: the `usize` in `fn(usize) -> bool`.
David Tolnay461d98e2018-01-07 11:07:19 -0800212 ///
213 /// *This type is available if Syn is built with the `"derive"` or `"full"`
214 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700215 pub struct BareFnArg {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800216 pub name: Option<(BareFnArgName, Token![:])>,
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800217 pub ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700218 }
219}
220
Alex Crichton23a15f62017-08-28 12:34:23 -0700221ast_enum! {
David Tolnay7949ae22018-01-07 00:14:51 -0800222 /// Name of an argument in a function type: the `n` in `fn(n: usize)`.
David Tolnay461d98e2018-01-07 11:07:19 -0800223 ///
224 /// *This type is available if Syn is built with the `"derive"` or `"full"`
225 /// feature.*
Alex Crichton23a15f62017-08-28 12:34:23 -0700226 pub enum BareFnArgName {
David Tolnay7949ae22018-01-07 00:14:51 -0800227 /// Argument given a name.
Alex Crichton23a15f62017-08-28 12:34:23 -0700228 Named(Ident),
David Tolnay7949ae22018-01-07 00:14:51 -0800229 /// Argument not given a name, matched with `_`.
David Tolnayf8db7ba2017-11-11 22:52:16 -0800230 Wild(Token![_]),
Alex Crichton23a15f62017-08-28 12:34:23 -0700231 }
232}
Alex Crichton62a0a592017-05-22 13:58:53 -0700233
234ast_enum! {
David Tolnay7949ae22018-01-07 00:14:51 -0800235 /// Return type of a function signature.
David Tolnay461d98e2018-01-07 11:07:19 -0800236 ///
237 /// *This type is available if Syn is built with the `"derive"` or `"full"`
238 /// feature.*
David Tolnayf93b90d2017-11-11 19:21:26 -0800239 pub enum ReturnType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700240 /// Return type is not specified.
241 ///
David Tolnay7949ae22018-01-07 00:14:51 -0800242 /// Functions default to `()` and closures default to type inference.
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 Default,
David Tolnay7949ae22018-01-07 00:14:51 -0800244 /// A particular type is returned.
David Tolnay4a3f59a2017-12-28 21:21:12 -0500245 Type(Token![->], Box<Type>),
Alex Crichton62a0a592017-05-22 13:58:53 -0700246 }
David Tolnayb79ee962016-09-04 09:39:20 -0700247}
248
David Tolnay86eca752016-09-04 11:26:41 -0700249#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700250pub mod parsing {
251 use super::*;
David Tolnaya7d69fc2018-08-26 13:30:24 -0400252 use parse::{Parse, ParseStream, Result};
David Tolnayda4049b2016-09-04 10:59:23 -0700253
David Tolnaya7d69fc2018-08-26 13:30:24 -0400254 impl Parse for Type {
255 fn parse(input: ParseStream) -> Result<Self> {
256 ambig_ty(input, true)
Alex Crichton954046c2017-05-30 21:49:42 -0700257 }
258 }
David Tolnay0047c712016-12-21 21:59:25 -0500259
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800260 impl Type {
Michael Layzell9bf2b002017-06-04 18:49:53 -0400261 /// In some positions, types may not contain the `+` character, to
262 /// disambiguate them. For example in the expression `1 as T`, T may not
263 /// contain a `+` character.
264 ///
265 /// This parser does not allow a `+`, while the default parser does.
David Tolnaya7d69fc2018-08-26 13:30:24 -0400266 pub fn without_plus(input: ParseStream) -> Result<Self> {
267 ambig_ty(input, false)
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800268 }
Alex Crichton954046c2017-05-30 21:49:42 -0700269 }
David Tolnayb79ee962016-09-04 09:39:20 -0700270
David Tolnaya7d69fc2018-08-26 13:30:24 -0400271 fn ambig_ty(input: ParseStream, allow_plus: bool) -> Result<Type> {
272 if input.peek(token::Group) {
273 return input.parse().map(Type::Group);
274 }
275
276 let mut lifetimes = None::<BoundLifetimes>;
277 let mut lookahead = input.lookahead1();
278 if lookahead.peek(Token![for]) {
279 lifetimes = input.parse()?;
280 lookahead = input.lookahead1();
281 if !lookahead.peek(Ident)
282 && !lookahead.peek(Token![fn])
283 && !lookahead.peek(Token![unsafe])
284 && !lookahead.peek(Token![extern])
285 && !lookahead.peek(Token![super])
286 && !lookahead.peek(Token![self])
287 && !lookahead.peek(Token![Self])
288 && !lookahead.peek(Token![crate])
289 {
290 return Err(lookahead.error());
291 }
292 }
293
294 if lookahead.peek(token::Paren) {
295 let content;
296 let paren_token = parenthesized!(content in input);
297 if content.is_empty() {
298 return Ok(Type::Tuple(TypeTuple {
299 paren_token: paren_token,
300 elems: Punctuated::new(),
301 }));
302 }
303 if content.peek(Lifetime) {
304 return Ok(Type::Paren(TypeParen {
305 paren_token: paren_token,
306 elem: Box::new(Type::TraitObject(content.parse()?)),
307 }));
308 }
309 let first: Type = content.parse()?;
310 if content.peek(Token![,]) {
311 Ok(Type::Tuple(TypeTuple {
312 paren_token: paren_token,
313 elems: {
314 let mut elems = Punctuated::new();
315 elems.push_value(first);
316 elems.push_punct(content.parse()?);
317 let rest: Punctuated<Type, Token![,]> =
318 content.parse_terminated(Parse::parse)?;
319 elems.extend(rest);
320 elems
321 },
322 }))
323 } else {
324 Ok(Type::Paren(TypeParen {
325 paren_token: paren_token,
326 elem: Box::new(first),
327 }))
328 }
329 } else if lookahead.peek(Token![fn])
330 || lookahead.peek(Token![unsafe])
331 || lookahead.peek(Token![extern]) && !input.peek2(Token![::])
332 {
333 let mut bare_fn: TypeBareFn = input.parse()?;
334 bare_fn.lifetimes = lifetimes;
335 Ok(Type::BareFn(bare_fn))
336 } else if lookahead.peek(Ident)
337 || input.peek(Token![super])
338 || input.peek(Token![self])
339 || input.peek(Token![Self])
340 || input.peek(Token![crate])
341 || input.peek(Token![extern])
342 || lookahead.peek(Token![::])
343 || lookahead.peek(Token![<])
344 {
345 if input.peek(Token![dyn]) {
346 let mut trait_object: TypeTraitObject = input.parse()?;
347 if lifetimes.is_some() {
348 match *trait_object.bounds.iter_mut().next().unwrap() {
349 TypeParamBound::Trait(ref mut trait_bound) => {
350 trait_bound.lifetimes = lifetimes;
351 }
352 TypeParamBound::Lifetime(_) => unreachable!(),
353 }
354 }
355 return Ok(Type::TraitObject(trait_object));
356 }
357
358 let ty: TypePath = input.parse()?;
359 if ty.qself.is_some() {
360 return Ok(Type::Path(ty));
361 }
362
363 if input.peek(Token![!]) && !input.peek(Token![!=]) {
364 let mut contains_arguments = false;
365 for segment in &ty.path.segments {
366 match segment.arguments {
367 PathArguments::None => {}
368 PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => {
369 contains_arguments = true;
370 }
371 }
372 }
373
374 if !contains_arguments {
375 let bang_token: Token![!] = input.parse()?;
376 let (delimiter, tts) = mac::parse_delimiter(input)?;
377 return Ok(Type::Macro(TypeMacro {
378 mac: Macro {
379 path: ty.path,
380 bang_token: bang_token,
381 delimiter: delimiter,
382 tts: tts,
383 },
384 }));
385 }
386 }
387
388 if lifetimes.is_some() || allow_plus && input.peek(Token![+]) {
389 let mut bounds = Punctuated::new();
390 bounds.push_value(TypeParamBound::Trait(TraitBound {
391 paren_token: None,
392 modifier: TraitBoundModifier::None,
393 lifetimes: lifetimes,
394 path: ty.path,
395 }));
396 if allow_plus && input.peek(Token![+]) {
397 bounds.push_punct(input.parse()?);
398 let rest: Punctuated<TypeParamBound, Token![+]> =
399 input.parse_synom(Punctuated::parse_terminated_nonempty)?;
400 bounds.extend(rest);
401 }
402 return Ok(Type::TraitObject(TypeTraitObject {
403 dyn_token: None,
404 bounds: bounds,
405 }));
406 }
407
408 Ok(Type::Path(ty))
409 } else if lookahead.peek(token::Bracket) {
410 let content;
411 let bracket_token = bracketed!(content in input);
412 let elem: Type = content.parse()?;
413 if content.peek(Token![;]) {
414 Ok(Type::Array(TypeArray {
415 bracket_token: bracket_token,
David Tolnayeadbda32017-12-29 02:33:47 -0500416 elem: Box::new(elem),
David Tolnaya7d69fc2018-08-26 13:30:24 -0400417 semi_token: content.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -0700418 len: content.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400419 }))
420 } else {
421 Ok(Type::Slice(TypeSlice {
422 bracket_token: bracket_token,
423 elem: Box::new(elem),
424 }))
Alex Crichton954046c2017-05-30 21:49:42 -0700425 }
David Tolnaya7d69fc2018-08-26 13:30:24 -0400426 } else if lookahead.peek(Token![*]) {
427 input.parse().map(Type::Ptr)
428 } else if lookahead.peek(Token![&]) {
429 input.parse().map(Type::Reference)
430 } else if lookahead.peek(Token![!]) && !input.peek(Token![=]) {
431 input.parse().map(Type::Never)
432 } else if lookahead.peek(Token![impl ]) {
433 input.parse().map(Type::ImplTrait)
434 } else if lookahead.peek(Token![_]) {
435 input.parse().map(Type::Infer)
436 } else if lookahead.peek(Lifetime) {
437 input.parse().map(Type::TraitObject)
438 } else {
439 Err(lookahead.error())
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800440 }
Alex Crichton954046c2017-05-30 21:49:42 -0700441 }
David Tolnayfa94b6f2016-10-05 23:26:11 -0700442
David Tolnaya7d69fc2018-08-26 13:30:24 -0400443 impl Parse for TypeSlice {
444 fn parse(input: ParseStream) -> Result<Self> {
445 let content;
446 Ok(TypeSlice {
447 bracket_token: bracketed!(content in input),
448 elem: content.parse()?,
Michael Layzell92639a52017-06-01 00:07:44 -0400449 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800450 }
Alex Crichton954046c2017-05-30 21:49:42 -0700451 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700452
David Tolnaya7d69fc2018-08-26 13:30:24 -0400453 impl Parse for TypeArray {
454 fn parse(input: ParseStream) -> Result<Self> {
455 let content;
456 Ok(TypeArray {
457 bracket_token: bracketed!(content in input),
458 elem: content.parse()?,
459 semi_token: content.parse()?,
David Tolnay9389c382018-08-27 09:13:37 -0700460 len: content.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400461 })
462 }
463 }
464
465 impl Parse for TypePtr {
466 fn parse(input: ParseStream) -> Result<Self> {
467 let star_token: Token![*] = input.parse()?;
468
469 let lookahead = input.lookahead1();
470 let (const_token, mutability) = if lookahead.peek(Token![const]) {
471 (Some(input.parse()?), None)
472 } else if lookahead.peek(Token![mut]) {
473 (None, Some(input.parse()?))
474 } else {
475 return Err(lookahead.error());
476 };
477
478 Ok(TypePtr {
479 star_token: star_token,
480 const_token: const_token,
David Tolnay136aaa32017-12-29 02:37:36 -0500481 mutability: mutability,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400482 elem: Box::new(input.call(Type::without_plus)?),
Michael Layzell92639a52017-06-01 00:07:44 -0400483 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800484 }
Alex Crichton954046c2017-05-30 21:49:42 -0700485 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700486
David Tolnaya7d69fc2018-08-26 13:30:24 -0400487 impl Parse for TypeReference {
488 fn parse(input: ParseStream) -> Result<Self> {
489 Ok(TypeReference {
490 and_token: input.parse()?,
491 lifetime: input.parse()?,
492 mutability: input.parse()?,
493 // & binds tighter than +, so we don't allow + here.
494 elem: Box::new(input.call(Type::without_plus)?),
Michael Layzell92639a52017-06-01 00:07:44 -0400495 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800496 }
Alex Crichton954046c2017-05-30 21:49:42 -0700497 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700498
David Tolnaya7d69fc2018-08-26 13:30:24 -0400499 impl Parse for TypeBareFn {
500 fn parse(input: ParseStream) -> Result<Self> {
501 let args;
502 let allow_variadic;
503 Ok(TypeBareFn {
504 lifetimes: input.parse()?,
505 unsafety: input.parse()?,
506 abi: input.parse()?,
507 fn_token: input.parse()?,
508 paren_token: parenthesized!(args in input),
509 inputs: {
510 let inputs = args.parse_synom(Punctuated::parse_terminated)?;
511 allow_variadic = inputs.empty_or_trailing();
512 inputs
513 },
514 variadic: {
515 if allow_variadic && args.peek(Token![...]) {
516 Some(args.parse()?)
517 } else {
518 None
519 }
520 },
521 output: input.call(ReturnType::without_plus)?,
Michael Layzell92639a52017-06-01 00:07:44 -0400522 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800523 }
Alex Crichton954046c2017-05-30 21:49:42 -0700524 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700525
David Tolnaya7d69fc2018-08-26 13:30:24 -0400526 impl Parse for TypeNever {
527 fn parse(input: ParseStream) -> Result<Self> {
528 Ok(TypeNever {
529 bang_token: input.parse()?,
David Tolnay7d38c7e2017-12-25 22:31:50 -0500530 })
David Tolnaya7d69fc2018-08-26 13:30:24 -0400531 }
532 }
533
534 impl Parse for TypeInfer {
535 fn parse(input: ParseStream) -> Result<Self> {
536 Ok(TypeInfer {
537 underscore_token: input.parse()?,
538 })
539 }
540 }
541
542 impl Parse for TypeTuple {
543 fn parse(input: ParseStream) -> Result<Self> {
544 let content;
545 Ok(TypeTuple {
546 paren_token: parenthesized!(content in input),
547 elems: content.parse_terminated(<Type as Parse>::parse)?,
548 })
549 }
550 }
551
552 impl Parse for TypeMacro {
553 fn parse(input: ParseStream) -> Result<Self> {
554 Ok(TypeMacro {
555 mac: input.parse()?,
556 })
557 }
558 }
559
560 impl Parse for TypePath {
561 fn parse(input: ParseStream) -> Result<Self> {
562 let (qself, mut path) = if input.peek(Token![<]) {
563 let lt_token: Token![<] = input.parse()?;
564 let this: Type = input.parse()?;
565 let path = if input.peek(Token![as]) {
566 let as_token: Token![as] = input.parse()?;
567 let path: Path = input.parse()?;
568 Some((as_token, path))
569 } else {
570 None
571 };
572 let gt_token: Token![>] = input.parse()?;
573 let colon2_token: Token![::] = input.parse()?;
574 let rest = input.parse_synom(Punctuated::parse_separated_nonempty)?;
575 let (position, as_token, path) = match path {
576 Some((as_token, mut path)) => {
577 let pos = path.segments.len();
578 path.segments.push_punct(colon2_token);
579 path.segments.extend(rest.into_pairs());
580 (pos, Some(as_token), path)
581 }
582 None => {
583 let path = Path {
584 leading_colon: Some(colon2_token),
585 segments: rest,
586 };
587 (0, None, path)
588 }
589 };
590 let qself = QSelf {
591 lt_token: lt_token,
592 ty: Box::new(this),
593 position: position,
594 as_token: as_token,
595 gt_token: gt_token,
596 };
597 (Some(qself), path)
598 } else {
599 let path: Path = input.parse()?;
600 (None, path)
601 };
602
603 let parenthesized = if path.segments.last().unwrap().value().arguments.is_empty()
604 && input.peek(token::Paren)
605 {
606 let args: ParenthesizedGenericArguments = input.parse()?;
607 Some(args)
608 } else {
609 None
610 };
611
612 if let Some(parenthesized) = parenthesized {
613 let parenthesized = PathArguments::Parenthesized(parenthesized);
614 path.segments.last_mut().unwrap().value_mut().arguments = parenthesized;
615 }
616
617 Ok(TypePath {
618 qself: qself,
619 path: path,
620 })
621 }
David Tolnay7d38c7e2017-12-25 22:31:50 -0500622 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700623
Geoffry Songac02b182018-05-19 22:11:31 -0700624 impl ReturnType {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400625 pub fn without_plus(input: ParseStream) -> Result<Self> {
626 Self::parse(input, false)
627 }
Geoffry Songac02b182018-05-19 22:11:31 -0700628
David Tolnaya7d69fc2018-08-26 13:30:24 -0400629 pub fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
630 if input.peek(Token![->]) {
631 let arrow = input.parse()?;
632 let ty = ambig_ty(input, allow_plus)?;
633 Ok(ReturnType::Type(arrow, Box::new(ty)))
634 } else {
635 Ok(ReturnType::Default)
636 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800637 }
Alex Crichton954046c2017-05-30 21:49:42 -0700638 }
639
David Tolnaya7d69fc2018-08-26 13:30:24 -0400640 impl Parse for ReturnType {
641 fn parse(input: ParseStream) -> Result<Self> {
642 Self::parse(input, true)
643 }
644 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800645
David Tolnaya7d69fc2018-08-26 13:30:24 -0400646 impl Parse for TypeTraitObject {
647 fn parse(input: ParseStream) -> Result<Self> {
648 Self::parse(input, true)
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800649 }
David Tolnay0a169d42017-12-29 17:57:29 -0500650 }
651
David Tolnaye39b0702018-01-09 17:57:31 -0800652 fn at_least_one_type(bounds: &Punctuated<TypeParamBound, Token![+]>) -> bool {
653 for bound in bounds {
654 if let TypeParamBound::Trait(_) = *bound {
655 return true;
656 }
657 }
658 false
659 }
660
David Tolnay7d38c7e2017-12-25 22:31:50 -0500661 impl TypeTraitObject {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400662 pub fn without_plus(input: ParseStream) -> Result<Self> {
663 Self::parse(input, false)
664 }
David Tolnay0a169d42017-12-29 17:57:29 -0500665
David Tolnay7d38c7e2017-12-25 22:31:50 -0500666 // Only allow multiple trait references if allow_plus is true.
David Tolnaya7d69fc2018-08-26 13:30:24 -0400667 pub fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
668 Ok(TypeTraitObject {
669 dyn_token: input.parse()?,
670 bounds: {
671 let bounds = if allow_plus {
672 input.parse_synom(Punctuated::parse_terminated_nonempty)?
673 } else {
674 let mut bounds = Punctuated::new();
675 bounds.push_value(input.parse()?);
676 bounds
677 };
678 // Just lifetimes like `'a + 'b` is not a TraitObject.
679 if !at_least_one_type(&bounds) {
680 return Err(input.error("expected at least one type"));
681 }
David Tolnayf2cfd722017-12-31 18:02:51 -0500682 bounds
David Tolnaya7d69fc2018-08-26 13:30:24 -0400683 },
David Tolnay7d38c7e2017-12-25 22:31:50 -0500684 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800685 }
Alex Crichton954046c2017-05-30 21:49:42 -0700686 }
David Tolnayb79ee962016-09-04 09:39:20 -0700687
David Tolnaya7d69fc2018-08-26 13:30:24 -0400688 impl Parse for TypeImplTrait {
689 fn parse(input: ParseStream) -> Result<Self> {
690 Ok(TypeImplTrait {
691 impl_token: input.parse()?,
692 // NOTE: rust-lang/rust#34511 includes discussion about whether
693 // or not + should be allowed in ImplTrait directly without ().
694 bounds: input.parse_synom(Punctuated::parse_terminated_nonempty)?,
Michael Layzell93c36282017-06-04 20:43:14 -0400695 })
David Tolnay79777332018-01-07 10:04:42 -0800696 }
Michael Layzell93c36282017-06-04 20:43:14 -0400697 }
698
David Tolnaya7d69fc2018-08-26 13:30:24 -0400699 impl Parse for TypeGroup {
700 fn parse(input: ParseStream) -> Result<Self> {
701 let content;
702 Ok(TypeGroup {
703 group_token: grouped!(content in input),
704 elem: content.parse()?,
705 })
706 }
707 }
David Tolnay79777332018-01-07 10:04:42 -0800708
David Tolnaya7d69fc2018-08-26 13:30:24 -0400709 impl Parse for TypeParen {
710 fn parse(input: ParseStream) -> Result<Self> {
711 Self::parse(input, false)
David Tolnay79777332018-01-07 10:04:42 -0800712 }
David Tolnay0a169d42017-12-29 17:57:29 -0500713 }
714
715 impl TypeParen {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400716 fn parse(input: ParseStream, allow_plus: bool) -> Result<Self> {
717 let content;
718 Ok(TypeParen {
719 paren_token: parenthesized!(content in input),
720 elem: Box::new(ambig_ty(&content, allow_plus)?),
Michael Layzell92639a52017-06-01 00:07:44 -0400721 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800722 }
Alex Crichton954046c2017-05-30 21:49:42 -0700723 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700724
David Tolnaya7d69fc2018-08-26 13:30:24 -0400725 impl Parse for BareFnArg {
726 fn parse(input: ParseStream) -> Result<Self> {
727 Ok(BareFnArg {
728 name: {
729 if (input.peek(Ident) || input.peek(Token![_]))
730 && !input.peek2(Token![::])
731 && input.peek2(Token![:])
732 {
733 let name: BareFnArgName = input.parse()?;
734 let colon: Token![:] = input.parse()?;
735 Some((name, colon))
736 } else {
737 None
738 }
739 },
740 ty: input.parse()?,
741 })
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800742 }
Alex Crichton23a15f62017-08-28 12:34:23 -0700743 }
744
David Tolnaya7d69fc2018-08-26 13:30:24 -0400745 impl Parse for BareFnArgName {
746 fn parse(input: ParseStream) -> Result<Self> {
747 let lookahead = input.lookahead1();
748 if lookahead.peek(Ident) {
749 input.parse().map(BareFnArgName::Named)
750 } else if lookahead.peek(Token![_]) {
751 input.parse().map(BareFnArgName::Wild)
752 } else {
753 Err(lookahead.error())
754 }
755 }
756 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800757
David Tolnaya7d69fc2018-08-26 13:30:24 -0400758 impl Parse for Abi {
759 fn parse(input: ParseStream) -> Result<Self> {
760 Ok(Abi {
761 extern_token: input.parse()?,
762 name: input.parse()?,
763 })
764 }
765 }
766
767 impl Parse for Option<Abi> {
768 fn parse(input: ParseStream) -> Result<Self> {
769 if input.peek(Token![extern]) {
770 input.parse().map(Some)
771 } else {
772 Ok(None)
773 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800774 }
Alex Crichton954046c2017-05-30 21:49:42 -0700775 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700776}
David Tolnay87d0b442016-09-04 11:52:12 -0700777
778#[cfg(feature = "printing")]
779mod printing {
780 use super::*;
Alex Crichtona74a1c82018-05-16 10:20:44 -0700781 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -0700782 use quote::ToTokens;
David Tolnay87d0b442016-09-04 11:52:12 -0700783
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800784 impl ToTokens for TypeSlice {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700785 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700786 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500787 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700788 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700789 }
790 }
791
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800792 impl ToTokens for TypeArray {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700793 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700794 self.bracket_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500795 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700796 self.semi_token.to_tokens(tokens);
David Tolnayeadbda32017-12-29 02:33:47 -0500797 self.len.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700798 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700799 }
800 }
801
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800802 impl ToTokens for TypePtr {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700803 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700804 self.star_token.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500805 match self.mutability {
David Tolnay24237fb2017-12-29 02:15:26 -0500806 Some(ref tok) => tok.to_tokens(tokens),
807 None => {
Alex Crichton259ee532017-07-14 06:51:02 -0700808 TokensOrDefault(&self.const_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400809 }
810 }
David Tolnay136aaa32017-12-29 02:37:36 -0500811 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700812 }
813 }
814
David Tolnay0a89b4d2017-11-13 00:55:45 -0800815 impl ToTokens for TypeReference {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700816 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700817 self.and_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700818 self.lifetime.to_tokens(tokens);
David Tolnay136aaa32017-12-29 02:37:36 -0500819 self.mutability.to_tokens(tokens);
820 self.elem.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700821 }
822 }
823
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800824 impl ToTokens for TypeBareFn {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700825 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaybe7a9592017-12-29 02:39:53 -0500826 self.lifetimes.to_tokens(tokens);
827 self.unsafety.to_tokens(tokens);
828 self.abi.to_tokens(tokens);
829 self.fn_token.to_tokens(tokens);
830 self.paren_token.surround(tokens, |tokens| {
831 self.inputs.to_tokens(tokens);
832 if let Some(ref variadic) = self.variadic {
833 if !self.inputs.empty_or_trailing() {
David Tolnay7ac699c2018-08-24 14:00:58 -0400834 let span = variadic.spans[0];
835 Token![,](span).to_tokens(tokens);
David Tolnaybe7a9592017-12-29 02:39:53 -0500836 }
837 variadic.to_tokens(tokens);
838 }
839 });
840 self.output.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700841 }
842 }
843
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800844 impl ToTokens for TypeNever {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700845 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700846 self.bang_token.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700847 }
848 }
849
David Tolnay05362582017-12-26 01:33:57 -0500850 impl ToTokens for TypeTuple {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700851 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700852 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500853 self.elems.to_tokens(tokens);
David Tolnaydb402062018-03-31 22:23:30 +0200854 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700855 }
856 }
857
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800858 impl ToTokens for TypePath {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700859 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700860 PathTokens(&self.qself, &self.path).to_tokens(tokens);
861 }
862 }
863
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800864 impl ToTokens for TypeTraitObject {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700865 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye45b59f2017-12-25 18:44:49 -0500866 self.dyn_token.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700867 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700868 }
869 }
870
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800871 impl ToTokens for TypeImplTrait {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700872 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700873 self.impl_token.to_tokens(tokens);
874 self.bounds.to_tokens(tokens);
Alex Crichton62a0a592017-05-22 13:58:53 -0700875 }
876 }
877
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800878 impl ToTokens for TypeGroup {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700879 fn to_tokens(&self, tokens: &mut TokenStream) {
Michael Layzell93c36282017-06-04 20:43:14 -0400880 self.group_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500881 self.elem.to_tokens(tokens);
Michael Layzell93c36282017-06-04 20:43:14 -0400882 });
883 }
884 }
885
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800886 impl ToTokens for TypeParen {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700887 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700888 self.paren_token.surround(tokens, |tokens| {
David Tolnayeadbda32017-12-29 02:33:47 -0500889 self.elem.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700890 });
Alex Crichton62a0a592017-05-22 13:58:53 -0700891 }
892 }
893
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800894 impl ToTokens for TypeInfer {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700895 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700896 self.underscore_token.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700897 }
898 }
899
David Tolnay323279a2017-12-29 11:26:32 -0500900 impl ToTokens for TypeMacro {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700901 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay323279a2017-12-29 11:26:32 -0500902 self.mac.to_tokens(tokens);
903 }
904 }
905
David Tolnay2ae520a2017-12-29 11:19:50 -0500906 impl ToTokens for TypeVerbatim {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700907 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2ae520a2017-12-29 11:19:50 -0500908 self.tts.to_tokens(tokens);
909 }
910 }
911
David Tolnayf93b90d2017-11-11 19:21:26 -0800912 impl ToTokens for ReturnType {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700913 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700914 match *self {
David Tolnayf93b90d2017-11-11 19:21:26 -0800915 ReturnType::Default => {}
David Tolnay4a3f59a2017-12-28 21:21:12 -0500916 ReturnType::Type(ref arrow, ref ty) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700917 arrow.to_tokens(tokens);
918 ty.to_tokens(tokens);
919 }
David Tolnay87d0b442016-09-04 11:52:12 -0700920 }
921 }
922 }
923
David Tolnay62f374c2016-10-02 13:37:00 -0700924 impl ToTokens for BareFnArg {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700925 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700926 if let Some((ref name, ref colon)) = self.name {
David Tolnay62f374c2016-10-02 13:37:00 -0700927 name.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700928 colon.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700929 }
930 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700931 }
932 }
David Tolnayb8d8ef52016-10-29 14:30:08 -0700933
Alex Crichton23a15f62017-08-28 12:34:23 -0700934 impl ToTokens for BareFnArgName {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700935 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichton23a15f62017-08-28 12:34:23 -0700936 match *self {
937 BareFnArgName::Named(ref t) => t.to_tokens(tokens),
938 BareFnArgName::Wild(ref t) => t.to_tokens(tokens),
939 }
940 }
941 }
942
David Tolnayb8d8ef52016-10-29 14:30:08 -0700943 impl ToTokens for Abi {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700944 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700945 self.extern_token.to_tokens(tokens);
David Tolnayd5125762017-12-29 02:42:17 -0500946 self.name.to_tokens(tokens);
David Tolnayb8d8ef52016-10-29 14:30:08 -0700947 }
948 }
David Tolnay87d0b442016-09-04 11:52:12 -0700949}