Use ecow::EcowVec in Record

This commit is contained in:
Ian Manske 2024-04-22 17:35:33 -04:00
parent bed236362a
commit 16c8c6cd7d
4 changed files with 27 additions and 15 deletions

12
Cargo.lock generated
View File

@ -398,7 +398,7 @@ dependencies = [
"bitflags 2.5.0", "bitflags 2.5.0",
"cexpr", "cexpr",
"clang-sys", "clang-sys",
"itertools 0.11.0", "itertools 0.12.1",
"lazy_static", "lazy_static",
"lazycell", "lazycell",
"proc-macro2", "proc-macro2",
@ -1348,6 +1348,15 @@ version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125" checksum = "0d6ef0072f8a535281e4876be788938b528e9a1d43900b82c2569af7da799125"
[[package]]
name = "ecow"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54bfbb1708988623190a6c4dbedaeaf0f53c20c6395abd6a01feb327b3146f4b"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "ego-tree" name = "ego-tree"
version = "0.6.2" version = "0.6.2"
@ -3300,6 +3309,7 @@ dependencies = [
"byte-unit", "byte-unit",
"chrono", "chrono",
"chrono-humanize", "chrono-humanize",
"ecow",
"fancy-regex", "fancy-regex",
"indexmap", "indexmap",
"lru", "lru",

View File

@ -31,6 +31,7 @@ serde = { workspace = true, default-features = false }
serde_json = { workspace = true, optional = true } serde_json = { workspace = true, optional = true }
thiserror = "1.0" thiserror = "1.0"
typetag = "0.2" typetag = "0.2"
ecow = { version = "0.2.2", features = ["serde"] }
[features] [features]
plugin = [ plugin = [

View File

@ -110,7 +110,7 @@ pub enum Value {
internal_span: Span, internal_span: Span,
}, },
Record { Record {
val: SharedCow<Record>, val: 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")]

View File

@ -1,12 +1,11 @@
use std::{iter::FusedIterator, ops::RangeBounds};
use crate::{ShellError, Span, Value}; use crate::{ShellError, Span, Value};
use ecow::EcoVec;
use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize}; use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize};
use std::{iter::FusedIterator, ops::RangeBounds};
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct Record { pub struct Record {
inner: Vec<(String, Value)>, inner: EcoVec<(String, Value)>,
} }
impl Record { impl Record {
@ -16,7 +15,7 @@ impl Record {
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
Self { Self {
inner: Vec::with_capacity(capacity), inner: EcoVec::with_capacity(capacity),
} }
} }
@ -139,7 +138,7 @@ impl Record {
where where
F: FnMut(&str, &Value) -> bool, 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. /// Remove elements in-place that do not satisfy `keep` while allowing mutation of the value.
@ -183,7 +182,8 @@ impl Record {
where where
F: FnMut(&str, &mut Value) -> bool, 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. /// Truncate record to the first `len` elements.
@ -259,9 +259,10 @@ impl Record {
where where
R: RangeBounds<usize> + Clone, R: RangeBounds<usize> + Clone,
{ {
Drain { todo!()
iter: self.inner.drain(range), // Drain {
} // iter: self.inner.drain(range),
// }
} }
/// Sort the record by its columns. /// Sort the record by its columns.
@ -382,7 +383,7 @@ impl Extend<(String, Value)> for Record {
} }
pub struct IntoIter { pub struct IntoIter {
iter: std::vec::IntoIter<(String, Value)>, iter: ecow::vec::IntoIter<(String, Value)>,
} }
impl Iterator for IntoIter { impl Iterator for IntoIter {
@ -538,7 +539,7 @@ impl<'a> ExactSizeIterator for Columns<'a> {
impl FusedIterator for Columns<'_> {} impl FusedIterator for Columns<'_> {}
pub struct IntoColumns { pub struct IntoColumns {
iter: std::vec::IntoIter<(String, Value)>, iter: ecow::vec::IntoIter<(String, Value)>,
} }
impl Iterator for IntoColumns { impl Iterator for IntoColumns {
@ -598,7 +599,7 @@ impl<'a> ExactSizeIterator for Values<'a> {
impl FusedIterator for Values<'_> {} impl FusedIterator for Values<'_> {}
pub struct IntoValues { pub struct IntoValues {
iter: std::vec::IntoIter<(String, Value)>, iter: ecow::vec::IntoIter<(String, Value)>,
} }
impl Iterator for IntoValues { impl Iterator for IntoValues {