From 90effeedc48a187831ba71cabf616650c98a011b Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Tue, 16 May 2023 14:01:36 +0200 Subject: [PATCH] gccrs: libproc_macro: Change constructor in ffistring 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 --- .../libproc_macro/rust/bridge/ffistring.rs | 11 +++-- libgrust/libproc_macro/rust/bridge/literal.rs | 40 +++++++++---------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/libgrust/libproc_macro/rust/bridge/ffistring.rs b/libgrust/libproc_macro/rust/bridge/ffistring.rs index 0cdbf13851f..73162e901fe 100644 --- a/libgrust/libproc_macro/rust/bridge/ffistring.rs +++ b/libgrust/libproc_macro/rust/bridge/ffistring.rs @@ -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 From for FFIString +where + S: AsRef, +{ + 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()) } } diff --git a/libgrust/libproc_macro/rust/bridge/literal.rs b/libgrust/libproc_macro/rust/bridge/literal.rs index 1e15012c4a7..f54bfe20d8d 100644 --- a/libgrust/libproc_macro/rust/bridge/literal.rs +++ b/libgrust/libproc_macro/rust/bridge/literal.rs @@ -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