mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 02:14:56 +02:00
Spanned Value step 1: span all value cases (#10042)
# Description This doesn't really do much that the user could see, but it helps get us ready to do the steps of the refactor to split the span off of Value, so that values can be spanless. This allows us to have top-level values that can hold both a Value and a Span, without requiring that all values have them. We expect to see significant memory reduction by removing so many unnecessary spans from values. For example, a table of 100,000 rows and 5 columns would have a savings of ~8megs in just spans that are almost always duplicated. # User-Facing Changes Nothing yet # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
This commit is contained in:
@ -83,8 +83,9 @@ fn abs_helper(val: Value, head: Span) -> Value {
|
||||
exp_input_type: "numeric".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -74,8 +74,9 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
exp_input_type: "numeric".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -74,8 +74,9 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
exp_input_type: "numeric".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -109,6 +109,7 @@ fn operate(value: Value, head: Span, base: f64) -> Value {
|
||||
head,
|
||||
span,
|
||||
)),
|
||||
span,
|
||||
};
|
||||
}
|
||||
// Specialize for better precision/performance
|
||||
@ -128,8 +129,9 @@ fn operate(value: Value, head: Span, base: f64) -> Value {
|
||||
exp_input_type: "numeric".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -90,9 +90,9 @@ pub fn median(values: &[Value], span: Span, head: Span) -> Result<Value, ShellEr
|
||||
return Err(ShellError::OperatorMismatch {
|
||||
op_span: head,
|
||||
lhs_ty: elem[0].get_type().to_string(),
|
||||
lhs_span: elem[0].span()?,
|
||||
lhs_span: elem[0].span(),
|
||||
rhs_ty: elem[1].get_type().to_string(),
|
||||
rhs_span: elem[1].span()?,
|
||||
rhs_span: elem[1].span(),
|
||||
});
|
||||
}
|
||||
Ok(elem[0].partial_cmp(&elem[1]).unwrap_or(Ordering::Equal))
|
||||
|
@ -116,9 +116,9 @@ pub fn mode(values: &[Value], _span: Span, head: Span) -> Result<Value, ShellErr
|
||||
return Err(ShellError::OperatorMismatch {
|
||||
op_span: head,
|
||||
lhs_ty: elem[0].get_type().to_string(),
|
||||
lhs_span: elem[0].span()?,
|
||||
lhs_span: elem[0].span(),
|
||||
rhs_ty: elem[1].get_type().to_string(),
|
||||
rhs_span: elem[1].span()?,
|
||||
rhs_span: elem[1].span(),
|
||||
});
|
||||
}
|
||||
Ok(elem[0].partial_cmp(&elem[1]).unwrap_or(Ordering::Equal))
|
||||
@ -143,12 +143,12 @@ pub fn mode(values: &[Value], _span: Span, head: Span) -> Result<Value, ShellErr
|
||||
Value::Filesize { val, .. } => {
|
||||
Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Filesize))
|
||||
}
|
||||
Value::Error { error } => Err(*error.clone()),
|
||||
Value::Error { error, .. } => Err(*error.clone()),
|
||||
other => Err(ShellError::UnsupportedInput(
|
||||
"Unable to give a result with this input".to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
other.span(),
|
||||
)),
|
||||
})
|
||||
.collect::<Result<Vec<HashableType>, ShellError>>()?;
|
||||
|
@ -42,9 +42,9 @@ pub fn max(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
|
||||
return Err(ShellError::OperatorMismatch {
|
||||
op_span: head,
|
||||
lhs_ty: biggest.get_type().to_string(),
|
||||
lhs_span: biggest.span()?,
|
||||
lhs_span: biggest.span(),
|
||||
rhs_ty: value.get_type().to_string(),
|
||||
rhs_span: value.span()?,
|
||||
rhs_span: value.span(),
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -73,9 +73,9 @@ pub fn min(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
|
||||
return Err(ShellError::OperatorMismatch {
|
||||
op_span: head,
|
||||
lhs_ty: smallest.get_type().to_string(),
|
||||
lhs_span: smallest.span()?,
|
||||
lhs_span: smallest.span(),
|
||||
rhs_ty: value.get_type().to_string(),
|
||||
rhs_span: value.span()?,
|
||||
rhs_span: value.span(),
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -112,13 +112,13 @@ pub fn sum(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellError
|
||||
| Value::Duration { .. } => {
|
||||
acc = acc.add(head, value, head)?;
|
||||
}
|
||||
Value::Error { error } => return Err(*error.clone()),
|
||||
Value::Error { error, .. } => return Err(*error.clone()),
|
||||
other => {
|
||||
return Err(ShellError::UnsupportedInput(
|
||||
"Attempted to compute the sum of a value that cannot be summed".to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
other.span(),
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -145,14 +145,14 @@ pub fn product(data: Vec<Value>, span: Span, head: Span) -> Result<Value, ShellE
|
||||
Value::Int { .. } | Value::Float { .. } => {
|
||||
acc = acc.mul(head, value, head)?;
|
||||
}
|
||||
Value::Error { error } => return Err(*error.clone()),
|
||||
Value::Error { error, .. } => return Err(*error.clone()),
|
||||
other => {
|
||||
return Err(ShellError::UnsupportedInput(
|
||||
"Attempted to compute the product of a value that cannot be multiplied"
|
||||
.to_string(),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
other.expect_span(),
|
||||
other.span(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -126,8 +126,9 @@ fn operate(value: Value, head: Span, precision: Option<i64>) -> Value {
|
||||
exp_input_type: "numeric".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -83,8 +83,9 @@ fn operate(value: Value, head: Span) -> Value {
|
||||
exp_input_type: "numeric".into(),
|
||||
wrong_type: other.get_type().to_string(),
|
||||
dst_span: head,
|
||||
src_span: other.expect_span(),
|
||||
src_span: other.span(),
|
||||
}),
|
||||
span: head,
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -97,6 +98,7 @@ fn error_negative_sqrt(head: Span, span: Span) -> Value {
|
||||
head,
|
||||
span,
|
||||
)),
|
||||
span,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,10 +34,10 @@ fn helper_for_tables(
|
||||
.or_insert_with(|| vec![value.clone()]);
|
||||
}
|
||||
}
|
||||
Value::Error { error } => return Err(*error.clone()),
|
||||
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.span(), name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -64,13 +64,13 @@ fn sum_of_squares(values: &[Value], span: Span) -> Result<Value, ShellError> {
|
||||
for value in values {
|
||||
let v = match &value {
|
||||
Value::Int { .. } | Value::Float { .. } => Ok(value),
|
||||
Value::Error { error } => Err(*error.clone()),
|
||||
Value::Error { error, .. } => Err(*error.clone()),
|
||||
_ => Err(ShellError::UnsupportedInput(
|
||||
"Attempted to compute the sum of squares of a non-integer, non-float value"
|
||||
.to_string(),
|
||||
"value originates from here".into(),
|
||||
span,
|
||||
value.expect_span(),
|
||||
value.span(),
|
||||
)),
|
||||
}?;
|
||||
let v_squared = &v.mul(span, v, span)?;
|
||||
|
Reference in New Issue
Block a user