mirror of
https://github.com/nushell/nushell.git
synced 2025-08-18 18:28:25 +02:00
Use Vec
for Closure
captures (#10940)
# Description Changes the `captures` field in `Closure` from a `HashMap` to a `Vec` and makes `Stack::captures_to_stack` take an owned `Vec` instead of a borrowed `HashMap`. This eliminates the conversion to a `Vec` inside `captures_to_stack` and makes it possible to avoid clones altogether when using an owned `Closure` (which is the case for most commands). Additionally, using a `Vec` reduces the size of `Value` by 8 bytes (down to 72). # User-Facing Changes Breaking API change for `nu-protocol`.
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{BlockId, Value, VarId};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -7,7 +5,7 @@ use serde::{Deserialize, Serialize};
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Closure {
|
||||
pub block_id: BlockId,
|
||||
pub captures: HashMap<VarId, Value>,
|
||||
pub captures: Vec<(VarId, Value)>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
@@ -138,19 +138,13 @@ impl Stack {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn captures_to_stack(&self, captures: &HashMap<VarId, Value>) -> Stack {
|
||||
pub fn captures_to_stack(&self, captures: Vec<(VarId, Value)>) -> Stack {
|
||||
// FIXME: this is probably slow
|
||||
let mut env_vars = self.env_vars.clone();
|
||||
env_vars.push(HashMap::new());
|
||||
|
||||
// FIXME make this more efficient
|
||||
let mut vars = vec![];
|
||||
for (id, val) in captures {
|
||||
vars.push((*id, val.clone()));
|
||||
}
|
||||
|
||||
Stack {
|
||||
vars,
|
||||
vars: captures,
|
||||
env_vars,
|
||||
env_hidden: self.env_hidden.clone(),
|
||||
active_overlays: self.active_overlays.clone(),
|
||||
|
Reference in New Issue
Block a user