2 use crate::punctuated
::Punctuated
;
3 #[cfg(feature = "extra-traits")]
4 use crate::tt
::TokenStreamHelper
;
5 use proc_macro2
::TokenStream
;
6 #[cfg(feature = "extra-traits")]
7 use std
::hash
::{Hash, Hasher}
;
10 /// The possible types that a Rust value could have.
12 /// *This type is available if Syn is built with the `"derive"` or `"full"`
15 /// # Syntax tree enum
17 /// This type is a [syntax tree enum].
19 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
21 // TODO: change syntax-tree-enum link to an intra rustdoc link, currently
22 // blocked on https://github.com/rust-lang/rust/issues/62833
23 pub enum Type
#manual_extra_traits {
24 /// A fixed size array type: `[T; n]`.
27 /// A bare function type: `fn(usize) -> bool`.
30 /// A type contained within invisible delimiters.
33 /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or
35 ImplTrait(TypeImplTrait
),
37 /// Indication that a type should be inferred by the compiler: `_`.
40 /// A macro in the type position.
43 /// The never type: `!`.
46 /// A parenthesized type equivalent to the inner type.
49 /// A path like `std::slice::Iter`, optionally qualified with a
50 /// self-type as in `<Vec<T> as SomeTrait>::Associated`.
53 /// A raw pointer type: `*const T` or `*mut T`.
56 /// A reference type: `&'a T` or `&'a mut T`.
57 Reference(TypeReference
),
59 /// A dynamically sized slice type: `[T]`.
62 /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a
63 /// trait or a lifetime.
64 TraitObject(TypeTraitObject
),
66 /// A tuple type: `(A, B, C, String)`.
69 /// Tokens in type position not interpreted by Syn.
70 Verbatim(TokenStream
),
78 /// A fixed size array type: `[T; n]`.
80 /// *This type is available if Syn is built with the `"derive"` or
81 /// `"full"` feature.*
82 pub struct TypeArray
{
83 pub bracket_token
: token
::Bracket
,
85 pub semi_token
: Token
![;],
91 /// A bare function type: `fn(usize) -> bool`.
93 /// *This type is available if Syn is built with the `"derive"` or
94 /// `"full"` feature.*
95 pub struct TypeBareFn
{
96 pub lifetimes
: Option
<BoundLifetimes
>,
97 pub unsafety
: Option
<Token
![unsafe]>,
99 pub fn_token
: Token
![fn],
100 pub paren_token
: token
::Paren
,
101 pub inputs
: Punctuated
<BareFnArg
, Token
![,]>,
102 pub variadic
: Option
<Variadic
>,
103 pub output
: ReturnType
,
108 /// A type contained within invisible delimiters.
110 /// *This type is available if Syn is built with the `"derive"` or
111 /// `"full"` feature.*
112 pub struct TypeGroup
{
113 pub group_token
: token
::Group
,
119 /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or
122 /// *This type is available if Syn is built with the `"derive"` or
123 /// `"full"` feature.*
124 pub struct TypeImplTrait
{
125 pub impl_token
: Token
![impl],
126 pub bounds
: Punctuated
<TypeParamBound
, Token
![+]>,
131 /// Indication that a type should be inferred by the compiler: `_`.
133 /// *This type is available if Syn is built with the `"derive"` or
134 /// `"full"` feature.*
135 pub struct TypeInfer
{
136 pub underscore_token
: Token
![_
],
141 /// A macro in the type position.
143 /// *This type is available if Syn is built with the `"derive"` or
144 /// `"full"` feature.*
145 pub struct TypeMacro
{
151 /// The never type: `!`.
153 /// *This type is available if Syn is built with the `"derive"` or
154 /// `"full"` feature.*
155 pub struct TypeNever
{
156 pub bang_token
: Token
![!],
161 /// A parenthesized type equivalent to the inner type.
163 /// *This type is available if Syn is built with the `"derive"` or
164 /// `"full"` feature.*
165 pub struct TypeParen
{
166 pub paren_token
: token
::Paren
,
172 /// A path like `std::slice::Iter`, optionally qualified with a
173 /// self-type as in `<Vec<T> as SomeTrait>::Associated`.
175 /// *This type is available if Syn is built with the `"derive"` or
176 /// `"full"` feature.*
177 pub struct TypePath
{
178 pub qself
: Option
<QSelf
>,
184 /// A raw pointer type: `*const T` or `*mut T`.
186 /// *This type is available if Syn is built with the `"derive"` or
187 /// `"full"` feature.*
189 pub star_token
: Token
![*],
190 pub const_token
: Option
<Token
![const]>,
191 pub mutability
: Option
<Token
![mut]>,
197 /// A reference type: `&'a T` or `&'a mut T`.
199 /// *This type is available if Syn is built with the `"derive"` or
200 /// `"full"` feature.*
201 pub struct TypeReference
{
202 pub and_token
: Token
![&],
203 pub lifetime
: Option
<Lifetime
>,
204 pub mutability
: Option
<Token
![mut]>,
210 /// A dynamically sized slice type: `[T]`.
212 /// *This type is available if Syn is built with the `"derive"` or
213 /// `"full"` feature.*
214 pub struct TypeSlice
{
215 pub bracket_token
: token
::Bracket
,
221 /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a
222 /// trait or a lifetime.
224 /// *This type is available if Syn is built with the `"derive"` or
225 /// `"full"` feature.*
226 pub struct TypeTraitObject
{
227 pub dyn_token
: Option
<Token
![dyn]>,
228 pub bounds
: Punctuated
<TypeParamBound
, Token
![+]>,
233 /// A tuple type: `(A, B, C, String)`.
235 /// *This type is available if Syn is built with the `"derive"` or
236 /// `"full"` feature.*
237 pub struct TypeTuple
{
238 pub paren_token
: token
::Paren
,
239 pub elems
: Punctuated
<Type
, Token
![,]>,
243 #[cfg(feature = "extra-traits")]
246 #[cfg(feature = "extra-traits")]
247 impl PartialEq
for Type
{
248 fn eq(&self, other
: &Self) -> bool
{
249 match (self, other
) {
250 (Type
::Array(this
), Type
::Array(other
)) => this
== other
,
251 (Type
::BareFn(this
), Type
::BareFn(other
)) => this
== other
,
252 (Type
::Group(this
), Type
::Group(other
)) => this
== other
,
253 (Type
::ImplTrait(this
), Type
::ImplTrait(other
)) => this
== other
,
254 (Type
::Infer(this
), Type
::Infer(other
)) => this
== other
,
255 (Type
::Macro(this
), Type
::Macro(other
)) => this
== other
,
256 (Type
::Never(this
), Type
::Never(other
)) => this
== other
,
257 (Type
::Paren(this
), Type
::Paren(other
)) => this
== other
,
258 (Type
::Path(this
), Type
::Path(other
)) => this
== other
,
259 (Type
::Ptr(this
), Type
::Ptr(other
)) => this
== other
,
260 (Type
::Reference(this
), Type
::Reference(other
)) => this
== other
,
261 (Type
::Slice(this
), Type
::Slice(other
)) => this
== other
,
262 (Type
::TraitObject(this
), Type
::TraitObject(other
)) => this
== other
,
263 (Type
::Tuple(this
), Type
::Tuple(other
)) => this
== other
,
264 (Type
::Verbatim(this
), Type
::Verbatim(other
)) => {
265 TokenStreamHelper(this
) == TokenStreamHelper(other
)
272 #[cfg(feature = "extra-traits")]
274 fn hash
<H
>(&self, hash
: &mut H
)
283 Type
::BareFn(ty
) => {
291 Type
::ImplTrait(ty
) => {
319 Type
::Reference(ty
) => {
327 Type
::TraitObject(ty
) => {
335 Type
::Verbatim(ty
) => {
337 TokenStreamHelper(ty
).hash(hash
);
339 Type
::__Nonexhaustive
=> unreachable
!(),
345 /// The binary interface of a function: `extern "C"`.
347 /// *This type is available if Syn is built with the `"derive"` or `"full"`
350 pub extern_token
: Token
![extern],
351 pub name
: Option
<LitStr
>,
356 /// An argument in a function type: the `usize` in `fn(usize) -> bool`.
358 /// *This type is available if Syn is built with the `"derive"` or `"full"`
360 pub struct BareFnArg
{
361 pub attrs
: Vec
<Attribute
>,
362 pub name
: Option
<(Ident
, Token
![:])>,
368 /// The variadic argument of a foreign function.
375 /// fn printf(format: *const c_char, ...) -> c_int;
380 /// *This type is available if Syn is built with the `"derive"` or `"full"`
382 pub struct Variadic
{
383 pub attrs
: Vec
<Attribute
>,
384 pub dots
: Token
![...],
389 /// Return type of a function signature.
391 /// *This type is available if Syn is built with the `"derive"` or `"full"`
393 pub enum ReturnType
{
394 /// Return type is not specified.
396 /// Functions default to `()` and closures default to type inference.
398 /// A particular type is returned.
399 Type(Token
![->], Box
<Type
>),
403 #[cfg(feature = "parsing")]
407 use crate::ext
::IdentExt
;
408 use crate::parse
::{Parse, ParseStream, Result}
;
410 use proc_macro2
::{Punct, Spacing, TokenTree}
;
411 use std
::iter
::FromIterator
;
413 impl Parse
for Type
{
414 fn parse(input
: ParseStream
) -> Result
<Self> {
415 ambig_ty(input
, true)
420 /// In some positions, types may not contain the `+` character, to
421 /// disambiguate them. For example in the expression `1 as T`, T may not
422 /// contain a `+` character.
424 /// This parser does not allow a `+`, while the default parser does.
425 pub fn without_plus(input
: ParseStream
) -> Result
<Self> {
426 ambig_ty(input
, false)
430 fn ambig_ty(input
: ParseStream
, allow_plus
: bool
) -> Result
<Type
> {
431 if input
.peek(token
::Group
) {
432 return input
.parse().map(Type
::Group
);
435 let mut lifetimes
= None
::<BoundLifetimes
>;
436 let mut lookahead
= input
.lookahead1();
437 if lookahead
.peek(Token
![for]) {
438 lifetimes
= input
.parse()?
;
439 lookahead
= input
.lookahead1();
440 if !lookahead
.peek(Ident
)
441 && !lookahead
.peek(Token
![fn])
442 && !lookahead
.peek(Token
![unsafe])
443 && !lookahead
.peek(Token
![extern])
444 && !lookahead
.peek(Token
![super])
445 && !lookahead
.peek(Token
![self])
446 && !lookahead
.peek(Token
![Self])
447 && !lookahead
.peek(Token
![crate])
449 return Err(lookahead
.error());
453 if lookahead
.peek(token
::Paren
) {
455 let paren_token
= parenthesized
!(content
in input
);
456 if content
.is_empty() {
457 return Ok(Type
::Tuple(TypeTuple
{
459 elems
: Punctuated
::new(),
462 if content
.peek(Lifetime
) {
463 return Ok(Type
::Paren(TypeParen
{
465 elem
: Box
::new(Type
::TraitObject(content
.parse()?
)),
468 if content
.peek(Token
![?
]) {
469 return Ok(Type
::TraitObject(TypeTraitObject
{
472 let mut bounds
= Punctuated
::new();
473 bounds
.push_value(TypeParamBound
::Trait(TraitBound
{
474 paren_token
: Some(paren_token
),
477 while let Some(plus
) = input
.parse()?
{
478 bounds
.push_punct(plus
);
479 bounds
.push_value(input
.parse()?
);
485 let mut first
: Type
= content
.parse()?
;
486 if content
.peek(Token
![,]) {
487 return Ok(Type
::Tuple(TypeTuple
{
490 let mut elems
= Punctuated
::new();
491 elems
.push_value(first
);
492 elems
.push_punct(content
.parse()?
);
493 let rest
: Punctuated
<Type
, Token
![,]> =
494 content
.parse_terminated(Parse
::parse
)?
;
500 if allow_plus
&& input
.peek(Token
![+]) {
502 let first
= match first
{
503 Type
::Path(TypePath { qself: None, path }
) => {
504 TypeParamBound
::Trait(TraitBound
{
505 paren_token
: Some(paren_token
),
506 modifier
: TraitBoundModifier
::None
,
511 Type
::TraitObject(TypeTraitObject
{
515 if bounds
.len() > 1 || bounds
.trailing_punct() {
516 first
= Type
::TraitObject(TypeTraitObject
{
522 match bounds
.into_iter().next().unwrap() {
523 TypeParamBound
::Trait(trait_bound
) => {
524 TypeParamBound
::Trait(TraitBound
{
525 paren_token
: Some(paren_token
),
534 return Ok(Type
::TraitObject(TypeTraitObject
{
537 let mut bounds
= Punctuated
::new();
538 bounds
.push_value(first
);
539 while let Some(plus
) = input
.parse()?
{
540 bounds
.push_punct(plus
);
541 bounds
.push_value(input
.parse()?
);
548 Ok(Type
::Paren(TypeParen
{
550 elem
: Box
::new(first
),
552 } else if lookahead
.peek(Token
![fn])
553 || lookahead
.peek(Token
![unsafe])
554 || lookahead
.peek(Token
![extern]) && !input
.peek2(Token
![::])
556 let mut bare_fn
: TypeBareFn
= input
.parse()?
;
557 bare_fn
.lifetimes
= lifetimes
;
558 Ok(Type
::BareFn(bare_fn
))
559 } else if lookahead
.peek(Ident
)
560 || input
.peek(Token
![super])
561 || input
.peek(Token
![self])
562 || input
.peek(Token
![Self])
563 || input
.peek(Token
![crate])
564 || input
.peek(Token
![extern])
565 || lookahead
.peek(Token
![::])
566 || lookahead
.peek(Token
![<])
568 if input
.peek(Token
![dyn]) {
569 let mut trait_object
: TypeTraitObject
= input
.parse()?
;
570 if lifetimes
.is_some() {
571 match trait_object
.bounds
.iter_mut().next().unwrap() {
572 TypeParamBound
::Trait(trait_bound
) => {
573 trait_bound
.lifetimes
= lifetimes
;
575 TypeParamBound
::Lifetime(_
) => unreachable
!(),
578 return Ok(Type
::TraitObject(trait_object
));
581 let ty
: TypePath
= input
.parse()?
;
582 if ty
.qself
.is_some() {
583 return Ok(Type
::Path(ty
));
586 if input
.peek(Token
![!]) && !input
.peek(Token
![!=]) {
587 let mut contains_arguments
= false;
588 for segment
in &ty
.path
.segments
{
589 match segment
.arguments
{
590 PathArguments
::None
=> {}
591 PathArguments
::AngleBracketed(_
) | PathArguments
::Parenthesized(_
) => {
592 contains_arguments
= true;
597 if !contains_arguments
{
598 let bang_token
: Token
![!] = input
.parse()?
;
599 let (delimiter
, tokens
) = mac
::parse_delimiter(input
)?
;
600 return Ok(Type
::Macro(TypeMacro
{
611 if lifetimes
.is_some() || allow_plus
&& input
.peek(Token
![+]) {
612 let mut bounds
= Punctuated
::new();
613 bounds
.push_value(TypeParamBound
::Trait(TraitBound
{
615 modifier
: TraitBoundModifier
::None
,
620 while input
.peek(Token
![+]) {
621 bounds
.push_punct(input
.parse()?
);
622 if input
.peek(Token
![>]) {
625 bounds
.push_value(input
.parse()?
);
628 return Ok(Type
::TraitObject(TypeTraitObject
{
635 } else if lookahead
.peek(token
::Bracket
) {
637 let bracket_token
= bracketed
!(content
in input
);
638 let elem
: Type
= content
.parse()?
;
639 if content
.peek(Token
![;]) {
640 Ok(Type
::Array(TypeArray
{
642 elem
: Box
::new(elem
),
643 semi_token
: content
.parse()?
,
644 len
: content
.parse()?
,
647 Ok(Type
::Slice(TypeSlice
{
649 elem
: Box
::new(elem
),
652 } else if lookahead
.peek(Token
![*]) {
653 input
.parse().map(Type
::Ptr
)
654 } else if lookahead
.peek(Token
![&]) {
655 input
.parse().map(Type
::Reference
)
656 } else if lookahead
.peek(Token
![!]) && !input
.peek(Token
![=]) {
657 input
.parse().map(Type
::Never
)
658 } else if lookahead
.peek(Token
![impl]) {
659 input
.parse().map(Type
::ImplTrait
)
660 } else if lookahead
.peek(Token
![_
]) {
661 input
.parse().map(Type
::Infer
)
662 } else if lookahead
.peek(Lifetime
) {
663 input
.parse().map(Type
::TraitObject
)
665 Err(lookahead
.error())
669 impl Parse
for TypeSlice
{
670 fn parse(input
: ParseStream
) -> Result
<Self> {
673 bracket_token
: bracketed
!(content
in input
),
674 elem
: content
.parse()?
,
679 impl Parse
for TypeArray
{
680 fn parse(input
: ParseStream
) -> Result
<Self> {
683 bracket_token
: bracketed
!(content
in input
),
684 elem
: content
.parse()?
,
685 semi_token
: content
.parse()?
,
686 len
: content
.parse()?
,
691 impl Parse
for TypePtr
{
692 fn parse(input
: ParseStream
) -> Result
<Self> {
693 let star_token
: Token
![*] = input
.parse()?
;
695 let lookahead
= input
.lookahead1();
696 let (const_token
, mutability
) = if lookahead
.peek(Token
![const]) {
697 (Some(input
.parse()?
), None
)
698 } else if lookahead
.peek(Token
![mut]) {
699 (None
, Some(input
.parse()?
))
701 return Err(lookahead
.error());
708 elem
: Box
::new(input
.call(Type
::without_plus
)?
),
713 impl Parse
for TypeReference
{
714 fn parse(input
: ParseStream
) -> Result
<Self> {
716 and_token
: input
.parse()?
,
717 lifetime
: input
.parse()?
,
718 mutability
: input
.parse()?
,
719 // & binds tighter than +, so we don't allow + here.
720 elem
: Box
::new(input
.call(Type
::without_plus
)?
),
725 impl Parse
for TypeBareFn
{
726 fn parse(input
: ParseStream
) -> Result
<Self> {
728 let mut variadic
= None
;
730 lifetimes
: input
.parse()?
,
731 unsafety
: input
.parse()?
,
733 fn_token
: input
.parse()?
,
734 paren_token
: parenthesized
!(args
in input
),
736 let mut inputs
= Punctuated
::new();
738 while !args
.is_empty() {
739 let attrs
= args
.call(Attribute
::parse_outer
)?
;
741 if inputs
.empty_or_trailing() && args
.peek(Token
![...]) {
742 variadic
= Some(Variadic
{
749 inputs
.push_value(BareFnArg
{
757 inputs
.push_punct(args
.parse()?
);
763 output
: input
.call(ReturnType
::without_plus
)?
,
768 impl Parse
for TypeNever
{
769 fn parse(input
: ParseStream
) -> Result
<Self> {
771 bang_token
: input
.parse()?
,
776 impl Parse
for TypeInfer
{
777 fn parse(input
: ParseStream
) -> Result
<Self> {
779 underscore_token
: input
.parse()?
,
784 impl Parse
for TypeTuple
{
785 fn parse(input
: ParseStream
) -> Result
<Self> {
787 let paren_token
= parenthesized
!(content
in input
);
789 if content
.is_empty() {
790 return Ok(TypeTuple
{
792 elems
: Punctuated
::new(),
796 let first
: Type
= content
.parse()?
;
800 let mut elems
= Punctuated
::new();
801 elems
.push_value(first
);
802 elems
.push_punct(content
.parse()?
);
803 let rest
: Punctuated
<Type
, Token
![,]> =
804 content
.parse_terminated(Parse
::parse
)?
;
812 impl Parse
for TypeMacro
{
813 fn parse(input
: ParseStream
) -> Result
<Self> {
820 impl Parse
for TypePath
{
821 fn parse(input
: ParseStream
) -> Result
<Self> {
822 let (qself
, mut path
) = path
::parsing
::qpath(input
, false)?
;
824 if path
.segments
.last().unwrap().arguments
.is_empty() && input
.peek(token
::Paren
) {
825 let args
: ParenthesizedGenericArguments
= input
.parse()?
;
826 let parenthesized
= PathArguments
::Parenthesized(args
);
827 path
.segments
.last_mut().unwrap().arguments
= parenthesized
;
830 Ok(TypePath { qself, path }
)
835 pub fn without_plus(input
: ParseStream
) -> Result
<Self> {
836 Self::parse(input
, false)
839 pub fn parse(input
: ParseStream
, allow_plus
: bool
) -> Result
<Self> {
840 if input
.peek(Token
![->]) {
841 let arrow
= input
.parse()?
;
842 let ty
= ambig_ty(input
, allow_plus
)?
;
843 Ok(ReturnType
::Type(arrow
, Box
::new(ty
)))
845 Ok(ReturnType
::Default
)
850 impl Parse
for ReturnType
{
851 fn parse(input
: ParseStream
) -> Result
<Self> {
852 Self::parse(input
, true)
856 impl Parse
for TypeTraitObject
{
857 fn parse(input
: ParseStream
) -> Result
<Self> {
858 Self::parse(input
, true)
862 fn at_least_one_type(bounds
: &Punctuated
<TypeParamBound
, Token
![+]>) -> bool
{
863 for bound
in bounds
{
864 if let TypeParamBound
::Trait(_
) = *bound
{
871 impl TypeTraitObject
{
872 pub fn without_plus(input
: ParseStream
) -> Result
<Self> {
873 Self::parse(input
, false)
876 // Only allow multiple trait references if allow_plus is true.
877 pub fn parse(input
: ParseStream
, allow_plus
: bool
) -> Result
<Self> {
879 dyn_token
: input
.parse()?
,
881 let mut bounds
= Punctuated
::new();
884 bounds
.push_value(input
.parse()?
);
885 if !input
.peek(Token
![+]) {
888 bounds
.push_punct(input
.parse()?
);
889 if input
.peek(Token
![>]) {
894 bounds
.push_value(input
.parse()?
);
896 // Just lifetimes like `'a + 'b` is not a TraitObject.
897 if !at_least_one_type(&bounds
) {
898 return Err(input
.error("expected at least one type"));
906 impl Parse
for TypeImplTrait
{
907 fn parse(input
: ParseStream
) -> Result
<Self> {
909 impl_token
: input
.parse()?
,
910 // NOTE: rust-lang/rust#34511 includes discussion about whether
911 // or not + should be allowed in ImplTrait directly without ().
913 let mut bounds
= Punctuated
::new();
915 bounds
.push_value(input
.parse()?
);
916 if !input
.peek(Token
![+]) {
919 bounds
.push_punct(input
.parse()?
);
927 impl Parse
for TypeGroup
{
928 fn parse(input
: ParseStream
) -> Result
<Self> {
929 let group
= crate::group
::parse_group(input
)?
;
931 group_token
: group
.token
,
932 elem
: group
.content
.parse()?
,
937 impl Parse
for TypeParen
{
938 fn parse(input
: ParseStream
) -> Result
<Self> {
939 Self::parse(input
, false)
944 fn parse(input
: ParseStream
, allow_plus
: bool
) -> Result
<Self> {
947 paren_token
: parenthesized
!(content
in input
),
948 elem
: Box
::new(ambig_ty(&content
, allow_plus
)?
),
953 impl Parse
for BareFnArg
{
954 fn parse(input
: ParseStream
) -> Result
<Self> {
956 attrs
: input
.call(Attribute
::parse_outer
)?
,
958 if (input
.peek(Ident
) || input
.peek(Token
![_
]))
959 && input
.peek2(Token
![:])
960 && !input
.peek2(Token
![::])
962 let name
= input
.call(Ident
::parse_any
)?
;
963 let colon
: Token
![:] = input
.parse()?
;
969 ty
: match input
.parse
::<Option
<Token
![...]>>()?
{
972 TokenTree
::Punct(Punct
::new('
.'
, Spacing
::Joint
)),
973 TokenTree
::Punct(Punct
::new('
.'
, Spacing
::Joint
)),
974 TokenTree
::Punct(Punct
::new('
.'
, Spacing
::Alone
)),
976 let tokens
= TokenStream
::from_iter(args
.into_iter().zip(&dot3
.spans
).map(
982 Type
::Verbatim(tokens
)
984 None
=> input
.parse()?
,
991 fn parse(input
: ParseStream
) -> Result
<Self> {
993 extern_token
: input
.parse()?
,
994 name
: input
.parse()?
,
999 impl Parse
for Option
<Abi
> {
1000 fn parse(input
: ParseStream
) -> Result
<Self> {
1001 if input
.peek(Token
![extern]) {
1002 input
.parse().map(Some
)
1010 #[cfg(feature = "printing")]
1014 use proc_macro2
::TokenStream
;
1015 use quote
::{ToTokens, TokenStreamExt}
;
1017 use crate::attr
::FilterAttrs
;
1018 use crate::print
::TokensOrDefault
;
1020 impl ToTokens
for TypeSlice
{
1021 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1022 self.bracket_token
.surround(tokens
, |tokens
| {
1023 self.elem
.to_tokens(tokens
);
1028 impl ToTokens
for TypeArray
{
1029 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1030 self.bracket_token
.surround(tokens
, |tokens
| {
1031 self.elem
.to_tokens(tokens
);
1032 self.semi_token
.to_tokens(tokens
);
1033 self.len
.to_tokens(tokens
);
1038 impl ToTokens
for TypePtr
{
1039 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1040 self.star_token
.to_tokens(tokens
);
1041 match &self.mutability
{
1042 Some(tok
) => tok
.to_tokens(tokens
),
1044 TokensOrDefault(&self.const_token
).to_tokens(tokens
);
1047 self.elem
.to_tokens(tokens
);
1051 impl ToTokens
for TypeReference
{
1052 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1053 self.and_token
.to_tokens(tokens
);
1054 self.lifetime
.to_tokens(tokens
);
1055 self.mutability
.to_tokens(tokens
);
1056 self.elem
.to_tokens(tokens
);
1060 impl ToTokens
for TypeBareFn
{
1061 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1062 self.lifetimes
.to_tokens(tokens
);
1063 self.unsafety
.to_tokens(tokens
);
1064 self.abi
.to_tokens(tokens
);
1065 self.fn_token
.to_tokens(tokens
);
1066 self.paren_token
.surround(tokens
, |tokens
| {
1067 self.inputs
.to_tokens(tokens
);
1068 if let Some(variadic
) = &self.variadic
{
1069 if !self.inputs
.empty_or_trailing() {
1070 let span
= variadic
.dots
.spans
[0];
1071 Token
![,](span
).to_tokens(tokens
);
1073 variadic
.to_tokens(tokens
);
1076 self.output
.to_tokens(tokens
);
1080 impl ToTokens
for TypeNever
{
1081 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1082 self.bang_token
.to_tokens(tokens
);
1086 impl ToTokens
for TypeTuple
{
1087 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1088 self.paren_token
.surround(tokens
, |tokens
| {
1089 self.elems
.to_tokens(tokens
);
1094 impl ToTokens
for TypePath
{
1095 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1096 private
::print_path(tokens
, &self.qself
, &self.path
);
1100 impl ToTokens
for TypeTraitObject
{
1101 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1102 self.dyn_token
.to_tokens(tokens
);
1103 self.bounds
.to_tokens(tokens
);
1107 impl ToTokens
for TypeImplTrait
{
1108 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1109 self.impl_token
.to_tokens(tokens
);
1110 self.bounds
.to_tokens(tokens
);
1114 impl ToTokens
for TypeGroup
{
1115 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1116 self.group_token
.surround(tokens
, |tokens
| {
1117 self.elem
.to_tokens(tokens
);
1122 impl ToTokens
for TypeParen
{
1123 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1124 self.paren_token
.surround(tokens
, |tokens
| {
1125 self.elem
.to_tokens(tokens
);
1130 impl ToTokens
for TypeInfer
{
1131 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1132 self.underscore_token
.to_tokens(tokens
);
1136 impl ToTokens
for TypeMacro
{
1137 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1138 self.mac
.to_tokens(tokens
);
1142 impl ToTokens
for ReturnType
{
1143 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1145 ReturnType
::Default
=> {}
1146 ReturnType
::Type(arrow
, ty
) => {
1147 arrow
.to_tokens(tokens
);
1148 ty
.to_tokens(tokens
);
1154 impl ToTokens
for BareFnArg
{
1155 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1156 tokens
.append_all(self.attrs
.outer());
1157 if let Some((name
, colon
)) = &self.name
{
1158 name
.to_tokens(tokens
);
1159 colon
.to_tokens(tokens
);
1161 self.ty
.to_tokens(tokens
);
1165 impl ToTokens
for Variadic
{
1166 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1167 tokens
.append_all(self.attrs
.outer());
1168 self.dots
.to_tokens(tokens
);
1172 impl ToTokens
for Abi
{
1173 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1174 self.extern_token
.to_tokens(tokens
);
1175 self.name
.to_tokens(tokens
);