From 9cf3d99e91c893da4d92dd9f6e29335ec9f24db8 Mon Sep 17 00:00:00 2001 From: Maximiliano Sandoval R Date: Sat, 3 Jun 2023 09:15:27 +0200 Subject: [PATCH] strv: Implement From for constant GStr slices This was implemented for GString and &str but not for &GStr. --- glib/src/collections/strv.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/glib/src/collections/strv.rs b/glib/src/collections/strv.rs index 623aded4078c..2e3387002605 100644 --- a/glib/src/collections/strv.rs +++ b/glib/src/collections/strv.rs @@ -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 { @@ -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 = [