mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 08:23:24 +01:00
Record cleanup (#11726)
# Description Does a little cleanup in `record.rs`: - Makes the `record!` macro more hygienic. - Converts regular comments to doc comments from #11718. - Converts the `Record` iterators to new types.
This commit is contained in:
parent
20aa59085b
commit
f8a8eca836
@ -4,10 +4,11 @@ mod from_value;
|
|||||||
mod lazy_record;
|
mod lazy_record;
|
||||||
mod path;
|
mod path;
|
||||||
mod range;
|
mod range;
|
||||||
mod record;
|
|
||||||
mod stream;
|
mod stream;
|
||||||
mod unit;
|
mod unit;
|
||||||
|
|
||||||
|
pub mod record;
|
||||||
|
|
||||||
use crate::ast::{Bits, Boolean, CellPath, Comparison, PathMember};
|
use crate::ast::{Bits, Boolean, CellPath, Comparison, PathMember};
|
||||||
use crate::ast::{Math, Operator};
|
use crate::ast::{Math, Operator};
|
||||||
use crate::engine::{Closure, EngineState};
|
use crate::engine::{Closure, EngineState};
|
||||||
|
@ -26,25 +26,25 @@ impl Record {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructor that checks that `cols` and `vals` are of the same length.
|
/// Constructor that checks that `cols` and `vals` are of the same length.
|
||||||
//
|
///
|
||||||
// WARNING! Panics with assertion failure if cols and vals have different length!
|
/// WARNING! Panics with assertion failure if cols and vals have different length!
|
||||||
// Should be used only when the same lengths are guaranteed!
|
/// Should be used only when the same lengths are guaranteed!
|
||||||
//
|
///
|
||||||
// For perf reasons does not validate the rest of the record assumptions.
|
/// For perf reasons does not validate the rest of the record assumptions.
|
||||||
// - unique keys
|
/// - unique keys
|
||||||
pub fn from_raw_cols_vals_unchecked(cols: Vec<String>, vals: Vec<Value>) -> Self {
|
pub fn from_raw_cols_vals_unchecked(cols: Vec<String>, vals: Vec<Value>) -> Self {
|
||||||
assert_eq!(cols.len(), vals.len());
|
assert_eq!(cols.len(), vals.len());
|
||||||
|
|
||||||
Self { cols, vals }
|
Self { cols, vals }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructor that checks that `cols` and `vals` are of the same length.
|
/// Constructor that checks that `cols` and `vals` are of the same length.
|
||||||
//
|
///
|
||||||
// Returns None if cols and vals have different length.
|
/// Returns None if cols and vals have different length.
|
||||||
//
|
///
|
||||||
// For perf reasons does not validate the rest of the record assumptions.
|
/// For perf reasons does not validate the rest of the record assumptions.
|
||||||
// - unique keys
|
/// - unique keys
|
||||||
pub fn from_raw_cols_vals(
|
pub fn from_raw_cols_vals(
|
||||||
cols: Vec<String>,
|
cols: Vec<String>,
|
||||||
vals: Vec<Value>,
|
vals: Vec<Value>,
|
||||||
@ -314,6 +314,7 @@ impl Record {
|
|||||||
impl FromIterator<(String, Value)> for Record {
|
impl FromIterator<(String, Value)> for Record {
|
||||||
fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
|
fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
|
||||||
let (cols, vals) = iter.into_iter().unzip();
|
let (cols, vals) = iter.into_iter().unzip();
|
||||||
|
// TODO: should this check for duplicate keys/columns?
|
||||||
Self { cols, vals }
|
Self { cols, vals }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -328,7 +329,29 @@ impl Extend<(String, Value)> for Record {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type IntoIter = std::iter::Zip<std::vec::IntoIter<String>, std::vec::IntoIter<Value>>;
|
pub struct IntoIter {
|
||||||
|
iter: std::iter::Zip<std::vec::IntoIter<String>, std::vec::IntoIter<Value>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for IntoIter {
|
||||||
|
type Item = (String, Value);
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
self.iter.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DoubleEndedIterator for IntoIter {
|
||||||
|
fn next_back(&mut self) -> Option<Self::Item> {
|
||||||
|
self.iter.next_back()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExactSizeIterator for IntoIter {
|
||||||
|
fn len(&self) -> usize {
|
||||||
|
self.iter.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl IntoIterator for Record {
|
impl IntoIterator for Record {
|
||||||
type Item = (String, Value);
|
type Item = (String, Value);
|
||||||
@ -336,11 +359,35 @@ impl IntoIterator for Record {
|
|||||||
type IntoIter = IntoIter;
|
type IntoIter = IntoIter;
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
self.cols.into_iter().zip(self.vals)
|
IntoIter {
|
||||||
|
iter: self.cols.into_iter().zip(self.vals),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Iter<'a> = std::iter::Zip<std::slice::Iter<'a, String>, std::slice::Iter<'a, Value>>;
|
pub struct Iter<'a> {
|
||||||
|
iter: std::iter::Zip<std::slice::Iter<'a, String>, std::slice::Iter<'a, Value>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for Iter<'a> {
|
||||||
|
type Item = (&'a String, &'a Value);
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
self.iter.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> DoubleEndedIterator for Iter<'a> {
|
||||||
|
fn next_back(&mut self) -> Option<Self::Item> {
|
||||||
|
self.iter.next_back()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ExactSizeIterator for Iter<'a> {
|
||||||
|
fn len(&self) -> usize {
|
||||||
|
self.iter.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> IntoIterator for &'a Record {
|
impl<'a> IntoIterator for &'a Record {
|
||||||
type Item = (&'a String, &'a Value);
|
type Item = (&'a String, &'a Value);
|
||||||
@ -348,11 +395,35 @@ impl<'a> IntoIterator for &'a Record {
|
|||||||
type IntoIter = Iter<'a>;
|
type IntoIter = Iter<'a>;
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
self.cols.iter().zip(&self.vals)
|
Iter {
|
||||||
|
iter: self.cols.iter().zip(&self.vals),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type IterMut<'a> = std::iter::Zip<std::slice::Iter<'a, String>, std::slice::IterMut<'a, Value>>;
|
pub struct IterMut<'a> {
|
||||||
|
iter: std::iter::Zip<std::slice::Iter<'a, String>, std::slice::IterMut<'a, Value>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for IterMut<'a> {
|
||||||
|
type Item = (&'a String, &'a mut Value);
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
self.iter.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> DoubleEndedIterator for IterMut<'a> {
|
||||||
|
fn next_back(&mut self) -> Option<Self::Item> {
|
||||||
|
self.iter.next_back()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ExactSizeIterator for IterMut<'a> {
|
||||||
|
fn len(&self) -> usize {
|
||||||
|
self.iter.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> IntoIterator for &'a mut Record {
|
impl<'a> IntoIterator for &'a mut Record {
|
||||||
type Item = (&'a String, &'a mut Value);
|
type Item = (&'a String, &'a mut Value);
|
||||||
@ -360,7 +431,9 @@ impl<'a> IntoIterator for &'a mut Record {
|
|||||||
type IntoIter = IterMut<'a>;
|
type IntoIter = IterMut<'a>;
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
self.cols.iter().zip(&mut self.vals)
|
IterMut {
|
||||||
|
iter: self.cols.iter().zip(&mut self.vals),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,9 +553,9 @@ impl ExactSizeIterator for Drain<'_> {
|
|||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! record {
|
macro_rules! record {
|
||||||
{$($col:expr => $val:expr),+ $(,)?} => {
|
{$($col:expr => $val:expr),+ $(,)?} => {
|
||||||
$crate::Record::from_raw_cols_vals_unchecked (
|
$crate::Record::from_raw_cols_vals_unchecked(
|
||||||
vec![$($col.into(),)+],
|
::std::vec![$($col.into(),)+],
|
||||||
vec![$($val,)+]
|
::std::vec![$($val,)+]
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
{} => {
|
{} => {
|
||||||
|
Loading…
Reference in New Issue
Block a user