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 <pierre-emmanuel.patry@embecosm.com>
This commit is contained in:
Pierre-Emmanuel Patry 2023-05-16 14:01:36 +02:00 committed by Arthur Cohen
parent 9fc94c1562
commit 90effeedc4
2 changed files with 27 additions and 24 deletions

View file

@ -16,15 +16,18 @@ pub struct FFIString {
len: u64, len: u64,
} }
impl FFIString { impl<S> From<S> for FFIString
pub fn new(string: &str) -> FFIString { where
unsafe { FFIString__new(string.as_ptr(), string.len() as u64) } 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 { impl Clone for FFIString {
fn clone(&self) -> Self { fn clone(&self) -> Self {
FFIString::new(&self.to_string()) FFIString::from(&self.to_string())
} }
} }

View file

@ -37,8 +37,8 @@ macro_rules! suffixed_int_literals {
pub fn $name(n : $kind) -> Literal { pub fn $name(n : $kind) -> Literal {
Literal { Literal {
kind : LitKind::Integer, kind : LitKind::Integer,
text: FFIString::new(&n.to_string()), text: FFIString::from(&n.to_string()),
suffix: FFIString::new(stringify!($kind)) suffix: FFIString::from(stringify!($kind))
} }
} }
)*) )*)
@ -49,8 +49,8 @@ macro_rules! unsuffixed_int_literals {
pub fn $name(n : $kind) -> Literal { pub fn $name(n : $kind) -> Literal {
Literal { Literal {
kind : LitKind::Integer, kind : LitKind::Integer,
text: FFIString::new(&n.to_string()), text: FFIString::from(&n.to_string()),
suffix: FFIString::new("") suffix: FFIString::from("")
} }
} }
)*) )*)
@ -95,16 +95,16 @@ impl Literal {
Literal { Literal {
kind: LitKind::Float, kind: LitKind::Float,
text: FFIString::new(&repr), text: FFIString::from(&repr),
suffix: FFIString::new(""), suffix: FFIString::from(""),
} }
} }
pub fn f32_suffixed(n: f32) -> Self { pub fn f32_suffixed(n: f32) -> Self {
Literal { Literal {
kind: LitKind::Float, kind: LitKind::Float,
text: FFIString::new(&n.to_string()), text: FFIString::from(&n.to_string()),
suffix: FFIString::new("f32"), suffix: FFIString::from("f32"),
} }
} }
@ -116,40 +116,40 @@ impl Literal {
Literal { Literal {
kind: LitKind::Float, kind: LitKind::Float,
text: FFIString::new(&repr), text: FFIString::from(&repr),
suffix: FFIString::new(""), suffix: FFIString::from(""),
} }
} }
pub fn f64_suffixed(n: f64) -> Self { pub fn f64_suffixed(n: f64) -> Self {
Literal { Literal {
kind: LitKind::Float, kind: LitKind::Float,
text: FFIString::new(&n.to_string()), text: FFIString::from(&n.to_string()),
suffix: FFIString::new("f64"), suffix: FFIString::from("f64"),
} }
} }
pub fn string(string: &str) -> Self { pub fn string(string: &str) -> Self {
Literal { Literal {
kind: LitKind::Str, kind: LitKind::Str,
text: FFIString::new(string), text: FFIString::from(string),
suffix: FFIString::new(""), suffix: FFIString::from(""),
} }
} }
pub fn character(c: char) -> Self { pub fn character(c: char) -> Self {
Literal { Literal {
kind: LitKind::Char, kind: LitKind::Char,
text: FFIString::new(&c.to_string()), text: FFIString::from(&c.to_string()),
suffix: FFIString::new(""), suffix: FFIString::from(""),
} }
} }
pub fn byte_string(bytes: &[u8]) -> Self { pub fn byte_string(bytes: &[u8]) -> Self {
Literal { Literal {
kind: LitKind::ByteStr, kind: LitKind::ByteStr,
text: FFIString::new(&bytes.escape_ascii().to_string()), text: FFIString::from(&bytes.escape_ascii().to_string()),
suffix: FFIString::new(""), suffix: FFIString::from(""),
} }
} }
@ -219,8 +219,8 @@ impl FromStr for Literal {
// Structure that will be filled in by the cpp // Structure that will be filled in by the cpp
let mut lit = Literal { let mut lit = Literal {
kind: LitKind::Err, kind: LitKind::Err,
text: FFIString::new(""), text: FFIString::from(""),
suffix: FFIString::new(""), suffix: FFIString::from(""),
}; };
// TODO: We might want to pass a LexError by reference to retrieve // TODO: We might want to pass a LexError by reference to retrieve
// error information // error information