From 16c8c6cd7db312a892ff30772befb997c9594967 Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Mon, 22 Apr 2024 17:35:33 -0400 Subject: [PATCH] Use `ecow::EcowVec` in `Record` --- Cargo.lock | 12 +++++++++++- crates/nu-protocol/Cargo.toml | 1 + crates/nu-protocol/src/value/mod.rs | 2 +- crates/nu-protocol/src/value/record.rs | 27 +++++++++++++------------- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 865c4bd112..369b489c17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -398,7 +398,7 @@ dependencies = [ "bitflags 2.5.0", "cexpr", "clang-sys", - "itertools 0.11.0", + "itertools 0.12.1", "lazy_static", "lazycell", "proc-macro2", @@ -1348,6 +1348,15 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" +[[package]] +name = "ecow" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54bfbb1708988623190a6c4dbedaeaf0f53c20c6395abd6a01feb327b3146f4b" +dependencies = [ + "serde", +] + [[package]] name = "ego-tree" version = "0.6.2" @@ -3300,6 +3309,7 @@ dependencies = [ "byte-unit", "chrono", "chrono-humanize", + "ecow", "fancy-regex", "indexmap", "lru", diff --git a/crates/nu-protocol/Cargo.toml b/crates/nu-protocol/Cargo.toml index a491a38e74..d51b34a622 100644 --- a/crates/nu-protocol/Cargo.toml +++ b/crates/nu-protocol/Cargo.toml @@ -31,6 +31,7 @@ serde = { workspace = true, default-features = false } serde_json = { workspace = true, optional = true } thiserror = "1.0" typetag = "0.2" +ecow = { version = "0.2.2", features = ["serde"] } [features] plugin = [ diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 29319ae8f2..cf142856ba 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -110,7 +110,7 @@ pub enum Value { internal_span: Span, }, Record { - val: SharedCow, + val: Record, // note: spans are being refactored out of Value // please use .span() instead of matching this span value #[serde(rename = "span")] diff --git a/crates/nu-protocol/src/value/record.rs b/crates/nu-protocol/src/value/record.rs index 8b61e61f7f..0f8aedbf9d 100644 --- a/crates/nu-protocol/src/value/record.rs +++ b/crates/nu-protocol/src/value/record.rs @@ -1,12 +1,11 @@ -use std::{iter::FusedIterator, ops::RangeBounds}; - use crate::{ShellError, Span, Value}; - +use ecow::EcoVec; use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize}; +use std::{iter::FusedIterator, ops::RangeBounds}; #[derive(Debug, Clone, Default)] pub struct Record { - inner: Vec<(String, Value)>, + inner: EcoVec<(String, Value)>, } impl Record { @@ -16,7 +15,7 @@ impl Record { pub fn with_capacity(capacity: usize) -> Self { Self { - inner: Vec::with_capacity(capacity), + inner: EcoVec::with_capacity(capacity), } } @@ -139,7 +138,7 @@ impl Record { where F: FnMut(&str, &Value) -> bool, { - self.retain_mut(|k, v| keep(k, v)); + self.inner.retain(|(k, v)| keep(k, v)) } /// Remove elements in-place that do not satisfy `keep` while allowing mutation of the value. @@ -183,7 +182,8 @@ impl Record { where F: FnMut(&str, &mut Value) -> bool, { - self.inner.retain_mut(|(col, val)| keep(col, val)); + todo!() + // self.inner.retain_mut(|(col, val)| keep(col, val)); } /// Truncate record to the first `len` elements. @@ -259,9 +259,10 @@ impl Record { where R: RangeBounds + Clone, { - Drain { - iter: self.inner.drain(range), - } + todo!() + // Drain { + // iter: self.inner.drain(range), + // } } /// Sort the record by its columns. @@ -382,7 +383,7 @@ impl Extend<(String, Value)> for Record { } pub struct IntoIter { - iter: std::vec::IntoIter<(String, Value)>, + iter: ecow::vec::IntoIter<(String, Value)>, } impl Iterator for IntoIter { @@ -538,7 +539,7 @@ impl<'a> ExactSizeIterator for Columns<'a> { impl FusedIterator for Columns<'_> {} pub struct IntoColumns { - iter: std::vec::IntoIter<(String, Value)>, + iter: ecow::vec::IntoIter<(String, Value)>, } impl Iterator for IntoColumns { @@ -598,7 +599,7 @@ impl<'a> ExactSizeIterator for Values<'a> { impl FusedIterator for Values<'_> {} pub struct IntoValues { - iter: std::vec::IntoIter<(String, Value)>, + iter: ecow::vec::IntoIter<(String, Value)>, } impl Iterator for IntoValues {