Skip to content

Commit 942531d

Browse files
committed
glib: Implement Default for GString
1 parent 130b601 commit 942531d

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

glib/src/gstring.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ impl GString {
433433
}
434434
}
435435

436+
impl Default for GString {
437+
fn default() -> Self {
438+
unsafe { GStr::from_bytes_with_nul_unchecked(b"\0") }.to_owned()
439+
}
440+
}
441+
436442
impl Clone for GString {
437443
fn clone(&self) -> GString {
438444
self.as_gstr().to_owned()
@@ -931,7 +937,7 @@ impl<'a> ToGlibPtr<'a, *mut i8> for GString {
931937
impl<'a> FromGlibContainer<*const c_char, *const i8> for GString {
932938
unsafe fn from_glib_none_num(ptr: *const i8, num: usize) -> Self {
933939
if num == 0 || ptr.is_null() {
934-
return Self::try_from("").unwrap();
940+
return Self::default();
935941
}
936942
let slice = slice::from_raw_parts(ptr as *const u8, num);
937943
// Also check if it's valid UTF-8
@@ -940,7 +946,7 @@ impl<'a> FromGlibContainer<*const c_char, *const i8> for GString {
940946

941947
unsafe fn from_glib_container_num(ptr: *const i8, num: usize) -> Self {
942948
if num == 0 || ptr.is_null() {
943-
return Self::try_from("").unwrap();
949+
return Self::default();
944950
}
945951

946952
// Check if it's valid UTF-8
@@ -955,7 +961,7 @@ impl<'a> FromGlibContainer<*const c_char, *const i8> for GString {
955961

956962
unsafe fn from_glib_full_num(ptr: *const i8, num: usize) -> Self {
957963
if num == 0 || ptr.is_null() {
958-
return Self::try_from("").unwrap();
964+
return Self::default();
959965
}
960966

961967
// Check if it's valid UTF-8

0 commit comments

Comments
 (0)