mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 01:15:14 +02:00
Boxes record for smaller Value enum. (#12252)
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> Boxes `Record` inside `Value` to reduce memory usage, `Value` goes from `72` -> `56` bytes after this change. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `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:
@ -182,7 +182,7 @@ fn run_histogram(
|
||||
match v {
|
||||
// parse record, and fill valid value to actual input.
|
||||
Value::Record { val, .. } => {
|
||||
for (c, v) in val {
|
||||
for (c, v) in *val {
|
||||
if &c == col_name {
|
||||
if let Ok(v) = HashableValue::from_value(v, head_span) {
|
||||
inputs.push(v);
|
||||
|
@ -135,7 +135,7 @@ fn into_record(
|
||||
.collect(),
|
||||
span,
|
||||
),
|
||||
Value::Record { val, .. } => Value::record(val, span),
|
||||
Value::Record { val, .. } => Value::record(*val, span),
|
||||
Value::Error { .. } => input,
|
||||
other => Value::error(
|
||||
ShellError::TypeMismatch {
|
||||
|
2
crates/nu-command/src/env/load_env.rs
vendored
2
crates/nu-command/src/env/load_env.rs
vendored
@ -58,7 +58,7 @@ impl Command for LoadEnv {
|
||||
}
|
||||
None => match input {
|
||||
PipelineData::Value(Value::Record { val, .. }, ..) => {
|
||||
for (env_var, rhs) in val {
|
||||
for (env_var, rhs) in *val {
|
||||
let env_var_ = env_var.as_str();
|
||||
if ["FILE_PWD", "CURRENT_FILE"].contains(&env_var_) {
|
||||
return Err(ShellError::AutomaticEnvVarSetManually {
|
||||
|
4
crates/nu-command/src/env/with_env.rs
vendored
4
crates/nu-command/src/env/with_env.rs
vendored
@ -95,7 +95,7 @@ fn with_env(
|
||||
// single row([[X W]; [Y Z]])
|
||||
match &table[0] {
|
||||
Value::Record { val, .. } => {
|
||||
for (k, v) in val {
|
||||
for (k, v) in &**val {
|
||||
env.insert(k.to_string(), v.clone());
|
||||
}
|
||||
}
|
||||
@ -123,7 +123,7 @@ fn with_env(
|
||||
}
|
||||
// when get object by `open x.json` or `from json`
|
||||
Value::Record { val, .. } => {
|
||||
for (k, v) in val {
|
||||
for (k, v) in &**val {
|
||||
env.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
|
@ -112,7 +112,7 @@ fn default(
|
||||
record.push(column.item.clone(), value.clone());
|
||||
}
|
||||
|
||||
Value::record(record, span)
|
||||
Value::record(*record, span)
|
||||
}
|
||||
_ => item,
|
||||
}
|
||||
|
@ -129,7 +129,7 @@ fn drop_cols(
|
||||
} => {
|
||||
let len = record.len().saturating_sub(columns);
|
||||
record.truncate(len);
|
||||
Ok(Value::record(record, span).into_pipeline_data_with_metadata(metadata))
|
||||
Ok(Value::record(*record, span).into_pipeline_data_with_metadata(metadata))
|
||||
}
|
||||
// Propagate errors
|
||||
Value::Error { error, .. } => Err(*error),
|
||||
|
@ -170,7 +170,7 @@ fn flat_value(columns: &[CellPath], item: Value, all: bool) -> Vec<Value> {
|
||||
match value {
|
||||
Value::Record { val, .. } => {
|
||||
if need_flatten {
|
||||
for (col, val) in val {
|
||||
for (col, val) in *val {
|
||||
if out.contains_key(&col) {
|
||||
out.insert(format!("{column}_{col}"), val);
|
||||
} else {
|
||||
@ -178,9 +178,9 @@ fn flat_value(columns: &[CellPath], item: Value, all: bool) -> Vec<Value> {
|
||||
}
|
||||
}
|
||||
} else if out.contains_key(&column) {
|
||||
out.insert(format!("{column}_{column}"), Value::record(val, span));
|
||||
out.insert(format!("{column}_{column}"), Value::record(*val, span));
|
||||
} else {
|
||||
out.insert(column, Value::record(val, span));
|
||||
out.insert(column, Value::record(*val, span));
|
||||
}
|
||||
}
|
||||
Value::List { vals, .. } => {
|
||||
|
@ -228,7 +228,7 @@ fn rename(
|
||||
}
|
||||
}
|
||||
|
||||
Value::record(record, span)
|
||||
Value::record(*record, span)
|
||||
}
|
||||
// Propagate errors by explicitly matching them before the final case.
|
||||
Value::Error { .. } => item.clone(),
|
||||
|
@ -149,7 +149,7 @@ impl Command for Sort {
|
||||
// Records have two sorting methods, toggled by presence or absence of -v
|
||||
PipelineData::Value(Value::Record { val, .. }, ..) => {
|
||||
let sort_by_value = call.has_flag(engine_state, stack, "values")?;
|
||||
let record = sort_record(val, span, sort_by_value, reverse, insensitive, natural);
|
||||
let record = sort_record(*val, span, sort_by_value, reverse, insensitive, natural);
|
||||
Ok(record.into_pipeline_data())
|
||||
}
|
||||
// Other values are sorted here
|
||||
|
@ -111,7 +111,7 @@ pub fn get_values<'a>(
|
||||
for item in input {
|
||||
match item {
|
||||
Value::Record { val, .. } => {
|
||||
for (k, v) in val {
|
||||
for (k, v) in &**val {
|
||||
if let Some(vec) = output.get_mut(k) {
|
||||
vec.push(v.clone());
|
||||
} else {
|
||||
|
@ -417,7 +417,7 @@ mod tests {
|
||||
content_tag(
|
||||
"nu",
|
||||
indexmap! {},
|
||||
&vec![
|
||||
&[
|
||||
content_tag("dev", indexmap! {}, &[content_string("Andrés")]),
|
||||
content_tag("dev", indexmap! {}, &[content_string("JT")]),
|
||||
content_tag("dev", indexmap! {}, &[content_string("Yehuda")])
|
||||
|
@ -135,7 +135,7 @@ pub fn value_to_json_value(v: &Value) -> Result<nu_json::Value, ShellError> {
|
||||
}
|
||||
Value::Record { val, .. } => {
|
||||
let mut m = nu_json::Map::new();
|
||||
for (k, v) in val {
|
||||
for (k, v) in &**val {
|
||||
m.insert(k.clone(), value_to_json_value(v)?);
|
||||
}
|
||||
nu_json::Value::Object(m)
|
||||
|
@ -252,7 +252,7 @@ pub fn value_to_string(
|
||||
)),
|
||||
Value::Record { val, .. } => {
|
||||
let mut collection = vec![];
|
||||
for (col, val) in val {
|
||||
for (col, val) in &**val {
|
||||
collection.push(if needs_quotes(col) {
|
||||
format!(
|
||||
"{idt_po}\"{}\": {}",
|
||||
|
@ -60,7 +60,7 @@ fn helper(engine_state: &EngineState, v: &Value) -> Result<toml::Value, ShellErr
|
||||
Value::String { val, .. } | Value::Glob { val, .. } => toml::Value::String(val.clone()),
|
||||
Value::Record { val, .. } => {
|
||||
let mut m = toml::map::Map::new();
|
||||
for (k, v) in val {
|
||||
for (k, v) in &**val {
|
||||
m.insert(k.clone(), helper(engine_state, v)?);
|
||||
}
|
||||
toml::Value::Table(m)
|
||||
|
@ -331,7 +331,7 @@ impl Job {
|
||||
// content: null}, {tag: a}. See to_xml_entry for more
|
||||
let attrs = match attrs {
|
||||
Value::Record { val, .. } => val,
|
||||
Value::Nothing { .. } => Record::new(),
|
||||
Value::Nothing { .. } => Box::new(Record::new()),
|
||||
_ => {
|
||||
return Err(ShellError::CantConvert {
|
||||
to_type: "XML".into(),
|
||||
@ -355,7 +355,7 @@ impl Job {
|
||||
}
|
||||
};
|
||||
|
||||
self.write_tag(entry_span, tag, tag_span, attrs, content)
|
||||
self.write_tag(entry_span, tag, tag_span, *attrs, content)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,7 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
||||
}
|
||||
Value::Record { val, .. } => {
|
||||
let mut m = serde_yaml::Mapping::new();
|
||||
for (k, v) in val {
|
||||
for (k, v) in &**val {
|
||||
m.insert(
|
||||
serde_yaml::Value::String(k.clone()),
|
||||
value_to_yaml_value(v)?,
|
||||
|
@ -186,7 +186,7 @@ pub fn highlight_search_in_table(
|
||||
)?;
|
||||
|
||||
if has_match {
|
||||
matches.push(Value::record(record, record_span));
|
||||
matches.push(Value::record(*record, record_span));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ fn helper_for_tables(
|
||||
for val in values {
|
||||
match val {
|
||||
Value::Record { val, .. } => {
|
||||
for (key, value) in val {
|
||||
for (key, value) in &**val {
|
||||
column_values
|
||||
.entry(key.clone())
|
||||
.and_modify(|v: &mut Vec<Value>| v.push(value.clone()))
|
||||
@ -90,7 +90,7 @@ pub fn calculate(
|
||||
*val = mf(slice::from_ref(val), span, name)?;
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(Value::record(record, span))
|
||||
Ok(Value::record(*record, span))
|
||||
}
|
||||
PipelineData::Value(Value::Range { val, .. }, ..) => {
|
||||
let new_vals: Result<Vec<Value>, ShellError> = val
|
||||
|
@ -221,7 +221,7 @@ pub fn send_request(
|
||||
Value::Record { val, .. } if body_type == BodyType::Form => {
|
||||
let mut data: Vec<(String, String)> = Vec::with_capacity(val.len());
|
||||
|
||||
for (col, val) in val {
|
||||
for (col, val) in *val {
|
||||
data.push((col, val.coerce_into_string()?))
|
||||
}
|
||||
|
||||
@ -335,7 +335,7 @@ pub fn request_add_custom_headers(
|
||||
|
||||
match &headers {
|
||||
Value::Record { val, .. } => {
|
||||
for (k, v) in val {
|
||||
for (k, v) in &**val {
|
||||
custom_headers.insert(k.to_string(), v.clone());
|
||||
}
|
||||
}
|
||||
@ -345,7 +345,7 @@ pub fn request_add_custom_headers(
|
||||
// single row([key1 key2]; [val1 val2])
|
||||
match &table[0] {
|
||||
Value::Record { val, .. } => {
|
||||
for (k, v) in val {
|
||||
for (k, v) in &**val {
|
||||
custom_headers.insert(k.to_string(), v.clone());
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ fn to_url(input: PipelineData, head: Span) -> Result<PipelineData, ShellError> {
|
||||
match value {
|
||||
Value::Record { ref val, .. } => {
|
||||
let mut row_vec = vec![];
|
||||
for (k, v) in val {
|
||||
for (k, v) in &**val {
|
||||
match v.coerce_string() {
|
||||
Ok(s) => {
|
||||
row_vec.push((k.clone(), s));
|
||||
|
@ -413,7 +413,7 @@ fn handle_table_command(
|
||||
}
|
||||
PipelineData::Value(Value::Record { val, .. }, ..) => {
|
||||
input.data = PipelineData::Empty;
|
||||
handle_record(input, cfg, val)
|
||||
handle_record(input, cfg, *val)
|
||||
}
|
||||
PipelineData::Value(Value::LazyRecord { val, .. }, ..) => {
|
||||
input.data = val.collect()?.into_pipeline_data();
|
||||
|
Reference in New Issue
Block a user