diff options
| author | Morgan Thomas <corp@m0rg.dev> | 2022-03-12 05:01:25 -0800 |
|---|---|---|
| committer | Morgan Thomas <corp@m0rg.dev> | 2022-03-12 05:01:25 -0800 |
| commit | f27c0ef1cf41ad381ac4168bb291442b68a4932b (patch) | |
| tree | 22a51aa9337576ac990f906c72a88dabfc2c4de0 | |
| parent | d430ddd809c7bb9721f4a5550cc1ff63eb700845 (diff) | |
| download | rust-f27c0ef1cf41ad381ac4168bb291442b68a4932b.tar.gz rust-f27c0ef1cf41ad381ac4168bb291442b68a4932b.zip | |
Reduce intermediate string allocations in render::compound::render_record and ::render_tuple
| -rw-r--r-- | crates/ide_completion/src/render/compound.rs | 56 |
1 files changed, 24 insertions, 32 deletions
diff --git a/crates/ide_completion/src/render/compound.rs b/crates/ide_completion/src/render/compound.rs index a1a199c6e2e..c7f3bd1f79a 100644 --- a/crates/ide_completion/src/render/compound.rs +++ b/crates/ide_completion/src/render/compound.rs @@ -22,24 +22,21 @@ pub(crate) fn render_record( fields: &[hir::Field], name: Option<&str>, ) -> RenderedCompound { - let fields = fields.iter(); + let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| { + if snippet_cap.is_some() { + f(&format_args!("{}: ${{{}:()}}", field.name(db), idx + 1)) + } else { + f(&format_args!("{}: ()", field.name(db))) + } + }); + + let types = fields.iter().format_with(", ", |field, f| { + f(&format_args!("{}: {}", field.name(db), field.ty(db).display(db))) + }); - let (completions, types): (Vec<_>, Vec<_>) = fields - .enumerate() - .map(|(idx, field)| { - ( - if snippet_cap.is_some() { - format!("{}: ${{{}:()}}", field.name(db), idx + 1) - } else { - format!("{}: ()", field.name(db)) - }, - format!("{}: {}", field.name(db), field.ty(db).display(db)), - ) - }) - .unzip(); RenderedCompound { - literal: format!("{} {{ {} }}", name.unwrap_or(""), completions.iter().format(", ")), - detail: format!("{} {{ {} }}", name.unwrap_or(""), types.iter().format(", ")), + literal: format!("{} {{ {} }}", name.unwrap_or(""), completions), + detail: format!("{} {{ {} }}", name.unwrap_or(""), types), } } @@ -51,24 +48,19 @@ pub(crate) fn render_tuple( fields: &[hir::Field], name: Option<&str>, ) -> RenderedCompound { - let fields = fields.iter(); + let completions = fields.iter().enumerate().format_with(", ", |(idx, _), f| { + if snippet_cap.is_some() { + f(&format_args!("${{{}:()}}", idx + 1)) + } else { + f(&format_args!("()")) + } + }); + + let types = fields.iter().format_with(", ", |field, f| f(&field.ty(db).display(db))); - let (completions, types): (Vec<_>, Vec<_>) = fields - .enumerate() - .map(|(idx, field)| { - ( - if snippet_cap.is_some() { - format!("${{{}:()}}", (idx + 1).to_string()) - } else { - "()".to_string() - }, - field.ty(db).display(db).to_string(), - ) - }) - .unzip(); RenderedCompound { - literal: format!("{}({})", name.unwrap_or(""), completions.iter().format(", ")), - detail: format!("{}({})", name.unwrap_or(""), types.iter().format(", ")), + literal: format!("{}({})", name.unwrap_or(""), completions), + detail: format!("{}({})", name.unwrap_or(""), types), } } |
