blob: 7e732115335a2956549c15dd074edc7816397419 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
use std::num::NonZeroU16;
use proc_macro::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree};
pub(crate) trait ToTokenStream: Sized {
fn append_to(self, ts: &mut TokenStream);
}
pub(crate) trait ToTokenTree: Sized {
fn into_token_tree(self) -> TokenTree;
}
impl<T: ToTokenTree> ToTokenStream for T {
fn append_to(self, ts: &mut TokenStream) {
ts.extend([self.into_token_tree()])
}
}
impl ToTokenTree for bool {
fn into_token_tree(self) -> TokenTree {
let lit = if self { "true" } else { "false" };
TokenTree::Ident(Ident::new(lit, Span::mixed_site()))
}
}
impl ToTokenStream for TokenStream {
fn append_to(self, ts: &mut TokenStream) {
ts.extend(self)
}
}
impl ToTokenTree for TokenTree {
fn into_token_tree(self) -> TokenTree {
self
}
}
impl ToTokenTree for &str {
fn into_token_tree(self) -> TokenTree {
TokenTree::Literal(Literal::string(self))
}
}
impl ToTokenTree for NonZeroU16 {
fn into_token_tree(self) -> TokenTree {
quote_group! {{
unsafe { ::core::num::NonZeroU16::new_unchecked(#(self.get())) }
}}
}
}
macro_rules! impl_for_tree_types {
($($type:ty)*) => {$(
impl ToTokenTree for $type {
fn into_token_tree(self) -> TokenTree {
TokenTree::from(self)
}
}
)*};
}
impl_for_tree_types![Ident Literal Group Punct];
macro_rules! impl_for_int {
($($type:ty => $method:ident)*) => {$(
impl ToTokenTree for $type {
fn into_token_tree(self) -> TokenTree {
TokenTree::from(Literal::$method(self))
}
}
)*};
}
impl_for_int! {
i8 => i8_unsuffixed
u8 => u8_unsuffixed
u16 => u16_unsuffixed
i32 => i32_unsuffixed
u32 => u32_unsuffixed
}
|