]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: libproc_macro: Change constructor in ffistring
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Tue, 16 May 2023 12:01:36 +0000 (14:01 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:37:20 +0000 (18:37 +0100)
The "new" constructor wasn't fitting it's usage well.

libgrust/ChangeLog:

* libproc_macro/rust/bridge/ffistring.rs: Implement
From trait for FFIString.
* libproc_macro/rust/bridge/literal.rs: Change
constructor call.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
libgrust/libproc_macro/rust/bridge/ffistring.rs
libgrust/libproc_macro/rust/bridge/literal.rs

index 0cdbf13851f70705beb45b758de10bcc9c4a59ae..73162e901fed75c3e442fe61357b08984d720538 100644 (file)
@@ -16,15 +16,18 @@ pub struct FFIString {
     len: u64,
 }
 
-impl FFIString {
-    pub fn new(string: &str) -> FFIString {
-        unsafe { FFIString__new(string.as_ptr(), string.len() as u64) }
+impl<S> From<S> for FFIString
+where
+    S: AsRef<str>,
+{
+    fn from(s: S) -> Self {
+        unsafe { FFIString__new(s.as_ref().as_ptr(), s.as_ref().len() as u64) }
     }
 }
 
 impl Clone for FFIString {
     fn clone(&self) -> Self {
-        FFIString::new(&self.to_string())
+        FFIString::from(&self.to_string())
     }
 }
 
index 1e15012c4a712f62a6a108a0de21dd437a605928..f54bfe20d8d244319e263c4e340ddc9a337a362b 100644 (file)
@@ -37,8 +37,8 @@ macro_rules! suffixed_int_literals {
         pub fn $name(n : $kind) -> Literal {
             Literal {
                 kind : LitKind::Integer,
-                text: FFIString::new(&n.to_string()),
-                suffix: FFIString::new(stringify!($kind))
+                text: FFIString::from(&n.to_string()),
+                suffix: FFIString::from(stringify!($kind))
             }
         }
     )*)
@@ -49,8 +49,8 @@ macro_rules! unsuffixed_int_literals {
         pub fn $name(n : $kind) -> Literal {
             Literal {
                 kind : LitKind::Integer,
-                text: FFIString::new(&n.to_string()),
-                suffix: FFIString::new("")
+                text: FFIString::from(&n.to_string()),
+                suffix: FFIString::from("")
             }
         }
     )*)
@@ -95,16 +95,16 @@ impl Literal {
 
         Literal {
             kind: LitKind::Float,
-            text: FFIString::new(&repr),
-            suffix: FFIString::new(""),
+            text: FFIString::from(&repr),
+            suffix: FFIString::from(""),
         }
     }
 
     pub fn f32_suffixed(n: f32) -> Self {
         Literal {
             kind: LitKind::Float,
-            text: FFIString::new(&n.to_string()),
-            suffix: FFIString::new("f32"),
+            text: FFIString::from(&n.to_string()),
+            suffix: FFIString::from("f32"),
         }
     }
 
@@ -116,40 +116,40 @@ impl Literal {
 
         Literal {
             kind: LitKind::Float,
-            text: FFIString::new(&repr),
-            suffix: FFIString::new(""),
+            text: FFIString::from(&repr),
+            suffix: FFIString::from(""),
         }
     }
 
     pub fn f64_suffixed(n: f64) -> Self {
         Literal {
             kind: LitKind::Float,
-            text: FFIString::new(&n.to_string()),
-            suffix: FFIString::new("f64"),
+            text: FFIString::from(&n.to_string()),
+            suffix: FFIString::from("f64"),
         }
     }
 
     pub fn string(string: &str) -> Self {
         Literal {
             kind: LitKind::Str,
-            text: FFIString::new(string),
-            suffix: FFIString::new(""),
+            text: FFIString::from(string),
+            suffix: FFIString::from(""),
         }
     }
 
     pub fn character(c: char) -> Self {
         Literal {
             kind: LitKind::Char,
-            text: FFIString::new(&c.to_string()),
-            suffix: FFIString::new(""),
+            text: FFIString::from(&c.to_string()),
+            suffix: FFIString::from(""),
         }
     }
 
     pub fn byte_string(bytes: &[u8]) -> Self {
         Literal {
             kind: LitKind::ByteStr,
-            text: FFIString::new(&bytes.escape_ascii().to_string()),
-            suffix: FFIString::new(""),
+            text: FFIString::from(&bytes.escape_ascii().to_string()),
+            suffix: FFIString::from(""),
         }
     }
 
@@ -219,8 +219,8 @@ impl FromStr for Literal {
         // Structure that will be filled in by the cpp
         let mut lit = Literal {
             kind: LitKind::Err,
-            text: FFIString::new(""),
-            suffix: FFIString::new(""),
+            text: FFIString::from(""),
+            suffix: FFIString::from(""),
         };
         // TODO: We might want to pass a LexError by reference to retrieve
         // error information