diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-02-20 16:01:55 +0100 |
---|---|---|
committer | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-03-30 13:52:31 +0200 |
commit | 3762177c58e4780f16f6eaf8dd237d8e50fa9c94 (patch) | |
tree | 00dd84fd9678e600d9cc7b5f275c8cf807847806 | |
parent | 453a246bb4d4bd13e1a5d245b9f38923abbfc94e (diff) | |
download | gcc-3762177c58e4780f16f6eaf8dd237d8e50fa9c94.zip gcc-3762177c58e4780f16f6eaf8dd237d8e50fa9c94.tar.gz gcc-3762177c58e4780f16f6eaf8dd237d8e50fa9c94.tar.bz2 |
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>
-rw-r--r-- | librust/proc_macro/rust/bridge/token_stream.rs | 26 | ||||
-rw-r--r-- | 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 027f450..2ddde8e 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 13f66f0..aa90a51 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) } } |