Use slices directly instead of &Vec (#10328)

Simplifies the signature, makes it more flexible.
Detected a few unnecessary allocations in the process.
This commit is contained in:
Stefan Holderbach
2023-09-12 05:38:20 +02:00
committed by GitHub
parent 84c10de864
commit e90b099622
17 changed files with 48 additions and 46 deletions

View File

@ -235,7 +235,7 @@ mod util {
}
}
fn convert_records_to_dataset(cols: &Vec<String>, records: Vec<Value>) -> Vec<Vec<String>> {
fn convert_records_to_dataset(cols: &[String], records: Vec<Value>) -> Vec<Vec<String>> {
if !cols.is_empty() {
create_table_for_record(cols, &records)
} else if cols.is_empty() && records.is_empty() {

View File

@ -294,7 +294,7 @@ fn record_matches_regex(values: &[Value], re: &Regex, config: &Config) -> bool {
#[allow(clippy::too_many_arguments)]
fn highlight_terms_in_record_with_search_columns(
search_cols: &Vec<String>,
search_cols: &[String],
record: &Record,
span: Span,
config: &Config,
@ -507,7 +507,7 @@ fn value_should_be_printed(
filter_config: &Config,
lower_terms: &[Value],
span: Span,
columns_to_search: &Vec<String>,
columns_to_search: &[String],
invert: bool,
) -> bool {
let lower_value = Value::string(value.into_string("", filter_config).to_lowercase(), span);
@ -561,7 +561,7 @@ fn term_equals_value(term: &Value, value: &Value, span: Span) -> bool {
fn record_matches_term(
record: &Record,
columns_to_search: &Vec<String>,
columns_to_search: &[String],
filter_config: &Config,
term: &Value,
span: Span,

View File

@ -210,7 +210,7 @@ pub fn group_no_grouper(values: Vec<Value>, span: Span) -> Result<Value, ShellEr
// TODO: refactor this, it's a bit of a mess
fn group_closure(
values: &Vec<Value>,
values: &[Value],
span: Span,
block: Option<Closure>,
stack: &mut Stack,
@ -219,7 +219,7 @@ fn group_closure(
) -> Result<Value, ShellError> {
let error_key = "error";
let mut keys: Vec<Result<String, ShellError>> = vec![];
let value_list = Value::list(values.clone(), span);
let value_list = Value::list(values.to_vec(), span);
for value in values {
if let Some(capture_block) = &block {

View File

@ -23,8 +23,6 @@ enum IncludeInner {
Yes,
}
const EMPTY_COL_NAMES: &Vec<String> = &vec![];
impl Command for Join {
fn name(&self) -> &str {
"join"
@ -142,8 +140,8 @@ fn join_type(call: &Call) -> Result<JoinType, nu_protocol::ShellError> {
}
fn join(
left: &Vec<Value>,
right: &Vec<Value>,
left: &[Value],
right: &[Value],
left_join_key: &str,
right_join_key: &str,
join_type: JoinType,
@ -248,7 +246,7 @@ fn join(
#[allow(clippy::too_many_arguments)]
fn join_rows(
result: &mut Vec<Value>,
this: &Vec<Value>,
this: &[Value],
this_join_key: &str,
other: HashMap<String, Vec<&Record>>,
other_keys: &[String],
@ -323,20 +321,20 @@ fn join_rows(
// Return column names (i.e. ordered keys from the first row; we assume that
// these are the same for all rows).
fn column_names(table: &[Value]) -> &Vec<String> {
fn column_names(table: &[Value]) -> &[String] {
table
.iter()
.find_map(|val| match val {
Value::Record { val, .. } => Some(&val.cols),
Value::Record { val, .. } => Some(&*val.cols),
_ => None,
})
.unwrap_or(EMPTY_COL_NAMES)
.unwrap_or_default()
}
// Create a map from value in `on` column to a list of the rows having that
// value.
fn lookup_table<'a>(
rows: &'a Vec<Value>,
rows: &'a [Value],
on: &str,
sep: &str,
cap: usize,

View File

@ -76,7 +76,7 @@ impl Command for UniqBy {
let metadata = input.metadata();
let vec: Vec<_> = input.into_iter().collect();
match validate(vec.clone(), &columns, call.head) {
match validate(&vec, &columns, call.head) {
Ok(_) => {}
Err(err) => {
return Err(err);
@ -113,7 +113,7 @@ impl Command for UniqBy {
}
}
fn validate(vec: Vec<Value>, columns: &Vec<String>, span: Span) -> Result<(), ShellError> {
fn validate(vec: &[Value], columns: &[String], span: Span) -> Result<(), ShellError> {
let first = vec.first();
if let Some(v) = first {
let val_span = v.span();
@ -129,7 +129,7 @@ fn validate(vec: Vec<Value>, columns: &Vec<String>, span: Span) -> Result<(), Sh
));
}
if let Some(nonexistent) = nonexistent_column(columns.clone(), record.cols.clone()) {
if let Some(nonexistent) = nonexistent_column(columns, &record.cols) {
return Err(ShellError::CantFindColumn {
col_name: nonexistent,
span,

View File

@ -46,7 +46,7 @@ fn record_to_delimited(
}
fn table_to_delimited(
vals: &Vec<Value>,
vals: &[Value],
span: Span,
separator: char,
config: &Config,

View File

@ -77,8 +77,7 @@ pub fn sort(
));
}
if let Some(nonexistent) = nonexistent_column(sort_columns.clone(), record.cols.clone())
{
if let Some(nonexistent) = nonexistent_column(&sort_columns, &record.cols) {
return Err(ShellError::CantFindColumn {
col_name: nonexistent,
span,