Skip to content

Commit 101cdcf

Browse files
committed
Implement From<&[char]> for GString
1 parent c6d5205 commit 101cdcf

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

+16
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: `char` is by definition a valid Unicode scalar value.
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

+2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ fn string_chars() {
6969
// Empty tests regression from #228: Null pointer passed to slice::from_raw_parts().
7070
let string = GString::new();
7171
assert_eq!(string.chars(), &[]);
72+
assert_eq!(string, GString::from([].as_slice()));
7273

7374
let string = String::from("some_string");
7475
let string_chars: Vec<char> = string.chars().collect();
7576
let gstring = GString::from(string);
7677

7778
assert_eq!(string_chars, gstring.chars().to_vec());
79+
assert_eq!(gstring, GString::from(string_chars.as_slice()));
7880
}
7981

8082
#[itest]

0 commit comments

Comments
 (0)