summaryrefslogtreecommitdiffstats
path: root/vendor/time-macros/src/to_tokens.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /vendor/time-macros/src/to_tokens.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/time-macros/src/to_tokens.rs')
-rw-r--r--vendor/time-macros/src/to_tokens.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/vendor/time-macros/src/to_tokens.rs b/vendor/time-macros/src/to_tokens.rs
new file mode 100644
index 000000000..3a293925c
--- /dev/null
+++ b/vendor/time-macros/src/to_tokens.rs
@@ -0,0 +1,68 @@
+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))
+ }
+}
+
+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
+}