diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-05-16 14:01:36 +0200 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2023-05-17 16:13:45 +0000 |
commit | ad7c998b21c719cfcfd40215fe737b4211b6eb66 (patch) | |
tree | 6996b08c103fa690632b046bb1bf3973f309e907 | |
parent | 70c245e4e3e2c8c794266f0d025af501dbd947fd (diff) | |
download | gcc-ad7c998b21c719cfcfd40215fe737b4211b6eb66.zip gcc-ad7c998b21c719cfcfd40215fe737b4211b6eb66.tar.gz gcc-ad7c998b21c719cfcfd40215fe737b4211b6eb66.tar.bz2 |
libproc_macro: Change constructor in ffistring
The "new" constructor wasn't fitting it's usage well.
ChangeLog:
* libgrust/libproc_macro/rust/bridge/ffistring.rs: Implement
From trait for FFIString.
* libgrust/libproc_macro/rust/bridge/literal.rs: Change
constructor call.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
-rw-r--r-- | libgrust/libproc_macro/rust/bridge/ffistring.rs | 11 | ||||
-rw-r--r-- | 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 0cdbf13..73162e9 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<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()) } } diff --git a/libgrust/libproc_macro/rust/bridge/literal.rs b/libgrust/libproc_macro/rust/bridge/literal.rs index 1e15012..f54bfe2 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 |