Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions glib/src/gstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,16 +2415,17 @@ impl ToValue for Vec<GString> {

impl From<Vec<GString>> for Value {
#[inline]
fn from(mut v: Vec<GString>) -> Self {
fn from(v: Vec<GString>) -> Self {
unsafe {
let v_ptr =
ffi::g_malloc(mem::size_of::<*mut c_char>() * (v.len() + 1)) as *mut *mut c_char;
v_ptr.add(v.len()).write(ptr::null_mut());
for (i, s) in v.into_iter().enumerate() {
v_ptr.add(i).write(s.into_glib_ptr());
}

let mut value = Value::for_value_type::<Vec<GString>>();
let container =
ToGlibContainerFromSlice::<*mut *mut c_char>::to_glib_container_from_slice(&v);
gobject_ffi::g_value_take_boxed(
value.to_glib_none_mut().0,
container.0 as *const c_void,
);
v.set_len(0);
gobject_ffi::g_value_take_boxed(value.to_glib_none_mut().0, v_ptr as *const c_void);
value
}
}
Expand Down Expand Up @@ -2598,6 +2599,21 @@ mod tests {
assert_eq!(s.as_str(), "foo");
}

#[test]
fn test_value_from_vec_gstring() {
fn roundtrip(s: GString) {
let vec = vec![s.clone()];
let value = crate::Value::from(vec);
let vec: Vec<GString> = value.get().unwrap();
assert_eq!(vec.len(), 1);
assert_eq!(s, vec[0]);
}

roundtrip(GString::from("foo"));
roundtrip(GString::from("very very very long string".to_owned()));
roundtrip(GString::from(gstr!("very very very long string")));
}

#[test]
fn test_as_ref_path() {
fn foo<P: AsRef<Path>>(_path: P) {}
Expand Down
Loading