Skip to content

Commit 9753a72

Browse files
jf2048sdroege
authored andcommitted
glib: add GStringBuilder::as_gstr and AsRef<GStr>
add some PartialEq and PartialOrd implementations for GStr too
1 parent fbc1814 commit 9753a72

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

glib/src/gstring_builder.rs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::{cmp, fmt, hash, mem, ops, ptr, slice, str};
44

5-
use crate::translate::*;
5+
use crate::{translate::*, GStr};
66

77
wrapper! {
88
// rustdoc-stripper-ignore-next
@@ -12,7 +12,7 @@ wrapper! {
1212
pub struct GStringBuilder(BoxedInline<ffi::GString>);
1313

1414
match fn {
15-
copy => |ptr| ffi::g_string_new((*ptr).str),
15+
copy => |ptr| ffi::g_string_new_len((*ptr).str, (*ptr).len.try_into().unwrap()),
1616
free => |ptr| ffi::g_string_free(ptr, ffi::GTRUE),
1717
init => |ptr| unsafe {
1818
let inner = ffi::GString {
@@ -111,7 +111,7 @@ impl GStringBuilder {
111111
}
112112

113113
// rustdoc-stripper-ignore-next
114-
/// Returns `&str` slice.
114+
/// Returns <code>&[str]</code> slice.
115115
pub fn as_str(&self) -> &str {
116116
unsafe {
117117
let ptr: *const u8 = self.inner.str as _;
@@ -125,7 +125,21 @@ impl GStringBuilder {
125125
}
126126

127127
// rustdoc-stripper-ignore-next
128-
/// Returns `&str` slice.
128+
/// Returns <code>&[GStr]</code> slice.
129+
pub fn as_gstr(&self) -> &GStr {
130+
unsafe {
131+
let ptr: *const u8 = self.inner.str as _;
132+
let len: usize = self.inner.len;
133+
if len == 0 {
134+
return Default::default();
135+
}
136+
let slice = slice::from_raw_parts(ptr, len + 1);
137+
GStr::from_utf8_with_nul_unchecked(slice)
138+
}
139+
}
140+
141+
// rustdoc-stripper-ignore-next
142+
/// Finalizes the builder, converting it to a [`GString`].
129143
#[must_use = "String returned from the builder should probably be used"]
130144
pub fn into_string(self) -> crate::GString {
131145
unsafe {
@@ -173,6 +187,18 @@ impl PartialEq<GStringBuilder> for str {
173187
}
174188
}
175189

190+
impl PartialEq<GStr> for GStringBuilder {
191+
fn eq(&self, other: &GStr) -> bool {
192+
self.as_gstr() == other
193+
}
194+
}
195+
196+
impl PartialEq<GStringBuilder> for GStr {
197+
fn eq(&self, other: &GStringBuilder) -> bool {
198+
self == other.as_gstr()
199+
}
200+
}
201+
176202
impl Eq for GStringBuilder {}
177203

178204
impl cmp::PartialOrd for GStringBuilder {
@@ -193,6 +219,18 @@ impl cmp::PartialOrd<GStringBuilder> for str {
193219
}
194220
}
195221

222+
impl cmp::PartialOrd<GStr> for GStringBuilder {
223+
fn partial_cmp(&self, other: &GStr) -> Option<cmp::Ordering> {
224+
Some(self.as_gstr().cmp(other))
225+
}
226+
}
227+
228+
impl cmp::PartialOrd<GStringBuilder> for GStr {
229+
fn partial_cmp(&self, other: &GStringBuilder) -> Option<cmp::Ordering> {
230+
Some(self.cmp(other.as_gstr()))
231+
}
232+
}
233+
196234
impl cmp::Ord for GStringBuilder {
197235
fn cmp(&self, other: &Self) -> cmp::Ordering {
198236
self.as_str().cmp(other.as_str())
@@ -220,6 +258,12 @@ impl AsRef<str> for GStringBuilder {
220258
}
221259
}
222260

261+
impl AsRef<GStr> for GStringBuilder {
262+
fn as_ref(&self) -> &GStr {
263+
self.as_gstr()
264+
}
265+
}
266+
223267
impl ops::Deref for GStringBuilder {
224268
type Target = str;
225269

0 commit comments

Comments
 (0)