Skip to main content

typed_quote/
proc_macro_n.rs

1use crate::{
2    IntoTokenTree, IntoTokens, RefWithSpan, Span, ToTokenTree, ToTokens, WithSpan, into_st::IntoST,
3    maybe_span::MaybeSpan, replace_span_of::ReplaceSpanOf, sealed,
4};
5
6trait MyInto<S> {
7    fn my_into(self) -> S;
8}
9
10impl<T> MyInto<T> for T {
11    fn my_into(self) -> T {
12        self
13    }
14}
15
16#[cfg(any(feature = "proc-macro", feature = "proc-macro2"))]
17trait IdentIsDollarCrate {
18    fn ident_is_dollar_crate(&self) -> bool;
19}
20
21#[cfg(feature = "proc-macro")]
22#[cfg(feature = "proc-macro2")]
23mod convert_12;
24
25#[cfg(feature = "proc-macro")]
26mod proc_macro_1;
27#[cfg(feature = "proc-macro2")]
28mod proc_macro_2;
29
30mod replace_span_of_ref;
31
32crate::impl_many!({
33    {
34        #[cfg(feature = "proc-macro")]
35        {
36            use proc_macro as pmn;
37        }
38        #[cfg(feature = "proc-macro2")]
39        {
40            use proc_macro2 as pmn;
41        }
42    }
43
44    impl sealed::IntoTokens for pmn::TokenStream {}
45    #[allow(clippy::useless_conversion)]
46    impl IntoTokens for pmn::TokenStream {
47        crate::impl_into_tokens!(
48            |self, ts| {
49                ts.extend(pm::TokenStream::from(self));
50            },
51            self.into(),
52        );
53    }
54
55    impl sealed::ToTokens for pmn::TokenStream {}
56    impl ToTokens for pmn::TokenStream {
57        crate::impl_to_tokens!(
58            #[proxy]
59            |self| self.clone()
60        );
61    }
62
63    impl sealed::WithSpan for pmn::TokenStream {}
64    impl WithSpan for pmn::TokenStream {
65        type WithDefaultSpan<S: Span> = Self;
66
67        fn with_default_span<S: Span>(self, _: S) -> Self::WithDefaultSpan<S> {
68            self
69        }
70
71        type WithReplacedSpan<S: Span> = <S as ReplaceSpanOf<Self>>::ReplaceSpanOf;
72
73        fn with_replaced_span<S: Span>(self, span: S) -> Self::WithReplacedSpan<S> {
74            span.replace_span_of(self)
75        }
76    }
77
78    impl sealed::RefWithSpan for pmn::TokenStream {}
79    impl RefWithSpan for pmn::TokenStream {
80        type RefWithDefaultSpan<'a, S: Span>
81            = &'a Self
82        where
83            Self: 'a;
84
85        fn ref_with_default_span<S: Span>(&self, _: S) -> Self::RefWithDefaultSpan<'_, S> {
86            self
87        }
88
89        type RefWithReplacedSpan<'a, S: Span>
90            = replace_span_of_ref::ReplaceSpanOfRef<'a, Self, S>
91        where
92            Self: 'a;
93
94        fn ref_with_replaced_span<S: Span>(&self, span: S) -> Self::RefWithReplacedSpan<'_, S> {
95            replace_span_of_ref::ReplaceSpanOfRef(self, span)
96        }
97    }
98
99    // token tree
100    crate::impl_many!({
101        {
102            {
103                use pmn::TokenTree as TT;
104            }
105            {
106                use pmn::Group as TT;
107            }
108            {
109                use pmn::Ident as TT;
110            }
111            {
112                use pmn::Punct as TT;
113            }
114            {
115                use pmn::Literal as TT;
116            }
117        }
118
119        impl sealed::IntoTokenTree for TT {}
120        #[allow(clippy::useless_conversion)]
121        impl IntoTokenTree for TT {
122            crate::impl_into_token_tree!(|self| pmn::TokenTree::from(self).my_into());
123        }
124
125        impl sealed::ToTokenTree for TT {}
126        impl ToTokenTree for TT {
127            crate::impl_to_token_tree!(|self| self.clone().into_st());
128        }
129
130        impl sealed::IntoTokens for TT {}
131        impl IntoTokens for TT {
132            crate::impl_into_tokens! {tt}
133        }
134
135        impl sealed::ToTokens for TT {}
136        impl ToTokens for TT {
137            crate::impl_to_tokens! {tt}
138        }
139
140        impl sealed::WithSpan for TT {}
141        impl WithSpan for TT {
142            type WithDefaultSpan<S: Span> = Self;
143
144            fn with_default_span<S: Span>(self, _: S) -> Self::WithDefaultSpan<S> {
145                self
146            }
147
148            type WithReplacedSpan<S: Span> = <S as ReplaceSpanOf<TT>>::ReplaceSpanOf;
149
150            fn with_replaced_span<S: Span>(self, span: S) -> Self::WithReplacedSpan<S> {
151                span.replace_span_of(self)
152            }
153        }
154
155        impl sealed::RefWithSpan for TT {}
156        impl RefWithSpan for TT {
157            type RefWithDefaultSpan<'a, S: Span>
158                = &'a Self
159            where
160                Self: 'a;
161
162            fn ref_with_default_span<S: Span>(&self, _: S) -> Self::RefWithDefaultSpan<'_, S> {
163                self
164            }
165
166            type RefWithReplacedSpan<'a, S: Span>
167                = replace_span_of_ref::ReplaceSpanOfRef<'a, Self, S>
168            where
169                Self: 'a;
170
171            fn ref_with_replaced_span<S: Span>(&self, span: S) -> Self::RefWithReplacedSpan<'_, S> {
172                replace_span_of_ref::ReplaceSpanOfRef(self, span)
173            }
174        }
175    });
176
177    // span
178    impl sealed::MaybeSpan for pmn::Span {}
179    impl MaybeSpan for pmn::Span {
180        #[cfg(feature = "proc-macro")]
181        fn into_span_or_call_site(self) -> proc_macro::Span {
182            self.my_into()
183        }
184        #[cfg(feature = "proc-macro")]
185        fn make_punct(self, mut punct: proc_macro::Punct) -> proc_macro::Punct {
186            punct.set_span(self.my_into());
187            punct
188        }
189        #[cfg(feature = "proc-macro")]
190        fn make_group(self, mut g: proc_macro::Group) -> proc_macro::Group {
191            g.set_span(self.my_into());
192            g
193        }
194        #[cfg(feature = "proc-macro")]
195        fn make_literal(self, mut literal: proc_macro::Literal) -> proc_macro::Literal {
196            literal.set_span(self.my_into());
197            literal
198        }
199
200        #[cfg(feature = "proc-macro2")]
201        fn into_span2_or_call_site(self) -> proc_macro2::Span {
202            #[allow(clippy::useless_conversion)]
203            self.into()
204        }
205        #[cfg(feature = "proc-macro2")]
206        fn make_punct2(self, mut punct: proc_macro2::Punct) -> proc_macro2::Punct {
207            #[allow(clippy::useless_conversion)]
208            punct.set_span(self.into());
209            punct
210        }
211        #[cfg(feature = "proc-macro2")]
212        fn make_group2(self, mut g: proc_macro2::Group) -> proc_macro2::Group {
213            #[allow(clippy::useless_conversion)]
214            g.set_span(self.into());
215            g
216        }
217        #[cfg(feature = "proc-macro2")]
218        fn make_literal2(self, mut literal: proc_macro2::Literal) -> proc_macro2::Literal {
219            #[allow(clippy::useless_conversion)]
220            literal.set_span(self.into());
221            literal
222        }
223
224        type Span = Self;
225        fn try_into_span(self) -> Option<Self::Span> {
226            Some(self)
227        }
228
229        type WithDefaultSpan<S: crate::Span> = Self;
230        fn with_default_span<S: crate::Span>(self, _: S) -> Self::WithDefaultSpan<S> {
231            self
232        }
233
234        type WithReplacedSpan<S: crate::Span> = S;
235        fn with_replaced_span<S: crate::Span>(self, span: S) -> Self::WithReplacedSpan<S> {
236            span
237        }
238    }
239
240    impl sealed::Span for pmn::Span {}
241    impl Span for pmn::Span {}
242});