Skip to content

Commit 65a32e4

Browse files
glib: Implement more From traits for StrV
1 parent 65a06b0 commit 65a32e4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

glib/src/collections/strv.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,29 @@ impl From<StrV> for Vec<GString> {
529529
}
530530
}
531531

532+
impl From<Vec<String>> for StrV {
533+
#[inline]
534+
fn from(value: Vec<String>) -> Self {
535+
unsafe {
536+
let len = value.len();
537+
let mut s = Self::with_capacity(len);
538+
for (i, item) in value.into_iter().enumerate() {
539+
*s.ptr.as_ptr().add(i) = GString::from(item).into_glib_ptr();
540+
}
541+
s.len = len;
542+
*s.ptr.as_ptr().add(s.len) = ptr::null_mut();
543+
s
544+
}
545+
}
546+
}
547+
548+
impl<'a> From<Vec<&'a str>> for StrV {
549+
#[inline]
550+
fn from(value: Vec<&'a str>) -> Self {
551+
value.as_slice().into()
552+
}
553+
}
554+
532555
impl From<Vec<GString>> for StrV {
533556
#[inline]
534557
fn from(value: Vec<GString>) -> Self {
@@ -561,6 +584,21 @@ impl<const N: usize> From<[GString; N]> for StrV {
561584
}
562585
}
563586

587+
impl<'a, const N: usize> From<[&'a str; N]> for StrV {
588+
#[inline]
589+
fn from(value: [&'a str; N]) -> Self {
590+
unsafe {
591+
let mut s = Self::with_capacity(value.len());
592+
for (i, item) in value.iter().enumerate() {
593+
*s.ptr.as_ptr().add(i) = GString::from(*item).into_glib_ptr();
594+
}
595+
s.len = value.len();
596+
*s.ptr.as_ptr().add(s.len) = ptr::null_mut();
597+
s
598+
}
599+
}
600+
}
601+
564602
impl<'a> From<&'a [&'a str]> for StrV {
565603
#[inline]
566604
fn from(value: &'a [&'a str]) -> Self {

0 commit comments

Comments
 (0)