on master help sign in
fix: remove unneeded allocations when calling helix_view::Info::new
commitb58899bc8e0f339fd83a1740a896fcc62976f7a7 authorAlexis (Poliorcetics) Bourget <alexis.bourget@gmail.com> dateSat 8 Oct 2022, 20:06 (4 years ago) parent28cb89e
2 files changed+24−17
helix-term/src/commands.rs+1−7 helix-view/src/info.rs+23−10
helix-term/src/commands.rs+1−7
@@ -4455,13 +4455,7 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
44554455 (" ", "... or any character acting as a pair"),
44564456 ];
44574457
4458− cx.editor.autoinfo = Some(Info::new(
4459− title,
4460− help_text
4461− .into_iter()
4462− .map(|(col1, col2)| (col1.to_string(), col2.to_string()))
4463− .collect(),
4464− ));
4458+ cx.editor.autoinfo = Some(Info::new(title, &help_text));
44654459}
44664460
44674461fn surround_add(cx: &mut Context) {
helix-view/src/info.rs+23−10
@@ -16,7 +16,11 @@ pub struct Info {
1616}
1717
1818impl Info {
19− pub fn new(title: &str, body: Vec<(String, String)>) -> Self {
19+ pub fn new<T, U>(title: &str, body: &[(T, U)]) -> Self
20+ where
21+ T: AsRef<str>,
22+ U: AsRef<str>,
23+ {
2024 if body.is_empty() {
2125 return Self {
2226 title: title.to_string(),
@@ -26,11 +30,21 @@ impl Info {
2630 };
2731 }
2832
29− let item_width = body.iter().map(|(item, _)| item.width()).max().unwrap();
33+ let item_width = body
34+ .iter()
35+ .map(|(item, _)| item.as_ref().width())
36+ .max()
37+ .unwrap();
3038 let mut text = String::new();
3139
32− for (item, desc) in &body {
33− let _ = writeln!(text, "{:width$} {}", item, desc, width = item_width);
40+ for (item, desc) in body {
41+ let _ = writeln!(
42+ text,
43+ "{:width$} {}",
44+ item.as_ref(),
45+ desc.as_ref(),
46+ width = item_width
47+ );
3448 }
3549
3650 Self {
@@ -42,19 +56,19 @@ impl Info {
4256 }
4357
4458 pub fn from_keymap(title: &str, body: Vec<(&str, BTreeSet<KeyEvent>)>) -> Self {
45− let body = body
59+ let body: Vec<_> = body
4660 .into_iter()
4761 .map(|(desc, events)| {
4862 let events = events.iter().map(ToString::to_string).collect::<Vec<_>>();
49− (events.join(", "), desc.to_string())
63+ (events.join(", "), desc)
5064 })
5165 .collect();
5266
53− Self::new(title, body)
67+ Self::new(title, &body)
5468 }
5569
5670 pub fn from_registers(registers: &Registers) -> Self {
57− let body = registers
71+ let body: Vec<_> = registers
5872 .inner()
5973 .iter()
6074 .map(|(ch, reg)| {
@@ -62,13 +76,12 @@ impl Info {
6276 .read()
6377 .get(0)
6478 .and_then(|s| s.lines().next())
65− .map(String::from)
6679 .unwrap_or_default();
6780 (ch.to_string(), content)
6881 })
6982 .collect();
7083
71− let mut infobox = Self::new("Registers", body);
84+ let mut infobox = Self::new("Registers", &body);
7285 infobox.width = 30; // copied content could be very long
7386 infobox
7487 }