From aff886da645f070476e1dca414496e9f1054ec78 Mon Sep 17 00:00:00 2001 From: Christopher Serr Date: Mon, 27 Mar 2023 23:14:40 +0200 Subject: [PATCH] Allow buffer reuse in `BufferLine` With the way `BufferLine::set_text` was written, you would always clone the `String` / `str` that you are passing in, resulting in the function almost not being any better than simply creating a new `BufferLine`. This ensures the internal buffer is reused. --- src/buffer_line.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/buffer_line.rs b/src/buffer_line.rs index 3ff511a..fb808f0 100644 --- a/src/buffer_line.rs +++ b/src/buffer_line.rs @@ -38,13 +38,11 @@ impl BufferLine { /// /// Will reset shape and layout if it differs from current text and attributes list. /// Returns true if the line was reset - pub fn set_text + Into>( - &mut self, - text: T, - attrs_list: AttrsList, - ) -> bool { - if text.as_ref() != self.text || attrs_list != self.attrs_list { - self.text = text.into(); + pub fn set_text>(&mut self, text: T, attrs_list: AttrsList) -> bool { + let text = text.as_ref(); + if text != self.text || attrs_list != self.attrs_list { + self.text.clear(); + self.text.push_str(text); self.attrs_list = attrs_list; self.reset(); true