Skip to content

Commit ea8237d

Browse files
authored
Merge pull request #862 from Des-Nerger/master
Implement `From<&[char]>` for `GString`
2 parents c6d5205 + f8edeca commit ea8237d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

godot-core/src/builtin/string/gstring.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,22 @@ impl From<&str> for GString {
225225
}
226226
}
227227

228+
impl From<&[char]> for GString {
229+
fn from(chars: &[char]) -> Self {
230+
// SAFETY: A `char` value is by definition a valid Unicode code point.
231+
unsafe {
232+
Self::new_with_string_uninit(|string_ptr| {
233+
let ctor = interface_fn!(string_new_with_utf32_chars_and_len);
234+
ctor(
235+
string_ptr,
236+
chars.as_ptr() as *const sys::char32_t,
237+
chars.len() as i64,
238+
);
239+
})
240+
}
241+
}
242+
}
243+
228244
impl From<String> for GString {
229245
fn from(value: String) -> Self {
230246
value.as_str().into()

itest/rust/src/builtin_tests/string/gstring_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ fn string_clone() {
6868
fn string_chars() {
6969
// Empty tests regression from #228: Null pointer passed to slice::from_raw_parts().
7070
let string = GString::new();
71-
assert_eq!(string.chars(), &[]);
71+
let empty_char_slice: &[char] = &[];
72+
assert_eq!(string.chars(), empty_char_slice);
73+
assert_eq!(string, GString::from(empty_char_slice));
7274

7375
let string = String::from("some_string");
7476
let string_chars: Vec<char> = string.chars().collect();
7577
let gstring = GString::from(string);
7678

7779
assert_eq!(string_chars, gstring.chars().to_vec());
80+
assert_eq!(gstring, GString::from(string_chars.as_slice()));
7881
}
7982

8083
#[itest]

0 commit comments

Comments
 (0)