File tree Expand file tree Collapse file tree 2 files changed +17
-10
lines changed Expand file tree Collapse file tree 2 files changed +17
-10
lines changed Original file line number Diff line number Diff line change @@ -440,6 +440,21 @@ impl GString {
440
440
pub unsafe fn from_ptr_lossy < ' a > ( ptr : * const c_char ) -> Cow < ' a , GStr > {
441
441
GStr :: from_ptr_lossy ( ptr)
442
442
}
443
+
444
+ // rustdoc-stripper-ignore-next
445
+ /// Wraps a raw C string with a safe GLib string wrapper. The provided C string **must** be
446
+ /// nul-terminated. All constraints from [`std::ffi::CStr::from_ptr`] also apply here.
447
+ ///
448
+ /// `len` is the length without the nul-terminator, i.e. if `len == 0` is passed then `*ptr`
449
+ /// must be the nul-terminator.
450
+ pub unsafe fn from_ptr_and_len_unchecked ( ptr : * const c_char , len : usize ) -> Self {
451
+ assert ! ( !ptr. is_null( ) ) ;
452
+
453
+ GString ( Inner :: Foreign {
454
+ ptr : ptr:: NonNull :: new_unchecked ( ptr as * mut _ ) ,
455
+ len,
456
+ } )
457
+ }
443
458
}
444
459
445
460
impl IntoGlibPtr < * mut c_char > for GString {
Original file line number Diff line number Diff line change @@ -116,10 +116,6 @@ impl GStringBuilder {
116
116
117
117
// rustdoc-stripper-ignore-next
118
118
/// Returns `&str` slice.
119
- ///
120
- /// # Panics
121
- ///
122
- /// If the string builder contains invalid UTF-8 this function panics.
123
119
pub fn as_str ( & self ) -> & str {
124
120
unsafe {
125
121
let ptr: * const u8 = self . inner . str as _ ;
@@ -128,21 +124,17 @@ impl GStringBuilder {
128
124
return "" ;
129
125
}
130
126
let slice = slice:: from_raw_parts ( ptr, len) ;
131
- std:: str:: from_utf8 ( slice) . unwrap ( )
127
+ std:: str:: from_utf8_unchecked ( slice)
132
128
}
133
129
}
134
130
135
131
// rustdoc-stripper-ignore-next
136
132
/// Returns `&str` slice.
137
- ///
138
- /// # Panics
139
- ///
140
- /// If the string builder contains invalid UTF-8 this function panics.
141
133
#[ must_use = "String returned from the builder should probably be used" ]
142
134
pub fn into_string ( self ) -> crate :: GString {
143
135
unsafe {
144
136
let s = mem:: ManuallyDrop :: new ( self ) ;
145
- crate :: GString :: from_glib_full_num ( s. inner . str , s. inner . len )
137
+ crate :: GString :: from_ptr_and_len_unchecked ( s. inner . str , s. inner . len )
146
138
}
147
139
}
148
140
}
You can’t perform that action at this time.
0 commit comments