2 use crate::punctuated
::Punctuated
;
3 use proc_macro2
::TokenStream
;
6 /// The possible types that a Rust value could have.
8 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
11 /// # Syntax tree enum
13 /// This type is a [syntax tree enum].
15 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
17 // TODO: change syntax-tree-enum link to an intra rustdoc link, currently
18 // blocked on https://github.com/rust-lang/rust/issues/62833
20 /// A fixed size array type: `[T; n]`.
23 /// A bare function type: `fn(usize) -> bool`.
26 /// A type contained within invisible delimiters.
29 /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or
31 ImplTrait(TypeImplTrait
),
33 /// Indication that a type should be inferred by the compiler: `_`.
36 /// A macro in the type position.
39 /// The never type: `!`.
42 /// A parenthesized type equivalent to the inner type.
45 /// A path like `std::slice::Iter`, optionally qualified with a
46 /// self-type as in `<Vec<T> as SomeTrait>::Associated`.
49 /// A raw pointer type: `*const T` or `*mut T`.
52 /// A reference type: `&'a T` or `&'a mut T`.
53 Reference(TypeReference
),
55 /// A dynamically sized slice type: `[T]`.
58 /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a
59 /// trait or a lifetime.
60 TraitObject(TypeTraitObject
),
62 /// A tuple type: `(A, B, C, String)`.
65 /// Tokens in type position not interpreted by Syn.
66 Verbatim(TokenStream
),
74 /// A fixed size array type: `[T; n]`.
76 /// *This type is available only if Syn is built with the `"derive"` or
77 /// `"full"` feature.*
78 pub struct TypeArray
{
79 pub bracket_token
: token
::Bracket
,
81 pub semi_token
: Token
![;],
87 /// A bare function type: `fn(usize) -> bool`.
89 /// *This type is available only if Syn is built with the `"derive"` or
90 /// `"full"` feature.*
91 pub struct TypeBareFn
{
92 pub lifetimes
: Option
<BoundLifetimes
>,
93 pub unsafety
: Option
<Token
![unsafe]>,
95 pub fn_token
: Token
![fn],
96 pub paren_token
: token
::Paren
,
97 pub inputs
: Punctuated
<BareFnArg
, Token
![,]>,
98 pub variadic
: Option
<Variadic
>,
99 pub output
: ReturnType
,
104 /// A type contained within invisible delimiters.
106 /// *This type is available only if Syn is built with the `"derive"` or
107 /// `"full"` feature.*
108 pub struct TypeGroup
{
109 pub group_token
: token
::Group
,
115 /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or
118 /// *This type is available only if Syn is built with the `"derive"` or
119 /// `"full"` feature.*
120 pub struct TypeImplTrait
{
121 pub impl_token
: Token
![impl],
122 pub bounds
: Punctuated
<TypeParamBound
, Token
![+]>,
127 /// Indication that a type should be inferred by the compiler: `_`.
129 /// *This type is available only if Syn is built with the `"derive"` or
130 /// `"full"` feature.*
131 pub struct TypeInfer
{
132 pub underscore_token
: Token
![_
],
137 /// A macro in the type position.
139 /// *This type is available only if Syn is built with the `"derive"` or
140 /// `"full"` feature.*
141 pub struct TypeMacro
{
147 /// The never type: `!`.
149 /// *This type is available only if Syn is built with the `"derive"` or
150 /// `"full"` feature.*
151 pub struct TypeNever
{
152 pub bang_token
: Token
![!],
157 /// A parenthesized type equivalent to the inner type.
159 /// *This type is available only if Syn is built with the `"derive"` or
160 /// `"full"` feature.*
161 pub struct TypeParen
{
162 pub paren_token
: token
::Paren
,
168 /// A path like `std::slice::Iter`, optionally qualified with a
169 /// self-type as in `<Vec<T> as SomeTrait>::Associated`.
171 /// *This type is available only if Syn is built with the `"derive"` or
172 /// `"full"` feature.*
173 pub struct TypePath
{
174 pub qself
: Option
<QSelf
>,
180 /// A raw pointer type: `*const T` or `*mut T`.
182 /// *This type is available only if Syn is built with the `"derive"` or
183 /// `"full"` feature.*
185 pub star_token
: Token
![*],
186 pub const_token
: Option
<Token
![const]>,
187 pub mutability
: Option
<Token
![mut]>,
193 /// A reference type: `&'a T` or `&'a mut T`.
195 /// *This type is available only if Syn is built with the `"derive"` or
196 /// `"full"` feature.*
197 pub struct TypeReference
{
198 pub and_token
: Token
![&],
199 pub lifetime
: Option
<Lifetime
>,
200 pub mutability
: Option
<Token
![mut]>,
206 /// A dynamically sized slice type: `[T]`.
208 /// *This type is available only if Syn is built with the `"derive"` or
209 /// `"full"` feature.*
210 pub struct TypeSlice
{
211 pub bracket_token
: token
::Bracket
,
217 /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a
218 /// trait or a lifetime.
220 /// *This type is available only if Syn is built with the `"derive"` or
221 /// `"full"` feature.*
222 pub struct TypeTraitObject
{
223 pub dyn_token
: Option
<Token
![dyn]>,
224 pub bounds
: Punctuated
<TypeParamBound
, Token
![+]>,
229 /// A tuple type: `(A, B, C, String)`.
231 /// *This type is available only if Syn is built with the `"derive"` or
232 /// `"full"` feature.*
233 pub struct TypeTuple
{
234 pub paren_token
: token
::Paren
,
235 pub elems
: Punctuated
<Type
, Token
![,]>,
240 /// The binary interface of a function: `extern "C"`.
242 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
245 pub extern_token
: Token
![extern],
246 pub name
: Option
<LitStr
>,
251 /// An argument in a function type: the `usize` in `fn(usize) -> bool`.
253 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
255 pub struct BareFnArg
{
256 pub attrs
: Vec
<Attribute
>,
257 pub name
: Option
<(Ident
, Token
![:])>,
263 /// The variadic argument of a foreign function.
270 /// fn printf(format: *const c_char, ...) -> c_int;
275 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
277 pub struct Variadic
{
278 pub attrs
: Vec
<Attribute
>,
279 pub dots
: Token
![...],
284 /// Return type of a function signature.
286 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
288 pub enum ReturnType
{
289 /// Return type is not specified.
291 /// Functions default to `()` and closures default to type inference.
293 /// A particular type is returned.
294 Type(Token
![->], Box
<Type
>),
298 #[cfg(feature = "parsing")]
302 use crate::ext
::IdentExt
;
303 use crate::parse
::{Parse, ParseStream, Result}
;
305 use proc_macro2
::{Punct, Spacing, TokenTree}
;
306 use std
::iter
::FromIterator
;
308 impl Parse
for Type
{
309 fn parse(input
: ParseStream
) -> Result
<Self> {
310 let allow_plus
= true;
311 ambig_ty(input
, allow_plus
)
316 /// In some positions, types may not contain the `+` character, to
317 /// disambiguate them. For example in the expression `1 as T`, T may not
318 /// contain a `+` character.
320 /// This parser does not allow a `+`, while the default parser does.
321 pub fn without_plus(input
: ParseStream
) -> Result
<Self> {
322 let allow_plus
= false;
323 ambig_ty(input
, allow_plus
)
327 fn ambig_ty(input
: ParseStream
, allow_plus
: bool
) -> Result
<Type
> {
328 if input
.peek(token
::Group
) && !input
.peek2(Token
![::]) && !input
.peek2(Token
![<]) {
329 return input
.parse().map(Type
::Group
);
332 let begin
= input
.fork();
333 let mut lifetimes
= None
::<BoundLifetimes
>;
334 let mut lookahead
= input
.lookahead1();
335 if lookahead
.peek(Token
![for]) {
336 lifetimes
= input
.parse()?
;
337 lookahead
= input
.lookahead1();
338 if !lookahead
.peek(Ident
)
339 && !lookahead
.peek(Token
![fn])
340 && !lookahead
.peek(Token
![unsafe])
341 && !lookahead
.peek(Token
![extern])
342 && !lookahead
.peek(Token
![super])
343 && !lookahead
.peek(Token
![self])
344 && !lookahead
.peek(Token
![Self])
345 && !lookahead
.peek(Token
![crate])
347 return Err(lookahead
.error());
351 if lookahead
.peek(token
::Paren
) {
353 let paren_token
= parenthesized
!(content
in input
);
354 if content
.is_empty() {
355 return Ok(Type
::Tuple(TypeTuple
{
357 elems
: Punctuated
::new(),
360 if content
.peek(Lifetime
) {
361 return Ok(Type
::Paren(TypeParen
{
363 elem
: Box
::new(Type
::TraitObject(content
.parse()?
)),
366 if content
.peek(Token
![?
]) {
367 return Ok(Type
::TraitObject(TypeTraitObject
{
370 let mut bounds
= Punctuated
::new();
371 bounds
.push_value(TypeParamBound
::Trait(TraitBound
{
372 paren_token
: Some(paren_token
),
375 while let Some(plus
) = input
.parse()?
{
376 bounds
.push_punct(plus
);
377 bounds
.push_value(input
.parse()?
);
383 let mut first
: Type
= content
.parse()?
;
384 if content
.peek(Token
![,]) {
385 return Ok(Type
::Tuple(TypeTuple
{
388 let mut elems
= Punctuated
::new();
389 elems
.push_value(first
);
390 elems
.push_punct(content
.parse()?
);
391 let rest
: Punctuated
<Type
, Token
![,]> =
392 content
.parse_terminated(Parse
::parse
)?
;
398 if allow_plus
&& input
.peek(Token
![+]) {
400 let first
= match first
{
401 Type
::Path(TypePath { qself: None, path }
) => {
402 TypeParamBound
::Trait(TraitBound
{
403 paren_token
: Some(paren_token
),
404 modifier
: TraitBoundModifier
::None
,
409 Type
::TraitObject(TypeTraitObject
{
413 if bounds
.len() > 1 || bounds
.trailing_punct() {
414 first
= Type
::TraitObject(TypeTraitObject
{
420 match bounds
.into_iter().next().unwrap() {
421 TypeParamBound
::Trait(trait_bound
) => {
422 TypeParamBound
::Trait(TraitBound
{
423 paren_token
: Some(paren_token
),
427 other @ TypeParamBound
::Lifetime(_
) => other
,
432 return Ok(Type
::TraitObject(TypeTraitObject
{
435 let mut bounds
= Punctuated
::new();
436 bounds
.push_value(first
);
437 while let Some(plus
) = input
.parse()?
{
438 bounds
.push_punct(plus
);
439 bounds
.push_value(input
.parse()?
);
446 Ok(Type
::Paren(TypeParen
{
448 elem
: Box
::new(first
),
450 } else if lookahead
.peek(Token
![fn])
451 || lookahead
.peek(Token
![unsafe])
452 || lookahead
.peek(Token
![extern])
454 let allow_mut_self
= true;
455 if let Some(mut bare_fn
) = parse_bare_fn(input
, allow_mut_self
)?
{
456 bare_fn
.lifetimes
= lifetimes
;
457 Ok(Type
::BareFn(bare_fn
))
459 Ok(Type
::Verbatim(verbatim
::between(begin
, input
)))
461 } else if lookahead
.peek(Ident
)
462 || input
.peek(Token
![super])
463 || input
.peek(Token
![self])
464 || input
.peek(Token
![Self])
465 || input
.peek(Token
![crate])
466 || lookahead
.peek(Token
![::])
467 || lookahead
.peek(Token
![<])
469 if input
.peek(Token
![dyn]) {
470 let mut trait_object
: TypeTraitObject
= input
.parse()?
;
471 if lifetimes
.is_some() {
472 match trait_object
.bounds
.iter_mut().next().unwrap() {
473 TypeParamBound
::Trait(trait_bound
) => {
474 trait_bound
.lifetimes
= lifetimes
;
476 TypeParamBound
::Lifetime(_
) => unreachable
!(),
479 return Ok(Type
::TraitObject(trait_object
));
482 let ty
: TypePath
= input
.parse()?
;
483 if ty
.qself
.is_some() {
484 return Ok(Type
::Path(ty
));
487 if input
.peek(Token
![!]) && !input
.peek(Token
![!=]) {
488 let mut contains_arguments
= false;
489 for segment
in &ty
.path
.segments
{
490 match segment
.arguments
{
491 PathArguments
::None
=> {}
492 PathArguments
::AngleBracketed(_
) | PathArguments
::Parenthesized(_
) => {
493 contains_arguments
= true;
498 if !contains_arguments
{
499 let bang_token
: Token
![!] = input
.parse()?
;
500 let (delimiter
, tokens
) = mac
::parse_delimiter(input
)?
;
501 return Ok(Type
::Macro(TypeMacro
{
512 if lifetimes
.is_some() || allow_plus
&& input
.peek(Token
![+]) {
513 let mut bounds
= Punctuated
::new();
514 bounds
.push_value(TypeParamBound
::Trait(TraitBound
{
516 modifier
: TraitBoundModifier
::None
,
521 while input
.peek(Token
![+]) {
522 bounds
.push_punct(input
.parse()?
);
523 if input
.peek(Token
![>]) {
526 bounds
.push_value(input
.parse()?
);
529 return Ok(Type
::TraitObject(TypeTraitObject
{
536 } else if lookahead
.peek(token
::Bracket
) {
538 let bracket_token
= bracketed
!(content
in input
);
539 let elem
: Type
= content
.parse()?
;
540 if content
.peek(Token
![;]) {
541 Ok(Type
::Array(TypeArray
{
543 elem
: Box
::new(elem
),
544 semi_token
: content
.parse()?
,
545 len
: content
.parse()?
,
548 Ok(Type
::Slice(TypeSlice
{
550 elem
: Box
::new(elem
),
553 } else if lookahead
.peek(Token
![*]) {
554 input
.parse().map(Type
::Ptr
)
555 } else if lookahead
.peek(Token
![&]) {
556 input
.parse().map(Type
::Reference
)
557 } else if lookahead
.peek(Token
![!]) && !input
.peek(Token
![=]) {
558 input
.parse().map(Type
::Never
)
559 } else if lookahead
.peek(Token
![impl]) {
560 input
.parse().map(Type
::ImplTrait
)
561 } else if lookahead
.peek(Token
![_
]) {
562 input
.parse().map(Type
::Infer
)
563 } else if lookahead
.peek(Lifetime
) {
564 input
.parse().map(Type
::TraitObject
)
566 Err(lookahead
.error())
570 impl Parse
for TypeSlice
{
571 fn parse(input
: ParseStream
) -> Result
<Self> {
574 bracket_token
: bracketed
!(content
in input
),
575 elem
: content
.parse()?
,
580 impl Parse
for TypeArray
{
581 fn parse(input
: ParseStream
) -> Result
<Self> {
584 bracket_token
: bracketed
!(content
in input
),
585 elem
: content
.parse()?
,
586 semi_token
: content
.parse()?
,
587 len
: content
.parse()?
,
592 impl Parse
for TypePtr
{
593 fn parse(input
: ParseStream
) -> Result
<Self> {
594 let star_token
: Token
![*] = input
.parse()?
;
596 let lookahead
= input
.lookahead1();
597 let (const_token
, mutability
) = if lookahead
.peek(Token
![const]) {
598 (Some(input
.parse()?
), None
)
599 } else if lookahead
.peek(Token
![mut]) {
600 (None
, Some(input
.parse()?
))
602 return Err(lookahead
.error());
609 elem
: Box
::new(input
.call(Type
::without_plus
)?
),
614 impl Parse
for TypeReference
{
615 fn parse(input
: ParseStream
) -> Result
<Self> {
617 and_token
: input
.parse()?
,
618 lifetime
: input
.parse()?
,
619 mutability
: input
.parse()?
,
620 // & binds tighter than +, so we don't allow + here.
621 elem
: Box
::new(input
.call(Type
::without_plus
)?
),
626 impl Parse
for TypeBareFn
{
627 fn parse(input
: ParseStream
) -> Result
<Self> {
628 let allow_mut_self
= false;
629 parse_bare_fn(input
, allow_mut_self
).map(Option
::unwrap
)
633 fn parse_bare_fn(input
: ParseStream
, allow_mut_self
: bool
) -> Result
<Option
<TypeBareFn
>> {
635 let mut variadic
= None
;
636 let mut has_mut_self
= false;
638 let bare_fn
= TypeBareFn
{
639 lifetimes
: input
.parse()?
,
640 unsafety
: input
.parse()?
,
642 fn_token
: input
.parse()?
,
643 paren_token
: parenthesized
!(args
in input
),
645 let mut inputs
= Punctuated
::new();
647 while !args
.is_empty() {
648 let attrs
= args
.call(Attribute
::parse_outer
)?
;
650 if inputs
.empty_or_trailing() && args
.peek(Token
![...]) {
651 variadic
= Some(Variadic
{
658 if let Some(arg
) = parse_bare_fn_arg(&args
, allow_mut_self
)?
{
659 inputs
.push_value(BareFnArg { attrs, ..arg }
);
667 inputs
.push_punct(args
.parse()?
);
673 output
: input
.call(ReturnType
::without_plus
)?
,
683 impl Parse
for TypeNever
{
684 fn parse(input
: ParseStream
) -> Result
<Self> {
686 bang_token
: input
.parse()?
,
691 impl Parse
for TypeInfer
{
692 fn parse(input
: ParseStream
) -> Result
<Self> {
694 underscore_token
: input
.parse()?
,
699 impl Parse
for TypeTuple
{
700 fn parse(input
: ParseStream
) -> Result
<Self> {
702 let paren_token
= parenthesized
!(content
in input
);
704 if content
.is_empty() {
705 return Ok(TypeTuple
{
707 elems
: Punctuated
::new(),
711 let first
: Type
= content
.parse()?
;
715 let mut elems
= Punctuated
::new();
716 elems
.push_value(first
);
717 elems
.push_punct(content
.parse()?
);
718 let rest
: Punctuated
<Type
, Token
![,]> =
719 content
.parse_terminated(Parse
::parse
)?
;
727 impl Parse
for TypeMacro
{
728 fn parse(input
: ParseStream
) -> Result
<Self> {
735 impl Parse
for TypePath
{
736 fn parse(input
: ParseStream
) -> Result
<Self> {
737 let (qself
, mut path
) = path
::parsing
::qpath(input
, false)?
;
739 if path
.segments
.last().unwrap().arguments
.is_empty() && input
.peek(token
::Paren
) {
740 let args
: ParenthesizedGenericArguments
= input
.parse()?
;
741 let parenthesized
= PathArguments
::Parenthesized(args
);
742 path
.segments
.last_mut().unwrap().arguments
= parenthesized
;
745 Ok(TypePath { qself, path }
)
750 pub fn without_plus(input
: ParseStream
) -> Result
<Self> {
751 let allow_plus
= false;
752 Self::parse(input
, allow_plus
)
756 pub fn parse(input
: ParseStream
, allow_plus
: bool
) -> Result
<Self> {
757 if input
.peek(Token
![->]) {
758 let arrow
= input
.parse()?
;
759 let ty
= ambig_ty(input
, allow_plus
)?
;
760 Ok(ReturnType
::Type(arrow
, Box
::new(ty
)))
762 Ok(ReturnType
::Default
)
767 impl Parse
for ReturnType
{
768 fn parse(input
: ParseStream
) -> Result
<Self> {
769 Self::parse(input
, true)
773 impl Parse
for TypeTraitObject
{
774 fn parse(input
: ParseStream
) -> Result
<Self> {
775 Self::parse(input
, true)
779 fn at_least_one_type(bounds
: &Punctuated
<TypeParamBound
, Token
![+]>) -> bool
{
780 for bound
in bounds
{
781 if let TypeParamBound
::Trait(_
) = *bound
{
788 impl TypeTraitObject
{
789 pub fn without_plus(input
: ParseStream
) -> Result
<Self> {
790 let allow_plus
= false;
791 Self::parse(input
, allow_plus
)
794 // Only allow multiple trait references if allow_plus is true.
796 pub fn parse(input
: ParseStream
, allow_plus
: bool
) -> Result
<Self> {
798 dyn_token
: input
.parse()?
,
800 let mut bounds
= Punctuated
::new();
803 bounds
.push_value(input
.parse()?
);
804 if !input
.peek(Token
![+]) {
807 bounds
.push_punct(input
.parse()?
);
808 if input
.peek(Token
![>]) {
813 bounds
.push_value(input
.parse()?
);
815 // Just lifetimes like `'a + 'b` is not a TraitObject.
816 if !at_least_one_type(&bounds
) {
817 return Err(input
.error("expected at least one type"));
825 impl Parse
for TypeImplTrait
{
826 fn parse(input
: ParseStream
) -> Result
<Self> {
828 impl_token
: input
.parse()?
,
829 // NOTE: rust-lang/rust#34511 includes discussion about whether
830 // or not + should be allowed in ImplTrait directly without ().
832 let mut bounds
= Punctuated
::new();
834 bounds
.push_value(input
.parse()?
);
835 if !input
.peek(Token
![+]) {
838 bounds
.push_punct(input
.parse()?
);
846 impl Parse
for TypeGroup
{
847 fn parse(input
: ParseStream
) -> Result
<Self> {
848 let group
= crate::group
::parse_group(input
)?
;
850 group_token
: group
.token
,
851 elem
: group
.content
.parse()?
,
856 impl Parse
for TypeParen
{
857 fn parse(input
: ParseStream
) -> Result
<Self> {
858 let allow_plus
= false;
859 Self::parse(input
, allow_plus
)
864 fn parse(input
: ParseStream
, allow_plus
: bool
) -> Result
<Self> {
867 paren_token
: parenthesized
!(content
in input
),
868 elem
: Box
::new(ambig_ty(&content
, allow_plus
)?
),
873 impl Parse
for BareFnArg
{
874 fn parse(input
: ParseStream
) -> Result
<Self> {
875 let allow_mut_self
= false;
876 parse_bare_fn_arg(input
, allow_mut_self
).map(Option
::unwrap
)
880 fn parse_bare_fn_arg(
882 mut allow_mut_self
: bool
,
883 ) -> Result
<Option
<BareFnArg
>> {
884 let mut has_mut_self
= false;
885 let arg
= BareFnArg
{
886 attrs
: input
.call(Attribute
::parse_outer
)?
,
888 if (input
.peek(Ident
) || input
.peek(Token
![_
]) || input
.peek(Token
![self]))
889 && input
.peek2(Token
![:])
890 && !input
.peek2(Token
![::])
892 let name
= input
.call(Ident
::parse_any
)?
;
893 let colon
: Token
![:] = input
.parse()?
;
895 } else if allow_mut_self
896 && input
.peek(Token
![mut])
897 && input
.peek2(Token
![self])
898 && input
.peek3(Token
![:])
899 && !input
.peek3(Token
![::])
902 allow_mut_self
= false;
903 input
.parse
::<Token
![mut]>()?
;
904 input
.parse
::<Token
![self]>()?
;
905 input
.parse
::<Token
![:]>()?
;
911 ty
: if !has_mut_self
&& input
.peek(Token
![...]) {
912 let dot3
= input
.parse
::<Token
![...]>()?
;
914 TokenTree
::Punct(Punct
::new('
.'
, Spacing
::Joint
)),
915 TokenTree
::Punct(Punct
::new('
.'
, Spacing
::Joint
)),
916 TokenTree
::Punct(Punct
::new('
.'
, Spacing
::Alone
)),
918 let tokens
= TokenStream
::from_iter(args
.into_iter().zip(&dot3
.spans
).map(
924 Type
::Verbatim(tokens
)
925 } else if allow_mut_self
&& input
.peek(Token
![mut]) && input
.peek2(Token
![self]) {
927 input
.parse
::<Token
![mut]>()?
;
928 Type
::Path(TypePath
{
930 path
: input
.parse
::<Token
![self]>()?
.into(),
945 fn parse(input
: ParseStream
) -> Result
<Self> {
947 extern_token
: input
.parse()?
,
948 name
: input
.parse()?
,
953 impl Parse
for Option
<Abi
> {
954 fn parse(input
: ParseStream
) -> Result
<Self> {
955 if input
.peek(Token
![extern]) {
956 input
.parse().map(Some
)
964 #[cfg(feature = "printing")]
968 use proc_macro2
::TokenStream
;
969 use quote
::{ToTokens, TokenStreamExt}
;
971 use crate::attr
::FilterAttrs
;
972 use crate::print
::TokensOrDefault
;
974 impl ToTokens
for TypeSlice
{
975 fn to_tokens(&self, tokens
: &mut TokenStream
) {
976 self.bracket_token
.surround(tokens
, |tokens
| {
977 self.elem
.to_tokens(tokens
);
982 impl ToTokens
for TypeArray
{
983 fn to_tokens(&self, tokens
: &mut TokenStream
) {
984 self.bracket_token
.surround(tokens
, |tokens
| {
985 self.elem
.to_tokens(tokens
);
986 self.semi_token
.to_tokens(tokens
);
987 self.len
.to_tokens(tokens
);
992 impl ToTokens
for TypePtr
{
993 fn to_tokens(&self, tokens
: &mut TokenStream
) {
994 self.star_token
.to_tokens(tokens
);
995 match &self.mutability
{
996 Some(tok
) => tok
.to_tokens(tokens
),
998 TokensOrDefault(&self.const_token
).to_tokens(tokens
);
1001 self.elem
.to_tokens(tokens
);
1005 impl ToTokens
for TypeReference
{
1006 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1007 self.and_token
.to_tokens(tokens
);
1008 self.lifetime
.to_tokens(tokens
);
1009 self.mutability
.to_tokens(tokens
);
1010 self.elem
.to_tokens(tokens
);
1014 impl ToTokens
for TypeBareFn
{
1015 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1016 self.lifetimes
.to_tokens(tokens
);
1017 self.unsafety
.to_tokens(tokens
);
1018 self.abi
.to_tokens(tokens
);
1019 self.fn_token
.to_tokens(tokens
);
1020 self.paren_token
.surround(tokens
, |tokens
| {
1021 self.inputs
.to_tokens(tokens
);
1022 if let Some(variadic
) = &self.variadic
{
1023 if !self.inputs
.empty_or_trailing() {
1024 let span
= variadic
.dots
.spans
[0];
1025 Token
![,](span
).to_tokens(tokens
);
1027 variadic
.to_tokens(tokens
);
1030 self.output
.to_tokens(tokens
);
1034 impl ToTokens
for TypeNever
{
1035 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1036 self.bang_token
.to_tokens(tokens
);
1040 impl ToTokens
for TypeTuple
{
1041 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1042 self.paren_token
.surround(tokens
, |tokens
| {
1043 self.elems
.to_tokens(tokens
);
1048 impl ToTokens
for TypePath
{
1049 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1050 private
::print_path(tokens
, &self.qself
, &self.path
);
1054 impl ToTokens
for TypeTraitObject
{
1055 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1056 self.dyn_token
.to_tokens(tokens
);
1057 self.bounds
.to_tokens(tokens
);
1061 impl ToTokens
for TypeImplTrait
{
1062 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1063 self.impl_token
.to_tokens(tokens
);
1064 self.bounds
.to_tokens(tokens
);
1068 impl ToTokens
for TypeGroup
{
1069 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1070 self.group_token
.surround(tokens
, |tokens
| {
1071 self.elem
.to_tokens(tokens
);
1076 impl ToTokens
for TypeParen
{
1077 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1078 self.paren_token
.surround(tokens
, |tokens
| {
1079 self.elem
.to_tokens(tokens
);
1084 impl ToTokens
for TypeInfer
{
1085 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1086 self.underscore_token
.to_tokens(tokens
);
1090 impl ToTokens
for TypeMacro
{
1091 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1092 self.mac
.to_tokens(tokens
);
1096 impl ToTokens
for ReturnType
{
1097 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1099 ReturnType
::Default
=> {}
1100 ReturnType
::Type(arrow
, ty
) => {
1101 arrow
.to_tokens(tokens
);
1102 ty
.to_tokens(tokens
);
1108 impl ToTokens
for BareFnArg
{
1109 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1110 tokens
.append_all(self.attrs
.outer());
1111 if let Some((name
, colon
)) = &self.name
{
1112 name
.to_tokens(tokens
);
1113 colon
.to_tokens(tokens
);
1115 self.ty
.to_tokens(tokens
);
1119 impl ToTokens
for Variadic
{
1120 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1121 tokens
.append_all(self.attrs
.outer());
1122 self.dots
.to_tokens(tokens
);
1126 impl ToTokens
for Abi
{
1127 fn to_tokens(&self, tokens
: &mut TokenStream
) {
1128 self.extern_token
.to_tokens(tokens
);
1129 self.name
.to_tokens(tokens
);