Move from using a Block to an Arc'd Block (#3289)

This commit is contained in:
Jonathan Turner
2021-04-09 20:12:25 +12:00
committed by GitHub
parent ac070ae942
commit b791d1ab0d
11 changed files with 81 additions and 54 deletions

View File

@ -61,7 +61,7 @@ impl Scope {
}
}
pub fn get_custom_commands_with_name(&self, name: &str) -> Option<Vec<Block>> {
pub fn get_custom_commands_with_name(&self, name: &str) -> Option<Vec<Arc<Block>>> {
let custom_commands: Vec<_> = self
.frames
.lock()
@ -307,7 +307,7 @@ impl ParserScope for Scope {
self.get_command(name).is_some()
}
fn add_definition(&self, block: Block) {
fn add_definition(&self, block: Arc<Block>) {
if let Some(frame) = self.frames.lock().last_mut() {
let name = block.params.name.clone();
frame.custom_commands.insert(name.clone(), block.clone());
@ -315,7 +315,7 @@ impl ParserScope for Scope {
}
}
fn get_definitions(&self) -> Vec<Block> {
fn get_definitions(&self) -> Vec<Arc<Block>> {
let mut blocks = vec![];
if let Some(frame) = self.frames.lock().last() {
for (_, custom_command) in &frame.custom_commands {
@ -356,7 +356,7 @@ pub struct ScopeFrame {
pub vars: IndexMap<String, Value>,
pub env: IndexMap<String, String>,
pub commands: IndexMap<String, Command>,
pub custom_commands: IndexMap<String, Block>,
pub custom_commands: IndexMap<String, Arc<Block>>,
pub aliases: IndexMap<String, Vec<Spanned<String>>>,
///Optional tag to better identify this scope frame later
pub tag: Option<String>,

View File

@ -44,7 +44,7 @@ pub trait WholeStreamCommand: Send + Sync {
// implement a WholeStreamCommand
#[allow(clippy::suspicious_else_formatting)]
impl WholeStreamCommand for Block {
impl WholeStreamCommand for Arc<Block> {
fn name(&self) -> &str {
&self.params.name
}
@ -61,7 +61,10 @@ impl WholeStreamCommand for Block {
let call_info = args.call_info.clone();
let mut block = self.clone();
block.set_redirect(call_info.args.external_redirection);
if let Some(block) = std::sync::Arc::<nu_protocol::hir::Block>::get_mut(&mut block) {
block.set_redirect(call_info.args.external_redirection);
}
let ctx = EvaluationContext::from_args(&args);
let evaluated = call_info.evaluate(&ctx)?;