Skip to content

Commit ad7c998

Browse files
P-E-Pphilberty
authored andcommitted
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>
1 parent 70c245e commit ad7c998

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

libgrust/libproc_macro/rust/bridge/ffistring.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ pub struct FFIString {
1616
len: u64,
1717
}
1818

19-
impl FFIString {
20-
pub fn new(string: &str) -> FFIString {
21-
unsafe { FFIString__new(string.as_ptr(), string.len() as u64) }
19+
impl<S> From<S> for FFIString
20+
where
21+
S: AsRef<str>,
22+
{
23+
fn from(s: S) -> Self {
24+
unsafe { FFIString__new(s.as_ref().as_ptr(), s.as_ref().len() as u64) }
2225
}
2326
}
2427

2528
impl Clone for FFIString {
2629
fn clone(&self) -> Self {
27-
FFIString::new(&self.to_string())
30+
FFIString::from(&self.to_string())
2831
}
2932
}
3033

libgrust/libproc_macro/rust/bridge/literal.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ macro_rules! suffixed_int_literals {
3737
pub fn $name(n : $kind) -> Literal {
3838
Literal {
3939
kind : LitKind::Integer,
40-
text: FFIString::new(&n.to_string()),
41-
suffix: FFIString::new(stringify!($kind))
40+
text: FFIString::from(&n.to_string()),
41+
suffix: FFIString::from(stringify!($kind))
4242
}
4343
}
4444
)*)
@@ -49,8 +49,8 @@ macro_rules! unsuffixed_int_literals {
4949
pub fn $name(n : $kind) -> Literal {
5050
Literal {
5151
kind : LitKind::Integer,
52-
text: FFIString::new(&n.to_string()),
53-
suffix: FFIString::new("")
52+
text: FFIString::from(&n.to_string()),
53+
suffix: FFIString::from("")
5454
}
5555
}
5656
)*)
@@ -95,16 +95,16 @@ impl Literal {
9595

9696
Literal {
9797
kind: LitKind::Float,
98-
text: FFIString::new(&repr),
99-
suffix: FFIString::new(""),
98+
text: FFIString::from(&repr),
99+
suffix: FFIString::from(""),
100100
}
101101
}
102102

103103
pub fn f32_suffixed(n: f32) -> Self {
104104
Literal {
105105
kind: LitKind::Float,
106-
text: FFIString::new(&n.to_string()),
107-
suffix: FFIString::new("f32"),
106+
text: FFIString::from(&n.to_string()),
107+
suffix: FFIString::from("f32"),
108108
}
109109
}
110110

@@ -116,40 +116,40 @@ impl Literal {
116116

117117
Literal {
118118
kind: LitKind::Float,
119-
text: FFIString::new(&repr),
120-
suffix: FFIString::new(""),
119+
text: FFIString::from(&repr),
120+
suffix: FFIString::from(""),
121121
}
122122
}
123123

124124
pub fn f64_suffixed(n: f64) -> Self {
125125
Literal {
126126
kind: LitKind::Float,
127-
text: FFIString::new(&n.to_string()),
128-
suffix: FFIString::new("f64"),
127+
text: FFIString::from(&n.to_string()),
128+
suffix: FFIString::from("f64"),
129129
}
130130
}
131131

132132
pub fn string(string: &str) -> Self {
133133
Literal {
134134
kind: LitKind::Str,
135-
text: FFIString::new(string),
136-
suffix: FFIString::new(""),
135+
text: FFIString::from(string),
136+
suffix: FFIString::from(""),
137137
}
138138
}
139139

140140
pub fn character(c: char) -> Self {
141141
Literal {
142142
kind: LitKind::Char,
143-
text: FFIString::new(&c.to_string()),
144-
suffix: FFIString::new(""),
143+
text: FFIString::from(&c.to_string()),
144+
suffix: FFIString::from(""),
145145
}
146146
}
147147

148148
pub fn byte_string(bytes: &[u8]) -> Self {
149149
Literal {
150150
kind: LitKind::ByteStr,
151-
text: FFIString::new(&bytes.escape_ascii().to_string()),
152-
suffix: FFIString::new(""),
151+
text: FFIString::from(&bytes.escape_ascii().to_string()),
152+
suffix: FFIString::from(""),
153153
}
154154
}
155155

@@ -219,8 +219,8 @@ impl FromStr for Literal {
219219
// Structure that will be filled in by the cpp
220220
let mut lit = Literal {
221221
kind: LitKind::Err,
222-
text: FFIString::new(""),
223-
suffix: FFIString::new(""),
222+
text: FFIString::from(""),
223+
suffix: FFIString::from(""),
224224
};
225225
// TODO: We might want to pass a LexError by reference to retrieve
226226
// error information

0 commit comments

Comments
 (0)