Skip to content

Commit 5937980

Browse files
committed
glib: Fully transfer ownership in <Value as From<Vec<GString>>>::from
`g_value_take_boxed` takes a full ownership of a provided object, so we should convert `GString`s by `into_glib_ptr`.
1 parent 1940e34 commit 5937980

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

glib/src/gstring.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,16 +2415,17 @@ impl ToValue for Vec<GString> {
24152415

24162416
impl From<Vec<GString>> for Value {
24172417
#[inline]
2418-
fn from(mut v: Vec<GString>) -> Self {
2418+
fn from(v: Vec<GString>) -> Self {
24192419
unsafe {
2420+
let v_ptr =
2421+
ffi::g_malloc(mem::size_of::<*mut c_char>() * (v.len() + 1)) as *mut *mut c_char;
2422+
v_ptr.add(v.len()).write(ptr::null_mut());
2423+
for (i, s) in v.into_iter().enumerate() {
2424+
v_ptr.add(i).write(s.into_glib_ptr());
2425+
}
2426+
24202427
let mut value = Value::for_value_type::<Vec<GString>>();
2421-
let container =
2422-
ToGlibContainerFromSlice::<*mut *mut c_char>::to_glib_container_from_slice(&v);
2423-
gobject_ffi::g_value_take_boxed(
2424-
value.to_glib_none_mut().0,
2425-
container.0 as *const c_void,
2426-
);
2427-
v.set_len(0);
2428+
gobject_ffi::g_value_take_boxed(value.to_glib_none_mut().0, v_ptr as *const c_void);
24282429
value
24292430
}
24302431
}
@@ -2598,6 +2599,21 @@ mod tests {
25982599
assert_eq!(s.as_str(), "foo");
25992600
}
26002601

2602+
#[test]
2603+
fn test_value_from_vec_gstring() {
2604+
fn roundtrip(s: GString) {
2605+
let vec = vec![s.clone()];
2606+
let value = crate::Value::from(vec);
2607+
let vec: Vec<GString> = value.get().unwrap();
2608+
assert_eq!(vec.len(), 1);
2609+
assert_eq!(s, vec[0]);
2610+
}
2611+
2612+
roundtrip(GString::from("foo"));
2613+
roundtrip(GString::from("very very very long string".to_owned()));
2614+
roundtrip(GString::from(gstr!("very very very long string")));
2615+
}
2616+
26012617
#[test]
26022618
fn test_as_ref_path() {
26032619
fn foo<P: AsRef<Path>>(_path: P) {}

0 commit comments

Comments
 (0)