Skip to content

Commit 36ef929

Browse files
authored
Merge pull request #1688 from sdroege/gstringptr-from
glib: Add From<AsRef<str>> impl for GStringPtr
2 parents 0d468c1 + 21d47a1 commit 36ef929

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

glib/src/gstring.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,18 @@ impl std::hash::Hash for GStringPtr {
10401040
}
10411041
}
10421042

1043+
impl<T: AsRef<str>> From<T> for GStringPtr {
1044+
fn from(value: T) -> Self {
1045+
unsafe {
1046+
let value = value.as_ref();
1047+
GStringPtr(ptr::NonNull::new_unchecked(ffi::g_strndup(
1048+
value.as_ptr() as *const _,
1049+
value.len(),
1050+
)))
1051+
}
1052+
}
1053+
}
1054+
10431055
// size_of::<Inner>() minus two bytes for length and enum discriminant
10441056
const INLINE_LEN: usize =
10451057
mem::size_of::<Option<Box<str>>>() + mem::size_of::<usize>() - mem::size_of::<u8>() * 2;

0 commit comments

Comments
 (0)