Skip to content

Commit a6d1cdf

Browse files
committed
glib: Use GLib allocator for GStr::to_owned
1 parent db614bb commit a6d1cdf

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

glib/src/gstring.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,16 @@ impl ToOwned for GStr {
249249

250250
#[inline]
251251
fn to_owned(&self) -> Self::Owned {
252-
GString(Inner::Native(Some(self.as_c_str().to_owned())))
252+
let b = self.to_bytes_with_nul();
253+
let inner = unsafe {
254+
let copy = ffi::g_malloc(b.len()) as *mut c_char;
255+
ptr::copy_nonoverlapping(b.as_ptr() as *const c_char, copy, b.len());
256+
Inner::Foreign {
257+
ptr: ptr::NonNull::new_unchecked(copy),
258+
len: b.len() - 1,
259+
}
260+
};
261+
GString(inner)
253262
}
254263
}
255264

0 commit comments

Comments
 (0)