2
2
3
3
use std:: { cmp, fmt, hash, mem, ops, ptr, slice, str} ;
4
4
5
- use crate :: translate:: * ;
5
+ use crate :: { translate:: * , GStr } ;
6
6
7
7
wrapper ! {
8
8
// rustdoc-stripper-ignore-next
@@ -12,7 +12,7 @@ wrapper! {
12
12
pub struct GStringBuilder ( BoxedInline <ffi:: GString >) ;
13
13
14
14
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 ( ) ) ,
16
16
free => |ptr| ffi:: g_string_free( ptr, ffi:: GTRUE ) ,
17
17
init => |ptr| unsafe {
18
18
let inner = ffi:: GString {
@@ -111,7 +111,7 @@ impl GStringBuilder {
111
111
}
112
112
113
113
// rustdoc-stripper-ignore-next
114
- /// Returns `& str` slice.
114
+ /// Returns <code>&[ str]</code> slice.
115
115
pub fn as_str ( & self ) -> & str {
116
116
unsafe {
117
117
let ptr: * const u8 = self . inner . str as _ ;
@@ -125,7 +125,21 @@ impl GStringBuilder {
125
125
}
126
126
127
127
// 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`].
129
143
#[ must_use = "String returned from the builder should probably be used" ]
130
144
pub fn into_string ( self ) -> crate :: GString {
131
145
unsafe {
@@ -173,6 +187,18 @@ impl PartialEq<GStringBuilder> for str {
173
187
}
174
188
}
175
189
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
+
176
202
impl Eq for GStringBuilder { }
177
203
178
204
impl cmp:: PartialOrd for GStringBuilder {
@@ -193,6 +219,18 @@ impl cmp::PartialOrd<GStringBuilder> for str {
193
219
}
194
220
}
195
221
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
+
196
234
impl cmp:: Ord for GStringBuilder {
197
235
fn cmp ( & self , other : & Self ) -> cmp:: Ordering {
198
236
self . as_str ( ) . cmp ( other. as_str ( ) )
@@ -220,6 +258,12 @@ impl AsRef<str> for GStringBuilder {
220
258
}
221
259
}
222
260
261
+ impl AsRef < GStr > for GStringBuilder {
262
+ fn as_ref ( & self ) -> & GStr {
263
+ self . as_gstr ( )
264
+ }
265
+ }
266
+
223
267
impl ops:: Deref for GStringBuilder {
224
268
type Target = str ;
225
269
0 commit comments