Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strv: Implement From for constant GStr slices #1114

Merged
merged 1 commit into from Jun 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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