@@ -168,7 +168,7 @@ impl GStr {
168
168
/// This function is the equivalent of [`GStr::to_bytes`] except that it will retain the
169
169
/// trailing nul terminator instead of chopping it off.
170
170
#[ inline]
171
- pub fn as_bytes_with_nul ( & self ) -> & [ u8 ] {
171
+ pub const fn as_bytes_with_nul ( & self ) -> & [ u8 ] {
172
172
self . 0 . as_bytes ( )
173
173
}
174
174
// rustdoc-stripper-ignore-next
@@ -177,7 +177,7 @@ impl GStr {
177
177
/// The returned slice will **not** contain the trailing nul terminator that this GLib
178
178
/// string has.
179
179
#[ inline]
180
- pub fn as_bytes ( & self ) -> & [ u8 ] {
180
+ pub const fn as_bytes ( & self ) -> & [ u8 ] {
181
181
self . as_str ( ) . as_bytes ( )
182
182
}
183
183
// rustdoc-stripper-ignore-next
@@ -192,15 +192,20 @@ impl GStr {
192
192
/// writes to it) causes undefined behavior. It is your responsibility to make
193
193
/// sure that the underlying memory is not freed too early.
194
194
#[ inline]
195
- pub fn as_ptr ( & self ) -> * const c_char {
195
+ pub const fn as_ptr ( & self ) -> * const c_char {
196
196
self . 0 . as_ptr ( ) as * const _
197
197
}
198
198
// rustdoc-stripper-ignore-next
199
199
/// Converts this GLib string to a string slice.
200
200
#[ inline]
201
- pub fn as_str ( & self ) -> & str {
201
+ pub const fn as_str ( & self ) -> & str {
202
202
// Clip off the nul-byte
203
- unsafe { self . 0 . get_unchecked ( ..self . 0 . len ( ) - 1 ) }
203
+ unsafe {
204
+ std:: str:: from_utf8_unchecked ( std:: slice:: from_raw_parts (
205
+ self . as_ptr ( ) as * const _ ,
206
+ self . 0 . len ( ) - 1 ,
207
+ ) )
208
+ }
204
209
}
205
210
// rustdoc-stripper-ignore-next
206
211
/// Converts this GLib string to a C string slice, checking for interior nul-bytes.
@@ -227,7 +232,7 @@ impl GStr {
227
232
/// `self` **must** not contain any interior nul-bytes besides the final terminating nul-byte.
228
233
/// It is undefined behavior to call this on a string that contains interior nul-bytes.
229
234
#[ inline]
230
- pub unsafe fn to_cstr_unchecked ( & self ) -> & CStr {
235
+ pub const unsafe fn to_cstr_unchecked ( & self ) -> & CStr {
231
236
CStr :: from_bytes_with_nul_unchecked ( self . as_bytes_with_nul ( ) )
232
237
}
233
238
0 commit comments