From dceefcdaba6fbb0e8b1634ef9ad366b49053c568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20H=C3=B8rl=C3=BCck=20Berg?= <36937807+henrikhorluck@users.noreply.github.com> Date: Sun, 13 Aug 2023 23:14:40 +0200 Subject: [PATCH] Add an appenln method to output_stream_t This is an alternative to the very common pattern of ```rust streams.err.append(output); streams.err.append1('\n'); ``` Which has negative performance implications, see https://github.com/fish-shell/fish-shell/pull/9229 It takes `Into` to hopefully avoid allocating anew when the argument is a WString with leftover capacity --- fish-rust/src/builtins/shared.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fish-rust/src/builtins/shared.rs b/fish-rust/src/builtins/shared.rs index f27cb43b9..b6c86415d 100644 --- a/fish-rust/src/builtins/shared.rs +++ b/fish-rust/src/builtins/shared.rs @@ -114,6 +114,12 @@ pub fn append>(&mut self, s: Str) -> bool { self.ffi().append(&s.as_ref().into_cpp()) } + /// Append a &wstr or WString with a newline + pub fn appendln(&mut self, s: impl Into) -> bool { + let s = s.into() + L!("\n"); + self.ffi().append(&s.into_cpp()) + } + /// Append a char. pub fn append1(&mut self, c: char) -> bool { self.append(wstr::from_char_slice(&[c]))