public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] libproc_macro: Implement Display for TokenStream
@ 2023-04-06 21:31 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:31 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:3762177c58e4780f16f6eaf8dd237d8e50fa9c94

commit 3762177c58e4780f16f6eaf8dd237d8e50fa9c94
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Mon Feb 20 16:01:55 2023 +0100

    libproc_macro: Implement Display for TokenStream
    
    Implement the Display trait for the external rust structure TokenStream.
    
    ChangeLog:
    
            * librust/proc_macro/rust/bridge/token_stream.rs: Add internal
            TokenStream Display implementation.
            * librust/proc_macro/rust/lib.rs: Add external Display
            implementation for TokenStream.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 librust/proc_macro/rust/bridge/token_stream.rs | 26 ++++++++++++++++++++++++++
 librust/proc_macro/rust/lib.rs                 |  4 ++--
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/librust/proc_macro/rust/bridge/token_stream.rs b/librust/proc_macro/rust/bridge/token_stream.rs
index 027f450b826..2ddde8ed0b6 100644
--- a/librust/proc_macro/rust/bridge/token_stream.rs
+++ b/librust/proc_macro/rust/bridge/token_stream.rs
@@ -1,5 +1,7 @@
 use bridge::{group::Group, ident::Ident, literal::Literal, punct::Punct};
 use std::convert::TryInto;
+use std::fmt;
+use std::slice;
 
 type ExternalTokenTree = crate::TokenTree;
 type ExternalTokenStream = crate::TokenStream;
@@ -17,6 +19,17 @@ pub enum TokenTree {
     Literal(Literal),
 }
 
+impl fmt::Display for TokenTree {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self {
+            TokenTree::Group(group) => group.fmt(f),
+            TokenTree::Ident(ident) => ident.fmt(f),
+            TokenTree::Punct(punct) => punct.fmt(f),
+            TokenTree::Literal(literal) => literal.fmt(f),
+        }
+    }
+}
+
 impl From<ExternalTokenTree> for TokenTree {
     fn from(value: ExternalTokenTree) -> Self {
         match value {
@@ -103,3 +116,16 @@ impl Extend<ExternalTokenStream> for TokenStream {
         }
     }
 }
+
+impl fmt::Display for TokenStream {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        for i in unsafe { slice::from_raw_parts(self.data, self.size.try_into().unwrap()) } {
+            i.fmt(f)?;
+            match i {
+                TokenTree::Punct(_) => (),
+                _ => f.write_str(" ")?,
+            }
+        }
+        Ok(())
+    }
+}
diff --git a/librust/proc_macro/rust/lib.rs b/librust/proc_macro/rust/lib.rs
index 13f66f04fac..aa90a51db54 100644
--- a/librust/proc_macro/rust/lib.rs
+++ b/librust/proc_macro/rust/lib.rs
@@ -154,8 +154,8 @@ impl TokenStream {
 }
 
 impl fmt::Display for TokenStream {
-    fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        todo!("Implement this function")
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        self.0.fmt(f)
     }
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-06 21:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:31 [gcc/devel/rust/master] libproc_macro: Implement Display for TokenStream Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).