Replace &Span with Span since Span is Copy (#9770)

# Description
`Span` is `Copy`, so we probably should not be passing references of
`Span` around. This PR replaces all instances of `&Span` with `Span`,
copying spans where necessary.

# User-Facing Changes
This alters some public functions to take `Span` instead of `&Span` as
input. Namely, `EngineState::get_span_contents`,
`nu_protocol::extract_value`, a bunch of the math commands, and
`Gstat::gstat`.
This commit is contained in:
Ian Manske
2023-07-31 19:47:46 +00:00
committed by GitHub
parent 94bec72079
commit 583ef8674e
35 changed files with 356 additions and 365 deletions

View File

@ -50,9 +50,9 @@ impl Command for SubCommand {
}
}
pub fn average(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
pub fn average(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
let sum = reducer_for(Reduce::Summation);
let total = &sum(Value::int(0, *head), values.to_vec(), span, *head)?;
let total = &sum(Value::int(0, head), values.to_vec(), span, head)?;
match total {
Value::Filesize { val, span } => Ok(Value::Filesize {
val: val / values.len() as i64,
@ -62,7 +62,7 @@ pub fn average(values: &[Value], span: Span, head: &Span) -> Result<Value, Shell
val: val / values.len() as i64,
span: *span,
}),
_ => total.div(*head, &Value::int(values.len() as i64, *head), *head),
_ => total.div(head, &Value::int(values.len() as i64, head), head),
}
}

View File

@ -60,9 +60,9 @@ impl Command for SubCommand {
}
}
pub fn maximum(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
pub fn maximum(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
let max_func = reducer_for(Reduce::Maximum);
max_func(Value::nothing(*head), values.to_vec(), span, *head)
max_func(Value::nothing(head), values.to_vec(), span, head)
}
#[cfg(test)]

View File

@ -69,7 +69,7 @@ enum Pick {
Median,
}
pub fn median(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
pub fn median(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
let take = if values.len() % 2 == 0 {
Pick::MedianAverage
} else {
@ -87,7 +87,7 @@ pub fn median(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellE
.map(|elem| {
if elem[0].partial_cmp(&elem[1]).is_none() {
return Err(ShellError::OperatorMismatch {
op_span: *head,
op_span: head,
lhs_ty: elem[0].get_type().to_string(),
lhs_span: elem[0].span()?,
rhs_ty: elem[1].get_type().to_string(),
@ -110,7 +110,7 @@ pub fn median(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellE
ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
*head,
head,
span,
)
})?;
@ -126,7 +126,7 @@ pub fn median(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellE
ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
*head,
head,
span,
)
})?
@ -138,7 +138,7 @@ pub fn median(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellE
ShellError::UnsupportedInput(
"Empty input".to_string(),
"value originates from here".into(),
*head,
head,
span,
)
})?

View File

@ -65,9 +65,9 @@ impl Command for SubCommand {
}
}
pub fn minimum(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
pub fn minimum(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
let min_func = reducer_for(Reduce::Minimum);
min_func(Value::nothing(*head), values.to_vec(), span, *head)
min_func(Value::nothing(head), values.to_vec(), span, head)
}
#[cfg(test)]

View File

@ -107,13 +107,13 @@ impl Command for SubCommand {
}
}
pub fn mode(values: &[Value], _span: Span, head: &Span) -> Result<Value, ShellError> {
pub fn mode(values: &[Value], _span: Span, head: Span) -> Result<Value, ShellError> {
if let Some(Err(values)) = values
.windows(2)
.map(|elem| {
if elem[0].partial_cmp(&elem[1]).is_none() {
return Err(ShellError::OperatorMismatch {
op_span: *head,
op_span: head,
lhs_ty: elem[0].get_type().to_string(),
lhs_span: elem[0].span()?,
rhs_ty: elem[1].get_type().to_string(),
@ -146,7 +146,7 @@ pub fn mode(values: &[Value], _span: Span, head: &Span) -> Result<Value, ShellEr
other => Err(ShellError::UnsupportedInput(
"Unable to give a result with this input".to_string(),
"value originates from here".into(),
*head,
head,
other.expect_span(),
)),
})
@ -165,10 +165,10 @@ pub fn mode(values: &[Value], _span: Span, head: &Span) -> Result<Value, ShellEr
Ordering::Less => {
max_freq = *frequency;
modes.clear();
modes.push(recreate_value(value, *head));
modes.push(recreate_value(value, head));
}
Ordering::Equal => {
modes.push(recreate_value(value, *head));
modes.push(recreate_value(value, head));
}
Ordering::Greater => (),
}
@ -177,7 +177,7 @@ pub fn mode(values: &[Value], _span: Span, head: &Span) -> Result<Value, ShellEr
modes.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
Ok(Value::List {
vals: modes,
span: *head,
span: head,
})
}

View File

@ -46,9 +46,9 @@ impl Command for SubCommand {
}
/// Calculate product of given values
pub fn product(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
pub fn product(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
let product_func = reducer_for(Reduce::Product);
product_func(Value::nothing(*head), values.to_vec(), span, *head)
product_func(Value::nothing(head), values.to_vec(), span, head)
}
#[cfg(test)]

View File

@ -65,8 +65,8 @@ impl Command for SubCommand {
}
}
pub fn compute_stddev(sample: bool) -> impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError> {
move |values: &[Value], span: Span, head: &Span| {
pub fn compute_stddev(sample: bool) -> impl Fn(&[Value], Span, Span) -> Result<Value, ShellError> {
move |values: &[Value], span: Span, head: Span| {
let variance = variance(sample)(values, span, head);
match variance {
Ok(Value::Float { val, span }) => Ok(Value::Float {

View File

@ -59,9 +59,9 @@ impl Command for SubCommand {
}
}
pub fn summation(values: &[Value], span: Span, head: &Span) -> Result<Value, ShellError> {
pub fn summation(values: &[Value], span: Span, head: Span) -> Result<Value, ShellError> {
let sum_func = reducer_for(Reduce::Summation);
sum_func(Value::nothing(*head), values.to_vec(), span, *head)
sum_func(Value::nothing(head), values.to_vec(), span, head)
}
#[cfg(test)]

View File

@ -5,7 +5,7 @@ use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Span, Spanned, Val
pub fn run_with_function(
call: &Call,
input: PipelineData,
mf: impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError>,
mf: impl Fn(&[Value], Span, Span) -> Result<Value, ShellError>,
) -> Result<PipelineData, ShellError> {
let name = call.head;
let res = calculate(input, name, mf);
@ -19,7 +19,7 @@ fn helper_for_tables(
values: &[Value],
val_span: Span,
name: Span,
mf: impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError>,
mf: impl Fn(&[Value], Span, Span) -> Result<Value, ShellError>,
) -> Result<Value, ShellError> {
// If we are not dealing with Primitives, then perhaps we are dealing with a table
// Create a key for each column name
@ -37,14 +37,14 @@ fn helper_for_tables(
Value::Error { error } => return Err(*error.clone()),
_ => {
//Turns out we are not dealing with a table
return mf(values, val.expect_span(), &name);
return mf(values, val.expect_span(), name);
}
}
}
// The mathematical function operates over the columns of the table
let mut column_totals = IndexMap::new();
for (col_name, col_vals) in column_values {
if let Ok(out) = mf(&col_vals, val_span, &name) {
if let Ok(out) = mf(&col_vals, val_span, name) {
column_totals.insert(col_name, out);
}
}
@ -66,7 +66,7 @@ fn helper_for_tables(
pub fn calculate(
values: PipelineData,
name: Span,
mf: impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError>,
mf: impl Fn(&[Value], Span, Span) -> Result<Value, ShellError>,
) -> Result<Value, ShellError> {
// TODO implement spans for ListStream, thus negating the need for unwrap_or().
let span = values.span().unwrap_or(name);
@ -81,13 +81,11 @@ pub fn calculate(
name,
mf,
),
_ => mf(vals, span, &name),
_ => mf(vals, span, name),
},
PipelineData::Value(Value::Record { vals, cols, span }, ..) => {
let new_vals: Result<Vec<Value>, ShellError> = vals
.into_iter()
.map(|val| mf(&[val], span, &name))
.collect();
let new_vals: Result<Vec<Value>, ShellError> =
vals.into_iter().map(|val| mf(&[val], span, name)).collect();
match new_vals {
Ok(vec) => Ok(Value::Record {
cols,
@ -100,12 +98,12 @@ pub fn calculate(
PipelineData::Value(Value::Range { val, span, .. }, ..) => {
let new_vals: Result<Vec<Value>, ShellError> = val
.into_range_iter(None)?
.map(|val| mf(&[val], span, &name))
.map(|val| mf(&[val], span, name))
.collect();
mf(&new_vals?, span, &name)
mf(&new_vals?, span, name)
}
PipelineData::Value(val, ..) => mf(&[val], span, &name),
PipelineData::Value(val, ..) => mf(&[val], span, name),
PipelineData::Empty { .. } => Err(ShellError::PipelineEmpty { dst_span: name }),
val => Err(ShellError::UnsupportedInput(
"Only integers, floats, lists, records or ranges are supported".into(),

View File

@ -57,10 +57,10 @@ impl Command for SubCommand {
}
}
fn sum_of_squares(values: &[Value], span: &Span) -> Result<Value, ShellError> {
let n = Value::int(values.len() as i64, *span);
let mut sum_x = Value::int(0, *span);
let mut sum_x2 = Value::int(0, *span);
fn sum_of_squares(values: &[Value], span: Span) -> Result<Value, ShellError> {
let n = Value::int(values.len() as i64, span);
let mut sum_x = Value::int(0, span);
let mut sum_x2 = Value::int(0, span);
for value in values {
let v = match &value {
Value::Int { .. } | Value::Float { .. } => Ok(value),
@ -69,36 +69,36 @@ fn sum_of_squares(values: &[Value], span: &Span) -> Result<Value, ShellError> {
"Attempted to compute the sum of squares of a non-integer, non-float value"
.to_string(),
"value originates from here".into(),
*span,
span,
value.expect_span(),
)),
}?;
let v_squared = &v.mul(*span, v, *span)?;
sum_x2 = sum_x2.add(*span, v_squared, *span)?;
sum_x = sum_x.add(*span, v, *span)?;
let v_squared = &v.mul(span, v, span)?;
sum_x2 = sum_x2.add(span, v_squared, span)?;
sum_x = sum_x.add(span, v, span)?;
}
let sum_x_squared = sum_x.mul(*span, &sum_x, *span)?;
let sum_x_squared_div_n = sum_x_squared.div(*span, &n, *span)?;
let sum_x_squared = sum_x.mul(span, &sum_x, span)?;
let sum_x_squared_div_n = sum_x_squared.div(span, &n, span)?;
let ss = sum_x2.sub(*span, &sum_x_squared_div_n, *span)?;
let ss = sum_x2.sub(span, &sum_x_squared_div_n, span)?;
Ok(ss)
}
pub fn compute_variance(
sample: bool,
) -> impl Fn(&[Value], Span, &Span) -> Result<Value, ShellError> {
move |values: &[Value], span: Span, head: &Span| {
) -> impl Fn(&[Value], Span, Span) -> Result<Value, ShellError> {
move |values: &[Value], span: Span, head: Span| {
let n = if sample {
values.len() - 1
} else {
values.len()
};
// sum_of_squares() needs the span of the original value, not the call head.
let ss = sum_of_squares(values, &span)?;
let n = Value::int(n as i64, *head);
ss.div(*head, &n, *head)
let ss = sum_of_squares(values, span)?;
let n = Value::int(n as i64, head);
ss.div(head, &n, head)
}
}