Deref &String arguments to &str where appropriate (#10321)

# Description
This generally makes for nicer APIs, as you are not forced to use an
existing allocation covering the full `String`.

Some exceptions remain where the underlying type requirements favor it.

# User-Facing Changes
None
This commit is contained in:
Stefan Holderbach
2023-09-12 08:06:56 +02:00
committed by GitHub
parent e90b099622
commit 6e9b6f22c9
11 changed files with 70 additions and 71 deletions

View File

@ -136,7 +136,7 @@ impl NuCompleter {
for (flat_idx, flat) in flattened.iter().enumerate() {
let is_passthrough_command = spans
.first()
.filter(|content| *content == &String::from("sudo"))
.filter(|content| content.as_str() == "sudo")
.is_some();
// Read the current spam to string
let current_span = working_set.get_span_contents(flat.0).to_vec();

View File

@ -116,9 +116,8 @@ pub fn evaluate_file(
if block.signature.name == "main" {
block.signature.name = source_filename.to_string_lossy().to_string();
} else if block.signature.name.starts_with("main ") {
block.signature.name = source_filename.to_string_lossy().to_string()
+ " "
+ &String::from_utf8_lossy(&block.signature.name.as_bytes()[5..]);
block.signature.name =
source_filename.to_string_lossy().to_string() + " " + &block.signature.name[5..];
}
}

View File

@ -2,7 +2,7 @@ use nu_test_support::fs::{files_exist_at, Stub::EmptyFile};
use nu_test_support::nu;
use nu_test_support::playground::Playground;
use std::fs;
use std::path::PathBuf;
use std::path::Path;
#[test]
fn removes_a_file() {
@ -376,10 +376,10 @@ fn removes_symlink() {
}
struct Cleanup<'a> {
dir_to_clean: &'a PathBuf,
dir_to_clean: &'a Path,
}
fn set_dir_read_only(directory: &PathBuf, read_only: bool) {
fn set_dir_read_only(directory: &Path, read_only: bool) {
let mut permissions = fs::metadata(directory).unwrap().permissions();
permissions.set_readonly(read_only);
fs::set_permissions(directory, permissions).expect("failed to set directory permissions");

View File

@ -376,8 +376,8 @@ impl Stack {
engine_state.env_vars.contains_key(name)
}
pub fn is_overlay_active(&self, name: &String) -> bool {
self.active_overlays.contains(name)
pub fn is_overlay_active(&self, name: &str) -> bool {
self.active_overlays.iter().any(|n| n == name)
}
pub fn add_overlay(&mut self, name: String) {
@ -385,7 +385,7 @@ impl Stack {
self.active_overlays.push(name);
}
pub fn remove_overlay(&mut self, name: &String) {
pub fn remove_overlay(&mut self, name: &str) {
self.active_overlays.retain(|o| o != name);
}
}

View File

@ -7,19 +7,19 @@ use crate::{
record, HistoryFileFormat, PipelineData, Range, Record, ShellError, Span, Value,
};
use nu_system::os_info::{get_kernel_version, get_os_arch, get_os_family, get_os_name};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Value, ShellError> {
fn canonicalize_path(engine_state: &EngineState, path: &PathBuf) -> PathBuf {
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
let cwd = engine_state.current_work_dir();
if path.exists() {
match nu_path::canonicalize_with(path, cwd) {
Ok(canon_path) => canon_path,
Err(_) => path.clone(),
Err(_) => path.to_owned(),
}
} else {
path.clone()
path.to_owned()
}
}

View File

@ -469,9 +469,9 @@ pub fn read_std(std: &[u8]) -> String {
out.replace('\n', "")
}
use std::{path::PathBuf, process::Command};
use std::{path::Path, process::Command};
pub fn run_command(executable_path: &PathBuf, target_cwd: &str) -> Command {
pub fn run_command(executable_path: &Path, target_cwd: &str) -> Command {
let mut command = Command::new(executable_path);
command

View File

@ -30,7 +30,7 @@ pub fn says() -> Play {
trait CheckerMatchers {
fn output(&self, actual: &Outcome) -> MatchResult;
fn std(&self, actual: &[u8], expected: Option<&String>, description: &str) -> MatchResult;
fn std(&self, actual: &[u8], expected: Option<&str>, description: &str) -> MatchResult;
fn stdout(&self, actual: &Outcome) -> MatchResult;
}
@ -40,10 +40,10 @@ impl CheckerMatchers for Play {
}
fn stdout(&self, actual: &Outcome) -> MatchResult {
self.std(&actual.out, self.stdout_expectation.as_ref(), "stdout")
self.std(&actual.out, self.stdout_expectation.as_deref(), "stdout")
}
fn std(&self, actual: &[u8], expected: Option<&String>, description: &str) -> MatchResult {
fn std(&self, actual: &[u8], expected: Option<&str>, description: &str) -> MatchResult {
let out = match expected {
Some(out) => out,
None => return Ok(()),
@ -51,7 +51,7 @@ impl CheckerMatchers for Play {
let actual =
str::from_utf8(actual).map_err(|_| format!("{description} was not utf8 encoded"))?;
if actual != *out {
if actual != out {
return Err(format!(
"not equal:\n actual: {actual}\n expected: {out}\n\n"
));

View File

@ -46,12 +46,12 @@ impl Dirs {
self.fixtures.join("playground/config")
}
pub fn root(&self) -> &PathBuf {
&self.root
pub fn root(&self) -> &Path {
self.root.as_path()
}
pub fn test(&self) -> &PathBuf {
&self.test
pub fn test(&self) -> &Path {
self.test.as_path()
}
}