Skip to content

Commit 9cf3d99

Browse files
committed
strv: Implement From for constant GStr slices
This was implemented for GString and &str but not for &GStr.
1 parent aa4b78e commit 9cf3d99

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

glib/src/collections/strv.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,21 @@ impl<'a, const N: usize> From<[&'a str; N]> for StrV {
358358
}
359359
}
360360

361+
impl<'a, const N: usize> From<[&'a GStr; N]> for StrV {
362+
#[inline]
363+
fn from(value: [&'a GStr; N]) -> Self {
364+
unsafe {
365+
let mut s = Self::with_capacity(value.len());
366+
for (i, item) in value.iter().enumerate() {
367+
*s.ptr.as_ptr().add(i) = GString::from(*item).into_glib_ptr();
368+
}
369+
s.len = value.len();
370+
*s.ptr.as_ptr().add(s.len) = ptr::null_mut();
371+
s
372+
}
373+
}
374+
}
375+
361376
impl<'a> From<&'a [&'a str]> for StrV {
362377
#[inline]
363378
fn from(value: &'a [&'a str]) -> Self {
@@ -1398,6 +1413,20 @@ mod test {
13981413
}
13991414
}
14001415

1416+
#[test]
1417+
fn test_from_slice() {
1418+
let items = [
1419+
crate::gstr!("str1"),
1420+
crate::gstr!("str2"),
1421+
crate::gstr!("str3"),
1422+
];
1423+
1424+
let slice1 = StrV::from(&items[..]);
1425+
let slice2 = StrV::from(items);
1426+
assert_eq!(slice1.len(), 3);
1427+
assert_eq!(slice1, slice2);
1428+
}
1429+
14011430
#[test]
14021431
fn test_safe_api() {
14031432
let items = [

0 commit comments

Comments
 (0)