Skip to content

Commit

Permalink
Merge pull request #1114 from A6GibKm/gstrv-from
Browse files Browse the repository at this point in the history
strv: Implement From for constant GStr slices
  • Loading branch information
sdroege committed Jun 6, 2023
2 parents aa4b78e + 9cf3d99 commit c86404b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions glib/src/collections/strv.rs
Expand Up @@ -358,6 +358,21 @@ impl<'a, const N: usize> From<[&'a str; N]> for StrV {
}
}

impl<'a, const N: usize> From<[&'a GStr; N]> for StrV {
#[inline]
fn from(value: [&'a GStr; N]) -> Self {
unsafe {
let mut s = Self::with_capacity(value.len());
for (i, item) in value.iter().enumerate() {
*s.ptr.as_ptr().add(i) = GString::from(*item).into_glib_ptr();
}
s.len = value.len();
*s.ptr.as_ptr().add(s.len) = ptr::null_mut();
s
}
}
}

impl<'a> From<&'a [&'a str]> for StrV {
#[inline]
fn from(value: &'a [&'a str]) -> Self {
Expand Down Expand Up @@ -1398,6 +1413,20 @@ mod test {
}
}

#[test]
fn test_from_slice() {
let items = [
crate::gstr!("str1"),
crate::gstr!("str2"),
crate::gstr!("str3"),
];

let slice1 = StrV::from(&items[..]);
let slice2 = StrV::from(items);
assert_eq!(slice1.len(), 3);
assert_eq!(slice1, slice2);
}

#[test]
fn test_safe_api() {
let items = [
Expand Down

0 comments on commit c86404b

Please sign in to comment.