public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] libproc_macro: Implement Display for Literal
@ 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:453a246bb4d4bd13e1a5d245b9f38923abbfc94e
commit 453a246bb4d4bd13e1a5d245b9f38923abbfc94e
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date: Mon Feb 20 15:52:09 2023 +0100
libproc_macro: Implement Display for Literal
Implement the Display trait on rust internal Literal structure.
ChangeLog:
* librust/proc_macro/rust/bridge/literal.rs: Implement Display.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diff:
---
librust/proc_macro/rust/bridge/literal.rs | 117 +++++++++++++++++++++++++++++-
1 file changed, 116 insertions(+), 1 deletion(-)
diff --git a/librust/proc_macro/rust/bridge/literal.rs b/librust/proc_macro/rust/bridge/literal.rs
index f44fdbc26ed..6f7bb512378 100644
--- a/librust/proc_macro/rust/bridge/literal.rs
+++ b/librust/proc_macro/rust/bridge/literal.rs
@@ -1,6 +1,7 @@
use bridge::span::Span;
-use std::convert::TryInto;
+use std::convert::{TryFrom, TryInto};
use std::ffi::c_uchar;
+use std::fmt;
extern "C" {
fn Literal__drop(literal: *const Literal);
@@ -210,3 +211,117 @@ impl Drop for Literal {
}
}
}
+
+impl fmt::Display for Literal {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Literal::String { data, size } => {
+ let slice =
+ unsafe { std::slice::from_raw_parts(*data, (*size).try_into().unwrap()) };
+ f.write_str("\"")?;
+ f.write_str(std::str::from_utf8(slice).unwrap())?;
+ f.write_str("\"")?;
+ }
+ Literal::ByteString { data, size } => {
+ f.write_str("b\"")?;
+ let slice =
+ unsafe { std::slice::from_raw_parts(*data, (*size).try_into().unwrap()) };
+ for &byte in slice {
+ if byte != b'"' && byte >= b' ' && byte <= b'z' {
+ char::try_from(byte).unwrap().fmt(f)?;
+ } else {
+ write!(f, "\\x{byte:02x}")?;
+ }
+ }
+ f.write_str("b\"")?;
+ }
+ Literal::Char(val) => {
+ let ch: char = (*val).try_into().unwrap();
+ match ch {
+ '\'' => f.write_str("'\\''")?,
+ '\0' => f.write_str("'\\0'")?,
+ '\n' => f.write_str("'\\n'")?,
+ ' '..='z' => write!(f, "'{ch}'")?,
+ _ => write!(f, "'\\u{val:x}'")?,
+ }
+ }
+ Literal::Unsigned(val, suffixed) => match val {
+ Unsigned::Unsigned8(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("u8")?;
+ }
+ }
+ Unsigned::Unsigned16(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("u16")?;
+ }
+ }
+ Unsigned::Unsigned32(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("u32")?;
+ }
+ }
+ Unsigned::Unsigned64(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("u64")?;
+ }
+ }
+ },
+ Literal::Signed(val, suffixed) => match val {
+ Signed::Signed8(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("i8")?;
+ }
+ }
+ Signed::Signed16(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("i16")?;
+ }
+ }
+ Signed::Signed32(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("i32")?;
+ }
+ }
+ Signed::Signed64(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("i64")?;
+ }
+ }
+ },
+ Literal::Usize(val, suffixed) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("usize")?;
+ }
+ }
+ Literal::ISize(val, suffixed) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("isize")?;
+ }
+ }
+ Literal::Float32(val, suffixed) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("f32")?;
+ }
+ }
+ Literal::Float64(val, suffixed) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("f64")?;
+ }
+ }
+ }
+ Ok(())
+ }
+}
^ 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 Literal 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).