Avoid taking unnecessary ownership of intermediates (#12740)

# Description

Judiciously try to avoid allocations/clone by changing the signature of
functions

- **Don't pass str by value unnecessarily if only read**
- **Don't require a vec in `Sandbox::with_files`**
- **Remove unnecessary string clone**
- **Fixup unnecessary borrow**
- **Use `&str` in shape color instead**
- **Vec -> Slice**
- **Elide string clone**
- **Elide `Path` clone**
- **Take &str to elide clone in tests**

# User-Facing Changes
None

# Tests + Formatting
This touches many tests purely in changing from owned to borrowed/static
data
This commit is contained in:
Stefan Holderbach
2024-05-04 02:53:15 +02:00
committed by GitHub
parent e6f473695c
commit 406df7f208
69 changed files with 527 additions and 553 deletions

View File

@ -13,7 +13,7 @@ pub fn lookup_ansi_color_style(s: &str) -> Style {
.and_then(|c| c.map(|c| c.normal()))
.unwrap_or_default()
} else if s.starts_with('{') {
color_string_to_nustyle(s.to_string())
color_string_to_nustyle(s)
} else {
lookup_style(s)
}
@ -74,13 +74,13 @@ fn get_style_from_value(record: &Record) -> Option<NuStyle> {
}
}
fn color_string_to_nustyle(color_string: String) -> Style {
fn color_string_to_nustyle(color_string: &str) -> Style {
// eprintln!("color_string: {}", &color_string);
if color_string.is_empty() {
return Style::default();
}
let nu_style = match nu_json::from_str::<NuStyle>(&color_string) {
let nu_style = match nu_json::from_str::<NuStyle>(color_string) {
Ok(s) => s,
Err(_) => return Style::default(),
};
@ -97,13 +97,13 @@ mod tests {
#[test]
fn test_color_string_to_nustyle_empty_string() {
let color_string = String::new();
let style = color_string_to_nustyle(color_string);
let style = color_string_to_nustyle(&color_string);
assert_eq!(style, Style::default());
}
#[test]
fn test_color_string_to_nustyle_valid_string() {
let color_string = r#"{"fg": "black", "bg": "white", "attr": "b"}"#.to_string();
let color_string = r#"{"fg": "black", "bg": "white", "attr": "b"}"#;
let style = color_string_to_nustyle(color_string);
assert_eq!(style.foreground, Some(Color::Black));
assert_eq!(style.background, Some(Color::White));
@ -112,7 +112,7 @@ mod tests {
#[test]
fn test_color_string_to_nustyle_invalid_string() {
let color_string = "invalid string".to_string();
let color_string = "invalid string";
let style = color_string_to_nustyle(color_string);
assert_eq!(style, Style::default());
}

View File

@ -3,8 +3,8 @@ use nu_ansi_term::{Color, Style};
use nu_protocol::{Config, Value};
// The default colors for shapes, used when there is no config for them.
pub fn default_shape_color(shape: String) -> Style {
match shape.as_ref() {
pub fn default_shape_color(shape: &str) -> Style {
match shape {
"shape_and" => Style::new().fg(Color::Purple).bold(),
"shape_binary" => Style::new().fg(Color::Purple).bold(),
"shape_block" => Style::new().fg(Color::Blue).bold(),
@ -45,8 +45,8 @@ pub fn default_shape_color(shape: String) -> Style {
}
}
pub fn get_shape_color(shape: String, conf: &Config) -> Style {
match conf.color_config.get(shape.as_str()) {
pub fn get_shape_color(shape: &str, conf: &Config) -> Style {
match conf.color_config.get(shape) {
Some(int_color) => {
// Shapes do not use color_config closures, currently.
match int_color {