Skip to content

Commit 8bf3a36

Browse files
Merge pull request #1016 from sdroege/into-gstr-for-string
glib: Optimize `IntoGStr` impl for `String` by simply appending a NUL-byte
2 parents 459bd8d + 1535024 commit 8bf3a36

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

glib/src/gstring.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,12 +2317,10 @@ impl IntoGStr for &str {
23172317

23182318
impl IntoGStr for String {
23192319
#[inline]
2320-
fn run_with_gstr<T, F: FnOnce(&GStr) -> T>(self, f: F) -> T {
2321-
if self.len() < MAX_STACK_ALLOCATION {
2322-
self.as_str().run_with_gstr(f)
2323-
} else {
2324-
f(GString::from(self).as_gstr())
2325-
}
2320+
fn run_with_gstr<T, F: FnOnce(&GStr) -> T>(mut self, f: F) -> T {
2321+
self.push('\0');
2322+
let gs = unsafe { GStr::from_utf8_with_nul_unchecked(self.as_bytes()) };
2323+
f(gs)
23262324
}
23272325
}
23282326

0 commit comments

Comments
 (0)