From c070e2d6f7b6c06d9c34744292736a9ea240d75e Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Mon, 7 Aug 2023 19:43:32 +0000 Subject: [PATCH] Make `Value::columns` return slice instead of cloned Vec (#9927) # Description This PR changes `Value::columns` to return a slice of columns instead of cloning said columns. If the caller needs an owned copy, they can use `slice::to_vec` or the like. This eliminates unnecessary Vec clones (e.g., in `update cells`). # User-Facing Changes Breaking change for `nu_protocol` API. --- crates/nu-command/src/viewers/griddle.rs | 2 +- crates/nu-protocol/src/value/mod.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/nu-command/src/viewers/griddle.rs b/crates/nu-command/src/viewers/griddle.rs index 4daa0dcbf..7b1a2e2ce 100644 --- a/crates/nu-command/src/viewers/griddle.rs +++ b/crates/nu-command/src/viewers/griddle.rs @@ -261,7 +261,7 @@ fn convert_to_list( let mut iter = iter.into_iter().peekable(); if let Some(first) = iter.peek() { - let mut headers = first.columns(); + let mut headers = first.columns().to_vec(); if !headers.is_empty() { headers.insert(0, "#".into()); diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 69a486707..81d517732 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -1732,10 +1732,10 @@ impl Value { matches!(self, Value::Bool { val: false, .. }) } - pub fn columns(&self) -> Vec { + pub fn columns(&self) -> &[String] { match self { - Value::Record { cols, .. } => cols.clone(), - _ => vec![], + Value::Record { cols, .. } => cols, + _ => &[], } }