Copy-on-write for record values (#12305)

# Description
This adds a `SharedCow` type as a transparent copy-on-write pointer that
clones to unique on mutate.

As an initial test, the `Record` within `Value::Record` is shared.

There are some pretty big wins for performance. I'll post benchmark
results in a comment. The biggest winner is nested access, as that would
have cloned the records for each cell path follow before and it doesn't
have to anymore.

The reusability of the `SharedCow` type is nice and I think it could be
used to clean up the previous work I did with `Arc` in `EngineState`.
It's meant to be a mostly transparent clone-on-write that just clones on
`.to_mut()` or `.into_owned()` if there are actually multiple
references, but avoids cloning if the reference is unique.

# User-Facing Changes
- `Value::Record` field is a different type (plugin authors)

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting
- [ ] use for `EngineState`
- [ ] use for `Value::List`
This commit is contained in:
Devyn Cairns 2024-04-13 18:42:03 -07:00 committed by GitHub
parent b508d1028c
commit 2ae9ad8676
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
52 changed files with 328 additions and 222 deletions

2
Cargo.lock generated
View File

@ -3081,6 +3081,7 @@ dependencies = [
"miette", "miette",
"nu-engine", "nu-engine",
"nu-protocol", "nu-protocol",
"nu-utils",
"rmp-serde", "rmp-serde",
"semver", "semver",
"serde", "serde",
@ -3210,6 +3211,7 @@ dependencies = [
"lscolors", "lscolors",
"nix", "nix",
"num-format", "num-format",
"serde",
"strip-ansi-escapes", "strip-ansi-escapes",
"sys-locale", "sys-locale",
"unicase", "unicase",

View File

@ -68,9 +68,7 @@ impl Completer for VariableCompletion {
self.var_context.1.clone().into_iter().skip(1).collect(); self.var_context.1.clone().into_iter().skip(1).collect();
if let Some(val) = env_vars.get(&target_var_str) { if let Some(val) = env_vars.get(&target_var_str) {
for suggestion in for suggestion in nested_suggestions(val, &nested_levels, current_span) {
nested_suggestions(val.clone(), nested_levels, current_span)
{
if options.match_algorithm.matches_u8_insensitive( if options.match_algorithm.matches_u8_insensitive(
options.case_sensitive, options.case_sensitive,
suggestion.suggestion.value.as_bytes(), suggestion.suggestion.value.as_bytes(),
@ -117,8 +115,7 @@ impl Completer for VariableCompletion {
nu_protocol::NU_VARIABLE_ID, nu_protocol::NU_VARIABLE_ID,
nu_protocol::Span::new(current_span.start, current_span.end), nu_protocol::Span::new(current_span.start, current_span.end),
) { ) {
for suggestion in for suggestion in nested_suggestions(&nuval, &self.var_context.1, current_span)
nested_suggestions(nuval, self.var_context.1.clone(), current_span)
{ {
if options.match_algorithm.matches_u8_insensitive( if options.match_algorithm.matches_u8_insensitive(
options.case_sensitive, options.case_sensitive,
@ -140,8 +137,7 @@ impl Completer for VariableCompletion {
// If the value exists and it's of type Record // If the value exists and it's of type Record
if let Ok(value) = var { if let Ok(value) = var {
for suggestion in for suggestion in nested_suggestions(&value, &self.var_context.1, current_span)
nested_suggestions(value, self.var_context.1.clone(), current_span)
{ {
if options.match_algorithm.matches_u8_insensitive( if options.match_algorithm.matches_u8_insensitive(
options.case_sensitive, options.case_sensitive,
@ -244,21 +240,21 @@ impl Completer for VariableCompletion {
// Find recursively the values for sublevels // Find recursively the values for sublevels
// if no sublevels are set it returns the current value // if no sublevels are set it returns the current value
fn nested_suggestions( fn nested_suggestions(
val: Value, val: &Value,
sublevels: Vec<Vec<u8>>, sublevels: &[Vec<u8>],
current_span: reedline::Span, current_span: reedline::Span,
) -> Vec<SemanticSuggestion> { ) -> Vec<SemanticSuggestion> {
let mut output: Vec<SemanticSuggestion> = vec![]; let mut output: Vec<SemanticSuggestion> = vec![];
let value = recursive_value(val, sublevels); let value = recursive_value(val, sublevels).unwrap_or_else(Value::nothing);
let kind = SuggestionKind::Type(value.get_type()); let kind = SuggestionKind::Type(value.get_type());
match value { match value {
Value::Record { val, .. } => { Value::Record { val, .. } => {
// Add all the columns as completion // Add all the columns as completion
for (col, _) in val.into_iter() { for col in val.columns() {
output.push(SemanticSuggestion { output.push(SemanticSuggestion {
suggestion: Suggestion { suggestion: Suggestion {
value: col, value: col.clone(),
description: None, description: None,
style: None, style: None,
extra: None, extra: None,
@ -311,56 +307,47 @@ fn nested_suggestions(
} }
// Extracts the recursive value (e.g: $var.a.b.c) // Extracts the recursive value (e.g: $var.a.b.c)
fn recursive_value(val: Value, sublevels: Vec<Vec<u8>>) -> Value { fn recursive_value(val: &Value, sublevels: &[Vec<u8>]) -> Result<Value, Span> {
// Go to next sublevel // Go to next sublevel
if let Some(next_sublevel) = sublevels.clone().into_iter().next() { if let Some((sublevel, next_sublevels)) = sublevels.split_first() {
let span = val.span(); let span = val.span();
match val { match val {
Value::Record { val, .. } => { Value::Record { val, .. } => {
for item in *val { if let Some((_, value)) = val.iter().find(|(key, _)| key.as_bytes() == sublevel) {
// Check if index matches with sublevel
if item.0.as_bytes().to_vec() == next_sublevel {
// If matches try to fetch recursively the next // If matches try to fetch recursively the next
return recursive_value(item.1, sublevels.into_iter().skip(1).collect()); recursive_value(value, next_sublevels)
} } else {
}
// Current sublevel value not found // Current sublevel value not found
return Value::nothing(span); Err(span)
}
} }
Value::LazyRecord { val, .. } => { Value::LazyRecord { val, .. } => {
for col in val.column_names() { for col in val.column_names() {
if col.as_bytes().to_vec() == next_sublevel { if col.as_bytes() == *sublevel {
return recursive_value( let val = val.get_column_value(col).map_err(|_| span)?;
val.get_column_value(col).unwrap_or_default(), return recursive_value(&val, next_sublevels);
sublevels.into_iter().skip(1).collect(),
);
} }
} }
// Current sublevel value not found // Current sublevel value not found
return Value::nothing(span); Err(span)
} }
Value::List { vals, .. } => { Value::List { vals, .. } => {
for col in get_columns(vals.as_slice()) { for col in get_columns(vals.as_slice()) {
if col.as_bytes().to_vec() == next_sublevel { if col.as_bytes() == *sublevel {
return recursive_value( let val = val.get_data_by_key(&col).ok_or(span)?;
Value::list(vals, span) return recursive_value(&val, next_sublevels);
.get_data_by_key(&col)
.unwrap_or_default(),
sublevels.into_iter().skip(1).collect(),
);
} }
} }
// Current sublevel value not found // Current sublevel value not found
return Value::nothing(span); Err(span)
} }
_ => return val, _ => Ok(val.clone()),
} }
} else {
Ok(val.clone())
} }
val
} }
impl MatchAlgorithm { impl MatchAlgorithm {

View File

@ -176,9 +176,11 @@ impl NuDataFrame {
conversion::insert_record(&mut column_values, record, &maybe_schema)? conversion::insert_record(&mut column_values, record, &maybe_schema)?
} }
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => conversion::insert_record(
conversion::insert_record(&mut column_values, *record, &maybe_schema)? &mut column_values,
} record.into_owned(),
&maybe_schema,
)?,
_ => { _ => {
let key = "0".to_string(); let key = "0".to_string();
conversion::insert_value(value, key, &mut column_values, &maybe_schema)? conversion::insert_value(value, key, &mut column_values, &maybe_schema)?

View File

@ -58,7 +58,7 @@ fn horizontal_rotate_value(
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
let rotations = by.map(|n| n % record.len()).unwrap_or(1); let rotations = by.map(|n| n % record.len()).unwrap_or(1);
let (mut cols, mut vals): (Vec<_>, Vec<_>) = record.into_iter().unzip(); let (mut cols, mut vals): (Vec<_>, Vec<_>) = record.into_owned().into_iter().unzip();
if !cells_only { if !cells_only {
match direction { match direction {
HorizontalDirection::Right => cols.rotate_right(rotations), HorizontalDirection::Right => cols.rotate_right(rotations),

View File

@ -171,7 +171,7 @@ pub fn rotate(
let span = val.span(); let span = val.span();
match val { match val {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
let (cols, vals): (Vec<_>, Vec<_>) = record.into_iter().unzip(); let (cols, vals): (Vec<_>, Vec<_>) = record.into_owned().into_iter().unzip();
old_column_names = cols; old_column_names = cols;
new_values.extend_from_slice(&vals); new_values.extend_from_slice(&vals);
} }

View File

@ -154,7 +154,8 @@ impl Iterator for UpdateCellIterator {
let span = val.span(); let span = val.span();
match val { match val {
Value::Record { val, .. } => Some(Value::record( Value::Record { val, .. } => Some(Value::record(
val.into_iter() val.into_owned()
.into_iter()
.map(|(col, val)| match &self.columns { .map(|(col, val)| match &self.columns {
Some(cols) if !cols.contains(&col) => (col, val), Some(cols) if !cols.contains(&col) => (col, val),
_ => ( _ => (

View File

@ -267,7 +267,7 @@ fn compact_primitive_description(mut value: Value) -> Value {
if val.len() != 1 { if val.len() != 1 {
return value; return value;
} }
if let Some(type_name) = val.get_mut("type") { if let Some(type_name) = val.to_mut().get_mut("type") {
return std::mem::take(type_name); return std::mem::take(type_name);
} }
} }
@ -303,7 +303,8 @@ fn describe_value(
), ),
head, head,
), ),
Value::Record { mut val, .. } => { Value::Record { val, .. } => {
let mut val = val.into_owned();
for (_k, v) in val.iter_mut() { for (_k, v) in val.iter_mut() {
*v = compact_primitive_description(describe_value( *v = compact_primitive_description(describe_value(
std::mem::take(v), std::mem::take(v),
@ -317,7 +318,7 @@ fn describe_value(
record!( record!(
"type" => Value::string("record", head), "type" => Value::string("record", head),
"lazy" => Value::bool(false, head), "lazy" => Value::bool(false, head),
"columns" => Value::record(*val, head), "columns" => Value::record(val, head),
), ),
head, head,
) )
@ -395,10 +396,11 @@ fn describe_value(
if options.collect_lazyrecords { if options.collect_lazyrecords {
let collected = val.collect()?; let collected = val.collect()?;
if let Value::Record { mut val, .. } = if let Value::Record { val, .. } =
describe_value(collected, head, engine_state, options)? describe_value(collected, head, engine_state, options)?
{ {
record.push("length", Value::int(val.len() as i64, head)); let mut val = Record::clone(&val);
for (_k, v) in val.iter_mut() { for (_k, v) in val.iter_mut() {
*v = compact_primitive_description(describe_value( *v = compact_primitive_description(describe_value(
std::mem::take(v), std::mem::take(v),
@ -408,7 +410,8 @@ fn describe_value(
)?); )?);
} }
record.push("columns", Value::record(*val, head)); record.push("length", Value::int(val.len() as i64, head));
record.push("columns", Value::record(val, head));
} else { } else {
let cols = val.column_names(); let cols = val.column_names();
record.push("length", Value::int(cols.len() as i64, head)); record.push("length", Value::int(cols.len() as i64, head));

View File

@ -177,9 +177,9 @@ fn run_histogram(
match v { match v {
// parse record, and fill valid value to actual input. // parse record, and fill valid value to actual input.
Value::Record { val, .. } => { Value::Record { val, .. } => {
for (c, v) in *val { for (c, v) in val.iter() {
if &c == col_name { if c == col_name {
if let Ok(v) = HashableValue::from_value(v, head_span) { if let Ok(v) = HashableValue::from_value(v.clone(), head_span) {
inputs.push(v); inputs.push(v);
} }
} }

View File

@ -131,7 +131,7 @@ fn into_record(
.collect(), .collect(),
span, span,
), ),
Value::Record { val, .. } => Value::record(*val, span), Value::Record { .. } => input,
Value::Error { .. } => input, Value::Error { .. } => input,
other => Value::error( other => Value::error(
ShellError::TypeMismatch { ShellError::TypeMismatch {

View File

@ -108,7 +108,8 @@ impl Iterator for UpdateCellIterator {
let span = val.span(); let span = val.span();
match val { match val {
Value::Record { val, .. } => Some(Value::record( Value::Record { val, .. } => Some(Value::record(
val.into_iter() val.into_owned()
.into_iter()
.map(|(col, val)| match &self.columns { .map(|(col, val)| match &self.columns {
Some(cols) if !cols.contains(&col) => (col, val), Some(cols) if !cols.contains(&col) => (col, val),
_ => ( _ => (

View File

@ -316,7 +316,7 @@ fn insert_value(
match stream_value { match stream_value {
// map each column value into its SQL representation // map each column value into its SQL representation
Value::Record { val, .. } => { Value::Record { val, .. } => {
let sql_vals = values_to_sql(val.into_values())?; let sql_vals = values_to_sql(val.values().cloned())?;
insert_statement insert_statement
.execute(rusqlite::params_from_iter(sql_vals)) .execute(rusqlite::params_from_iter(sql_vals))

View File

@ -500,7 +500,7 @@ pub fn nu_value_to_params(value: Value) -> Result<NuSqlParams, ShellError> {
Value::Record { val, .. } => { Value::Record { val, .. } => {
let mut params = Vec::with_capacity(val.len()); let mut params = Vec::with_capacity(val.len());
for (mut column, value) in val.into_iter() { for (mut column, value) in val.into_owned().into_iter() {
let sql_type_erased = value_to_sql(value)?; let sql_type_erased = value_to_sql(value)?;
if !column.starts_with([':', '@', '$']) { if !column.starts_with([':', '@', '$']) {

View File

@ -199,7 +199,7 @@ mod util {
let span = value.span(); let span = value.span();
match value { match value {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
let (cols, vals): (Vec<_>, Vec<_>) = record.into_iter().unzip(); let (cols, vals): (Vec<_>, Vec<_>) = record.into_owned().into_iter().unzip();
( (
cols, cols,
vec![vals vec![vals

View File

@ -53,7 +53,7 @@ impl Command for LoadEnv {
} }
None => match input { None => match input {
PipelineData::Value(Value::Record { val, .. }, ..) => { PipelineData::Value(Value::Record { val, .. }, ..) => {
for (env_var, rhs) in *val { for (env_var, rhs) in val.into_owned() {
let env_var_ = env_var.as_str(); let env_var_ = env_var.as_str();
if ["FILE_PWD", "CURRENT_FILE"].contains(&env_var_) { if ["FILE_PWD", "CURRENT_FILE"].contains(&env_var_) {
return Err(ShellError::AutomaticEnvVarSetManually { return Err(ShellError::AutomaticEnvVarSetManually {

View File

@ -118,6 +118,7 @@ fn getcol(
}) })
} }
Value::Record { val, .. } => Ok(val Value::Record { val, .. } => Ok(val
.into_owned()
.into_iter() .into_iter()
.map(move |(x, _)| Value::string(x, head)) .map(move |(x, _)| Value::string(x, head))
.into_pipeline_data(ctrlc) .into_pipeline_data(ctrlc)

View File

@ -85,15 +85,14 @@ fn default(
if let Some(column) = column { if let Some(column) = column {
input input
.map( .map(
move |item| { move |mut item| match item {
let span = item.span();
match item {
Value::Record { Value::Record {
val: mut record, .. val: ref mut record,
..
} => { } => {
let mut found = false; let mut found = false;
for (col, val) in record.iter_mut() { for (col, val) in record.to_mut().iter_mut() {
if *col == column.item { if *col == column.item {
found = true; found = true;
if matches!(val, Value::Nothing { .. }) { if matches!(val, Value::Nothing { .. }) {
@ -103,13 +102,12 @@ fn default(
} }
if !found { if !found {
record.push(column.item.clone(), value.clone()); record.to_mut().push(column.item.clone(), value.clone());
} }
Value::record(*record, span) item
} }
_ => item, _ => item,
}
}, },
ctrlc, ctrlc,
) )

View File

@ -106,7 +106,7 @@ fn drop_cols(
Ok(PipelineData::Empty) Ok(PipelineData::Empty)
} }
} }
PipelineData::Value(v, ..) => { PipelineData::Value(mut v, ..) => {
let span = v.span(); let span = v.span();
match v { match v {
Value::List { mut vals, .. } => { Value::List { mut vals, .. } => {
@ -119,11 +119,12 @@ fn drop_cols(
Ok(Value::list(vals, span).into_pipeline_data_with_metadata(metadata)) Ok(Value::list(vals, span).into_pipeline_data_with_metadata(metadata))
} }
Value::Record { Value::Record {
val: mut record, .. val: ref mut record,
..
} => { } => {
let len = record.len().saturating_sub(columns); let len = record.len().saturating_sub(columns);
record.truncate(len); record.to_mut().truncate(len);
Ok(Value::record(*record, span).into_pipeline_data_with_metadata(metadata)) Ok(v.into_pipeline_data_with_metadata(metadata))
} }
// Propagate errors // Propagate errors
Value::Error { error, .. } => Err(*error), Value::Error { error, .. } => Err(*error),
@ -143,7 +144,7 @@ fn drop_cols(
fn drop_cols_set(val: &mut Value, head: Span, drop: usize) -> Result<HashSet<String>, ShellError> { fn drop_cols_set(val: &mut Value, head: Span, drop: usize) -> Result<HashSet<String>, ShellError> {
if let Value::Record { val: record, .. } = val { if let Value::Record { val: record, .. } = val {
let len = record.len().saturating_sub(drop); let len = record.len().saturating_sub(drop);
Ok(record.drain(len..).map(|(col, _)| col).collect()) Ok(record.to_mut().drain(len..).map(|(col, _)| col).collect())
} else { } else {
Err(unsupported_value_error(val, head)) Err(unsupported_value_error(val, head))
} }
@ -155,7 +156,7 @@ fn drop_record_cols(
drop_cols: &HashSet<String>, drop_cols: &HashSet<String>,
) -> Result<(), ShellError> { ) -> Result<(), ShellError> {
if let Value::Record { val, .. } = val { if let Value::Record { val, .. } = val {
val.retain(|col, _| !drop_cols.contains(col)); val.to_mut().retain(|col, _| !drop_cols.contains(col));
Ok(()) Ok(())
} else { } else {
Err(unsupported_value_error(val, head)) Err(unsupported_value_error(val, head))

View File

@ -156,15 +156,15 @@ fn flat_value(columns: &[CellPath], item: Value, all: bool) -> Vec<Value> {
let mut out = IndexMap::<String, Value>::new(); let mut out = IndexMap::<String, Value>::new();
let mut inner_table = None; let mut inner_table = None;
for (column_index, (column, value)) in val.into_iter().enumerate() { for (column_index, (column, value)) in val.into_owned().into_iter().enumerate() {
let column_requested = columns.iter().find(|c| c.to_string() == column); let column_requested = columns.iter().find(|c| c.to_string() == column);
let need_flatten = { columns.is_empty() || column_requested.is_some() }; let need_flatten = { columns.is_empty() || column_requested.is_some() };
let span = value.span(); let span = value.span();
match value { match value {
Value::Record { val, .. } => { Value::Record { ref val, .. } => {
if need_flatten { if need_flatten {
for (col, val) in *val { for (col, val) in val.clone().into_owned() {
if out.contains_key(&col) { if out.contains_key(&col) {
out.insert(format!("{column}_{col}"), val); out.insert(format!("{column}_{col}"), val);
} else { } else {
@ -172,9 +172,9 @@ fn flat_value(columns: &[CellPath], item: Value, all: bool) -> Vec<Value> {
} }
} }
} else if out.contains_key(&column) { } else if out.contains_key(&column) {
out.insert(format!("{column}_{column}"), Value::record(*val, span)); out.insert(format!("{column}_{column}"), value);
} else { } else {
out.insert(column, Value::record(*val, span)); out.insert(column, value);
} }
} }
Value::List { vals, .. } => { Value::List { vals, .. } => {

View File

@ -149,6 +149,7 @@ fn replace_headers(
if let Value::Record { val: record, .. } = value { if let Value::Record { val: record, .. } = value {
Ok(Value::record( Ok(Value::record(
record record
.into_owned()
.into_iter() .into_iter()
.filter_map(|(col, val)| { .filter_map(|(col, val)| {
old_headers old_headers

View File

@ -86,6 +86,7 @@ impl Command for Items {
PipelineData::Empty => Ok(PipelineData::Empty), PipelineData::Empty => Ok(PipelineData::Empty),
PipelineData::Value(v, ..) => match v { PipelineData::Value(v, ..) => match v {
Value::Record { val, .. } => Ok(val Value::Record { val, .. } => Ok(val
.into_owned()
.into_iter() .into_iter()
.map_while(run_for_each_item) .map_while(run_for_each_item)
.into_pipeline_data(ctrlc)), .into_pipeline_data(ctrlc)),
@ -99,6 +100,7 @@ impl Command for Items {
})?, })?,
}; };
Ok(record Ok(record
.into_owned()
.into_iter() .into_iter()
.map_while(run_for_each_item) .map_while(run_for_each_item)
.into_pipeline_data(ctrlc)) .into_pipeline_data(ctrlc))

View File

@ -162,7 +162,7 @@ fn rename(
block_info.clone() block_info.clone()
{ {
record record
.into_iter() .into_owned().into_iter()
.map(|(col, val)| { .map(|(col, val)| {
stack.with_env(&env_vars, &env_hidden); stack.with_env(&env_vars, &env_hidden);
@ -191,7 +191,7 @@ fn rename(
// record columns are unique so we can track the number // record columns are unique so we can track the number
// of renamed columns to check if any were missed // of renamed columns to check if any were missed
let mut renamed = 0; let mut renamed = 0;
let record = record.into_iter().map(|(col, val)| { let record = record.into_owned().into_iter().map(|(col, val)| {
let col = if let Some(col) = columns.get(&col) { let col = if let Some(col) = columns.get(&col) {
renamed += 1; renamed += 1;
col.clone() col.clone()
@ -222,7 +222,7 @@ fn rename(
} }
} }
None => Ok(record None => Ok(record
.into_iter() .into_owned().into_iter()
.enumerate() .enumerate()
.map(|(i, (col, val))| { .map(|(i, (col, val))| {
(columns.get(i).cloned().unwrap_or(col), val) (columns.get(i).cloned().unwrap_or(col), val)
@ -237,7 +237,7 @@ fn rename(
} }
} }
// Propagate errors by explicitly matching them before the final case. // Propagate errors by explicitly matching them before the final case.
Value::Error { .. } => item.clone(), Value::Error { .. } => item,
other => Value::error( other => Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record".into(), exp_input_type: "record".into(),

View File

@ -144,7 +144,14 @@ impl Command for Sort {
// Records have two sorting methods, toggled by presence or absence of -v // Records have two sorting methods, toggled by presence or absence of -v
PipelineData::Value(Value::Record { val, .. }, ..) => { PipelineData::Value(Value::Record { val, .. }, ..) => {
let sort_by_value = call.has_flag(engine_state, stack, "values")?; 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.into_owned(),
span,
sort_by_value,
reverse,
insensitive,
natural,
);
Ok(record.into_pipeline_data()) Ok(record.into_pipeline_data())
} }
// Other values are sorted here // Other values are sorted here

View File

@ -187,11 +187,11 @@ pub fn data_split(
let span = v.span(); let span = v.span();
match v { match v {
Value::Record { val: grouped, .. } => { Value::Record { val: grouped, .. } => {
for (outer_key, list) in grouped.into_iter() { for (outer_key, list) in grouped.into_owned() {
match data_group(&list, splitter, span) { match data_group(&list, splitter, span) {
Ok(grouped_vals) => { Ok(grouped_vals) => {
if let Value::Record { val: sub, .. } = grouped_vals { if let Value::Record { val: sub, .. } = grouped_vals {
for (inner_key, subset) in sub.into_iter() { for (inner_key, subset) in sub.into_owned() {
let s: &mut IndexMap<String, Value> = let s: &mut IndexMap<String, Value> =
splits.entry(inner_key).or_default(); splits.entry(inner_key).or_default();

View File

@ -195,6 +195,7 @@ fn sort_attributes(val: Value) -> Value {
Value::Record { val, .. } => { Value::Record { val, .. } => {
// TODO: sort inplace // TODO: sort inplace
let sorted = val let sorted = val
.into_owned()
.into_iter() .into_iter()
.sorted_by(|a, b| a.0.cmp(&b.0)) .sorted_by(|a, b| a.0.cmp(&b.0))
.collect_vec(); .collect_vec();

View File

@ -157,7 +157,9 @@ fn values(
} }
} }
Value::Record { val, .. } => Ok(val Value::Record { val, .. } => Ok(val
.into_values() .values()
.cloned()
.collect::<Vec<_>>()
.into_pipeline_data_with_metadata(metadata, ctrlc)), .into_pipeline_data_with_metadata(metadata, ctrlc)),
Value::LazyRecord { val, .. } => { Value::LazyRecord { val, .. } => {
let record = match val.collect()? { let record = match val.collect()? {
@ -169,6 +171,7 @@ fn values(
})?, })?,
}; };
Ok(record Ok(record
.into_owned()
.into_values() .into_values()
.into_pipeline_data_with_metadata(metadata, ctrlc)) .into_pipeline_data_with_metadata(metadata, ctrlc))
} }

View File

@ -125,6 +125,7 @@ fn local_into_string(value: Value, separator: &str, config: &Config) -> String {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(separator), .join(separator),
Value::Record { val, .. } => val Value::Record { val, .. } => val
.into_owned()
.into_iter() .into_iter()
.map(|(x, y)| format!("{}: {}", x, local_into_string(y, ", ", config))) .map(|(x, y)| format!("{}: {}", x, local_into_string(y, ", ", config)))
.collect::<Vec<_>>() .collect::<Vec<_>>()

View File

@ -325,8 +325,8 @@ impl Job {
// alternatives like {tag: a attributes: {} content: []}, {tag: a attribbutes: null // alternatives like {tag: a attributes: {} content: []}, {tag: a attribbutes: null
// content: null}, {tag: a}. See to_xml_entry for more // content: null}, {tag: a}. See to_xml_entry for more
let attrs = match attrs { let attrs = match attrs {
Value::Record { val, .. } => val, Value::Record { val, .. } => val.into_owned(),
Value::Nothing { .. } => Box::new(Record::new()), Value::Nothing { .. } => Record::new(),
_ => { _ => {
return Err(ShellError::CantConvert { return Err(ShellError::CantConvert {
to_type: "XML".into(), to_type: "XML".into(),
@ -350,7 +350,7 @@ impl Job {
} }
}; };
self.write_tag(entry_span, tag, tag_span, *attrs, content) self.write_tag(entry_span, tag, tag_span, attrs, content)
} }
} }

View File

@ -136,7 +136,7 @@ used as the next argument to the closure, otherwise generation stops.
match value { match value {
// {out: ..., next: ...} -> output and continue // {out: ..., next: ...} -> output and continue
Value::Record { val, .. } => { Value::Record { val, .. } => {
let iter = val.into_iter(); let iter = val.into_owned().into_iter();
let mut out = None; let mut out = None;
let mut next = None; let mut next = None;
let mut err = None; let mut err = None;

View File

@ -139,19 +139,20 @@ pub fn highlight_search_in_table(
let search_string = search_string.to_folded_case(); let search_string = search_string.to_folded_case();
let mut matches = vec![]; let mut matches = vec![];
for record in table { for mut value in table {
let span = record.span(); let Value::Record {
let (mut record, record_span) = if let Value::Record { val, .. } = record { val: ref mut record,
(val, span) ..
} else { } = value
else {
return Err(ShellError::NushellFailedSpanned { return Err(ShellError::NushellFailedSpanned {
msg: "Expected record".to_string(), msg: "Expected record".to_string(),
label: format!("got {}", record.get_type()), label: format!("got {}", value.get_type()),
span: record.span(), span: value.span(),
}); });
}; };
let has_match = record.iter_mut().try_fold( let has_match = record.to_mut().iter_mut().try_fold(
false, false,
|acc: bool, (col, val)| -> Result<bool, ShellError> { |acc: bool, (col, val)| -> Result<bool, ShellError> {
if !searched_cols.contains(&col.as_str()) { if !searched_cols.contains(&col.as_str()) {
@ -180,7 +181,7 @@ pub fn highlight_search_in_table(
)?; )?;
if has_match { if has_match {
matches.push(Value::record(*record, record_span)); matches.push(value);
} }
} }

View File

@ -80,15 +80,15 @@ pub fn calculate(
), ),
_ => mf(vals, span, name), _ => mf(vals, span, name),
}, },
PipelineData::Value(Value::Record { val: record, .. }, ..) => { PipelineData::Value(Value::Record { val, .. }, ..) => {
let mut record = record; let mut record = val.into_owned();
record record
.iter_mut() .iter_mut()
.try_for_each(|(_, val)| -> Result<(), ShellError> { .try_for_each(|(_, val)| -> Result<(), ShellError> {
*val = mf(slice::from_ref(val), span, name)?; *val = mf(slice::from_ref(val), span, name)?;
Ok(()) Ok(())
})?; })?;
Ok(Value::record(*record, span)) Ok(Value::record(record, span))
} }
PipelineData::Value(Value::Range { val, .. }, ..) => { PipelineData::Value(Value::Range { val, .. }, ..) => {
let new_vals: Result<Vec<Value>, ShellError> = val let new_vals: Result<Vec<Value>, ShellError> = val

View File

@ -222,7 +222,7 @@ pub fn send_request(
Value::Record { val, .. } if body_type == BodyType::Form => { Value::Record { val, .. } if body_type == BodyType::Form => {
let mut data: Vec<(String, String)> = Vec::with_capacity(val.len()); let mut data: Vec<(String, String)> = Vec::with_capacity(val.len());
for (col, val) in *val { for (col, val) in val.into_owned() {
data.push((col, val.coerce_into_string()?)) data.push((col, val.coerce_into_string()?))
} }

View File

@ -92,6 +92,7 @@ impl Command for SubCommand {
match value { match value {
Value::Record { val, .. } => { Value::Record { val, .. } => {
let url_components = val let url_components = val
.into_owned()
.into_iter() .into_iter()
.try_fold(UrlComponents::new(), |url, (k, v)| { .try_fold(UrlComponents::new(), |url, (k, v)| {
url.add_component(k, v, span, engine_state) url.add_component(k, v, span, engine_state)
@ -179,6 +180,7 @@ impl UrlComponents {
return match value { return match value {
Value::Record { val, .. } => { Value::Record { val, .. } => {
let mut qs = val let mut qs = val
.into_owned()
.into_iter() .into_iter()
.map(|(k, v)| match v.coerce_into_string() { .map(|(k, v)| match v.coerce_into_string() {
Ok(val) => Ok(format!("{k}={val}")), Ok(val) => Ok(format!("{k}={val}")),

View File

@ -108,7 +108,7 @@ prints out the list properly."#
// dbg!("value::record"); // dbg!("value::record");
let mut items = vec![]; let mut items = vec![];
for (i, (c, v)) in val.into_iter().enumerate() { for (i, (c, v)) in val.into_owned().into_iter().enumerate() {
items.push((i, c, v.to_expanded_string(", ", config))) items.push((i, c, v.to_expanded_string(", ", config)))
} }

View File

@ -392,7 +392,7 @@ fn handle_table_command(
} }
PipelineData::Value(Value::Record { val, .. }, ..) => { PipelineData::Value(Value::Record { val, .. }, ..) => {
input.data = PipelineData::Empty; input.data = PipelineData::Empty;
handle_record(input, cfg, *val) handle_record(input, cfg, val.into_owned())
} }
PipelineData::Value(Value::LazyRecord { val, .. }, ..) => { PipelineData::Value(Value::LazyRecord { val, .. }, ..) => {
input.data = val.collect()?.into_pipeline_data(); input.data = val.collect()?.into_pipeline_data();
@ -557,7 +557,7 @@ fn handle_row_stream(
stream.map(move |mut x| match &mut x { stream.map(move |mut x| match &mut x {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
// Only the name column gets special colors, for now // Only the name column gets special colors, for now
if let Some(value) = record.get_mut("name") { if let Some(value) = record.to_mut().get_mut("name") {
let span = value.span(); let span = value.span();
if let Value::String { val, .. } = value { if let Value::String { val, .. } = value {
if let Some(val) = render_path_name(val, &config, &ls_colors, span) if let Some(val) = render_path_name(val, &config, &ls_colors, span)
@ -583,7 +583,7 @@ fn handle_row_stream(
ListStream::from_stream( ListStream::from_stream(
stream.map(move |mut x| match &mut x { stream.map(move |mut x| match &mut x {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
for (rec_col, rec_val) in record.iter_mut() { for (rec_col, rec_val) in record.to_mut().iter_mut() {
// Every column in the HTML theme table except 'name' is colored // Every column in the HTML theme table except 'name' is colored
if rec_col != "name" { if rec_col != "name" {
continue; continue;

View File

@ -16,7 +16,7 @@ pub fn try_build_table(
let span = value.span(); let span = value.span();
match value { match value {
Value::List { vals, .. } => try_build_list(vals, ctrlc, config, span, style_computer), Value::List { vals, .. } => try_build_list(vals, ctrlc, config, span, style_computer),
Value::Record { val, .. } => try_build_map(*val, span, style_computer, ctrlc, config), Value::Record { val, .. } => try_build_map(&val, span, style_computer, ctrlc, config),
val if matches!(val, Value::String { .. }) => { val if matches!(val, Value::String { .. }) => {
nu_value_to_string_clean(&val, config, style_computer).0 nu_value_to_string_clean(&val, config, style_computer).0
} }
@ -25,7 +25,7 @@ pub fn try_build_table(
} }
fn try_build_map( fn try_build_map(
record: Record, record: &Record,
span: Span, span: Span,
style_computer: &StyleComputer, style_computer: &StyleComputer,
ctrlc: Option<Arc<AtomicBool>>, ctrlc: Option<Arc<AtomicBool>>,
@ -42,11 +42,11 @@ fn try_build_map(
0, 0,
false, false,
); );
let result = ExpandedTable::new(None, false, String::new()).build_map(&record, opts); let result = ExpandedTable::new(None, false, String::new()).build_map(record, opts);
match result { match result {
Ok(Some(result)) => result, Ok(Some(result)) => result,
Ok(None) | Err(_) => { Ok(None) | Err(_) => {
nu_value_to_string(&Value::record(record, span), config, style_computer).0 nu_value_to_string(&Value::record(record.clone(), span), config, style_computer).0
} }
} }
} }

View File

@ -87,7 +87,7 @@ pub fn collect_input(value: Value) -> (Vec<String>, Vec<Vec<Value>>) {
let span = value.span(); let span = value.span();
match value { match value {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
let (key, val) = record.into_iter().unzip(); let (key, val) = record.into_owned().into_iter().unzip();
(key, vec![val]) (key, vec![val])
} }
Value::List { vals, .. } => { Value::List { vals, .. } => {

View File

@ -1038,7 +1038,7 @@ fn set_config(hm: &mut HashMap<String, Value>, path: &[&str], value: Value) -> b
if path.len() == 2 { if path.len() == 2 {
let key = path[1]; let key = path[1];
record.insert(key, value); record.to_mut().insert(key, value);
} else { } else {
let mut hm2: HashMap<String, Value> = HashMap::new(); let mut hm2: HashMap<String, Value> = HashMap::new();
for (k, v) in record.iter() { for (k, v) in record.iter() {

View File

@ -319,8 +319,8 @@ impl PluginTest {
// reorder cols and vals to make more logically compare. // reorder cols and vals to make more logically compare.
// more general, if two record have same col and values, // more general, if two record have same col and values,
// the order of cols shouldn't affect the equal property. // the order of cols shouldn't affect the equal property.
let mut a_rec = a_rec.clone(); let mut a_rec = a_rec.clone().into_owned();
let mut b_rec = b_rec.clone(); let mut b_rec = b_rec.clone().into_owned();
a_rec.sort_cols(); a_rec.sort_cols();
b_rec.sort_cols(); b_rec.sort_cols();

View File

@ -13,6 +13,7 @@ bench = false
[dependencies] [dependencies]
nu-engine = { path = "../nu-engine", version = "0.92.3" } nu-engine = { path = "../nu-engine", version = "0.92.3" }
nu-protocol = { path = "../nu-protocol", version = "0.92.3" } nu-protocol = { path = "../nu-protocol", version = "0.92.3" }
nu-utils = { path = "../nu-utils", version = "0.92.3" }
bincode = "1.3" bincode = "1.3"
rmp-serde = "1.1" rmp-serde = "1.1"

View File

@ -1,10 +1,13 @@
use std::{cmp::Ordering, sync::Arc}; use std::cmp::Ordering;
use std::sync::Arc;
use crate::{ use crate::{
plugin::{PluginInterface, PluginSource}, plugin::{PluginInterface, PluginSource},
util::with_custom_values_in, util::with_custom_values_in,
}; };
use nu_protocol::{ast::Operator, CustomValue, IntoSpanned, ShellError, Span, Spanned, Value}; use nu_protocol::{ast::Operator, CustomValue, IntoSpanned, ShellError, Span, Spanned, Value};
use nu_utils::SharedCow;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(test)] #[cfg(test)]
@ -28,7 +31,7 @@ mod tests;
#[doc(hidden)] #[doc(hidden)]
pub struct PluginCustomValue { pub struct PluginCustomValue {
#[serde(flatten)] #[serde(flatten)]
shared: SerdeArc<SharedContent>, shared: SharedCow<SharedContent>,
/// Which plugin the custom value came from. This is not defined on the plugin side. The engine /// Which plugin the custom value came from. This is not defined on the plugin side. The engine
/// side is responsible for maintaining it, and it is not sent over the serialization boundary. /// side is responsible for maintaining it, and it is not sent over the serialization boundary.
@ -152,11 +155,11 @@ impl PluginCustomValue {
source: Option<Arc<PluginSource>>, source: Option<Arc<PluginSource>>,
) -> PluginCustomValue { ) -> PluginCustomValue {
PluginCustomValue { PluginCustomValue {
shared: SerdeArc(Arc::new(SharedContent { shared: SharedCow::new(SharedContent {
name, name,
data, data,
notify_on_drop, notify_on_drop,
})), }),
source, source,
} }
} }
@ -379,7 +382,8 @@ impl Drop for PluginCustomValue {
fn drop(&mut self) { fn drop(&mut self) {
// If the custom value specifies notify_on_drop and this is the last copy, we need to let // If the custom value specifies notify_on_drop and this is the last copy, we need to let
// the plugin know about it if we can. // the plugin know about it if we can.
if self.source.is_some() && self.notify_on_drop() && Arc::strong_count(&self.shared) == 1 { if self.source.is_some() && self.notify_on_drop() && SharedCow::ref_count(&self.shared) == 1
{
self.get_plugin(None, "drop") self.get_plugin(None, "drop")
// While notifying drop, we don't need a copy of the source // While notifying drop, we don't need a copy of the source
.and_then(|plugin| { .and_then(|plugin| {
@ -396,40 +400,3 @@ impl Drop for PluginCustomValue {
} }
} }
} }
/// A serializable `Arc`, to avoid having to have the serde `rc` feature enabled.
#[derive(Clone, Debug)]
#[repr(transparent)]
struct SerdeArc<T>(Arc<T>);
impl<T> Serialize for SerdeArc<T>
where
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}
impl<'de, T> Deserialize<'de> for SerdeArc<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
T::deserialize(deserializer).map(Arc::new).map(SerdeArc)
}
}
impl<T> std::ops::Deref for SerdeArc<T> {
type Target = Arc<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

View File

@ -201,13 +201,13 @@ impl Value {
// the `2`. // the `2`.
if let Value::Record { val, .. } = self { if let Value::Record { val, .. } = self {
val.retain_mut(|key, value| { val.to_mut().retain_mut(|key, value| {
let span = value.span(); let span = value.span();
match key { match key {
// Grouped options // Grouped options
"ls" => { "ls" => {
if let Value::Record { val, .. } = value { if let Value::Record { val, .. } = value {
val.retain_mut(|key2, value| { val.to_mut().retain_mut(|key2, value| {
let span = value.span(); let span = value.span();
match key2 { match key2 {
"use_ls_colors" => { "use_ls_colors" => {
@ -236,7 +236,7 @@ impl Value {
} }
"rm" => { "rm" => {
if let Value::Record { val, .. } = value { if let Value::Record { val, .. } = value {
val.retain_mut(|key2, value| { val.to_mut().retain_mut(|key2, value| {
let span = value.span(); let span = value.span();
match key2 { match key2 {
"always_trash" => { "always_trash" => {
@ -263,7 +263,7 @@ impl Value {
"history" => { "history" => {
let history = &mut config.history; let history = &mut config.history;
if let Value::Record { val, .. } = value { if let Value::Record { val, .. } = value {
val.retain_mut(|key2, value| { val.to_mut().retain_mut(|key2, value| {
let span = value.span(); let span = value.span();
match key2 { match key2 {
"isolation" => { "isolation" => {
@ -305,7 +305,7 @@ impl Value {
} }
"completions" => { "completions" => {
if let Value::Record { val, .. } = value { if let Value::Record { val, .. } = value {
val.retain_mut(|key2, value| { val.to_mut().retain_mut(|key2, value| {
let span = value.span(); let span = value.span();
match key2 { match key2 {
"quick" => { "quick" => {
@ -326,7 +326,7 @@ impl Value {
} }
"external" => { "external" => {
if let Value::Record { val, .. } = value { if let Value::Record { val, .. } = value {
val.retain_mut(|key3, value| val.to_mut().retain_mut(|key3, value|
{ {
let span = value.span(); let span = value.span();
match key3 { match key3 {
@ -393,7 +393,7 @@ impl Value {
} }
"cursor_shape" => { "cursor_shape" => {
if let Value::Record { val, .. } = value { if let Value::Record { val, .. } = value {
val.retain_mut(|key2, value| { val.to_mut().retain_mut(|key2, value| {
let span = value.span(); let span = value.span();
let config_point = match key2 { let config_point = match key2 {
"vi_insert" => &mut config.cursor_shape_vi_insert, "vi_insert" => &mut config.cursor_shape_vi_insert,
@ -426,7 +426,7 @@ impl Value {
} }
"table" => { "table" => {
if let Value::Record { val, .. } = value { if let Value::Record { val, .. } = value {
val.retain_mut(|key2, value| { val.to_mut().retain_mut(|key2, value| {
let span = value.span(); let span = value.span();
match key2 { match key2 {
"mode" => { "mode" => {
@ -451,7 +451,7 @@ impl Value {
} }
Value::Record { val, .. } => { Value::Record { val, .. } => {
let mut invalid = false; let mut invalid = false;
val.retain(|key3, value| { val.to_mut().retain(|key3, value| {
match key3 { match key3 {
"left" => { "left" => {
match value.as_int() { match value.as_int() {
@ -546,7 +546,7 @@ impl Value {
} }
"filesize" => { "filesize" => {
if let Value::Record { val, .. } = value { if let Value::Record { val, .. } = value {
val.retain_mut(|key2, value| { val.to_mut().retain_mut(|key2, value| {
let span = value.span(); let span = value.span();
match key2 { match key2 {
"metric" => { "metric" => {
@ -719,7 +719,7 @@ impl Value {
}, },
"datetime_format" => { "datetime_format" => {
if let Value::Record { val, .. } = value { if let Value::Record { val, .. } = value {
val.retain_mut(|key2, value| val.to_mut().retain_mut(|key2, value|
{ {
let span = value.span(); let span = value.span();
match key2 { match key2 {

View File

@ -39,7 +39,7 @@ impl PluginGcConfigs {
self.plugins = HashMap::new(); self.plugins = HashMap::new();
} }
val.retain_mut(|key, value| { val.to_mut().retain_mut(|key, value| {
let span = value.span(); let span = value.span();
match key { match key {
"default" => { "default" => {
@ -88,7 +88,7 @@ fn process_plugins(
// Remove any plugin configs that aren't in the value // Remove any plugin configs that aren't in the value
plugins.retain(|key, _| val.contains(key)); plugins.retain(|key, _| val.contains(key));
val.retain_mut(|key, value| { val.to_mut().retain_mut(|key, value| {
if matches!(value, Value::Record { .. }) { if matches!(value, Value::Record { .. }) {
plugins.entry(key.to_owned()).or_default().process( plugins.entry(key.to_owned()).or_default().process(
&join_path(path, &[key]), &join_path(path, &[key]),
@ -150,7 +150,7 @@ impl PluginGcConfig {
self.stop_after = PluginGcConfig::default().stop_after; self.stop_after = PluginGcConfig::default().stop_after;
} }
val.retain_mut(|key, value| { val.to_mut().retain_mut(|key, value| {
let span = value.span(); let span = value.span();
match key { match key {
"enabled" => process_bool_config(value, errors, &mut self.enabled), "enabled" => process_bool_config(value, errors, &mut self.enabled),

View File

@ -75,7 +75,7 @@ pub trait Eval {
RecordItem::Spread(_, inner) => { RecordItem::Spread(_, inner) => {
match Self::eval::<D>(state, mut_state, inner)? { match Self::eval::<D>(state, mut_state, inner)? {
Value::Record { val: inner_val, .. } => { Value::Record { val: inner_val, .. } => {
for (col_name, val) in *inner_val { for (col_name, val) in inner_val.into_owned() {
if let Some(orig_span) = col_names.get(&col_name) { if let Some(orig_span) = col_names.get(&col_name) {
return Err(ShellError::ColumnDefinedTwice { return Err(ShellError::ColumnDefinedTwice {
col_name, col_name,

View File

@ -538,7 +538,7 @@ impl FromValue for Vec<Value> {
impl FromValue for Record { impl FromValue for Record {
fn from_value(v: Value) -> Result<Self, ShellError> { fn from_value(v: Value) -> Result<Self, ShellError> {
match v { match v {
Value::Record { val, .. } => Ok(*val), Value::Record { val, .. } => Ok(val.into_owned()),
v => Err(ShellError::CantConvert { v => Err(ShellError::CantConvert {
to_type: "Record".into(), to_type: "Record".into(),
from_type: v.get_type().to_string(), from_type: v.get_type().to_string(),

View File

@ -29,7 +29,7 @@ use fancy_regex::Regex;
use nu_utils::{ use nu_utils::{
contains_emoji, contains_emoji,
locale::{get_system_locale_string, LOCALE_OVERRIDE_ENV_VAR}, locale::{get_system_locale_string, LOCALE_OVERRIDE_ENV_VAR},
IgnoreCaseExt, IgnoreCaseExt, SharedCow,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
@ -110,7 +110,7 @@ pub enum Value {
internal_span: Span, internal_span: Span,
}, },
Record { Record {
val: Box<Record>, val: SharedCow<Record>,
// note: spans are being refactored out of Value // note: spans are being refactored out of Value
// please use .span() instead of matching this span value // please use .span() instead of matching this span value
#[serde(rename = "span")] #[serde(rename = "span")]
@ -538,7 +538,7 @@ impl Value {
/// Unwraps the inner [`Record`] value or returns an error if this `Value` is not a record /// Unwraps the inner [`Record`] value or returns an error if this `Value` is not a record
pub fn into_record(self) -> Result<Record, ShellError> { pub fn into_record(self) -> Result<Record, ShellError> {
if let Value::Record { val, .. } = self { if let Value::Record { val, .. } = self {
Ok(*val) Ok(val.into_owned())
} else { } else {
self.cant_convert_to("record") self.cant_convert_to("record")
} }
@ -1159,7 +1159,7 @@ impl Value {
match current { match current {
Value::Record { mut val, .. } => { Value::Record { mut val, .. } => {
// Make reverse iterate to avoid duplicate column leads to first value, actually last value is expected. // Make reverse iterate to avoid duplicate column leads to first value, actually last value is expected.
if let Some(found) = val.iter_mut().rev().find(|x| { if let Some(found) = val.to_mut().iter_mut().rev().find(|x| {
if insensitive { if insensitive {
x.0.eq_ignore_case(column_name) x.0.eq_ignore_case(column_name)
} else { } else {
@ -1220,13 +1220,15 @@ impl Value {
let val_span = val.span(); let val_span = val.span();
match val { match val {
Value::Record { mut val, .. } => { Value::Record { mut val, .. } => {
if let Some(found) = val.iter_mut().rev().find(|x| { if let Some(found) =
val.to_mut().iter_mut().rev().find(|x| {
if insensitive { if insensitive {
x.0.eq_ignore_case(column_name) x.0.eq_ignore_case(column_name)
} else { } else {
x.0 == column_name x.0 == column_name
} }
}) { })
{
Ok(std::mem::take(found.1)) Ok(std::mem::take(found.1))
} else if *optional { } else if *optional {
Ok(Value::nothing(*origin_span)) Ok(Value::nothing(*origin_span))
@ -1332,7 +1334,7 @@ impl Value {
for val in vals.iter_mut() { for val in vals.iter_mut() {
match val { match val {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if let Some(val) = record.get_mut(col_name) { if let Some(val) = record.to_mut().get_mut(col_name) {
val.upsert_data_at_cell_path(path, new_val.clone())?; val.upsert_data_at_cell_path(path, new_val.clone())?;
} else { } else {
let new_col = if path.is_empty() { let new_col = if path.is_empty() {
@ -1344,7 +1346,7 @@ impl Value {
.upsert_data_at_cell_path(path, new_val.clone())?; .upsert_data_at_cell_path(path, new_val.clone())?;
new_col new_col
}; };
record.push(col_name, new_col); record.to_mut().push(col_name, new_col);
} }
} }
Value::Error { error, .. } => return Err(*error.clone()), Value::Error { error, .. } => return Err(*error.clone()),
@ -1359,7 +1361,7 @@ impl Value {
} }
} }
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if let Some(val) = record.get_mut(col_name) { if let Some(val) = record.to_mut().get_mut(col_name) {
val.upsert_data_at_cell_path(path, new_val)?; val.upsert_data_at_cell_path(path, new_val)?;
} else { } else {
let new_col = if path.is_empty() { let new_col = if path.is_empty() {
@ -1369,7 +1371,7 @@ impl Value {
new_col.upsert_data_at_cell_path(path, new_val)?; new_col.upsert_data_at_cell_path(path, new_val)?;
new_col new_col
}; };
record.push(col_name, new_col); record.to_mut().push(col_name, new_col);
} }
} }
Value::LazyRecord { val, .. } => { Value::LazyRecord { val, .. } => {
@ -1456,7 +1458,7 @@ impl Value {
let v_span = val.span(); let v_span = val.span();
match val { match val {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if let Some(val) = record.get_mut(col_name) { if let Some(val) = record.to_mut().get_mut(col_name) {
val.update_data_at_cell_path(path, new_val.clone())?; val.update_data_at_cell_path(path, new_val.clone())?;
} else { } else {
return Err(ShellError::CantFindColumn { return Err(ShellError::CantFindColumn {
@ -1478,7 +1480,7 @@ impl Value {
} }
} }
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if let Some(val) = record.get_mut(col_name) { if let Some(val) = record.to_mut().get_mut(col_name) {
val.update_data_at_cell_path(path, new_val)?; val.update_data_at_cell_path(path, new_val)?;
} else { } else {
return Err(ShellError::CantFindColumn { return Err(ShellError::CantFindColumn {
@ -1548,7 +1550,7 @@ impl Value {
let v_span = val.span(); let v_span = val.span();
match val { match val {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if record.remove(col_name).is_none() && !optional { if record.to_mut().remove(col_name).is_none() && !optional {
return Err(ShellError::CantFindColumn { return Err(ShellError::CantFindColumn {
col_name: col_name.clone(), col_name: col_name.clone(),
span: *span, span: *span,
@ -1568,7 +1570,7 @@ impl Value {
Ok(()) Ok(())
} }
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if record.remove(col_name).is_none() && !optional { if record.to_mut().remove(col_name).is_none() && !optional {
return Err(ShellError::CantFindColumn { return Err(ShellError::CantFindColumn {
col_name: col_name.clone(), col_name: col_name.clone(),
span: *span, span: *span,
@ -1628,7 +1630,7 @@ impl Value {
let v_span = val.span(); let v_span = val.span();
match val { match val {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if let Some(val) = record.get_mut(col_name) { if let Some(val) = record.to_mut().get_mut(col_name) {
val.remove_data_at_cell_path(path)?; val.remove_data_at_cell_path(path)?;
} else if !optional { } else if !optional {
return Err(ShellError::CantFindColumn { return Err(ShellError::CantFindColumn {
@ -1650,7 +1652,7 @@ impl Value {
Ok(()) Ok(())
} }
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if let Some(val) = record.get_mut(col_name) { if let Some(val) = record.to_mut().get_mut(col_name) {
val.remove_data_at_cell_path(path)?; val.remove_data_at_cell_path(path)?;
} else if !optional { } else if !optional {
return Err(ShellError::CantFindColumn { return Err(ShellError::CantFindColumn {
@ -1720,7 +1722,7 @@ impl Value {
let v_span = val.span(); let v_span = val.span();
match val { match val {
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if let Some(val) = record.get_mut(col_name) { if let Some(val) = record.to_mut().get_mut(col_name) {
if path.is_empty() { if path.is_empty() {
return Err(ShellError::ColumnAlreadyExists { return Err(ShellError::ColumnAlreadyExists {
col_name: col_name.clone(), col_name: col_name.clone(),
@ -1747,7 +1749,7 @@ impl Value {
)?; )?;
new_col new_col
}; };
record.push(col_name, new_col); record.to_mut().push(col_name, new_col);
} }
} }
Value::Error { error, .. } => return Err(*error.clone()), Value::Error { error, .. } => return Err(*error.clone()),
@ -1763,7 +1765,7 @@ impl Value {
} }
} }
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => {
if let Some(val) = record.get_mut(col_name) { if let Some(val) = record.to_mut().get_mut(col_name) {
if path.is_empty() { if path.is_empty() {
return Err(ShellError::ColumnAlreadyExists { return Err(ShellError::ColumnAlreadyExists {
col_name: col_name.clone(), col_name: col_name.clone(),
@ -1781,7 +1783,7 @@ impl Value {
new_col.insert_data_at_cell_path(path, new_val, head_span)?; new_col.insert_data_at_cell_path(path, new_val, head_span)?;
new_col new_col
}; };
record.push(col_name, new_col); record.to_mut().push(col_name, new_col);
} }
} }
Value::LazyRecord { val, .. } => { Value::LazyRecord { val, .. } => {
@ -1854,6 +1856,7 @@ impl Value {
// Check for contained values // Check for contained values
match self { match self {
Value::Record { ref mut val, .. } => val Value::Record { ref mut val, .. } => val
.to_mut()
.iter_mut() .iter_mut()
.try_for_each(|(_, rec_value)| rec_value.recurse_mut(f)), .try_for_each(|(_, rec_value)| rec_value.recurse_mut(f)),
Value::List { ref mut vals, .. } => vals Value::List { ref mut vals, .. } => vals
@ -1989,7 +1992,7 @@ impl Value {
pub fn record(val: Record, span: Span) -> Value { pub fn record(val: Record, span: Span) -> Value {
Value::Record { Value::Record {
val: Box::new(val), val: SharedCow::new(val),
internal_span: span, internal_span: span,
} }
} }
@ -2432,8 +2435,8 @@ impl PartialOrd for Value {
// reorder cols and vals to make more logically compare. // reorder cols and vals to make more logically compare.
// more general, if two record have same col and values, // more general, if two record have same col and values,
// the order of cols shouldn't affect the equal property. // the order of cols shouldn't affect the equal property.
let mut lhs = lhs.clone(); let mut lhs = lhs.clone().into_owned();
let mut rhs = rhs.clone(); let mut rhs = rhs.clone().into_owned();
lhs.sort_cols(); lhs.sort_cols();
rhs.sort_cols(); rhs.sort_cols();

View File

@ -151,7 +151,7 @@ impl Record {
/// ///
/// fn remove_foo_recursively(val: &mut Value) { /// fn remove_foo_recursively(val: &mut Value) {
/// if let Value::Record {val, ..} = val { /// if let Value::Record {val, ..} = val {
/// val.retain_mut(keep_non_foo); /// val.to_mut().retain_mut(keep_non_foo);
/// } /// }
/// } /// }
/// ///

View File

@ -4,6 +4,7 @@ use crate::{
}; };
use nu_color_config::StyleComputer; use nu_color_config::StyleComputer;
use nu_protocol::{Config, Record, TableMode, Value}; use nu_protocol::{Config, Record, TableMode, Value};
use nu_utils::SharedCow;
pub struct CollapsedTable; pub struct CollapsedTable;
@ -48,8 +49,9 @@ fn colorize_value(value: &mut Value, config: &Config, style_computer: &StyleComp
// Take ownership of the record and reassign to &mut // Take ownership of the record and reassign to &mut
// We do this to have owned keys through `.into_iter` // We do this to have owned keys through `.into_iter`
let record = std::mem::take(val); let record = std::mem::take(val);
*val = Box::new( *val = SharedCow::new(
record record
.into_owned()
.into_iter() .into_iter()
.map(|(mut header, mut val)| { .map(|(mut header, mut val)| {
colorize_value(&mut val, config, style_computer); colorize_value(&mut val, config, style_computer);

View File

@ -89,7 +89,7 @@ fn build_table(
fn convert_nu_value_to_table_value(value: Value, config: &Config) -> TableValue { fn convert_nu_value_to_table_value(value: Value, config: &Config) -> TableValue {
match value { match value {
Value::Record { val, .. } => build_vertical_map(*val, config), Value::Record { val, .. } => build_vertical_map(val.into_owned(), config),
Value::List { vals, .. } => { Value::List { vals, .. } => {
let rebuild_array_as_map = is_valid_record(&vals) && count_columns_in_record(&vals) > 0; let rebuild_array_as_map = is_valid_record(&vals) && count_columns_in_record(&vals) > 0;
if rebuild_array_as_map { if rebuild_array_as_map {
@ -195,7 +195,8 @@ fn build_map_from_record(vals: Vec<Value>, config: &Config) -> TableValue {
for val in vals { for val in vals {
match val { match val {
Value::Record { val, .. } => { Value::Record { val, .. } => {
for (i, (_key, val)) in val.into_iter().take(count_columns).enumerate() { for (i, (_key, val)) in val.into_owned().into_iter().take(count_columns).enumerate()
{
let cell = convert_nu_value_to_table_value(val, config); let cell = convert_nu_value_to_table_value(val, config);
list[i].push(cell); list[i].push(cell);
} }

View File

@ -21,6 +21,7 @@ lscolors = { workspace = true, default-features = false, features = ["nu-ansi-te
log = { workspace = true } log = { workspace = true }
num-format = { workspace = true } num-format = { workspace = true }
strip-ansi-escapes = { workspace = true } strip-ansi-escapes = { workspace = true }
serde = { workspace = true }
sys-locale = "0.3" sys-locale = "0.3"
unicase = "2.7.0" unicase = "2.7.0"

View File

@ -4,6 +4,7 @@ mod deansi;
pub mod emoji; pub mod emoji;
pub mod filesystem; pub mod filesystem;
pub mod locale; pub mod locale;
mod shared_cow;
pub mod utils; pub mod utils;
pub use locale::get_system_locale; pub use locale::get_system_locale;
@ -17,6 +18,7 @@ pub use deansi::{
strip_ansi_likely, strip_ansi_string_likely, strip_ansi_string_unlikely, strip_ansi_unlikely, strip_ansi_likely, strip_ansi_string_likely, strip_ansi_string_unlikely, strip_ansi_unlikely,
}; };
pub use emoji::contains_emoji; pub use emoji::contains_emoji;
pub use shared_cow::SharedCow;
#[cfg(unix)] #[cfg(unix)]
pub use filesystem::users; pub use filesystem::users;

View File

@ -0,0 +1,113 @@
use serde::{Deserialize, Serialize};
use std::{fmt, ops, sync::Arc};
/// A container that transparently shares a value when possible, but clones on mutate.
///
/// Unlike `Arc`, this is only intended to help save memory usage and reduce the amount of effort
/// required to clone unmodified values with easy to use copy-on-write.
///
/// This should more or less reflect the API of [`std::borrow::Cow`] as much as is sensible.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(transparent)]
pub struct SharedCow<T: Clone>(Arc<T>);
impl<T: Clone> SharedCow<T> {
/// Create a new `Shared` value.
pub fn new(value: T) -> SharedCow<T> {
SharedCow(Arc::new(value))
}
/// Take an exclusive clone of the shared value, or move and take ownership if it wasn't shared.
pub fn into_owned(self: SharedCow<T>) -> T {
// Optimized: if the Arc is not shared, just unwraps the Arc
match Arc::try_unwrap(self.0) {
Ok(value) => value,
Err(arc) => (*arc).clone(),
}
}
/// Get a mutable reference to the value inside the [`SharedCow`]. This will result in a clone
/// being created only if the value was shared with multiple references.
pub fn to_mut(&mut self) -> &mut T {
Arc::make_mut(&mut self.0)
}
/// Convert the `Shared` value into an `Arc`
pub fn into_arc(value: SharedCow<T>) -> Arc<T> {
value.0
}
/// Return the number of references to the shared value.
pub fn ref_count(value: &SharedCow<T>) -> usize {
Arc::strong_count(&value.0)
}
}
impl<T> From<T> for SharedCow<T>
where
T: Clone,
{
fn from(value: T) -> Self {
SharedCow::new(value)
}
}
impl<T> From<Arc<T>> for SharedCow<T>
where
T: Clone,
{
fn from(value: Arc<T>) -> Self {
SharedCow(value)
}
}
impl<T> fmt::Debug for SharedCow<T>
where
T: fmt::Debug + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Appears transparent
(*self.0).fmt(f)
}
}
impl<T> fmt::Display for SharedCow<T>
where
T: fmt::Display + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*self.0).fmt(f)
}
}
impl<T: Clone> Serialize for SharedCow<T>
where
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}
impl<'de, T: Clone> Deserialize<'de> for SharedCow<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
T::deserialize(deserializer).map(Arc::new).map(SharedCow)
}
}
impl<T: Clone> ops::Deref for SharedCow<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}

View File

@ -174,9 +174,11 @@ impl NuDataFrame {
conversion::insert_record(&mut column_values, record, &maybe_schema)? conversion::insert_record(&mut column_values, record, &maybe_schema)?
} }
Value::Record { val: record, .. } => { Value::Record { val: record, .. } => conversion::insert_record(
conversion::insert_record(&mut column_values, *record, &maybe_schema)? &mut column_values,
} record.into_owned(),
&maybe_schema,
)?,
_ => { _ => {
let key = "0".to_string(); let key = "0".to_string();
conversion::insert_value(value, key, &mut column_values, &maybe_schema)? conversion::insert_value(value, key, &mut column_values, &maybe_schema)?