diff --git a/Cargo.lock b/Cargo.lock index 6acbea64f7..9cbc74bd97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3241,8 +3241,6 @@ version = "0.29.1" dependencies = [ "Inflector", "arboard", - "async-recursion", - "async-trait", "base64 0.13.0", "bigdecimal", "byte-unit", @@ -3265,8 +3263,6 @@ dependencies = [ "filesize", "fs_extra", "futures 0.3.13", - "futures-util", - "futures_codec", "getset", "glob", "hamcrest2", diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index ede076899d..6f41176718 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -123,13 +123,13 @@ pub fn search_paths() -> Vec { search_paths } -pub async fn run_script_file(options: Options) -> Result<(), Box> { +pub fn run_script_file(options: Options) -> Result<(), Box> { let context = create_default_context(false)?; if let Some(cfg) = options.config { - load_cfg_as_global_cfg(&context, PathBuf::from(cfg)).await; + load_cfg_as_global_cfg(&context, PathBuf::from(cfg)); } else { - load_global_cfg(&context).await; + load_global_cfg(&context); } let _ = register_plugins(&context); @@ -140,22 +140,22 @@ pub async fn run_script_file(options: Options) -> Result<(), Box> { .get(0) .ok_or_else(|| ShellError::unexpected("Nu source code not available"))?; - run_script_standalone(script.get_code().to_string(), options.stdin, &context, true).await?; + run_script_standalone(script.get_code().to_string(), options.stdin, &context, true)?; Ok(()) } #[cfg(feature = "rustyline-support")] -pub async fn cli(context: EvaluationContext, options: Options) -> Result<(), Box> { +pub fn cli(context: EvaluationContext, options: Options) -> Result<(), Box> { let _ = configure_ctrl_c(&context); // start time for running startup scripts (this metric includes loading of the cfg, but w/e) let startup_commands_start_time = std::time::Instant::now(); if let Some(cfg) = options.config { - load_cfg_as_global_cfg(&context, PathBuf::from(cfg)).await; + load_cfg_as_global_cfg(&context, PathBuf::from(cfg)); } else { - load_global_cfg(&context).await; + load_global_cfg(&context); } // Store cmd duration in an env var context.scope.add_env_var( @@ -196,7 +196,7 @@ pub async fn cli(context: EvaluationContext, options: Options) -> Result<(), Box }; //Check whether dir we start in contains local cfg file and if so load it. - load_local_cfg_if_present(&context).await; + load_local_cfg_if_present(&context); // Give ourselves a scope to work in context.scope.enter_scope(); @@ -240,11 +240,11 @@ pub async fn cli(context: EvaluationContext, options: Options) -> Result<(), Box format!("\x1b[32m{}{}\x1b[m> ", cwd, current_branch()) } else { - let run_result = run_block(&prompt_block, &context, InputStream::empty()).await; + let run_result = run_block(&prompt_block, &context, InputStream::empty()); context.scope.exit_scope(); match run_result { - Ok(result) => match result.collect_string(Tag::unknown()).await { + Ok(result) => match result.collect_string(Tag::unknown()) { Ok(string_result) => { let errors = context.get_errors(); maybe_print_errors(&context, Text::from(prompt_line)); @@ -302,16 +302,13 @@ pub async fn cli(context: EvaluationContext, options: Options) -> Result<(), Box let cmd_start_time = std::time::Instant::now(); let line = match convert_rustyline_result_to_string(readline) { - LineResult::Success(_) => { - process_script( - &session_text[line_start..], - &context, - false, - line_start, - true, - ) - .await - } + LineResult::Success(_) => process_script( + &session_text[line_start..], + &context, + false, + line_start, + true, + ), x => x, }; @@ -404,11 +401,11 @@ pub async fn cli(context: EvaluationContext, options: Options) -> Result<(), Box Ok(()) } -pub async fn load_local_cfg_if_present(context: &EvaluationContext) { +pub fn load_local_cfg_if_present(context: &EvaluationContext) { trace!("Loading local cfg if present"); match config::loadable_cfg_exists_in_dir(PathBuf::from(context.shell_manager.path())) { Ok(Some(cfg_path)) => { - if let Err(err) = context.load_config(&ConfigPath::Local(cfg_path)).await { + if let Err(err) = context.load_config(&ConfigPath::Local(cfg_path)) { context.host.lock().print_err(err, &Text::from("")) } } @@ -422,8 +419,8 @@ pub async fn load_local_cfg_if_present(context: &EvaluationContext) { } } -async fn load_cfg_as_global_cfg(context: &EvaluationContext, path: PathBuf) { - if let Err(err) = context.load_config(&ConfigPath::Global(path.clone())).await { +fn load_cfg_as_global_cfg(context: &EvaluationContext, path: PathBuf) { + if let Err(err) = context.load_config(&ConfigPath::Global(path.clone())) { context.host.lock().print_err(err, &Text::from("")); } else { //TODO current commands assume to find path to global cfg file under config-path @@ -435,10 +432,10 @@ async fn load_cfg_as_global_cfg(context: &EvaluationContext, path: PathBuf) { } } -pub async fn load_global_cfg(context: &EvaluationContext) { +pub fn load_global_cfg(context: &EvaluationContext) { match config::default_path() { Ok(path) => { - load_cfg_as_global_cfg(context, path).await; + load_cfg_as_global_cfg(context, path); } Err(e) => { context.host.lock().print_err(e, &Text::from("")); @@ -459,7 +456,7 @@ pub fn register_plugins(context: &EvaluationContext) -> Result<(), ShellError> { Ok(()) } -pub async fn parse_and_eval(line: &str, ctx: &EvaluationContext) -> Result { +pub fn parse_and_eval(line: &str, ctx: &EvaluationContext) -> Result { // FIXME: do we still need this? let line = if let Some(s) = line.strip_suffix('\n') { s @@ -477,10 +474,10 @@ pub async fn parse_and_eval(line: &str, ctx: &EvaluationContext) -> Result {{ if log::log_enabled!(target: $target, log::Level::Trace) { - use futures::stream::StreamExt; - let objects = $expr.inspect(move |o| { trace!( target: $target, @@ -46,7 +44,6 @@ macro_rules! trace_out_stream { }}; } -pub(crate) use futures::{Stream, StreamExt}; pub(crate) use nu_engine::Host; #[allow(unused_imports)] pub(crate) use nu_errors::ShellError; @@ -65,11 +62,11 @@ pub trait FromInputStream { impl FromInputStream for T where - T: Stream + Send + 'static, + T: Iterator + Send + Sync + 'static, { fn from_input_stream(self) -> OutputStream { OutputStream { - values: self.map(nu_protocol::ReturnSuccess::value).boxed(), + values: Box::new(self.map(nu_protocol::ReturnSuccess::value)), } } } @@ -81,12 +78,12 @@ pub trait ToOutputStream { impl ToOutputStream for T where - T: Stream + Send + 'static, + T: Iterator + Send + Sync + 'static, U: Into, { fn to_output_stream(self) -> OutputStream { OutputStream { - values: self.map(|item| item.into()).boxed(), + values: Box::new(self.map(|item| item.into())), } } } diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index 2487b2fcfc..71e47aab8c 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -27,8 +27,6 @@ nu-ansi-term = { version = "0.29.1", path = "../nu-ansi-term" } Inflector = "0.11" arboard = { version = "1.1.0", optional = true } -async-recursion = "0.3.2" -async-trait = "0.1.42" base64 = "0.13.0" bigdecimal = { version = "0.2.0", features = ["serde"] } byte-unit = "4.0.9" @@ -51,8 +49,6 @@ encoding_rs = "0.8.28" filesize = "0.2.0" fs_extra = "1.2.0" futures = { version = "0.3.12", features = ["compat", "io-compat"] } -futures-util = "0.3.12" -futures_codec = "0.4.1" getset = "0.1.1" glob = "0.3.0" htmlescape = "0.3.1" diff --git a/crates/nu-command/src/commands/all.rs b/crates/nu-command/src/commands/all.rs index da17626f33..92637bb634 100644 --- a/crates/nu-command/src/commands/all.rs +++ b/crates/nu-command/src/commands/all.rs @@ -13,7 +13,6 @@ pub struct Arguments { block: CapturedBlock, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "all?" @@ -31,8 +30,8 @@ impl WholeStreamCommand for Command { "Find if the table rows matches the condition." } - async fn run(&self, args: CommandArgs) -> Result { - all(args).await + fn run(&self, args: CommandArgs) -> Result { + all(args) } fn examples(&self) -> Vec { @@ -53,10 +52,10 @@ impl WholeStreamCommand for Command { } } -async fn all(args: CommandArgs) -> Result { +fn all(args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); let tag = args.call_info.name_tag.clone(); - let (Arguments { block }, input) = args.process().await?; + let (Arguments { block }, input) = args.process()?; let condition = { if block.block.block.len() != 1 { @@ -99,28 +98,25 @@ async fn all(args: CommandArgs) -> Result { ctx.scope.add_vars(&block.captured.entries); ctx.scope.add_var("$it", row); - async move { - let condition = evaluate_baseline_expr(&condition, &*ctx).await.clone(); - ctx.scope.exit_scope(); + let condition = evaluate_baseline_expr(&condition, &*ctx); + ctx.scope.exit_scope(); - let curr = acc?.drain_vec().await; - let curr = curr - .get(0) - .ok_or_else(|| ShellError::unexpected("No value to check with"))?; - let cond = curr.as_bool()?; + let curr = acc?.drain_vec(); + let curr = curr + .get(0) + .ok_or_else(|| ShellError::unexpected("No value to check with"))?; + let cond = curr.as_bool()?; - match condition { - Ok(condition) => match condition.as_bool() { - Ok(b) => Ok(InputStream::one( - UntaggedValue::boolean(cond && b).into_value(&curr.tag), - )), - Err(e) => Err(e), - }, + match condition { + Ok(condition) => match condition.as_bool() { + Ok(b) => Ok(InputStream::one( + UntaggedValue::boolean(cond && b).into_value(&curr.tag), + )), Err(e) => Err(e), - } + }, + Err(e) => Err(e), } - }) - .await? + })? .to_output_stream()) } diff --git a/crates/nu-command/src/commands/ansi/command.rs b/crates/nu-command/src/commands/ansi/command.rs index aaa1720354..5d6b524055 100644 --- a/crates/nu-command/src/commands/ansi/command.rs +++ b/crates/nu-command/src/commands/ansi/command.rs @@ -14,7 +14,6 @@ struct AnsiArgs { osc: Option>, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "ansi" @@ -120,8 +119,8 @@ Format: # ] } - async fn run(&self, args: CommandArgs) -> Result { - let (AnsiArgs { code, escape, osc }, _) = args.process().await?; + fn run(&self, args: CommandArgs) -> Result { + let (AnsiArgs { code, escape, osc }, _) = args.process()?; if let Some(e) = escape { let esc_vec: Vec = e.item.chars().collect(); diff --git a/crates/nu-command/src/commands/ansi/strip.rs b/crates/nu-command/src/commands/ansi/strip.rs index a4856a75af..46001ff542 100644 --- a/crates/nu-command/src/commands/ansi/strip.rs +++ b/crates/nu-command/src/commands/ansi/strip.rs @@ -15,7 +15,6 @@ struct Arguments { rest: Vec, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "ansi strip" @@ -32,8 +31,8 @@ impl WholeStreamCommand for SubCommand { "strip ansi escape sequences from string" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -45,8 +44,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { rest }, input) = args.process()?; let column_paths: Vec<_> = rest; Ok(input diff --git a/crates/nu-command/src/commands/any.rs b/crates/nu-command/src/commands/any.rs index d9aec6b635..2c83e81db2 100644 --- a/crates/nu-command/src/commands/any.rs +++ b/crates/nu-command/src/commands/any.rs @@ -13,7 +13,6 @@ pub struct Arguments { block: CapturedBlock, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "any?" @@ -31,8 +30,8 @@ impl WholeStreamCommand for Command { "Find if the table rows matches the condition." } - async fn run(&self, args: CommandArgs) -> Result { - any(args).await + fn run(&self, args: CommandArgs) -> Result { + any(args) } fn examples(&self) -> Vec { @@ -53,10 +52,10 @@ impl WholeStreamCommand for Command { } } -async fn any(args: CommandArgs) -> Result { +fn any(args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); let tag = args.call_info.name_tag.clone(); - let (Arguments { block }, input) = args.process().await?; + let (Arguments { block }, input) = args.process()?; let condition = { if block.block.block.len() != 1 { @@ -99,28 +98,25 @@ async fn any(args: CommandArgs) -> Result { ctx.scope.add_vars(&block.captured.entries); ctx.scope.add_var("$it", row); - async move { - let condition = evaluate_baseline_expr(&condition, &*ctx).await.clone(); - ctx.scope.exit_scope(); + let condition = evaluate_baseline_expr(&condition, &*ctx); + ctx.scope.exit_scope(); - let curr = cond?.drain_vec().await; - let curr = curr - .get(0) - .ok_or_else(|| ShellError::unexpected("No value to check with"))?; - let cond = curr.as_bool()?; + let curr = cond?.drain_vec(); + let curr = curr + .get(0) + .ok_or_else(|| ShellError::unexpected("No value to check with"))?; + let cond = curr.as_bool()?; - match condition { - Ok(condition) => match condition.as_bool() { - Ok(b) => Ok(InputStream::one( - UntaggedValue::boolean(cond || b).into_value(&curr.tag), - )), - Err(e) => Err(e), - }, + match condition { + Ok(condition) => match condition.as_bool() { + Ok(b) => Ok(InputStream::one( + UntaggedValue::boolean(cond || b).into_value(&curr.tag), + )), Err(e) => Err(e), - } + }, + Err(e) => Err(e), } - }) - .await? + })? .to_output_stream()) } diff --git a/crates/nu-command/src/commands/append.rs b/crates/nu-command/src/commands/append.rs index 20ca99c7dc..e08107a875 100644 --- a/crates/nu-command/src/commands/append.rs +++ b/crates/nu-command/src/commands/append.rs @@ -10,7 +10,6 @@ struct Arguments { pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "append" @@ -28,10 +27,10 @@ impl WholeStreamCommand for Command { "Append a row to the table." } - async fn run(&self, args: CommandArgs) -> Result { - let (Arguments { mut value }, input) = args.process().await?; + fn run(&self, args: CommandArgs) -> Result { + let (Arguments { mut value }, input) = args.process()?; - let input: Vec = input.collect().await; + let input: Vec = input.collect(); if let Some(first) = input.get(0) { value.tag = first.tag(); @@ -48,18 +47,14 @@ impl WholeStreamCommand for Command { } } - Ok(futures::stream::iter( - input - .into_iter() - .chain(vec![value]) - .map(ReturnSuccess::value), - ) - .to_output_stream()) + Ok(input + .into_iter() + .chain(vec![value]) + .map(ReturnSuccess::value) + .to_output_stream()) } fn examples(&self) -> Vec { - use nu_protocol::row; - vec![ Example { description: "Add values to the end of the table", diff --git a/crates/nu-command/src/commands/autoenv.rs b/crates/nu-command/src/commands/autoenv.rs index 6a4925f341..db931bc12f 100644 --- a/crates/nu-command/src/commands/autoenv.rs +++ b/crates/nu-command/src/commands/autoenv.rs @@ -4,7 +4,6 @@ use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; pub struct Autoenv; -#[async_trait] impl WholeStreamCommand for Autoenv { fn name(&self) -> &str { "autoenv" @@ -26,7 +25,7 @@ The .nu-env file has the same format as your $HOME/nu/config.toml file. By loadi fn signature(&self) -> Signature { Signature::build("autoenv") } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(get_full_help(&Autoenv, &args.scope)).into_value(Tag::unknown()), ))) diff --git a/crates/nu-command/src/commands/autoenv_trust.rs b/crates/nu-command/src/commands/autoenv_trust.rs index b4e1e58c59..d411d8e2cc 100644 --- a/crates/nu-command/src/commands/autoenv_trust.rs +++ b/crates/nu-command/src/commands/autoenv_trust.rs @@ -8,7 +8,6 @@ use sha2::{Digest, Sha256}; use std::{fs, path::PathBuf}; pub struct AutoenvTrust; -#[async_trait] impl WholeStreamCommand for AutoenvTrust { fn name(&self) -> &str { "autoenv trust" @@ -22,11 +21,11 @@ impl WholeStreamCommand for AutoenvTrust { "Trust a .nu-env file in the current or given directory" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ctx = EvaluationContext::from_args(&args); - let file_to_trust = match args.call_info.evaluate(&ctx).await?.args.nth(0) { + let file_to_trust = match args.call_info.evaluate(&ctx)?.args.nth(0) { Some(Value { value: UntaggedValue::Primitive(Primitive::String(ref path)), tag: _, diff --git a/crates/nu-command/src/commands/autoenv_untrust.rs b/crates/nu-command/src/commands/autoenv_untrust.rs index 2a8f636577..35c7f2fe5f 100644 --- a/crates/nu-command/src/commands/autoenv_untrust.rs +++ b/crates/nu-command/src/commands/autoenv_untrust.rs @@ -8,7 +8,6 @@ use std::io::Read; use std::{fs, path::PathBuf}; pub struct AutoenvUnTrust; -#[async_trait] impl WholeStreamCommand for AutoenvUnTrust { fn name(&self) -> &str { "autoenv untrust" @@ -26,10 +25,10 @@ impl WholeStreamCommand for AutoenvUnTrust { "Untrust a .nu-env file in the current or given directory" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ctx = EvaluationContext::from_args(&args); - let file_to_untrust = match args.call_info.evaluate(&ctx).await?.args.nth(0) { + let file_to_untrust = match args.call_info.evaluate(&ctx)?.args.nth(0) { Some(Value { value: UntaggedValue::Primitive(Primitive::String(ref path)), tag: _, diff --git a/crates/nu-command/src/commands/autoview/command.rs b/crates/nu-command/src/commands/autoview/command.rs index 42bc628e97..ddb24c98f4 100644 --- a/crates/nu-command/src/commands/autoview/command.rs +++ b/crates/nu-command/src/commands/autoview/command.rs @@ -12,7 +12,6 @@ use std::sync::atomic::AtomicBool; pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "autoview" @@ -26,8 +25,8 @@ impl WholeStreamCommand for Command { "View the contents of the pipeline as a table or list." } - async fn run(&self, args: CommandArgs) -> Result { - autoview(RunnableContext::from_command_args(args)).await + fn run(&self, args: CommandArgs) -> Result { + autoview(RunnableContext::from_command_args(args)) } fn examples(&self) -> Vec { @@ -71,7 +70,7 @@ impl RunnableContextWithoutInput { } } -pub async fn autoview(context: RunnableContext) -> Result { +pub fn autoview(context: RunnableContext) -> Result { let configuration = AutoViewConfiguration::new(); let binary = context.get_command("binaryview"); @@ -84,21 +83,19 @@ pub async fn autoview(context: RunnableContext) -> Result { let ctrl_c = context.ctrl_c.clone(); let xy = vec![x, y]; - let xy_stream = futures::stream::iter(xy) - .chain(input_stream) - .interruptible(ctrl_c); + let xy_stream = xy.into_iter().chain(input_stream).interruptible(ctrl_c); let stream = InputStream::from_stream(xy_stream); if let Some(table) = table { let command_args = create_default_command_args(&context).with_input(stream); - let result = table.run(command_args).await?; - result.collect::>().await; + let result = table.run(command_args)?; + let _ = result.collect::>(); } } _ => { @@ -114,8 +111,8 @@ pub async fn autoview(context: RunnableContext) -> Result>().await; + let result = text.run(command_args)?; + let _ = result.collect::>(); } else { out!("{}", s); } @@ -196,8 +193,8 @@ pub async fn autoview(context: RunnableContext) -> Result>().await; + let result = binary.run(command_args)?; + let _ = result.collect::>(); } else { use pretty_hex::*; out!("{:?}", b.hex_dump()); @@ -262,8 +259,8 @@ pub async fn autoview(context: RunnableContext) -> Result>().await; + let result = table.run(command_args)?; + let _ = result.collect::>(); } else { out!("{:?}", item); } diff --git a/crates/nu-command/src/commands/benchmark.rs b/crates/nu-command/src/commands/benchmark.rs index 85e0e9741e..f84e5857a7 100644 --- a/crates/nu-command/src/commands/benchmark.rs +++ b/crates/nu-command/src/commands/benchmark.rs @@ -23,7 +23,6 @@ struct BenchmarkArgs { passthrough: Option, } -#[async_trait] impl WholeStreamCommand for Benchmark { fn name(&self) -> &str { "benchmark" @@ -48,8 +47,8 @@ impl WholeStreamCommand for Benchmark { "Runs a block and returns the time it took to execute it." } - async fn run(&self, args: CommandArgs) -> Result { - benchmark(args).await + fn run(&self, args: CommandArgs) -> Result { + benchmark(args) } fn examples(&self) -> Vec { @@ -68,11 +67,11 @@ impl WholeStreamCommand for Benchmark { } } -async fn benchmark(raw_args: CommandArgs) -> Result { +fn benchmark(raw_args: CommandArgs) -> Result { let tag = raw_args.call_info.args.span; let mut context = EvaluationContext::from_args(&raw_args); let scope = raw_args.scope.clone(); - let (BenchmarkArgs { block, passthrough }, input) = raw_args.process().await?; + let (BenchmarkArgs { block, passthrough }, input) = raw_args.process()?; let env = scope.get_env_vars(); let name = generate_free_name(&env); @@ -82,15 +81,15 @@ async fn benchmark(raw_args: CommandArgs) -> Result { let start_time = Instant::now(); // #[cfg(feature = "rich-benchmark")] - // let start = time().await; + // let start = time(); context.scope.enter_scope(); - let result = run_block(&block.block, &context, input).await; + let result = run_block(&block.block, &context, input); context.scope.exit_scope(); - let output = result?.into_vec().await; + let output = result?.into_vec(); // #[cfg(feature = "rich-benchmark")] - // let end = time().await; + // let end = time(); let end_time = Instant::now(); context.clear_errors(); @@ -102,7 +101,7 @@ async fn benchmark(raw_args: CommandArgs) -> Result { let real_time = into_big_int(end_time - start_time); indexmap.insert("real time".to_string(), real_time); - benchmark_output(indexmap, output, passthrough, &tag, &mut context).await + benchmark_output(indexmap, output, passthrough, &tag, &mut context) } // return advanced stats // #[cfg(feature = "rich-benchmark")] @@ -121,7 +120,7 @@ async fn benchmark(raw_args: CommandArgs) -> Result { // let idle_time = into_big_int(end.idle() - start.idle()); // indexmap.insert("idle time".to_string(), idle_time); - // benchmark_output(indexmap, output, passthrough, &tag, &mut context).await + // benchmark_output(indexmap, output, passthrough, &tag, &mut context) // } else { // Err(ShellError::untagged_runtime_error( // "Could not retrieve CPU time", @@ -129,7 +128,7 @@ async fn benchmark(raw_args: CommandArgs) -> Result { // } } -async fn benchmark_output( +fn benchmark_output( indexmap: IndexMap, block_output: Output, passthrough: Option, @@ -155,7 +154,7 @@ where let time_block = add_implicit_autoview(time_block.block); context.scope.enter_scope(); - let result = run_block(&time_block, context, benchmark_output).await; + let result = run_block(&time_block, context, benchmark_output); context.scope.exit_scope(); result?; context.clear_errors(); diff --git a/crates/nu-command/src/commands/build_string.rs b/crates/nu-command/src/commands/build_string.rs index 4b02f5fb41..f6251bb23e 100644 --- a/crates/nu-command/src/commands/build_string.rs +++ b/crates/nu-command/src/commands/build_string.rs @@ -12,7 +12,6 @@ pub struct BuildStringArgs { pub struct BuildString; -#[async_trait] impl WholeStreamCommand for BuildString { fn name(&self) -> &str { "build-string" @@ -27,9 +26,9 @@ impl WholeStreamCommand for BuildString { "Builds a string from the arguments." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (BuildStringArgs { rest }, _) = args.process().await?; + let (BuildStringArgs { rest }, _) = args.process()?; let mut output_string = String::new(); diff --git a/crates/nu-command/src/commands/cal.rs b/crates/nu-command/src/commands/cal.rs index 24d5e46e73..a616399f8d 100644 --- a/crates/nu-command/src/commands/cal.rs +++ b/crates/nu-command/src/commands/cal.rs @@ -7,7 +7,6 @@ use nu_protocol::{Dictionary, Signature, SyntaxShape, UntaggedValue, Value}; pub struct Cal; -#[async_trait] impl WholeStreamCommand for Cal { fn name(&self) -> &str { "cal" @@ -41,8 +40,8 @@ impl WholeStreamCommand for Cal { "Display a calendar." } - async fn run(&self, args: CommandArgs) -> Result { - cal(args).await + fn run(&self, args: CommandArgs) -> Result { + cal(args) } fn examples(&self) -> Vec { @@ -66,8 +65,8 @@ impl WholeStreamCommand for Cal { } } -pub async fn cal(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +pub fn cal(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let mut calendar_vec_deque = VecDeque::new(); let tag = args.call_info.name_tag.clone(); @@ -102,7 +101,7 @@ pub async fn cal(args: CommandArgs) -> Result { current_day_option, )?; - Ok(futures::stream::iter(calendar_vec_deque).to_output_stream()) + Ok(calendar_vec_deque.into_iter().to_output_stream()) } fn get_invalid_year_shell_error(year_tag: &Tag) -> ShellError { diff --git a/crates/nu-command/src/commands/cd.rs b/crates/nu-command/src/commands/cd.rs index 9ec72e1e6a..772090fb5c 100644 --- a/crates/nu-command/src/commands/cd.rs +++ b/crates/nu-command/src/commands/cd.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, SyntaxShape}; pub struct Cd; -#[async_trait] impl WholeStreamCommand for Cd { fn name(&self) -> &str { "cd" @@ -25,10 +24,10 @@ impl WholeStreamCommand for Cd { "Change to a new path." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let shell_manager = args.shell_manager.clone(); - let (args, _): (CdArgs, _) = args.process().await?; + let (args, _): (CdArgs, _) = args.process()?; shell_manager.cd(args, name) } diff --git a/crates/nu-command/src/commands/char_.rs b/crates/nu-command/src/commands/char_.rs index 0257864130..fc91a9a863 100644 --- a/crates/nu-command/src/commands/char_.rs +++ b/crates/nu-command/src/commands/char_.rs @@ -13,7 +13,6 @@ struct CharArgs { unicode: bool, } -#[async_trait] impl WholeStreamCommand for Char { fn name(&self) -> &str { "char" @@ -65,7 +64,7 @@ impl WholeStreamCommand for Char { ] } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let ( CharArgs { name, @@ -73,7 +72,7 @@ impl WholeStreamCommand for Char { unicode, }, _, - ) = args.process().await?; + ) = args.process()?; if unicode { if !rest.is_empty() { diff --git a/crates/nu-command/src/commands/chart.rs b/crates/nu-command/src/commands/chart.rs index 4e84542334..fab5f17f54 100644 --- a/crates/nu-command/src/commands/chart.rs +++ b/crates/nu-command/src/commands/chart.rs @@ -6,7 +6,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; #[derive(Clone)] pub struct Chart; -#[async_trait] impl WholeStreamCommand for Chart { fn name(&self) -> &str { "chart" @@ -20,7 +19,7 @@ impl WholeStreamCommand for Chart { "Displays charts." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { if args.scope.get_command("chart bar").is_none() { return Err(ShellError::untagged_runtime_error( "nu_plugin_chart not installed.", diff --git a/crates/nu-command/src/commands/classified/external.rs b/crates/nu-command/src/commands/classified/external.rs index 1a648c9ea7..72c767d31e 100644 --- a/crates/nu-command/src/commands/classified/external.rs +++ b/crates/nu-command/src/commands/classified/external.rs @@ -1,16 +1,14 @@ use crate::futures::ThreadedReceiver; use crate::prelude::*; -use nu_engine::evaluate_baseline_expr; +use nu_engine::{evaluate_baseline_expr, BufCodecReader}; use nu_engine::{MaybeTextCodec, StringOrBinary}; -use std::borrow::Cow; use std::io::Write; use std::ops::Deref; use std::process::{Command, Stdio}; use std::sync::mpsc; +use std::{borrow::Cow, io::BufReader}; -use futures::executor::block_on_stream; -use futures_codec::FramedRead; use log::trace; use nu_errors::ShellError; @@ -18,9 +16,8 @@ use nu_protocol::hir::Expression; use nu_protocol::hir::{ExternalCommand, ExternalRedirection}; use nu_protocol::{Primitive, ShellTypeName, UntaggedValue, Value}; use nu_source::Tag; -use nu_stream::trace_stream; -pub(crate) async fn run_external_command( +pub(crate) fn run_external_command( command: ExternalCommand, context: &mut EvaluationContext, input: InputStream, @@ -36,10 +33,10 @@ pub(crate) async fn run_external_command( )); } - run_with_stdin(command, context, input, external_redirection).await + run_with_stdin(command, context, input, external_redirection) } -async fn run_with_stdin( +fn run_with_stdin( command: ExternalCommand, context: &mut EvaluationContext, input: InputStream, @@ -47,12 +44,10 @@ async fn run_with_stdin( ) -> Result { let path = context.shell_manager.path(); - let input = trace_stream!(target: "nu::trace_stream::external::stdin", "input" = input); - let mut command_args = vec![]; for arg in command.args.iter() { let is_literal = matches!(arg.expr, Expression::Literal(_)); - let value = evaluate_baseline_expr(arg, context).await?; + let value = evaluate_baseline_expr(arg, context)?; // Skip any arguments that don't really exist, treating them as optional // FIXME: we may want to preserve the gap in the future, though it's hard to say @@ -219,7 +214,7 @@ fn spawn( .take() .expect("Internal error: could not get stdin pipe for external command"); - for value in block_on_stream(input) { + for value in input { match &value.value { UntaggedValue::Primitive(Primitive::Nothing) => continue, UntaggedValue::Primitive(Primitive::String(s)) => { @@ -274,10 +269,12 @@ fn spawn( return Err(()); }; - let file = futures::io::AllowStdIo::new(stdout); - let stream = FramedRead::new(file, MaybeTextCodec::default()); + // let file = futures::io::AllowStdIo::new(stdout); + // let stream = FramedRead::new(file, MaybeTextCodec::default()); + let buf_read = BufReader::new(stdout); + let buf_codec = BufCodecReader::new(buf_read, MaybeTextCodec::default()); - for line in block_on_stream(stream) { + for line in buf_codec { match line { Ok(line) => match line { StringOrBinary::String(s) => { @@ -345,10 +342,12 @@ fn spawn( return Err(()); }; - let file = futures::io::AllowStdIo::new(stderr); - let stream = FramedRead::new(file, MaybeTextCodec::default()); + // let file = futures::io::AllowStdIo::new(stderr); + // let stream = FramedRead::new(file, MaybeTextCodec::default()); + let buf_reader = BufReader::new(stderr); + let buf_codec = BufCodecReader::new(buf_reader, MaybeTextCodec::default()); - for line in block_on_stream(stream) { + for line in buf_codec { match line { Ok(line) => match line { StringOrBinary::String(s) => { @@ -506,16 +505,13 @@ mod tests { #[cfg(feature = "which")] use super::{run_external_command, InputStream}; - #[cfg(feature = "which")] - use futures::executor::block_on; #[cfg(feature = "which")] use nu_engine::basic_evaluation_context; - #[cfg(feature = "which")] - use nu_errors::ShellError; + #[cfg(feature = "which")] use nu_test_support::commands::ExternalBuilder; - // async fn read(mut stream: OutputStream) -> Option { - // match stream.try_next().await { + // fn read(mut stream: OutputStream) -> Option { + // match stream.try_next() { // Ok(val) => { // if let Some(val) = val { // val.raw_value() @@ -528,7 +524,7 @@ mod tests { // } #[cfg(feature = "which")] - async fn non_existent_run() -> Result<(), ShellError> { + fn non_existent_run() { use nu_protocol::hir::ExternalRedirection; let cmd = ExternalBuilder::for_name("i_dont_exist.exe").build(); @@ -536,24 +532,18 @@ mod tests { let mut ctx = basic_evaluation_context().expect("There was a problem creating a basic context."); - assert!( - run_external_command(cmd, &mut ctx, input, ExternalRedirection::Stdout) - .await - .is_err() - ); - - Ok(()) + assert!(run_external_command(cmd, &mut ctx, input, ExternalRedirection::Stdout).is_err()); } - // async fn failure_run() -> Result<(), ShellError> { + // fn failure_run() -> Result<(), ShellError> { // let cmd = ExternalBuilder::for_name("fail").build(); // let mut ctx = crate::cli::basic_evaluation_context().expect("There was a problem creating a basic context."); // let stream = run_external_command(cmd, &mut ctx, None, false) - // .await? + // ? // .expect("There was a problem running the external command."); - // match read(stream.into()).await { + // match read(stream.into()) { // Some(Value { // value: UntaggedValue::Error(_), // .. @@ -571,8 +561,8 @@ mod tests { #[cfg(feature = "which")] #[test] - fn identifies_command_not_found() -> Result<(), ShellError> { - block_on(non_existent_run()) + fn identifies_command_not_found() { + non_existent_run() } #[test] diff --git a/crates/nu-command/src/commands/clear.rs b/crates/nu-command/src/commands/clear.rs index 8c8208e5fc..9f0cff5f64 100644 --- a/crates/nu-command/src/commands/clear.rs +++ b/crates/nu-command/src/commands/clear.rs @@ -6,7 +6,6 @@ use std::process::Command; pub struct Clear; -#[async_trait] impl WholeStreamCommand for Clear { fn name(&self) -> &str { "clear" @@ -20,7 +19,7 @@ impl WholeStreamCommand for Clear { "Clears the terminal." } - async fn run(&self, _: CommandArgs) -> Result { + fn run(&self, _: CommandArgs) -> Result { if cfg!(windows) { Command::new("cmd") .args(&["/C", "cls"]) diff --git a/crates/nu-command/src/commands/clip.rs b/crates/nu-command/src/commands/clip.rs index 60fbbce43e..c4696f345c 100644 --- a/crates/nu-command/src/commands/clip.rs +++ b/crates/nu-command/src/commands/clip.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use futures::stream::StreamExt; + use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, Value}; @@ -8,7 +8,6 @@ use arboard::Clipboard; pub struct Clip; -#[async_trait] impl WholeStreamCommand for Clip { fn name(&self) -> &str { "clip" @@ -22,8 +21,8 @@ impl WholeStreamCommand for Clip { "Copy the contents of the pipeline to the copy/paste buffer." } - async fn run(&self, args: CommandArgs) -> Result { - clip(args).await + fn run(&self, args: CommandArgs) -> Result { + clip(args) } fn examples(&self) -> Vec { @@ -42,10 +41,10 @@ impl WholeStreamCommand for Clip { } } -pub async fn clip(args: CommandArgs) -> Result { +pub fn clip(args: CommandArgs) -> Result { let input = args.input; let name = args.call_info.name_tag.clone(); - let values: Vec = input.collect().await; + let values: Vec = input.collect(); if let Ok(mut clip_context) = Clipboard::new() { let mut new_copy_data = String::new(); diff --git a/crates/nu-command/src/commands/compact.rs b/crates/nu-command/src/commands/compact.rs index cc99b7301b..9576185fc8 100644 --- a/crates/nu-command/src/commands/compact.rs +++ b/crates/nu-command/src/commands/compact.rs @@ -1,6 +1,5 @@ use crate::prelude::*; -use futures::future; -use futures::stream::StreamExt; + use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; @@ -13,7 +12,6 @@ pub struct CompactArgs { rest: Vec>, } -#[async_trait] impl WholeStreamCommand for Compact { fn name(&self) -> &str { "compact" @@ -27,8 +25,8 @@ impl WholeStreamCommand for Compact { "Creates a table with non-empty rows." } - async fn run(&self, args: CommandArgs) -> Result { - compact(args).await + fn run(&self, args: CommandArgs) -> Result { + compact(args) } fn examples(&self) -> Vec { @@ -40,11 +38,11 @@ impl WholeStreamCommand for Compact { } } -pub async fn compact(args: CommandArgs) -> Result { - let (CompactArgs { rest: columns }, input) = args.process().await?; +pub fn compact(args: CommandArgs) -> Result { + let (CompactArgs { rest: columns }, input) = args.process()?; Ok(input .filter_map(move |item| { - future::ready(if columns.is_empty() { + if columns.is_empty() { if !item.is_empty() { Some(ReturnSuccess::value(item)) } else { @@ -67,7 +65,7 @@ pub async fn compact(args: CommandArgs) -> Result { } _ => None, } - }) + } }) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/config/clear.rs b/crates/nu-command/src/commands/config/clear.rs index 2885f90df9..48fd98c347 100644 --- a/crates/nu-command/src/commands/config/clear.rs +++ b/crates/nu-command/src/commands/config/clear.rs @@ -5,7 +5,6 @@ use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "config clear" @@ -19,8 +18,8 @@ impl WholeStreamCommand for SubCommand { "clear the config" } - async fn run(&self, args: CommandArgs) -> Result { - clear(args).await + fn run(&self, args: CommandArgs) -> Result { + clear(args) } fn examples(&self) -> Vec { @@ -32,7 +31,7 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn clear(args: CommandArgs) -> Result { +pub fn clear(args: CommandArgs) -> Result { let name_span = args.call_info.name_tag.clone(); let path = match args.scope.get_var("config-path") { diff --git a/crates/nu-command/src/commands/config/command.rs b/crates/nu-command/src/commands/config/command.rs index 6d4423225f..b573e9332d 100644 --- a/crates/nu-command/src/commands/config/command.rs +++ b/crates/nu-command/src/commands/config/command.rs @@ -7,7 +7,6 @@ use nu_stream::OutputStream; pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "config" @@ -21,22 +20,22 @@ impl WholeStreamCommand for Command { "Configuration management." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let name = args.call_info.name_tag; if let Some(global_cfg) = &args.configs.lock().global_config { let result = global_cfg.vars.clone(); - Ok(futures::stream::iter(vec![ReturnSuccess::value( + Ok(vec![ReturnSuccess::value( UntaggedValue::Row(result.into()).into_value(name), - )]) + )] + .into_iter() .to_output_stream()) } else { - Ok( - futures::stream::iter(vec![ReturnSuccess::value(UntaggedValue::Error( - ShellError::untagged_runtime_error("No global config found!"), - ))]) - .to_output_stream(), - ) + Ok(vec![ReturnSuccess::value(UntaggedValue::Error( + ShellError::untagged_runtime_error("No global config found!"), + ))] + .into_iter() + .to_output_stream()) } } } diff --git a/crates/nu-command/src/commands/config/get.rs b/crates/nu-command/src/commands/config/get.rs index ff432741e9..36f3cb9a5b 100644 --- a/crates/nu-command/src/commands/config/get.rs +++ b/crates/nu-command/src/commands/config/get.rs @@ -12,7 +12,6 @@ pub struct Arguments { column_path: ColumnPath, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "config get" @@ -30,8 +29,8 @@ impl WholeStreamCommand for SubCommand { "Gets a value from the config" } - async fn run(&self, args: CommandArgs) -> Result { - get(args).await + fn run(&self, args: CommandArgs) -> Result { + get(args) } fn examples(&self) -> Vec { @@ -43,10 +42,10 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn get(args: CommandArgs) -> Result { +pub fn get(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let scope = args.scope.clone(); - let (Arguments { column_path }, _) = args.process().await?; + let (Arguments { column_path }, _) = args.process()?; let path = match scope.get_var("config-path") { Some(Value { @@ -70,7 +69,7 @@ pub async fn get(args: CommandArgs) -> Result { .map(|x| ReturnSuccess::value(x.clone())) .collect(); - futures::stream::iter(list).to_output_stream() + list.into_iter().to_output_stream() } x => OutputStream::one(ReturnSuccess::value(x)), }) diff --git a/crates/nu-command/src/commands/config/path.rs b/crates/nu-command/src/commands/config/path.rs index 6ae182acdb..8ca4658733 100644 --- a/crates/nu-command/src/commands/config/path.rs +++ b/crates/nu-command/src/commands/config/path.rs @@ -5,7 +5,6 @@ use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "config path" @@ -19,8 +18,8 @@ impl WholeStreamCommand for SubCommand { "return the path to the config file" } - async fn run(&self, args: CommandArgs) -> Result { - path(args).await + fn run(&self, args: CommandArgs) -> Result { + path(args) } fn examples(&self) -> Vec { @@ -32,7 +31,7 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn path(args: CommandArgs) -> Result { +pub fn path(args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( match args.scope.get_var("config-path") { Some( diff --git a/crates/nu-command/src/commands/config/remove.rs b/crates/nu-command/src/commands/config/remove.rs index da8e1d3c6a..da7b1d7460 100644 --- a/crates/nu-command/src/commands/config/remove.rs +++ b/crates/nu-command/src/commands/config/remove.rs @@ -11,7 +11,6 @@ pub struct Arguments { remove: Tagged, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "config remove" @@ -29,8 +28,8 @@ impl WholeStreamCommand for SubCommand { "Removes a value from the config" } - async fn run(&self, args: CommandArgs) -> Result { - remove(args).await + fn run(&self, args: CommandArgs) -> Result { + remove(args) } fn examples(&self) -> Vec { @@ -42,10 +41,10 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn remove(args: CommandArgs) -> Result { +pub fn remove(args: CommandArgs) -> Result { let name_span = args.call_info.name_tag.clone(); let scope = args.scope.clone(); - let (Arguments { remove }, _) = args.process().await?; + let (Arguments { remove }, _) = args.process()?; let path = match scope.get_var("config-path") { Some(Value { @@ -62,9 +61,10 @@ pub async fn remove(args: CommandArgs) -> Result { if result.contains_key(&key) { result.swap_remove(&key); config::write(&result, &path)?; - Ok(futures::stream::iter(vec![ReturnSuccess::value( + Ok(vec![ReturnSuccess::value( UntaggedValue::Row(result.into()).into_value(remove.tag()), - )]) + )] + .into_iter() .to_output_stream()) } else { Err(ShellError::labeled_error( diff --git a/crates/nu-command/src/commands/config/set.rs b/crates/nu-command/src/commands/config/set.rs index 44296f3552..d73cd3bf56 100644 --- a/crates/nu-command/src/commands/config/set.rs +++ b/crates/nu-command/src/commands/config/set.rs @@ -13,7 +13,6 @@ pub struct Arguments { value: Value, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "config set" @@ -29,8 +28,8 @@ impl WholeStreamCommand for SubCommand { "Sets a value in the config" } - async fn run(&self, args: CommandArgs) -> Result { - set(args).await + fn run(&self, args: CommandArgs) -> Result { + set(args) } fn examples(&self) -> Vec { @@ -59,7 +58,7 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn set(args: CommandArgs) -> Result { +pub fn set(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let ctx = EvaluationContext::from_args(&args); let scope = args.scope.clone(); @@ -69,7 +68,7 @@ pub async fn set(args: CommandArgs) -> Result { mut value, }, _, - ) = args.process().await?; + ) = args.process()?; let path = match scope.get_var("config-path") { Some(Value { @@ -96,8 +95,7 @@ pub async fn set(args: CommandArgs) -> Result { config::write(&changes.entries, &path)?; ctx.reload_config(&ConfigPath::Global( path.expect("Global config path is always some"), - )) - .await?; + ))?; Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::Row(changes).into_value(name), diff --git a/crates/nu-command/src/commands/config/set_into.rs b/crates/nu-command/src/commands/config/set_into.rs index 4a99b0745d..e38242dcec 100644 --- a/crates/nu-command/src/commands/config/set_into.rs +++ b/crates/nu-command/src/commands/config/set_into.rs @@ -13,7 +13,6 @@ pub struct Arguments { set_into: Tagged, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "config set_into" @@ -31,8 +30,8 @@ impl WholeStreamCommand for SubCommand { "Sets a value in the config" } - async fn run(&self, args: CommandArgs) -> Result { - set_into(args).await + fn run(&self, args: CommandArgs) -> Result { + set_into(args) } fn examples(&self) -> Vec { @@ -44,11 +43,11 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn set_into(args: CommandArgs) -> Result { +pub fn set_into(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let ctx = EvaluationContext::from_args(&args); let scope = args.scope.clone(); - let (Arguments { set_into: v }, input) = args.process().await?; + let (Arguments { set_into: v }, input) = args.process()?; let path = match scope.get_var("config-path") { Some(Value { @@ -60,7 +59,7 @@ pub async fn set_into(args: CommandArgs) -> Result { let mut result = nu_data::config::read(&name, &path)?; - let rows: Vec = input.collect().await; + let rows: Vec = input.collect(); let key = v.to_string(); Ok(if rows.is_empty() { @@ -78,8 +77,7 @@ pub async fn set_into(args: CommandArgs) -> Result { config::write(&result, &path)?; ctx.reload_config(&ConfigPath::Global( path.expect("Global config path is always some"), - )) - .await?; + ))?; OutputStream::one(ReturnSuccess::value( UntaggedValue::Row(result.into()).into_value(name), @@ -93,8 +91,7 @@ pub async fn set_into(args: CommandArgs) -> Result { config::write(&result, &path)?; ctx.reload_config(&ConfigPath::Global( path.expect("Global config path is always some"), - )) - .await?; + ))?; OutputStream::one(ReturnSuccess::value( UntaggedValue::Row(result.into()).into_value(name), diff --git a/crates/nu-command/src/commands/cp.rs b/crates/nu-command/src/commands/cp.rs index 3eedefc906..36cbe83985 100644 --- a/crates/nu-command/src/commands/cp.rs +++ b/crates/nu-command/src/commands/cp.rs @@ -5,7 +5,6 @@ use nu_protocol::{Signature, SyntaxShape}; pub struct Cpy; -#[async_trait] impl WholeStreamCommand for Cpy { fn name(&self) -> &str { "cp" @@ -26,10 +25,10 @@ impl WholeStreamCommand for Cpy { "Copy files." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let shell_manager = args.shell_manager.clone(); let name = args.call_info.name_tag.clone(); - let (args, _) = args.process().await?; + let (args, _) = args.process()?; shell_manager.cp(args, name) } diff --git a/crates/nu-command/src/commands/date/command.rs b/crates/nu-command/src/commands/date/command.rs index 3503cf627e..4342ead8f9 100644 --- a/crates/nu-command/src/commands/date/command.rs +++ b/crates/nu-command/src/commands/date/command.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "date" @@ -19,7 +18,7 @@ impl WholeStreamCommand for Command { "Apply date function." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(get_full_help(&Command, &args.scope)).into_value(Tag::unknown()), ))) diff --git a/crates/nu-command/src/commands/date/format.rs b/crates/nu-command/src/commands/date/format.rs index 01893da306..83f1b63269 100644 --- a/crates/nu-command/src/commands/date/format.rs +++ b/crates/nu-command/src/commands/date/format.rs @@ -15,7 +15,6 @@ pub struct FormatArgs { table: bool, } -#[async_trait] impl WholeStreamCommand for Date { fn name(&self) -> &str { "date format" @@ -31,8 +30,8 @@ impl WholeStreamCommand for Date { "Format a given date using the given format string." } - async fn run(&self, args: CommandArgs) -> Result { - format(args).await + fn run(&self, args: CommandArgs) -> Result { + format(args) } fn examples(&self) -> Vec { @@ -51,9 +50,9 @@ impl WholeStreamCommand for Date { } } -pub async fn format(args: CommandArgs) -> Result { +pub fn format(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (FormatArgs { format, table }, input) = args.process().await?; + let (FormatArgs { format, table }, input) = args.process()?; Ok(input .map(move |value| match value { diff --git a/crates/nu-command/src/commands/date/list_timezone.rs b/crates/nu-command/src/commands/date/list_timezone.rs index 9c5d47b3ec..b472d6ec6f 100644 --- a/crates/nu-command/src/commands/date/list_timezone.rs +++ b/crates/nu-command/src/commands/date/list_timezone.rs @@ -7,7 +7,6 @@ use nu_protocol::{Dictionary, ReturnSuccess, Signature, UntaggedValue}; pub struct Date; -#[async_trait] impl WholeStreamCommand for Date { fn name(&self) -> &str { "date list-timezone" @@ -21,8 +20,8 @@ impl WholeStreamCommand for Date { "List supported time zones." } - async fn run(&self, args: CommandArgs) -> Result { - list_timezone(args).await + fn run(&self, args: CommandArgs) -> Result { + list_timezone(args) } fn examples(&self) -> Vec { @@ -41,8 +40,8 @@ impl WholeStreamCommand for Date { } } -async fn list_timezone(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn list_timezone(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.call_info.name_tag.clone(); let list = TZ_VARIANTS.iter().map(move |tz| { @@ -58,7 +57,7 @@ async fn list_timezone(args: CommandArgs) -> Result { )) }); - Ok(futures::stream::iter(list).to_output_stream()) + Ok(list.into_iter().to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/date/now.rs b/crates/nu-command/src/commands/date/now.rs index 386dd964d9..063fe3f532 100644 --- a/crates/nu-command/src/commands/date/now.rs +++ b/crates/nu-command/src/commands/date/now.rs @@ -6,7 +6,6 @@ use nu_protocol::{Signature, UntaggedValue}; pub struct Date; -#[async_trait] impl WholeStreamCommand for Date { fn name(&self) -> &str { "date now" @@ -20,13 +19,13 @@ impl WholeStreamCommand for Date { "Get the current date." } - async fn run(&self, args: CommandArgs) -> Result { - now(args).await + fn run(&self, args: CommandArgs) -> Result { + now(args) } } -pub async fn now(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +pub fn now(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.call_info.name_tag.clone(); let now: DateTime = Local::now(); diff --git a/crates/nu-command/src/commands/date/to_table.rs b/crates/nu-command/src/commands/date/to_table.rs index 656c8133fa..917933a801 100644 --- a/crates/nu-command/src/commands/date/to_table.rs +++ b/crates/nu-command/src/commands/date/to_table.rs @@ -7,7 +7,6 @@ use nu_protocol::{Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue pub struct Date; -#[async_trait] impl WholeStreamCommand for Date { fn name(&self) -> &str { "date to-table" @@ -21,8 +20,8 @@ impl WholeStreamCommand for Date { "Print the date in a structured table." } - async fn run(&self, args: CommandArgs) -> Result { - to_table(args).await + fn run(&self, args: CommandArgs) -> Result { + to_table(args) } fn examples(&self) -> Vec { @@ -34,8 +33,8 @@ impl WholeStreamCommand for Date { } } -async fn to_table(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn to_table(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.call_info.name_tag.clone(); let input = args.input; diff --git a/crates/nu-command/src/commands/date/to_timezone.rs b/crates/nu-command/src/commands/date/to_timezone.rs index fc6cbfce14..0a363c3e1b 100644 --- a/crates/nu-command/src/commands/date/to_timezone.rs +++ b/crates/nu-command/src/commands/date/to_timezone.rs @@ -12,7 +12,6 @@ struct DateToTimeZoneArgs { timezone: Tagged, } -#[async_trait] impl WholeStreamCommand for Date { fn name(&self) -> &str { "date to-timezone" @@ -34,8 +33,8 @@ impl WholeStreamCommand for Date { "Use 'date list-timezone' to list all supported time zones." } - async fn run(&self, args: CommandArgs) -> Result { - to_timezone(args).await + fn run(&self, args: CommandArgs) -> Result { + to_timezone(args) } fn examples(&self) -> Vec { @@ -59,9 +58,9 @@ impl WholeStreamCommand for Date { } } -async fn to_timezone(args: CommandArgs) -> Result { +fn to_timezone(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (DateToTimeZoneArgs { timezone }, input) = args.process().await?; + let (DateToTimeZoneArgs { timezone }, input) = args.process()?; Ok(input .map(move |value| match value { diff --git a/crates/nu-command/src/commands/date/utc.rs b/crates/nu-command/src/commands/date/utc.rs index d5f2d81b36..af222f59bb 100644 --- a/crates/nu-command/src/commands/date/utc.rs +++ b/crates/nu-command/src/commands/date/utc.rs @@ -8,7 +8,6 @@ use nu_protocol::Signature; pub struct Date; -#[async_trait] impl WholeStreamCommand for Date { fn name(&self) -> &str { "date utc" @@ -22,13 +21,13 @@ impl WholeStreamCommand for Date { "return the current date in utc." } - async fn run(&self, args: CommandArgs) -> Result { - utc(args).await + fn run(&self, args: CommandArgs) -> Result { + utc(args) } } -pub async fn utc(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +pub fn utc(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.call_info.name_tag.clone(); let no_fmt = "".to_string(); diff --git a/crates/nu-command/src/commands/debug.rs b/crates/nu-command/src/commands/debug.rs index aad906971c..807272ac85 100644 --- a/crates/nu-command/src/commands/debug.rs +++ b/crates/nu-command/src/commands/debug.rs @@ -10,7 +10,6 @@ pub struct DebugArgs { raw: bool, } -#[async_trait] impl WholeStreamCommand for Debug { fn name(&self) -> &str { "debug" @@ -24,13 +23,13 @@ impl WholeStreamCommand for Debug { "Print the Rust debug representation of the values." } - async fn run(&self, args: CommandArgs) -> Result { - debug_value(args).await + fn run(&self, args: CommandArgs) -> Result { + debug_value(args) } } -async fn debug_value(args: CommandArgs) -> Result { - let (DebugArgs { raw }, input) = args.process().await?; +fn debug_value(args: CommandArgs) -> Result { + let (DebugArgs { raw }, input) = args.process()?; Ok(input .map(move |v| { if raw { diff --git a/crates/nu-command/src/commands/def.rs b/crates/nu-command/src/commands/def.rs index e503d270c7..0a5b9424be 100644 --- a/crates/nu-command/src/commands/def.rs +++ b/crates/nu-command/src/commands/def.rs @@ -14,7 +14,6 @@ pub struct DefArgs { pub block: CapturedBlock, } -#[async_trait] impl WholeStreamCommand for Def { fn name(&self) -> &str { "def" @@ -35,7 +34,7 @@ impl WholeStreamCommand for Def { "Create a command and set it to a definition." } - async fn run(&self, _args: CommandArgs) -> Result { + fn run(&self, _args: CommandArgs) -> Result { // Currently, we don't do anything here because we should have already // installed the definition as we entered the scope // We just create a command so that we can get proper coloring diff --git a/crates/nu-command/src/commands/default.rs b/crates/nu-command/src/commands/default.rs index a0f2d77ffb..3e12781ce0 100644 --- a/crates/nu-command/src/commands/default.rs +++ b/crates/nu-command/src/commands/default.rs @@ -13,7 +13,6 @@ struct DefaultArgs { pub struct Default; -#[async_trait] impl WholeStreamCommand for Default { fn name(&self) -> &str { "default" @@ -33,8 +32,8 @@ impl WholeStreamCommand for Default { "Sets a default row's column if missing." } - async fn run(&self, args: CommandArgs) -> Result { - default(args).await + fn run(&self, args: CommandArgs) -> Result { + default(args) } fn examples(&self) -> Vec { @@ -46,8 +45,8 @@ impl WholeStreamCommand for Default { } } -async fn default(args: CommandArgs) -> Result { - let (DefaultArgs { column, value }, input) = args.process().await?; +fn default(args: CommandArgs) -> Result { + let (DefaultArgs { column, value }, input) = args.process()?; Ok(input .map(move |item| { diff --git a/crates/nu-command/src/commands/describe.rs b/crates/nu-command/src/commands/describe.rs index 64a69dc4c5..98223172ab 100644 --- a/crates/nu-command/src/commands/describe.rs +++ b/crates/nu-command/src/commands/describe.rs @@ -9,7 +9,6 @@ pub struct Describe; #[derive(Deserialize)] pub struct DescribeArgs {} -#[async_trait] impl WholeStreamCommand for Describe { fn name(&self) -> &str { "describe" @@ -23,12 +22,12 @@ impl WholeStreamCommand for Describe { "Describes the objects in the stream." } - async fn run(&self, args: CommandArgs) -> Result { - describe(args).await + fn run(&self, args: CommandArgs) -> Result { + describe(args) } } -pub async fn describe(args: CommandArgs) -> Result { +pub fn describe(args: CommandArgs) -> Result { Ok(args .input .map(|row| { diff --git a/crates/nu-command/src/commands/do_.rs b/crates/nu-command/src/commands/do_.rs index ae7a1c10c4..3ba98b4ae4 100644 --- a/crates/nu-command/src/commands/do_.rs +++ b/crates/nu-command/src/commands/do_.rs @@ -12,7 +12,6 @@ struct DoArgs { ignore_errors: bool, } -#[async_trait] impl WholeStreamCommand for Do { fn name(&self) -> &str { "do" @@ -32,8 +31,8 @@ impl WholeStreamCommand for Do { "Runs a block, optionally ignoring errors." } - async fn run(&self, args: CommandArgs) -> Result { - do_(args).await + fn run(&self, args: CommandArgs) -> Result { + do_(args) } fn examples(&self) -> Vec { @@ -52,7 +51,7 @@ impl WholeStreamCommand for Do { } } -async fn do_(raw_args: CommandArgs) -> Result { +fn do_(raw_args: CommandArgs) -> Result { let external_redirection = raw_args.call_info.args.external_redirection; let context = EvaluationContext::from_args(&raw_args); @@ -62,7 +61,7 @@ async fn do_(raw_args: CommandArgs) -> Result { mut block, }, input, - ) = raw_args.process().await?; + ) = raw_args.process()?; let block_redirection = match external_redirection { ExternalRedirection::None => { @@ -84,7 +83,7 @@ async fn do_(raw_args: CommandArgs) -> Result { block.block.set_redirect(block_redirection); context.scope.enter_scope(); - let result = run_block(&block.block, &context, input).await; + let result = run_block(&block.block, &context, input); context.scope.exit_scope(); if ignore_errors { @@ -93,9 +92,9 @@ async fn do_(raw_args: CommandArgs) -> Result { match result { Ok(mut stream) => { - let output = stream.drain_vec().await; + let output = stream.drain_vec(); context.clear_errors(); - Ok(futures::stream::iter(output).to_output_stream()) + Ok(output.into_iter().to_output_stream()) } Err(_) => Ok(OutputStream::empty()), } diff --git a/crates/nu-command/src/commands/drop/column.rs b/crates/nu-command/src/commands/drop/column.rs index 9c3aff1fa1..5a525eb570 100644 --- a/crates/nu-command/src/commands/drop/column.rs +++ b/crates/nu-command/src/commands/drop/column.rs @@ -12,7 +12,6 @@ pub struct Arguments { columns: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "drop column" @@ -30,12 +29,12 @@ impl WholeStreamCommand for SubCommand { "Remove the last number of columns. If you want to remove columns by name, try 'reject'." } - async fn run(&self, args: CommandArgs) -> Result { - drop(args).await + fn run(&self, args: CommandArgs) -> Result { + drop(args) } fn examples(&self) -> Vec { - use nu_protocol::{row, Value}; + use nu_protocol::Value; vec![Example { description: "Remove the last column of a table", @@ -48,8 +47,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn drop(args: CommandArgs) -> Result { - let (Arguments { columns }, input) = args.process().await?; +fn drop(args: CommandArgs) -> Result { + let (Arguments { columns }, input) = args.process()?; let to_drop = if let Some(quantity) = columns { *quantity as usize diff --git a/crates/nu-command/src/commands/drop/command.rs b/crates/nu-command/src/commands/drop/command.rs index 543a762566..02bc237dfb 100644 --- a/crates/nu-command/src/commands/drop/command.rs +++ b/crates/nu-command/src/commands/drop/command.rs @@ -11,7 +11,6 @@ pub struct Arguments { rows: Option>, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "drop" @@ -29,8 +28,8 @@ impl WholeStreamCommand for Command { "Remove the last number of rows or columns." } - async fn run(&self, args: CommandArgs) -> Result { - drop(args).await + fn run(&self, args: CommandArgs) -> Result { + drop(args) } fn examples(&self) -> Vec { @@ -52,9 +51,9 @@ impl WholeStreamCommand for Command { } } -async fn drop(args: CommandArgs) -> Result { - let (Arguments { rows }, input) = args.process().await?; - let v: Vec<_> = input.into_vec().await; +fn drop(args: CommandArgs) -> Result { + let (Arguments { rows }, input) = args.process()?; + let v: Vec<_> = input.into_vec(); let rows_to_drop = if let Some(quantity) = rows { *quantity as usize @@ -63,7 +62,7 @@ async fn drop(args: CommandArgs) -> Result { }; Ok(if rows_to_drop == 0 { - futures::stream::iter(v).to_output_stream() + v.into_iter().to_output_stream() } else { let k = if v.len() < rows_to_drop { 0 @@ -73,6 +72,6 @@ async fn drop(args: CommandArgs) -> Result { let iter = v.into_iter().take(k); - futures::stream::iter(iter).to_output_stream() + iter.to_output_stream() }) } diff --git a/crates/nu-command/src/commands/du.rs b/crates/nu-command/src/commands/du.rs index cb89b0948e..0d3a65a827 100644 --- a/crates/nu-command/src/commands/du.rs +++ b/crates/nu-command/src/commands/du.rs @@ -28,7 +28,6 @@ pub struct DuArgs { min_size: Option>, } -#[async_trait] impl WholeStreamCommand for Du { fn name(&self) -> &str { NAME @@ -71,8 +70,8 @@ impl WholeStreamCommand for Du { "Find disk usage sizes of specified items." } - async fn run(&self, args: CommandArgs) -> Result { - du(args).await + fn run(&self, args: CommandArgs) -> Result { + du(args) } fn examples(&self) -> Vec { @@ -84,12 +83,12 @@ impl WholeStreamCommand for Du { } } -async fn du(args: CommandArgs) -> Result { +fn du(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ctrl_c = args.ctrl_c.clone(); let ctrl_c_copy = ctrl_c.clone(); - let (args, _): (DuArgs, _) = args.process().await?; + let (args, _): (DuArgs, _) = args.process()?; let exclude = args.exclude.map_or(Ok(None), move |x| { Pattern::new(&x.item) .map(Option::Some) @@ -131,7 +130,7 @@ async fn du(args: CommandArgs) -> Result { all, }; - let inp = futures::stream::iter(paths); + let inp = paths; Ok(inp .flat_map(move |path| match path { @@ -146,9 +145,9 @@ async fn du(args: CommandArgs) -> Result { output.push(Ok(ReturnSuccess::Value(v.into()))); } } - futures::stream::iter(output) + output } - Err(e) => futures::stream::iter(vec![Err(e)]), + Err(e) => vec![Err(e)], }) .interruptible(ctrl_c_copy) .to_output_stream()) diff --git a/crates/nu-command/src/commands/each/command.rs b/crates/nu-command/src/commands/each/command.rs index db1649e0c0..41df256ed9 100644 --- a/crates/nu-command/src/commands/each/command.rs +++ b/crates/nu-command/src/commands/each/command.rs @@ -2,7 +2,6 @@ use crate::prelude::*; use nu_engine::run_block; use nu_engine::WholeStreamCommand; -use futures::stream::once; use nu_errors::ShellError; use nu_protocol::{ hir::CapturedBlock, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value, @@ -17,7 +16,6 @@ pub struct EachArgs { numbered: Tagged, } -#[async_trait] impl WholeStreamCommand for Each { fn name(&self) -> &str { "each" @@ -37,8 +35,8 @@ impl WholeStreamCommand for Each { "Run a block on each row of the table." } - async fn run(&self, args: CommandArgs) -> Result { - each(args).await + fn run(&self, args: CommandArgs) -> Result { + each(args) } fn examples(&self) -> Vec { @@ -67,7 +65,7 @@ impl WholeStreamCommand for Each { } } -pub async fn process_row( +pub fn process_row( captured_block: Arc>, context: Arc, input: Value, @@ -80,7 +78,7 @@ pub async fn process_row( let input_stream = if !captured_block.block.params.positional.is_empty() { InputStream::empty() } else { - once(async { Ok(input_clone) }).to_input_stream() + vec![Ok(input_clone)].into_iter().to_input_stream() }; context.scope.enter_scope(); @@ -95,7 +93,7 @@ pub async fn process_row( context.scope.add_var("$it", input); } - let result = run_block(&captured_block.block, &*context, input_stream).await; + let result = run_block(&captured_block.block, &*context, input_stream); context.scope.exit_scope(); @@ -110,40 +108,36 @@ pub(crate) fn make_indexed_item(index: usize, item: Value) -> Value { dict.into_value() } -async fn each(raw_args: CommandArgs) -> Result { +fn each(raw_args: CommandArgs) -> Result { let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let (each_args, input): (EachArgs, _) = raw_args.process().await?; + let (each_args, input): (EachArgs, _) = raw_args.process()?; let block = Arc::new(Box::new(each_args.block)); if each_args.numbered.item { Ok(input .enumerate() - .then(move |input| { + .map(move |input| { let block = block.clone(); let context = context.clone(); let row = make_indexed_item(input.0, input.1); - async { - match process_row(block, context, row).await { - Ok(s) => s, - Err(e) => OutputStream::one(Err(e)), - } + match process_row(block, context, row) { + Ok(s) => s, + Err(e) => OutputStream::one(Err(e)), } }) .flatten() .to_output_stream()) } else { Ok(input - .then(move |input| { + .map(move |input| { let block = block.clone(); let context = context.clone(); - async { - match process_row(block, context, input).await { - Ok(s) => s, - Err(e) => OutputStream::one(Err(e)), - } + match process_row(block, context, input) { + Ok(s) => s, + Err(e) => OutputStream::one(Err(e)), } }) .flatten() diff --git a/crates/nu-command/src/commands/each/group.rs b/crates/nu-command/src/commands/each/group.rs index bd3d6fadd5..05c46f8cd2 100644 --- a/crates/nu-command/src/commands/each/group.rs +++ b/crates/nu-command/src/commands/each/group.rs @@ -17,7 +17,6 @@ pub struct EachGroupArgs { //numbered: Tagged, } -#[async_trait] impl WholeStreamCommand for EachGroup { fn name(&self) -> &str { "each group" @@ -45,16 +44,54 @@ impl WholeStreamCommand for EachGroup { }] } - async fn run(&self, raw_args: CommandArgs) -> Result { + fn run(&self, raw_args: CommandArgs) -> Result { let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let (each_args, input): (EachGroupArgs, _) = raw_args.process().await?; + let (each_args, input): (EachGroupArgs, _) = raw_args.process()?; let block = Arc::new(Box::new(each_args.block)); - Ok(input - .chunks(each_args.group_size.item) - .then(move |input| run_block_on_vec(input, block.clone(), context.clone())) - .flatten() - .to_output_stream()) + let each_group_iterator = EachGroupIterator { + block, + context, + group_size: each_args.group_size.item, + input, + }; + + Ok(each_group_iterator.flatten().to_output_stream()) + } +} + +struct EachGroupIterator { + block: Arc>, + context: Arc, + group_size: usize, + input: InputStream, +} + +impl Iterator for EachGroupIterator { + type Item = OutputStream; + + fn next(&mut self) -> Option { + let mut group = vec![]; + let mut current_count = 0; + + while let Some(next) = self.input.next() { + group.push(next); + + current_count += 1; + if current_count >= self.group_size { + break; + } + } + + if group.is_empty() { + return None; + } + + Some(run_block_on_vec( + group, + self.block.clone(), + self.context.clone(), + )) } } @@ -62,43 +99,43 @@ pub(crate) fn run_block_on_vec( input: Vec, block: Arc>, context: Arc, -) -> impl Future { +) -> OutputStream { let value = Value { value: UntaggedValue::Table(input), tag: Tag::unknown(), }; - async { - match process_row(block, context, value).await { - Ok(s) => { - // We need to handle this differently depending on whether process_row - // returned just 1 value or if it returned multiple as a stream. - let vec = s.collect::>().await; + match process_row(block, context, value) { + Ok(s) => { + // We need to handle this differently depending on whether process_row + // returned just 1 value or if it returned multiple as a stream. + let vec = s.collect::>(); - // If it returned just one value, just take that value - if vec.len() == 1 { - return OutputStream::one(vec.into_iter().next().expect( - "This should be impossible, we just checked that vec.len() == 1.", - )); - } - - // If it returned multiple values, we need to put them into a table and - // return that. - let result = vec.into_iter().collect::, _>>(); - let result_table = match result { - Ok(t) => t, - Err(e) => return OutputStream::one(Err(e)), - }; - - let table = result_table - .into_iter() - .filter_map(|x| x.raw_value()) - .collect(); - - OutputStream::one(Ok(ReturnSuccess::Value(UntaggedValue::Table(table).into()))) + // If it returned just one value, just take that value + if vec.len() == 1 { + return OutputStream::one( + vec.into_iter() + .next() + .expect("This should be impossible, we just checked that vec.len() == 1."), + ); } - Err(e) => OutputStream::one(Err(e)), + + // If it returned multiple values, we need to put them into a table and + // return that. + let result = vec.into_iter().collect::, _>>(); + let result_table = match result { + Ok(t) => t, + Err(e) => return OutputStream::one(Err(e)), + }; + + let table = result_table + .into_iter() + .filter_map(|x| x.raw_value()) + .collect(); + + OutputStream::one(Ok(ReturnSuccess::Value(UntaggedValue::Table(table).into()))) } + Err(e) => OutputStream::one(Err(e)), } } diff --git a/crates/nu-command/src/commands/each/window.rs b/crates/nu-command/src/commands/each/window.rs index 1e8ad90f21..b008b351c8 100644 --- a/crates/nu-command/src/commands/each/window.rs +++ b/crates/nu-command/src/commands/each/window.rs @@ -16,7 +16,6 @@ pub struct EachWindowArgs { stride: Option>, } -#[async_trait] impl WholeStreamCommand for EachWindow { fn name(&self) -> &str { "each window" @@ -50,16 +49,15 @@ impl WholeStreamCommand for EachWindow { }] } - async fn run(&self, raw_args: CommandArgs) -> Result { + fn run(&self, raw_args: CommandArgs) -> Result { let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let (each_args, mut input): (EachWindowArgs, _) = raw_args.process().await?; + let (each_args, mut input): (EachWindowArgs, _) = raw_args.process()?; let block = Arc::new(Box::new(each_args.block)); let mut window: Vec<_> = input .by_ref() .take(*each_args.window_size - 1) - .collect::>() - .await; + .collect::>(); // `window` must start with dummy values, which will be removed on the first iteration let stride = each_args.stride.map(|x| *x).unwrap_or(1); @@ -67,7 +65,7 @@ impl WholeStreamCommand for EachWindow { Ok(input .enumerate() - .then(move |(i, input)| { + .map(move |(i, input)| { // This would probably be more efficient if `last` was a VecDeque // But we can't have that because it needs to be put into a Table window.remove(0); @@ -77,15 +75,13 @@ impl WholeStreamCommand for EachWindow { let context = context.clone(); let local_window = window.clone(); - async move { - if i % stride == 0 { - Some(run_block_on_vec(local_window, block, context).await) - } else { - None - } + if i % stride == 0 { + Some(run_block_on_vec(local_window, block, context)) + } else { + None } }) - .filter_map(|x| async { x }) + .filter_map(|x| x) .flatten() .to_output_stream()) } diff --git a/crates/nu-command/src/commands/echo.rs b/crates/nu-command/src/commands/echo.rs index 822964755a..825ad0aefa 100644 --- a/crates/nu-command/src/commands/echo.rs +++ b/crates/nu-command/src/commands/echo.rs @@ -13,7 +13,6 @@ pub struct EchoArgs { pub rest: Vec, } -#[async_trait] impl WholeStreamCommand for Echo { fn name(&self) -> &str { "echo" @@ -27,8 +26,8 @@ impl WholeStreamCommand for Echo { "Echo the arguments back to the user." } - async fn run(&self, args: CommandArgs) -> Result { - echo(args).await + fn run(&self, args: CommandArgs) -> Result { + echo(args) } fn examples(&self) -> Vec { @@ -47,8 +46,8 @@ impl WholeStreamCommand for Echo { } } -async fn echo(args: CommandArgs) -> Result { - let (args, _): (EchoArgs, _) = args.process().await?; +fn echo(args: CommandArgs) -> Result { + let (args, _): (EchoArgs, _) = args.process()?; let stream = args.rest.into_iter().map(|i| match i.as_string() { Ok(s) => OutputStream::one(Ok(ReturnSuccess::Value( @@ -58,17 +57,19 @@ async fn echo(args: CommandArgs) -> Result { Value { value: UntaggedValue::Table(table), .. - } => futures::stream::iter(table.into_iter().map(ReturnSuccess::value)) + } => table + .into_iter() + .map(ReturnSuccess::value) .to_output_stream(), Value { value: UntaggedValue::Primitive(Primitive::Range(range)), tag, - } => futures::stream::iter(RangeIterator::new(*range, tag)).to_output_stream(), + } => RangeIterator::new(*range, tag).to_output_stream(), x => OutputStream::one(Ok(ReturnSuccess::Value(x))), }, }); - Ok(futures::stream::iter(stream).flatten().to_output_stream()) + Ok(stream.flatten().to_output_stream()) } struct RangeIterator { diff --git a/crates/nu-command/src/commands/empty.rs b/crates/nu-command/src/commands/empty.rs index 96334349f3..e2a6978714 100644 --- a/crates/nu-command/src/commands/empty.rs +++ b/crates/nu-command/src/commands/empty.rs @@ -8,7 +8,6 @@ use nu_protocol::{ }; use crate::utils::arguments::arguments; -use futures::stream::once; use nu_value_ext::{as_string, ValueExt}; #[derive(Deserialize)] @@ -18,7 +17,6 @@ pub struct Arguments { pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "empty?" @@ -35,8 +33,8 @@ impl WholeStreamCommand for Command { "Check for empty values." } - async fn run(&self, args: CommandArgs) -> Result { - is_empty(args).await + fn run(&self, args: CommandArgs) -> Result { + is_empty(args) } fn examples(&self) -> Vec { @@ -81,32 +79,28 @@ impl WholeStreamCommand for Command { } } -async fn is_empty(args: CommandArgs) -> Result { +fn is_empty(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let name_tag = Arc::new(args.call_info.name_tag.clone()); let context = Arc::new(EvaluationContext::from_args(&args)); - let (Arguments { mut rest }, input) = args.process().await?; + let (Arguments { mut rest }, input) = args.process()?; let (columns, default_block): (Vec, Option>) = arguments(&mut rest)?; let default_block = Arc::new(default_block); if input.is_empty() { - let stream = futures::stream::iter(vec![ - UntaggedValue::Primitive(Primitive::Nothing).into_value(tag) - ]); + let stream = vec![UntaggedValue::Primitive(Primitive::Nothing).into_value(tag)].into_iter(); return Ok(InputStream::from_stream(stream) - .then(move |input| { + .map(move |input| { let tag = name_tag.clone(); let context = context.clone(); let block = default_block.clone(); let columns = vec![]; - async { - match process_row(context, input, block, columns, tag).await { - Ok(s) => s, - Err(e) => OutputStream::one(Err(e)), - } + match process_row(context, input, block, columns, tag) { + Ok(s) => s, + Err(e) => OutputStream::one(Err(e)), } }) .flatten() @@ -114,24 +108,22 @@ async fn is_empty(args: CommandArgs) -> Result { } Ok(input - .then(move |input| { + .map(move |input| { let tag = name_tag.clone(); let context = context.clone(); let block = default_block.clone(); let columns = columns.clone(); - async { - match process_row(context, input, block, columns, tag).await { - Ok(s) => s, - Err(e) => OutputStream::one(Err(e)), - } + match process_row(context, input, block, columns, tag) { + Ok(s) => s, + Err(e) => OutputStream::one(Err(e)), } }) .flatten() .to_output_stream()) } -async fn process_row( +fn process_row( context: Arc, input: Value, default_block: Arc>>, @@ -144,18 +136,18 @@ async fn process_row( if let Some(default_block) = &*default_block { let for_block = input.clone(); - let input_stream = once(async { Ok(for_block) }).to_input_stream(); + let input_stream = vec![Ok(for_block)].into_iter().to_input_stream(); context.scope.enter_scope(); context.scope.add_vars(&default_block.captured.entries); context.scope.add_var("$it", input.clone()); - let stream = run_block(&default_block.block, &*context, input_stream).await; + let stream = run_block(&default_block.block, &*context, input_stream); context.scope.exit_scope(); let mut stream = stream?; *results = Some({ - let values = stream.drain_vec().await; + let values = stream.drain_vec(); let errors = context.get_errors(); diff --git a/crates/nu-command/src/commands/enter.rs b/crates/nu-command/src/commands/enter.rs index bb22220172..0789b36fb3 100644 --- a/crates/nu-command/src/commands/enter.rs +++ b/crates/nu-command/src/commands/enter.rs @@ -17,7 +17,6 @@ pub struct EnterArgs { encoding: Option>, } -#[async_trait] impl WholeStreamCommand for Enter { fn name(&self) -> &str { "enter" @@ -51,8 +50,8 @@ For a more complete list of encodings please refer to the encoding_rs documentation link at https://docs.rs/encoding_rs/0.8.28/encoding_rs/#statics"# } - async fn run(&self, args: CommandArgs) -> Result { - enter(args).await + fn run(&self, args: CommandArgs) -> Result { + enter(args) } fn examples(&self) -> Vec { @@ -76,7 +75,7 @@ documentation link at https://docs.rs/encoding_rs/0.8.28/encoding_rs/#statics"# } } -async fn enter(raw_args: CommandArgs) -> Result { +fn enter(raw_args: CommandArgs) -> Result { let scope = raw_args.scope.clone(); let shell_manager = raw_args.shell_manager.clone(); let head = raw_args.call_info.args.head.clone(); @@ -85,13 +84,12 @@ async fn enter(raw_args: CommandArgs) -> Result { let current_errors = raw_args.current_errors.clone(); let host = raw_args.host.clone(); let tag = raw_args.call_info.name_tag.clone(); - let (EnterArgs { location, encoding }, _) = raw_args.process().await?; + let (EnterArgs { location, encoding }, _) = raw_args.process()?; let location_string = location.display().to_string(); - let location_clone = location_string.clone(); if location.is_dir() { Ok(OutputStream::one(ReturnSuccess::action( - CommandAction::EnterShell(location_clone), + CommandAction::EnterShell(location_string), ))) } else { // If it's a file, attempt to open the file as a value and enter it @@ -102,11 +100,10 @@ async fn enter(raw_args: CommandArgs) -> Result { let (file_extension, tagged_contents) = crate::commands::open::fetch( &full_path, - &PathBuf::from(location_clone), + &PathBuf::from(location_string), span, encoding, - ) - .await?; + )?; match tagged_contents.value { UntaggedValue::Primitive(Primitive::String(_)) => { @@ -127,18 +124,17 @@ async fn enter(raw_args: CommandArgs) -> Result { span: Span::unknown(), external_redirection: ExternalRedirection::Stdout, }, - name_tag: tag.clone(), + name_tag: tag, }, - scope: scope.clone(), + scope, }; let tag = tagged_contents.tag.clone(); - let mut result = converter - .run(new_args.with_input(vec![tagged_contents])) - .await?; - let result_vec: Vec> = - result.drain_vec().await; - Ok(futures::stream::iter(result_vec.into_iter().map( - move |res| match res { + let mut result = + converter.run(new_args.with_input(vec![tagged_contents]))?; + let result_vec: Vec> = result.drain_vec(); + Ok(result_vec + .into_iter() + .map(move |res| match res { Ok(ReturnSuccess::Value(Value { value, .. })) => Ok( ReturnSuccess::Action(CommandAction::EnterValueShell(Value { value, @@ -146,9 +142,8 @@ async fn enter(raw_args: CommandArgs) -> Result { })), ), x => x, - }, - )) - .to_output_stream()) + }) + .to_output_stream()) } else { Ok(OutputStream::one(ReturnSuccess::action( CommandAction::EnterValueShell(tagged_contents), diff --git a/crates/nu-command/src/commands/every.rs b/crates/nu-command/src/commands/every.rs index d5a716ba88..c7124b0343 100644 --- a/crates/nu-command/src/commands/every.rs +++ b/crates/nu-command/src/commands/every.rs @@ -12,7 +12,6 @@ pub struct EveryArgs { skip: Tagged, } -#[async_trait] impl WholeStreamCommand for Every { fn name(&self) -> &str { "every" @@ -36,8 +35,8 @@ impl WholeStreamCommand for Every { "Show (or skip) every n-th row, starting from the first one." } - async fn run(&self, args: CommandArgs) -> Result { - every(args).await + fn run(&self, args: CommandArgs) -> Result { + every(args) } fn examples(&self) -> Vec { @@ -63,15 +62,15 @@ impl WholeStreamCommand for Every { } } -async fn every(args: CommandArgs) -> Result { - let (EveryArgs { stride, skip }, input) = args.process().await?; +fn every(args: CommandArgs) -> Result { + let (EveryArgs { stride, skip }, input) = args.process()?; let stride = stride.item; let skip = skip.item; Ok(input .enumerate() - .filter_map(move |(i, value)| async move { + .filter_map(move |(i, value)| { let stride_desired = if stride < 1 { 1 } else { stride } as usize; let should_include = skip == (i % stride_desired != 0); diff --git a/crates/nu-command/src/commands/exec.rs b/crates/nu-command/src/commands/exec.rs index 524e0734b2..c7ef5af13e 100644 --- a/crates/nu-command/src/commands/exec.rs +++ b/crates/nu-command/src/commands/exec.rs @@ -13,7 +13,6 @@ pub struct ExecArgs { pub rest: Vec>, } -#[async_trait] impl WholeStreamCommand for Exec { fn name(&self) -> &str { "exec" @@ -32,8 +31,8 @@ impl WholeStreamCommand for Exec { "Execute command." } - async fn run(&self, args: CommandArgs) -> Result { - exec(args).await + fn run(&self, args: CommandArgs) -> Result { + exec(args) } fn examples(&self) -> Vec { @@ -53,12 +52,12 @@ impl WholeStreamCommand for Exec { } #[cfg(unix)] -async fn exec(args: CommandArgs) -> Result { +fn exec(args: CommandArgs) -> Result { use std::os::unix::process::CommandExt; use std::process::Command; let name = args.call_info.name_tag.clone(); - let (args, _): (ExecArgs, _) = args.process().await?; + let (args, _): (ExecArgs, _) = args.process()?; let mut command = Command::new(args.command.item); for tagged_arg in args.rest { @@ -75,7 +74,7 @@ async fn exec(args: CommandArgs) -> Result { } #[cfg(not(unix))] -async fn exec(args: CommandArgs) -> Result { +fn exec(args: CommandArgs) -> Result { Err(ShellError::labeled_error( "Error on exec", "exec is not supported on your platform", diff --git a/crates/nu-command/src/commands/exit.rs b/crates/nu-command/src/commands/exit.rs index 3d68e484c3..9b10ebed3f 100644 --- a/crates/nu-command/src/commands/exit.rs +++ b/crates/nu-command/src/commands/exit.rs @@ -4,7 +4,6 @@ use nu_protocol::{CommandAction, ReturnSuccess, Signature, SyntaxShape}; pub struct Exit; -#[async_trait] impl WholeStreamCommand for Exit { fn name(&self) -> &str { "exit" @@ -24,8 +23,8 @@ impl WholeStreamCommand for Exit { "Exit the current shell (or all shells)." } - async fn run(&self, args: CommandArgs) -> Result { - exit(args).await + fn run(&self, args: CommandArgs) -> Result { + exit(args) } fn examples(&self) -> Vec { @@ -44,8 +43,8 @@ impl WholeStreamCommand for Exit { } } -pub async fn exit(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +pub fn exit(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let code = if let Some(value) = args.call_info.args.nth(0) { value.as_i32()? diff --git a/crates/nu-command/src/commands/first.rs b/crates/nu-command/src/commands/first.rs index 3ba376c2c3..e437473b0d 100644 --- a/crates/nu-command/src/commands/first.rs +++ b/crates/nu-command/src/commands/first.rs @@ -11,7 +11,6 @@ pub struct FirstArgs { rows: Option>, } -#[async_trait] impl WholeStreamCommand for First { fn name(&self) -> &str { "first" @@ -29,8 +28,8 @@ impl WholeStreamCommand for First { "Show only the first number of rows." } - async fn run(&self, args: CommandArgs) -> Result { - first(args).await + fn run(&self, args: CommandArgs) -> Result { + first(args) } fn examples(&self) -> Vec { @@ -52,8 +51,8 @@ impl WholeStreamCommand for First { } } -async fn first(args: CommandArgs) -> Result { - let (FirstArgs { rows }, input) = args.process().await?; +fn first(args: CommandArgs) -> Result { + let (FirstArgs { rows }, input) = args.process()?; let rows_desired = if let Some(quantity) = rows { *quantity } else { diff --git a/crates/nu-command/src/commands/flatten.rs b/crates/nu-command/src/commands/flatten.rs index 3cb4bcfc2f..ad34911754 100644 --- a/crates/nu-command/src/commands/flatten.rs +++ b/crates/nu-command/src/commands/flatten.rs @@ -13,7 +13,6 @@ pub struct Arguments { rest: Vec>, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "flatten" @@ -27,8 +26,8 @@ impl WholeStreamCommand for Command { "Flatten the table." } - async fn run(&self, args: CommandArgs) -> Result { - flatten(args).await + fn run(&self, args: CommandArgs) -> Result { + flatten(args) } fn examples(&self) -> Vec { @@ -52,12 +51,12 @@ impl WholeStreamCommand for Command { } } -async fn flatten(args: CommandArgs) -> Result { +fn flatten(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (Arguments { rest: columns }, input) = args.process().await?; + let (Arguments { rest: columns }, input) = args.process()?; Ok(input - .map(move |item| futures::stream::iter(flat_value(&columns, &item, &tag).into_iter())) + .map(move |item| flat_value(&columns, &item, &tag).into_iter()) .flatten() .to_output_stream()) } diff --git a/crates/nu-command/src/commands/format/command.rs b/crates/nu-command/src/commands/format/command.rs index 241528bfaf..451e6d12a2 100644 --- a/crates/nu-command/src/commands/format/command.rs +++ b/crates/nu-command/src/commands/format/command.rs @@ -13,7 +13,6 @@ pub struct FormatArgs { pattern: Tagged, } -#[async_trait] impl WholeStreamCommand for Format { fn name(&self) -> &str { "format" @@ -31,8 +30,8 @@ impl WholeStreamCommand for Format { "Format columns into a string using a simple pattern." } - async fn run(&self, args: CommandArgs) -> Result { - format_command(args).await + fn run(&self, args: CommandArgs) -> Result { + format_command(args) } fn examples(&self) -> Vec { @@ -44,49 +43,46 @@ impl WholeStreamCommand for Format { } } -async fn format_command(args: CommandArgs) -> Result { +fn format_command(args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); - let (FormatArgs { pattern }, input) = args.process().await?; + let (FormatArgs { pattern }, input) = args.process()?; let format_pattern = format(&pattern); let commands = Arc::new(format_pattern); Ok(input - .then(move |value| { + .map(move |value| { let mut output = String::new(); let commands = commands.clone(); let ctx = ctx.clone(); - async move { - for command in &*commands { - match command { - FormatCommand::Text(s) => { - output.push_str(&s); - } - FormatCommand::Column(c) => { - // FIXME: use the correct spans - let full_column_path = nu_parser::parse_full_column_path( - &(c.to_string()).spanned(Span::unknown()), - &ctx.scope, - ); + for command in &*commands { + match command { + FormatCommand::Text(s) => { + output.push_str(&s); + } + FormatCommand::Column(c) => { + // FIXME: use the correct spans + let full_column_path = nu_parser::parse_full_column_path( + &(c.to_string()).spanned(Span::unknown()), + &ctx.scope, + ); - ctx.scope.enter_scope(); - ctx.scope.add_var("$it", value.clone()); - let result = evaluate_baseline_expr(&full_column_path.0, &*ctx).await; - ctx.scope.exit_scope(); + ctx.scope.enter_scope(); + ctx.scope.add_var("$it", value.clone()); + let result = evaluate_baseline_expr(&full_column_path.0, &*ctx); + ctx.scope.exit_scope(); - if let Ok(c) = result { - output - .push_str(&value::format_leaf(c.borrow()).plain_string(100_000)) - } else { - // That column doesn't match, so don't emit anything - } + if let Ok(c) = result { + output.push_str(&value::format_leaf(c.borrow()).plain_string(100_000)) + } else { + // That column doesn't match, so don't emit anything } } } - - ReturnSuccess::value(UntaggedValue::string(output).into_untagged_value()) } + + ReturnSuccess::value(UntaggedValue::string(output).into_untagged_value()) }) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/format/format_filesize.rs b/crates/nu-command/src/commands/format/format_filesize.rs index 5916b96082..b457d0f3c8 100644 --- a/crates/nu-command/src/commands/format/format_filesize.rs +++ b/crates/nu-command/src/commands/format/format_filesize.rs @@ -16,7 +16,6 @@ pub struct Arguments { format: Tagged, } -#[async_trait] impl WholeStreamCommand for FileSize { fn name(&self) -> &str { "format filesize" @@ -40,8 +39,8 @@ impl WholeStreamCommand for FileSize { "Converts a column of filesizes to some specified format" } - async fn run(&self, args: CommandArgs) -> Result { - filesize(args).await + fn run(&self, args: CommandArgs) -> Result { + filesize(args) } fn examples(&self) -> Vec { @@ -60,7 +59,7 @@ impl WholeStreamCommand for FileSize { } } -async fn process_row( +fn process_row( input: Value, format: Tagged, field: Arc, @@ -92,20 +91,18 @@ async fn process_row( }) } -async fn filesize(raw_args: CommandArgs) -> Result { - let (Arguments { field, format }, input) = raw_args.process().await?; +fn filesize(raw_args: CommandArgs) -> Result { + let (Arguments { field, format }, input) = raw_args.process()?; let field = Arc::new(field); Ok(input - .then(move |input| { + .map(move |input| { let format = format.clone(); let field = field.clone(); - async { - match process_row(input, format, field).await { - Ok(s) => s, - Err(e) => OutputStream::one(Err(e)), - } + match process_row(input, format, field) { + Ok(s) => s, + Err(e) => OutputStream::one(Err(e)), } }) .flatten() diff --git a/crates/nu-command/src/commands/from.rs b/crates/nu-command/src/commands/from.rs index 8cdafb9b19..02b7f4efdb 100644 --- a/crates/nu-command/src/commands/from.rs +++ b/crates/nu-command/src/commands/from.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; pub struct From; -#[async_trait] impl WholeStreamCommand for From { fn name(&self) -> &str { "from" @@ -19,7 +18,7 @@ impl WholeStreamCommand for From { "Parse content (string or binary) as a table (input format based on subcommand, like csv, ini, json, toml)." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(get_full_help(&From, &args.scope)).into_value(Tag::unknown()), ))) diff --git a/crates/nu-command/src/commands/from_csv.rs b/crates/nu-command/src/commands/from_csv.rs index 31ed0b2528..0843c7962a 100644 --- a/crates/nu-command/src/commands/from_csv.rs +++ b/crates/nu-command/src/commands/from_csv.rs @@ -12,7 +12,6 @@ pub struct FromCsvArgs { separator: Option, } -#[async_trait] impl WholeStreamCommand for FromCsv { fn name(&self) -> &str { "from csv" @@ -37,8 +36,8 @@ impl WholeStreamCommand for FromCsv { "Parse text as .csv and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_csv(args).await + fn run(&self, args: CommandArgs) -> Result { + from_csv(args) } fn examples(&self) -> Vec { @@ -67,7 +66,7 @@ impl WholeStreamCommand for FromCsv { } } -async fn from_csv(args: CommandArgs) -> Result { +fn from_csv(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let ( @@ -76,7 +75,7 @@ async fn from_csv(args: CommandArgs) -> Result { separator, }, input, - ) = args.process().await?; + ) = args.process()?; let sep = match separator { Some(Value { value: UntaggedValue::Primitive(Primitive::String(s)), @@ -100,7 +99,7 @@ async fn from_csv(args: CommandArgs) -> Result { _ => ',', }; - from_delimited_data(noheaders, sep, "CSV", input, name).await + from_delimited_data(noheaders, sep, "CSV", input, name) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/from_delimited_data.rs b/crates/nu-command/src/commands/from_delimited_data.rs index 79432ca5f0..1a78afc704 100644 --- a/crates/nu-command/src/commands/from_delimited_data.rs +++ b/crates/nu-command/src/commands/from_delimited_data.rs @@ -45,7 +45,7 @@ fn from_delimited_string_to_value( Ok(UntaggedValue::Table(rows).into_value(&tag)) } -pub async fn from_delimited_data( +pub fn from_delimited_data( noheaders: bool, sep: char, format_name: &'static str, @@ -53,7 +53,7 @@ pub async fn from_delimited_data( name: Tag, ) -> Result { let name_tag = name; - let concat_string = input.collect_string(name_tag.clone()).await?; + let concat_string = input.collect_string(name_tag.clone())?; let sample_lines = concat_string.item.lines().take(3).collect_vec().join("\n"); match from_delimited_string_to_value(concat_string.item, noheaders, sep, name_tag.clone()) { @@ -61,7 +61,7 @@ pub async fn from_delimited_data( Value { value: UntaggedValue::Table(list), .. - } => Ok(futures::stream::iter(list).to_output_stream()), + } => Ok(list.into_iter().to_output_stream()), x => Ok(OutputStream::one(x)), }, Err(err) => { @@ -80,7 +80,7 @@ pub async fn from_delimited_data( Err(ShellError::labeled_error_with_secondary( line_one, line_two, - name_tag.clone(), + name_tag, "value originates from here", concat_string.tag, )) diff --git a/crates/nu-command/src/commands/from_eml.rs b/crates/nu-command/src/commands/from_eml.rs index 0e3bcca82e..2c4f053d84 100644 --- a/crates/nu-command/src/commands/from_eml.rs +++ b/crates/nu-command/src/commands/from_eml.rs @@ -16,7 +16,6 @@ pub struct FromEmlArgs { preview_body: Option>, } -#[async_trait] impl WholeStreamCommand for FromEml { fn name(&self) -> &str { "from eml" @@ -35,8 +34,8 @@ impl WholeStreamCommand for FromEml { "Parse text as .eml and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_eml(args).await + fn run(&self, args: CommandArgs) -> Result { + from_eml(args) } } @@ -73,10 +72,10 @@ fn headerfieldvalue_to_value(tag: &Tag, value: &HeaderFieldValue) -> UntaggedVal } } -async fn from_eml(args: CommandArgs) -> Result { +fn from_eml(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (eml_args, input): (FromEmlArgs, _) = args.process().await?; - let value = input.collect_string(tag.clone()).await?; + let (eml_args, input): (FromEmlArgs, _) = args.process()?; + let value = input.collect_string(tag.clone())?; let body_preview = eml_args .preview_body diff --git a/crates/nu-command/src/commands/from_ics.rs b/crates/nu-command/src/commands/from_ics.rs index 61e0ce2b8a..b46d56ad75 100644 --- a/crates/nu-command/src/commands/from_ics.rs +++ b/crates/nu-command/src/commands/from_ics.rs @@ -9,7 +9,6 @@ use std::io::BufReader; pub struct FromIcs; -#[async_trait] impl WholeStreamCommand for FromIcs { fn name(&self) -> &str { "from ics" @@ -23,17 +22,17 @@ impl WholeStreamCommand for FromIcs { "Parse text as .ics and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_ics(args).await + fn run(&self, args: CommandArgs) -> Result { + from_ics(args) } } -async fn from_ics(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn from_ics(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.name_tag(); let input = args.input; - let input_string = input.collect_string(tag.clone()).await?.item; + let input_string = input.collect_string(tag.clone())?.item; let input_bytes = input_string.as_bytes(); let buf_reader = BufReader::new(input_bytes); let parser = ical::IcalParser::new(buf_reader); @@ -53,7 +52,7 @@ async fn from_ics(args: CommandArgs) -> Result { } } - Ok(futures::stream::iter(output).to_output_stream()) + Ok(output.into_iter().to_output_stream()) } fn calendar_to_value(calendar: IcalCalendar, tag: Tag) -> Value { diff --git a/crates/nu-command/src/commands/from_ini.rs b/crates/nu-command/src/commands/from_ini.rs index 97bd56fc9d..960a7e78cf 100644 --- a/crates/nu-command/src/commands/from_ini.rs +++ b/crates/nu-command/src/commands/from_ini.rs @@ -6,7 +6,6 @@ use std::collections::HashMap; pub struct FromIni; -#[async_trait] impl WholeStreamCommand for FromIni { fn name(&self) -> &str { "from ini" @@ -20,8 +19,8 @@ impl WholeStreamCommand for FromIni { "Parse text as .ini and create table" } - async fn run(&self, args: CommandArgs) -> Result { - from_ini(args).await + fn run(&self, args: CommandArgs) -> Result { + from_ini(args) } } @@ -60,18 +59,18 @@ pub fn from_ini_string_to_value( Ok(convert_ini_top_to_nu_value(&v, tag)) } -async fn from_ini(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn from_ini(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.name_tag(); let input = args.input; - let concat_string = input.collect_string(tag.clone()).await?; + let concat_string = input.collect_string(tag.clone())?; match from_ini_string_to_value(concat_string.item, tag.clone()) { Ok(x) => match x { Value { value: UntaggedValue::Table(list), .. - } => Ok(futures::stream::iter(list).to_output_stream()), + } => Ok(list.into_iter().to_output_stream()), x => Ok(OutputStream::one(x)), }, Err(_) => Err(ShellError::labeled_error_with_secondary( diff --git a/crates/nu-command/src/commands/from_json.rs b/crates/nu-command/src/commands/from_json.rs index dc0d9091ee..273686c297 100644 --- a/crates/nu-command/src/commands/from_json.rs +++ b/crates/nu-command/src/commands/from_json.rs @@ -10,7 +10,6 @@ pub struct FromJsonArgs { objects: bool, } -#[async_trait] impl WholeStreamCommand for FromJson { fn name(&self) -> &str { "from json" @@ -28,8 +27,8 @@ impl WholeStreamCommand for FromJson { "Parse text as .json and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_json(args).await + fn run(&self, args: CommandArgs) -> Result { + from_json(args) } } @@ -68,17 +67,18 @@ pub fn from_json_string_to_value(s: String, tag: impl Into) -> nu_json::Res Ok(convert_json_value_to_nu_value(&v, tag)) } -async fn from_json(args: CommandArgs) -> Result { +fn from_json(args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); - let (FromJsonArgs { objects }, input) = args.process().await?; - let concat_string = input.collect_string(name_tag.clone()).await?; + let (FromJsonArgs { objects }, input) = args.process()?; + let concat_string = input.collect_string(name_tag.clone())?; let string_clone: Vec<_> = concat_string.item.lines().map(|x| x.to_string()).collect(); if objects { - Ok( - futures::stream::iter(string_clone.into_iter().filter_map(move |json_str| { + Ok(string_clone + .into_iter() + .filter_map(move |json_str| { if json_str.is_empty() { return None; } @@ -99,19 +99,19 @@ async fn from_json(args: CommandArgs) -> Result { ))) } } - })) - .to_output_stream(), - ) + }) + .to_output_stream()) } else { match from_json_string_to_value(concat_string.item, name_tag.clone()) { Ok(x) => match x { Value { value: UntaggedValue::Table(list), .. - } => Ok( - futures::stream::iter(list.into_iter().map(ReturnSuccess::value)) - .to_output_stream(), - ), + } => Ok(list + .into_iter() + .map(ReturnSuccess::value) + .to_output_stream()), + x => Ok(OutputStream::one(ReturnSuccess::value(x))), }, Err(e) => { diff --git a/crates/nu-command/src/commands/from_ods.rs b/crates/nu-command/src/commands/from_ods.rs index 5ce3692c94..31ebb970de 100644 --- a/crates/nu-command/src/commands/from_ods.rs +++ b/crates/nu-command/src/commands/from_ods.rs @@ -13,7 +13,6 @@ pub struct FromOdsArgs { noheaders: bool, } -#[async_trait] impl WholeStreamCommand for FromOds { fn name(&self) -> &str { "from ods" @@ -31,12 +30,12 @@ impl WholeStreamCommand for FromOds { "Parse OpenDocument Spreadsheet(.ods) data and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_ods(args).await + fn run(&self, args: CommandArgs) -> Result { + from_ods(args) } } -async fn from_ods(args: CommandArgs) -> Result { +fn from_ods(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let span = tag.span; @@ -45,8 +44,8 @@ async fn from_ods(args: CommandArgs) -> Result { noheaders: _noheaders, }, input, - ) = args.process().await?; - let bytes = input.collect_binary(tag.clone()).await?; + ) = args.process()?; + let bytes = input.collect_binary(tag.clone())?; let buf: Cursor> = Cursor::new(bytes.item); let mut ods = Ods::<_>::new(buf).map_err(|_| { ShellError::labeled_error("Could not load ods file", "could not load ods file", &tag) diff --git a/crates/nu-command/src/commands/from_ssv.rs b/crates/nu-command/src/commands/from_ssv.rs index 23d75c0946..93da78fdfc 100644 --- a/crates/nu-command/src/commands/from_ssv.rs +++ b/crates/nu-command/src/commands/from_ssv.rs @@ -20,7 +20,6 @@ pub struct FromSsvArgs { const STRING_REPRESENTATION: &str = "from ssv"; const DEFAULT_MINIMUM_SPACES: usize = 2; -#[async_trait] impl WholeStreamCommand for FromSsv { fn name(&self) -> &str { STRING_REPRESENTATION @@ -46,8 +45,8 @@ impl WholeStreamCommand for FromSsv { "Parse text as space-separated values and create a table. The default minimum number of spaces counted as a separator is 2." } - async fn run(&self, args: CommandArgs) -> Result { - from_ssv(args).await + fn run(&self, args: CommandArgs) -> Result { + from_ssv(args) } } @@ -247,7 +246,7 @@ fn from_ssv_string_to_value( UntaggedValue::Table(rows).into_value(&tag) } -async fn from_ssv(args: CommandArgs) -> Result { +fn from_ssv(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let ( FromSsvArgs { @@ -256,8 +255,8 @@ async fn from_ssv(args: CommandArgs) -> Result { minimum_spaces, }, input, - ) = args.process().await?; - let concat_string = input.collect_string(name.clone()).await?; + ) = args.process()?; + let concat_string = input.collect_string(name.clone())?; let split_at = match minimum_spaces { Some(number) => number.item, None => DEFAULT_MINIMUM_SPACES, @@ -269,14 +268,15 @@ async fn from_ssv(args: CommandArgs) -> Result { noheaders, aligned_columns, split_at, - name.clone(), + name, ) { Value { value: UntaggedValue::Table(list), .. - } => { - futures::stream::iter(list.into_iter().map(ReturnSuccess::value)).to_output_stream() - } + } => list + .into_iter() + .map(ReturnSuccess::value) + .to_output_stream(), x => OutputStream::one(ReturnSuccess::value(x)), }, ) diff --git a/crates/nu-command/src/commands/from_toml.rs b/crates/nu-command/src/commands/from_toml.rs index 2698f964cc..6ab512cf28 100644 --- a/crates/nu-command/src/commands/from_toml.rs +++ b/crates/nu-command/src/commands/from_toml.rs @@ -5,7 +5,6 @@ use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, Untagg pub struct FromToml; -#[async_trait] impl WholeStreamCommand for FromToml { fn name(&self) -> &str { "from toml" @@ -19,8 +18,8 @@ impl WholeStreamCommand for FromToml { "Parse text as .toml and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_toml(args).await + fn run(&self, args: CommandArgs) -> Result { + from_toml(args) } } @@ -61,19 +60,21 @@ pub fn from_toml_string_to_value(s: String, tag: impl Into) -> Result Result { - let args = args.evaluate_once().await?; +pub fn from_toml(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.name_tag(); let input = args.input; - let concat_string = input.collect_string(tag.clone()).await?; + let concat_string = input.collect_string(tag.clone())?; Ok( match from_toml_string_to_value(concat_string.item, tag.clone()) { Ok(x) => match x { Value { value: UntaggedValue::Table(list), .. - } => futures::stream::iter(list.into_iter().map(ReturnSuccess::value)) + } => list + .into_iter() + .map(ReturnSuccess::value) .to_output_stream(), x => OutputStream::one(ReturnSuccess::value(x)), }, diff --git a/crates/nu-command/src/commands/from_tsv.rs b/crates/nu-command/src/commands/from_tsv.rs index d7eaa01787..5b9036dea6 100644 --- a/crates/nu-command/src/commands/from_tsv.rs +++ b/crates/nu-command/src/commands/from_tsv.rs @@ -11,7 +11,6 @@ pub struct FromTsvArgs { noheaders: bool, } -#[async_trait] impl WholeStreamCommand for FromTsv { fn name(&self) -> &str { "from tsv" @@ -29,16 +28,16 @@ impl WholeStreamCommand for FromTsv { "Parse text as .tsv and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_tsv(args).await + fn run(&self, args: CommandArgs) -> Result { + from_tsv(args) } } -async fn from_tsv(args: CommandArgs) -> Result { +fn from_tsv(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (FromTsvArgs { noheaders }, input) = args.process().await?; + let (FromTsvArgs { noheaders }, input) = args.process()?; - from_delimited_data(noheaders, '\t', "TSV", input, name).await + from_delimited_data(noheaders, '\t', "TSV", input, name) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/from_url.rs b/crates/nu-command/src/commands/from_url.rs index c8f92c99aa..2405463c8e 100644 --- a/crates/nu-command/src/commands/from_url.rs +++ b/crates/nu-command/src/commands/from_url.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue}; pub struct FromUrl; -#[async_trait] impl WholeStreamCommand for FromUrl { fn name(&self) -> &str { "from url" @@ -19,17 +18,17 @@ impl WholeStreamCommand for FromUrl { "Parse url-encoded string as a table." } - async fn run(&self, args: CommandArgs) -> Result { - from_url(args).await + fn run(&self, args: CommandArgs) -> Result { + from_url(args) } } -async fn from_url(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn from_url(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.name_tag(); let input = args.input; - let concat_string = input.collect_string(tag.clone()).await?; + let concat_string = input.collect_string(tag.clone())?; let result = serde_urlencoded::from_str::>(&concat_string.item); diff --git a/crates/nu-command/src/commands/from_vcf.rs b/crates/nu-command/src/commands/from_vcf.rs index 7020bc5c22..895ca06e5d 100644 --- a/crates/nu-command/src/commands/from_vcf.rs +++ b/crates/nu-command/src/commands/from_vcf.rs @@ -8,7 +8,6 @@ use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, Untagg pub struct FromVcf; -#[async_trait] impl WholeStreamCommand for FromVcf { fn name(&self) -> &str { "from vcf" @@ -22,17 +21,17 @@ impl WholeStreamCommand for FromVcf { "Parse text as .vcf and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_vcf(args).await + fn run(&self, args: CommandArgs) -> Result { + from_vcf(args) } } -async fn from_vcf(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn from_vcf(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.name_tag(); let input = args.input; - let input_string = input.collect_string(tag.clone()).await?.item; + let input_string = input.collect_string(tag.clone())?.item; let input_bytes = input_string.into_bytes(); let cursor = std::io::Cursor::new(input_bytes); let parser = ical::VcardParser::new(cursor); @@ -46,7 +45,9 @@ async fn from_vcf(args: CommandArgs) -> Result { )), }); - Ok(futures::stream::iter(iter).to_output_stream()) + let collected: Vec<_> = iter.collect(); + + Ok(collected.into_iter().to_output_stream()) } fn contact_to_value(contact: VcardContact, tag: Tag) -> Value { diff --git a/crates/nu-command/src/commands/from_xlsx.rs b/crates/nu-command/src/commands/from_xlsx.rs index d3b5e824ce..0efe82bfe7 100644 --- a/crates/nu-command/src/commands/from_xlsx.rs +++ b/crates/nu-command/src/commands/from_xlsx.rs @@ -13,7 +13,6 @@ pub struct FromXlsxArgs { noheaders: bool, } -#[async_trait] impl WholeStreamCommand for FromXlsx { fn name(&self) -> &str { "from xlsx" @@ -31,12 +30,12 @@ impl WholeStreamCommand for FromXlsx { "Parse binary Excel(.xlsx) data and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_xlsx(args).await + fn run(&self, args: CommandArgs) -> Result { + from_xlsx(args) } } -async fn from_xlsx(args: CommandArgs) -> Result { +fn from_xlsx(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let span = tag.span; let ( @@ -44,8 +43,8 @@ async fn from_xlsx(args: CommandArgs) -> Result { noheaders: _noheaders, }, input, - ) = args.process().await?; - let value = input.collect_binary(tag.clone()).await?; + ) = args.process()?; + let value = input.collect_binary(tag.clone())?; let buf: Cursor> = Cursor::new(value.item); let mut xls = Xlsx::<_>::new(buf).map_err(|_| { diff --git a/crates/nu-command/src/commands/from_xml.rs b/crates/nu-command/src/commands/from_xml.rs index 82fc975031..96809c7766 100644 --- a/crates/nu-command/src/commands/from_xml.rs +++ b/crates/nu-command/src/commands/from_xml.rs @@ -5,7 +5,6 @@ use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, Untagg pub struct FromXml; -#[async_trait] impl WholeStreamCommand for FromXml { fn name(&self) -> &str { "from xml" @@ -19,8 +18,8 @@ impl WholeStreamCommand for FromXml { "Parse text as .xml and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_xml(args).await + fn run(&self, args: CommandArgs) -> Result { + from_xml(args) } } @@ -95,12 +94,12 @@ pub fn from_xml_string_to_value(s: String, tag: impl Into) -> Result Result { - let args = args.evaluate_once().await?; +fn from_xml(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.name_tag(); let input = args.input; - let concat_string = input.collect_string(tag.clone()).await?; + let concat_string = input.collect_string(tag.clone())?; Ok( match from_xml_string_to_value(concat_string.item, tag.clone()) { @@ -108,7 +107,9 @@ async fn from_xml(args: CommandArgs) -> Result { Value { value: UntaggedValue::Table(list), .. - } => futures::stream::iter(list.into_iter().map(ReturnSuccess::value)) + } => list + .into_iter() + .map(ReturnSuccess::value) .to_output_stream(), x => OutputStream::one(ReturnSuccess::value(x)), }, diff --git a/crates/nu-command/src/commands/from_yaml.rs b/crates/nu-command/src/commands/from_yaml.rs index db9b3c3218..d3a183a697 100644 --- a/crates/nu-command/src/commands/from_yaml.rs +++ b/crates/nu-command/src/commands/from_yaml.rs @@ -5,7 +5,6 @@ use nu_protocol::{Primitive, Signature, TaggedDictBuilder, UntaggedValue, Value} pub struct FromYaml; -#[async_trait] impl WholeStreamCommand for FromYaml { fn name(&self) -> &str { "from yaml" @@ -19,14 +18,13 @@ impl WholeStreamCommand for FromYaml { "Parse text as .yaml/.yml and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_yaml(args).await + fn run(&self, args: CommandArgs) -> Result { + from_yaml(args) } } pub struct FromYml; -#[async_trait] impl WholeStreamCommand for FromYml { fn name(&self) -> &str { "from yml" @@ -40,8 +38,8 @@ impl WholeStreamCommand for FromYml { "Parse text as .yaml/.yml and create table." } - async fn run(&self, args: CommandArgs) -> Result { - from_yaml(args).await + fn run(&self, args: CommandArgs) -> Result { + from_yaml(args) } } @@ -133,19 +131,19 @@ pub fn from_yaml_string_to_value(s: String, tag: impl Into) -> Result Result { - let args = args.evaluate_once().await?; +fn from_yaml(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.name_tag(); let input = args.input; - let concat_string = input.collect_string(tag.clone()).await?; + let concat_string = input.collect_string(tag.clone())?; match from_yaml_string_to_value(concat_string.item, tag.clone()) { Ok(x) => match x { Value { value: UntaggedValue::Table(list), .. - } => Ok(futures::stream::iter(list).to_output_stream()), + } => Ok(list.into_iter().to_output_stream()), x => Ok(OutputStream::one(x)), }, Err(_) => Err(ShellError::labeled_error_with_secondary( diff --git a/crates/nu-command/src/commands/get.rs b/crates/nu-command/src/commands/get.rs index 7d8bc5a43a..d1fd5b9254 100644 --- a/crates/nu-command/src/commands/get.rs +++ b/crates/nu-command/src/commands/get.rs @@ -18,7 +18,6 @@ pub struct Arguments { rest: Vec, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "get" @@ -35,8 +34,8 @@ impl WholeStreamCommand for Command { "Open given cells as text." } - async fn run(&self, args: CommandArgs) -> Result { - get(args).await + fn run(&self, args: CommandArgs) -> Result { + get(args) } fn examples(&self) -> Vec { @@ -55,26 +54,28 @@ impl WholeStreamCommand for Command { } } -pub async fn get(args: CommandArgs) -> Result { - let (Arguments { mut rest }, mut input) = args.process().await?; +pub fn get(args: CommandArgs) -> Result { + let (Arguments { mut rest }, mut input) = args.process()?; let (column_paths, _) = arguments(&mut rest)?; if column_paths.is_empty() { - let vec = input.drain_vec().await; + let vec = input.drain_vec(); let descs = nu_protocol::merge_descriptors(&vec); - Ok(futures::stream::iter(descs.into_iter().map(ReturnSuccess::value)).to_output_stream()) + Ok(descs + .into_iter() + .map(ReturnSuccess::value) + .to_output_stream()) } else { trace!("get {:?}", column_paths); let output_stream = input .map(move |item| { - let output = column_paths + column_paths .iter() .map(move |path| get_output(&item, path)) .flatten() - .collect::>(); - futures::stream::iter(output) + .collect::>() }) .flatten() .to_output_stream(); diff --git a/crates/nu-command/src/commands/group_by.rs b/crates/nu-command/src/commands/group_by.rs index 422d3d136c..af84db7810 100644 --- a/crates/nu-command/src/commands/group_by.rs +++ b/crates/nu-command/src/commands/group_by.rs @@ -13,7 +13,6 @@ pub struct Arguments { grouper: Option, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "group-by" @@ -31,8 +30,8 @@ impl WholeStreamCommand for Command { "Create a new table grouped." } - async fn run(&self, args: CommandArgs) -> Result { - group_by(args).await + fn run(&self, args: CommandArgs) -> Result { + group_by(args) } #[allow(clippy::unwrap_used)] @@ -128,12 +127,12 @@ enum Grouper { ByBlock, } -pub async fn group_by(args: CommandArgs) -> Result { +pub fn group_by(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let context = Arc::new(EvaluationContext::from_args(&args)); - let (Arguments { grouper }, input) = args.process().await?; + let (Arguments { grouper }, input) = args.process()?; - let values: Vec = input.collect().await; + let values: Vec = input.collect(); let mut keys: Vec> = vec![]; let mut group_strategy = Grouper::ByColumn(None); @@ -149,10 +148,9 @@ pub async fn group_by(args: CommandArgs) -> Result { let run = block.clone(); let context = context.clone(); - match crate::commands::each::process_row(run, context, value.clone()).await { + match crate::commands::each::process_row(run, context, value.clone()) { Ok(mut s) => { - let collection: Vec> = - s.drain_vec().await; + let collection: Vec> = s.drain_vec(); if collection.len() > 1 { return Err(ShellError::labeled_error( @@ -209,7 +207,7 @@ pub async fn group_by(args: CommandArgs) -> Result { let group_value = match group_strategy { Grouper::ByBlock => { - let map = keys.clone(); + let map = keys; let block = Box::new(move |idx: usize, row: &Value| match map.get(idx) { Some(Ok(key)) => Ok(key.clone()), diff --git a/crates/nu-command/src/commands/group_by_date.rs b/crates/nu-command/src/commands/group_by_date.rs index 5bc354ae14..190dce4346 100644 --- a/crates/nu-command/src/commands/group_by_date.rs +++ b/crates/nu-command/src/commands/group_by_date.rs @@ -13,7 +13,6 @@ pub struct GroupByDateArgs { format: Option>, } -#[async_trait] impl WholeStreamCommand for GroupByDate { fn name(&self) -> &str { "group-by date" @@ -38,8 +37,8 @@ impl WholeStreamCommand for GroupByDate { "creates a table grouped by date." } - async fn run(&self, args: CommandArgs) -> Result { - group_by_date(args).await + fn run(&self, args: CommandArgs) -> Result { + group_by_date(args) } fn examples(&self) -> Vec { @@ -59,7 +58,7 @@ enum GroupByColumn { Name(Option>), } -pub async fn group_by_date(args: CommandArgs) -> Result { +pub fn group_by_date(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let ( GroupByDateArgs { @@ -67,8 +66,8 @@ pub async fn group_by_date(args: CommandArgs) -> Result = input.collect().await; + ) = args.process()?; + let values: Vec = input.collect(); if values.is_empty() { Err(ShellError::labeled_error( diff --git a/crates/nu-command/src/commands/hash_/base64_.rs b/crates/nu-command/src/commands/hash_/base64_.rs index d7c0c5793c..77bc686271 100644 --- a/crates/nu-command/src/commands/hash_/base64_.rs +++ b/crates/nu-command/src/commands/hash_/base64_.rs @@ -30,7 +30,6 @@ pub enum ActionType { } pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "hash base64" @@ -65,8 +64,8 @@ impl WholeStreamCommand for SubCommand { "base64 encode or decode a value" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -96,8 +95,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let name_tag = &args.call_info.name_tag.clone(); +fn operate(args: CommandArgs) -> Result { + let name_tag = args.call_info.name_tag.clone(); let ( Arguments { @@ -107,7 +106,7 @@ async fn operate(args: CommandArgs) -> Result { rest, }, input, - ) = args.process().await?; + ) = args.process()?; if encode.item && decode.item { return Ok(OutputStream::one(Err(ShellError::labeled_error( diff --git a/crates/nu-command/src/commands/hash_/command.rs b/crates/nu-command/src/commands/hash_/command.rs index a89282d977..53e10b635f 100644 --- a/crates/nu-command/src/commands/hash_/command.rs +++ b/crates/nu-command/src/commands/hash_/command.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "hash" @@ -22,7 +21,7 @@ impl WholeStreamCommand for Command { "Apply hash function." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(get_full_help(&Command, &args.scope)).into_value(Tag::unknown()), ))) diff --git a/crates/nu-command/src/commands/hash_/md5_.rs b/crates/nu-command/src/commands/hash_/md5_.rs index 5f0834260e..018eb101b1 100644 --- a/crates/nu-command/src/commands/hash_/md5_.rs +++ b/crates/nu-command/src/commands/hash_/md5_.rs @@ -14,7 +14,6 @@ pub struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "hash md5" @@ -31,8 +30,8 @@ impl WholeStreamCommand for SubCommand { "md5 encode a value" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -57,8 +56,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { rest }, input) = args.process()?; let column_paths: Vec<_> = rest; diff --git a/crates/nu-command/src/commands/headers.rs b/crates/nu-command/src/commands/headers.rs index a9e2650ee8..f3f790cc4d 100644 --- a/crates/nu-command/src/commands/headers.rs +++ b/crates/nu-command/src/commands/headers.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use futures::stream::StreamExt; + use indexmap::IndexMap; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; @@ -8,7 +8,6 @@ use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; pub struct Headers; -#[async_trait] impl WholeStreamCommand for Headers { fn name(&self) -> &str { "headers" @@ -22,8 +21,8 @@ impl WholeStreamCommand for Headers { "Use the first row of the table as column names." } - async fn run(&self, args: CommandArgs) -> Result { - headers(args).await + fn run(&self, args: CommandArgs) -> Result { + headers(args) } fn examples(&self) -> Vec { @@ -42,9 +41,9 @@ impl WholeStreamCommand for Headers { } } -pub async fn headers(args: CommandArgs) -> Result { +pub fn headers(args: CommandArgs) -> Result { let input = args.input; - let rows: Vec = input.collect().await; + let rows: Vec = input.collect(); if rows.is_empty() { return Err(ShellError::untagged_runtime_error( @@ -77,8 +76,10 @@ pub async fn headers(args: CommandArgs) -> Result { )), }?; - Ok( - futures::stream::iter(rows.into_iter().skip(1).map(move |r| { + Ok(rows + .into_iter() + .skip(1) + .map(move |r| { //Each row is a dictionary with the headers as keys match &r.value { UntaggedValue::Row(d) => { @@ -100,9 +101,8 @@ pub async fn headers(args: CommandArgs) -> Result { r.tag.span, )), } - })) - .to_output_stream(), - ) + }) + .to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/help.rs b/crates/nu-command/src/commands/help.rs index 775a079e41..e8eb4690f4 100644 --- a/crates/nu-command/src/commands/help.rs +++ b/crates/nu-command/src/commands/help.rs @@ -18,7 +18,6 @@ pub struct HelpArgs { rest: Vec>, } -#[async_trait] impl WholeStreamCommand for Help { fn name(&self) -> &str { "help" @@ -32,15 +31,15 @@ impl WholeStreamCommand for Help { "Display help information about commands." } - async fn run(&self, args: CommandArgs) -> Result { - help(args).await + fn run(&self, args: CommandArgs) -> Result { + help(args) } } -async fn help(args: CommandArgs) -> Result { +fn help(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let scope = args.scope.clone(); - let (HelpArgs { rest }, ..) = args.process().await?; + let (HelpArgs { rest }, ..) = args.process()?; if !rest.is_empty() { if rest[0].item == "commands" { @@ -155,7 +154,7 @@ async fn help(args: CommandArgs) -> Result { ReturnSuccess::value(short_desc.into_value()) }); - Ok(futures::stream::iter(iterator).to_output_stream()) + Ok(iterator.to_output_stream()) } else if rest[0].item == "generate_docs" { Ok(OutputStream::one(ReturnSuccess::value(generate_docs( &scope, diff --git a/crates/nu-command/src/commands/histogram.rs b/crates/nu-command/src/commands/histogram.rs index 059fc26664..536aa757f1 100644 --- a/crates/nu-command/src/commands/histogram.rs +++ b/crates/nu-command/src/commands/histogram.rs @@ -8,7 +8,6 @@ use nu_source::Tagged; pub struct Histogram; -#[async_trait] impl WholeStreamCommand for Histogram { fn name(&self) -> &str { "histogram" @@ -32,8 +31,8 @@ impl WholeStreamCommand for Histogram { "Creates a new table with a histogram based on the column name passed in." } - async fn run(&self, args: CommandArgs) -> Result { - histogram(args).await + fn run(&self, args: CommandArgs) -> Result { + histogram(args) } fn examples(&self) -> Vec { @@ -58,11 +57,11 @@ impl WholeStreamCommand for Histogram { } } -pub async fn histogram(args: CommandArgs) -> Result { +pub fn histogram(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (input, args) = args.evaluate_once().await?.parts(); + let (input, args) = args.evaluate_once()?.parts(); - let values: Vec = input.collect().await; + let values: Vec = input.collect(); let mut columns = args .positional_iter() @@ -114,73 +113,71 @@ pub async fn histogram(args: CommandArgs) -> Result { let labels = results.labels.y.clone(); let mut idx = 0; - Ok(futures::stream::iter( - results - .data - .table_entries() - .cloned() - .collect::>() - .into_iter() - .zip( - results - .percentages - .table_entries() - .cloned() - .collect::>() - .into_iter(), - ) - .map(move |(counts, percentages)| { - let percentage = percentages - .table_entries() - .cloned() - .last() - .unwrap_or_else(|| { - UntaggedValue::decimal_from_float(0.0, name.span).into_value(&name) - }); - let value = counts - .table_entries() - .cloned() - .last() - .unwrap_or_else(|| UntaggedValue::int(0).into_value(&name)); + Ok(results + .data + .table_entries() + .cloned() + .collect::>() + .into_iter() + .zip( + results + .percentages + .table_entries() + .cloned() + .collect::>() + .into_iter(), + ) + .map(move |(counts, percentages)| { + let percentage = percentages + .table_entries() + .cloned() + .last() + .unwrap_or_else(|| { + UntaggedValue::decimal_from_float(0.0, name.span).into_value(&name) + }); + let value = counts + .table_entries() + .cloned() + .last() + .unwrap_or_else(|| UntaggedValue::int(0).into_value(&name)); - let mut fact = TaggedDictBuilder::new(&name); - let column_value = labels - .get(idx) - .ok_or_else(|| { - ShellError::labeled_error( - "Unable to load group labels", - "unable to load group labels", - &name, - ) - })? - .clone(); + let mut fact = TaggedDictBuilder::new(&name); + let column_value = labels + .get(idx) + .ok_or_else(|| { + ShellError::labeled_error( + "Unable to load group labels", + "unable to load group labels", + &name, + ) + })? + .clone(); - fact.insert_value(&column.item, column_value); - fact.insert_untagged("count", value); + fact.insert_value(&column.item, column_value); + fact.insert_untagged("count", value); - let fmt_percentage = format!( - "{}%", - // Some(2) < the number of digits - // true < group the digits - crate::commands::str_::from::action(&percentage, &name, Some(2), true)? - .as_string()? - ); - fact.insert_untagged("percentage", UntaggedValue::string(fmt_percentage)); + let fmt_percentage = format!( + "{}%", + // Some(2) < the number of digits + // true < group the digits + crate::commands::str_::from::action(&percentage, &name, Some(2), true)? + .as_string()? + ); + fact.insert_untagged("percentage", UntaggedValue::string(fmt_percentage)); - let string = std::iter::repeat("*") - .take(percentage.as_u64().map_err(|_| { - ShellError::labeled_error("expected a number", "expected a number", &name) - })? as usize) - .collect::(); + let string = std::iter::repeat("*") + .take(percentage.as_u64().map_err(|_| { + ShellError::labeled_error("expected a number", "expected a number", &name) + })? as usize) + .collect::(); - fact.insert_untagged(&frequency_column_name, UntaggedValue::string(string)); + fact.insert_untagged(&frequency_column_name, UntaggedValue::string(string)); - idx += 1; + idx += 1; - ReturnSuccess::value(fact.into_value()) - }), - ) - .to_output_stream()) + ReturnSuccess::value(fact.into_value()) + }) + .to_output_stream()) } fn evaluator(by: ColumnPath) -> Box Result + Send> { diff --git a/crates/nu-command/src/commands/history.rs b/crates/nu-command/src/commands/history.rs index bf16d1fc81..92b27ffd61 100644 --- a/crates/nu-command/src/commands/history.rs +++ b/crates/nu-command/src/commands/history.rs @@ -13,7 +13,6 @@ struct Arguments { pub struct History; -#[async_trait] impl WholeStreamCommand for History { fn name(&self) -> &str { "history" @@ -27,15 +26,15 @@ impl WholeStreamCommand for History { "Display command history." } - async fn run(&self, args: CommandArgs) -> Result { - history(args).await + fn run(&self, args: CommandArgs) -> Result { + history(args) } } -async fn history(args: CommandArgs) -> Result { +fn history(args: CommandArgs) -> Result { let config: Box = Box::new(NuConfig::new()); let tag = args.call_info.name_tag.clone(); - let (Arguments { clear }, _) = args.process().await?; + let (Arguments { clear }, _) = args.process()?; let path = nu_data::config::path::history_path(&config); @@ -55,7 +54,7 @@ async fn history(args: CommandArgs) -> Result { Err(_) => None, }); - Ok(futures::stream::iter(output).to_output_stream()) + Ok(output.to_output_stream()) } else { Err(ShellError::labeled_error( "Could not open history", diff --git a/crates/nu-command/src/commands/if_.rs b/crates/nu-command/src/commands/if_.rs index 13e97f0a14..9893f5b4e1 100644 --- a/crates/nu-command/src/commands/if_.rs +++ b/crates/nu-command/src/commands/if_.rs @@ -16,7 +16,6 @@ pub struct IfArgs { else_case: CapturedBlock, } -#[async_trait] impl WholeStreamCommand for If { fn name(&self) -> &str { "if" @@ -45,8 +44,8 @@ impl WholeStreamCommand for If { "Run blocks if a condition is true or false." } - async fn run(&self, args: CommandArgs) -> Result { - if_command(args).await + fn run(&self, args: CommandArgs) -> Result { + if_command(args) } fn examples(&self) -> Vec { @@ -64,7 +63,7 @@ impl WholeStreamCommand for If { ] } } -async fn if_command(raw_args: CommandArgs) -> Result { +fn if_command(raw_args: CommandArgs) -> Result { let tag = raw_args.call_info.name_tag.clone(); let context = Arc::new(EvaluationContext::from_args(&raw_args)); @@ -75,7 +74,7 @@ async fn if_command(raw_args: CommandArgs) -> Result { else_case, }, input, - ) = raw_args.process().await?; + ) = raw_args.process()?; let cond = { if condition.block.block.len() != 1 { return Err(ShellError::labeled_error( @@ -109,22 +108,22 @@ async fn if_command(raw_args: CommandArgs) -> Result { context.scope.add_vars(&condition.captured.entries); //FIXME: should we use the scope that's brought in as well? - let condition = evaluate_baseline_expr(&cond, &*context).await; + let condition = evaluate_baseline_expr(&cond, &*context); match condition { Ok(condition) => match condition.as_bool() { Ok(b) => { let result = if b { - run_block(&then_case.block, &*context, input).await + run_block(&then_case.block, &*context, input) } else { - run_block(&else_case.block, &*context, input).await + run_block(&else_case.block, &*context, input) }; context.scope.exit_scope(); result.map(|x| x.to_output_stream()) } - Err(e) => Ok(futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream()), + Err(e) => Ok(vec![Err(e)].into_iter().to_output_stream()), }, - Err(e) => Ok(futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream()), + Err(e) => Ok(vec![Err(e)].into_iter().to_output_stream()), } } diff --git a/crates/nu-command/src/commands/insert.rs b/crates/nu-command/src/commands/insert.rs index 281b6f5b06..0e02dc3e9e 100644 --- a/crates/nu-command/src/commands/insert.rs +++ b/crates/nu-command/src/commands/insert.rs @@ -7,8 +7,6 @@ use nu_protocol::{ }; use nu_value_ext::ValueExt; -use futures::stream::once; - pub struct Command; #[derive(Deserialize)] @@ -17,7 +15,6 @@ pub struct Arguments { value: Value, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "insert" @@ -37,8 +34,8 @@ impl WholeStreamCommand for Command { "Insert a new column with a given value." } - async fn run(&self, args: CommandArgs) -> Result { - insert(args).await + fn run(&self, args: CommandArgs) -> Result { + insert(args) } fn examples(&self) -> Vec { @@ -64,7 +61,7 @@ impl WholeStreamCommand for Command { } } -async fn process_row( +fn process_row( context: Arc, input: Value, mut value: Arc, @@ -78,19 +75,19 @@ async fn process_row( tag: block_tag, } => { let for_block = input.clone(); - let input_stream = once(async { Ok(for_block) }).to_input_stream(); + let input_stream = vec![Ok(for_block)].into_iter().to_input_stream(); context.scope.enter_scope(); context.scope.add_vars(&block.captured.entries); context.scope.add_var("$it", input.clone()); - let result = run_block(&block.block, &*context, input_stream).await; + let result = run_block(&block.block, &*context, input_stream); context.scope.exit_scope(); match result { Ok(mut stream) => { - let values = stream.drain_vec().await; + let values = stream.drain_vec(); let errors = context.get_errors(); if let Some(error) = errors.first() { @@ -153,23 +150,21 @@ async fn process_row( }) } -async fn insert(raw_args: CommandArgs) -> Result { +fn insert(raw_args: CommandArgs) -> Result { let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let (Arguments { column, value }, input) = raw_args.process().await?; + let (Arguments { column, value }, input) = raw_args.process()?; let value = Arc::new(value); let column = Arc::new(column); Ok(input - .then(move |input| { + .map(move |input| { let context = context.clone(); let value = value.clone(); let column = column.clone(); - async { - match process_row(context, input, value, column).await { - Ok(s) => s, - Err(e) => OutputStream::one(Err(e)), - } + match process_row(context, input, value, column) { + Ok(s) => s, + Err(e) => OutputStream::one(Err(e)), } }) .flatten() diff --git a/crates/nu-command/src/commands/into_int.rs b/crates/nu-command/src/commands/into_int.rs index e784137b6b..5e88f46445 100644 --- a/crates/nu-command/src/commands/into_int.rs +++ b/crates/nu-command/src/commands/into_int.rs @@ -12,7 +12,6 @@ pub struct IntoIntArgs { pub rest: Vec, } -#[async_trait] impl WholeStreamCommand for IntoInt { fn name(&self) -> &str { "into-int" @@ -26,8 +25,8 @@ impl WholeStreamCommand for IntoInt { "Convert value to integer." } - async fn run(&self, args: CommandArgs) -> Result { - into_int(args).await + fn run(&self, args: CommandArgs) -> Result { + into_int(args) } fn examples(&self) -> Vec { @@ -46,8 +45,8 @@ impl WholeStreamCommand for IntoInt { } } -async fn into_int(args: CommandArgs) -> Result { - let (args, _): (IntoIntArgs, _) = args.process().await?; +fn into_int(args: CommandArgs) -> Result { + let (args, _): (IntoIntArgs, _) = args.process()?; let stream = args.rest.into_iter().map(|i| match i { Value { @@ -71,7 +70,7 @@ async fn into_int(args: CommandArgs) -> Result { _ => OutputStream::one(Ok(ReturnSuccess::Value(i))), }); - Ok(futures::stream::iter(stream).flatten().to_output_stream()) + Ok(stream.flatten().to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/keep/command.rs b/crates/nu-command/src/commands/keep/command.rs index ec5c33df45..c3be7d7e9b 100644 --- a/crates/nu-command/src/commands/keep/command.rs +++ b/crates/nu-command/src/commands/keep/command.rs @@ -11,7 +11,6 @@ pub struct Arguments { rows: Option>, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "keep" @@ -29,8 +28,8 @@ impl WholeStreamCommand for Command { "Keep the number of rows only." } - async fn run(&self, args: CommandArgs) -> Result { - keep(args).await + fn run(&self, args: CommandArgs) -> Result { + keep(args) } fn examples(&self) -> Vec { @@ -54,8 +53,8 @@ impl WholeStreamCommand for Command { } } -async fn keep(args: CommandArgs) -> Result { - let (Arguments { rows }, input) = args.process().await?; +fn keep(args: CommandArgs) -> Result { + let (Arguments { rows }, input) = args.process()?; let rows_desired = if let Some(quantity) = rows { *quantity } else { diff --git a/crates/nu-command/src/commands/keep/until.rs b/crates/nu-command/src/commands/keep/until.rs index ffe2987620..a9b4caec08 100644 --- a/crates/nu-command/src/commands/keep/until.rs +++ b/crates/nu-command/src/commands/keep/until.rs @@ -8,7 +8,6 @@ use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "keep until" @@ -28,10 +27,10 @@ impl WholeStreamCommand for SubCommand { "Keeps rows until the condition matches." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); - let call_info = args.evaluate_once().await?; + let call_info = args.evaluate_once()?; let block = call_info.args.expect_nth(0)?.clone(); @@ -88,13 +87,11 @@ impl WholeStreamCommand for SubCommand { ctx.scope.add_var("$it", item.clone()); trace!("ITEM = {:?}", item); - async move { - let result = evaluate_baseline_expr(&*condition, &*ctx).await; - ctx.scope.exit_scope(); - trace!("RESULT = {:?}", result); + let result = evaluate_baseline_expr(&*condition, &*ctx); + ctx.scope.exit_scope(); + trace!("RESULT = {:?}", result); - !matches!(result, Ok(ref v) if v.is_true()) - } + !matches!(result, Ok(ref v) if v.is_true()) }) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/keep/while_.rs b/crates/nu-command/src/commands/keep/while_.rs index 95f848342d..5c1150bcdd 100644 --- a/crates/nu-command/src/commands/keep/while_.rs +++ b/crates/nu-command/src/commands/keep/while_.rs @@ -7,7 +7,6 @@ use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "keep while" @@ -27,9 +26,9 @@ impl WholeStreamCommand for SubCommand { "Keeps rows while the condition matches." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); - let call_info = args.evaluate_once().await?; + let call_info = args.evaluate_once()?; let block = call_info.args.expect_nth(0)?.clone(); @@ -87,13 +86,11 @@ impl WholeStreamCommand for SubCommand { ctx.scope.add_vars(&captured.entries); trace!("ITEM = {:?}", item); - async move { - let result = evaluate_baseline_expr(&*condition, &*ctx).await; - ctx.scope.exit_scope(); - trace!("RESULT = {:?}", result); + let result = evaluate_baseline_expr(&*condition, &*ctx); + ctx.scope.exit_scope(); + trace!("RESULT = {:?}", result); - matches!(result, Ok(ref v) if v.is_true()) - } + matches!(result, Ok(ref v) if v.is_true()) }) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/kill.rs b/crates/nu-command/src/commands/kill.rs index 07ee757a53..0d407b7aba 100644 --- a/crates/nu-command/src/commands/kill.rs +++ b/crates/nu-command/src/commands/kill.rs @@ -16,7 +16,6 @@ pub struct KillArgs { pub signal: Option>, } -#[async_trait] impl WholeStreamCommand for Kill { fn name(&self) -> &str { "kill" @@ -49,8 +48,8 @@ impl WholeStreamCommand for Kill { "Kill a process using the process id." } - async fn run(&self, args: CommandArgs) -> Result { - kill(args).await + fn run(&self, args: CommandArgs) -> Result { + kill(args) } fn examples(&self) -> Vec { @@ -74,7 +73,7 @@ impl WholeStreamCommand for Kill { } } -async fn kill(args: CommandArgs) -> Result { +fn kill(args: CommandArgs) -> Result { let ( KillArgs { pid, @@ -84,7 +83,7 @@ async fn kill(args: CommandArgs) -> Result { signal, }, .., - ) = args.process().await?; + ) = args.process()?; let mut cmd = if cfg!(windows) { let mut cmd = Command::new("taskkill"); diff --git a/crates/nu-command/src/commands/last.rs b/crates/nu-command/src/commands/last.rs index da90055780..47ab7a9b27 100644 --- a/crates/nu-command/src/commands/last.rs +++ b/crates/nu-command/src/commands/last.rs @@ -11,7 +11,6 @@ pub struct LastArgs { rows: Option>, } -#[async_trait] impl WholeStreamCommand for Last { fn name(&self) -> &str { "last" @@ -29,8 +28,8 @@ impl WholeStreamCommand for Last { "Show only the last number of rows." } - async fn run(&self, args: CommandArgs) -> Result { - last(args).await + fn run(&self, args: CommandArgs) -> Result { + last(args) } fn examples(&self) -> Vec { @@ -53,9 +52,9 @@ impl WholeStreamCommand for Last { } } -async fn last(args: CommandArgs) -> Result { - let (LastArgs { rows }, input) = args.process().await?; - let v: Vec<_> = input.into_vec().await; +fn last(args: CommandArgs) -> Result { + let (LastArgs { rows }, input) = args.process()?; + let v: Vec<_> = input.into_vec(); let end_rows_desired = if let Some(quantity) = rows { *quantity as usize @@ -71,7 +70,7 @@ async fn last(args: CommandArgs) -> Result { let iter = v.into_iter().skip(beginning_rows_to_skip); - Ok(futures::stream::iter(iter).to_output_stream()) + Ok((iter).to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/length.rs b/crates/nu-command/src/commands/length.rs index 6d0e8b188a..b3fdc565b4 100644 --- a/crates/nu-command/src/commands/length.rs +++ b/crates/nu-command/src/commands/length.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use futures::stream::StreamExt; + use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue, Value}; @@ -11,7 +11,6 @@ pub struct LengthArgs { column: bool, } -#[async_trait] impl WholeStreamCommand for Length { fn name(&self) -> &str { "length" @@ -29,10 +28,10 @@ impl WholeStreamCommand for Length { "Show the total number of rows or items." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (LengthArgs { column }, input) = args.process().await?; - let rows: Vec = input.collect().await; + let (LengthArgs { column }, input) = args.process()?; + let rows: Vec = input.collect(); let length = if column { if rows.is_empty() { diff --git a/crates/nu-command/src/commands/let_.rs b/crates/nu-command/src/commands/let_.rs index 4a2375fb37..a1cad04822 100644 --- a/crates/nu-command/src/commands/let_.rs +++ b/crates/nu-command/src/commands/let_.rs @@ -14,7 +14,6 @@ pub struct LetArgs { pub rhs: CapturedBlock, } -#[async_trait] impl WholeStreamCommand for Let { fn name(&self) -> &str { "let" @@ -35,8 +34,8 @@ impl WholeStreamCommand for Let { "Create a variable and give it a value." } - async fn run(&self, args: CommandArgs) -> Result { - letcmd(args).await + fn run(&self, args: CommandArgs) -> Result { + letcmd(args) } fn examples(&self) -> Vec { @@ -44,11 +43,11 @@ impl WholeStreamCommand for Let { } } -pub async fn letcmd(args: CommandArgs) -> Result { +pub fn letcmd(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ctx = EvaluationContext::from_args(&args); - let (LetArgs { name, rhs, .. }, _) = args.process().await?; + let (LetArgs { name, rhs, .. }, _) = args.process()?; let (expr, captured) = { if rhs.block.block.len() != 1 { @@ -82,14 +81,14 @@ pub async fn letcmd(args: CommandArgs) -> Result { ctx.scope.enter_scope(); ctx.scope.add_vars(&captured.entries); - let value = evaluate_baseline_expr(&expr, &ctx).await; + let value = evaluate_baseline_expr(&expr, &ctx); ctx.scope.exit_scope(); let value = value?; let name = if name.item.starts_with('$') { - name.item.clone() + name.item } else { format!("${}", name.item) }; diff --git a/crates/nu-command/src/commands/let_env.rs b/crates/nu-command/src/commands/let_env.rs index 109f46e8a0..b4ab1f3869 100644 --- a/crates/nu-command/src/commands/let_env.rs +++ b/crates/nu-command/src/commands/let_env.rs @@ -14,7 +14,6 @@ pub struct LetEnvArgs { pub rhs: CapturedBlock, } -#[async_trait] impl WholeStreamCommand for LetEnv { fn name(&self) -> &str { "let-env" @@ -39,8 +38,8 @@ impl WholeStreamCommand for LetEnv { "Create an environment variable and give it a value." } - async fn run(&self, args: CommandArgs) -> Result { - set_env(args).await + fn run(&self, args: CommandArgs) -> Result { + set_env(args) } fn examples(&self) -> Vec { @@ -48,11 +47,11 @@ impl WholeStreamCommand for LetEnv { } } -pub async fn set_env(args: CommandArgs) -> Result { +pub fn set_env(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ctx = EvaluationContext::from_args(&args); - let (LetEnvArgs { name, rhs, .. }, _) = args.process().await?; + let (LetEnvArgs { name, rhs, .. }, _) = args.process()?; let (expr, captured) = { if rhs.block.block.len() != 1 { @@ -86,14 +85,14 @@ pub async fn set_env(args: CommandArgs) -> Result { ctx.scope.enter_scope(); ctx.scope.add_vars(&captured.entries); - let value = evaluate_baseline_expr(&expr, &ctx).await; + let value = evaluate_baseline_expr(&expr, &ctx); ctx.scope.exit_scope(); let value = value?; let value = value.as_string()?; - let name = name.item.clone(); + let name = name.item; // Note: this is a special case for setting the context from a command // In this case, if we don't set it now, we'll lose the scope that this diff --git a/crates/nu-command/src/commands/lines.rs b/crates/nu-command/src/commands/lines.rs index e3409929a6..ee941e0b15 100644 --- a/crates/nu-command/src/commands/lines.rs +++ b/crates/nu-command/src/commands/lines.rs @@ -6,7 +6,6 @@ use parking_lot::Mutex; pub struct Lines; -#[async_trait] impl WholeStreamCommand for Lines { fn name(&self) -> &str { "lines" @@ -20,8 +19,8 @@ impl WholeStreamCommand for Lines { "Split single string into rows, one per line." } - async fn run(&self, args: CommandArgs) -> Result { - lines(args).await + fn run(&self, args: CommandArgs) -> Result { + lines(args) } fn examples(&self) -> Vec { @@ -43,83 +42,74 @@ fn ends_with_line_ending(st: &str) -> bool { } } -async fn lines(args: CommandArgs) -> Result { +fn lines(args: CommandArgs) -> Result { let leftover_string = Arc::new(Mutex::new(String::new())); - let args = args.evaluate_once().await?; + let args = args.evaluate_once()?; let tag = args.name_tag(); let name_span = tag.span; - let eos = futures::stream::iter(vec![ - UntaggedValue::Primitive(Primitive::EndOfStream).into_untagged_value() - ]); + let eos = vec![UntaggedValue::Primitive(Primitive::EndOfStream).into_untagged_value()]; Ok(args .input .chain(eos) .filter_map(move |item| { let leftover_string = leftover_string.clone(); - async move { - match item { - Value { - value: UntaggedValue::Primitive(Primitive::String(st)), - .. - } => { - let mut leftover_string = leftover_string.lock(); + match item { + Value { + value: UntaggedValue::Primitive(Primitive::String(st)), + .. + } => { + let mut leftover_string = leftover_string.lock(); - let mut buffer = leftover_string.clone(); - buffer.push_str(&st); + let mut buffer = leftover_string.clone(); + buffer.push_str(&st); - let mut lines: Vec = - buffer.lines().map(|x| x.to_string()).collect(); + let mut lines: Vec = buffer.lines().map(|x| x.to_string()).collect(); - leftover_string.clear(); + leftover_string.clear(); - if !ends_with_line_ending(&st) { - if let Some(last) = lines.pop() { - leftover_string.push_str(&last); - } - } - - if !lines.is_empty() { - let success_lines: Vec<_> = lines - .iter() - .map(|x| { - ReturnSuccess::value( - UntaggedValue::string(x).into_untagged_value(), - ) - }) - .collect(); - - Some(futures::stream::iter(success_lines)) - } else { - None + if !ends_with_line_ending(&st) { + if let Some(last) = lines.pop() { + leftover_string.push_str(&last); } } - Value { - value: UntaggedValue::Primitive(Primitive::EndOfStream), - .. - } => { - let st = (&*leftover_string).lock().clone(); - if !st.is_empty() { - Some(futures::stream::iter(vec![ReturnSuccess::value( - UntaggedValue::string(st).into_untagged_value(), - )])) - } else { - None - } + + if !lines.is_empty() { + let success_lines: Vec<_> = lines + .iter() + .map(|x| { + ReturnSuccess::value(UntaggedValue::string(x).into_untagged_value()) + }) + .collect(); + + Some(success_lines) + } else { + None } - Value { - tag: value_span, .. - } => Some(futures::stream::iter(vec![Err( - ShellError::labeled_error_with_secondary( - "Expected a string from pipeline", - "requires string input", - name_span, - "value originates from here", - value_span, - ), - )])), } + Value { + value: UntaggedValue::Primitive(Primitive::EndOfStream), + .. + } => { + let st = (&*leftover_string).lock().clone(); + if !st.is_empty() { + Some(vec![ReturnSuccess::value( + UntaggedValue::string(st).into_untagged_value(), + )]) + } else { + None + } + } + Value { + tag: value_span, .. + } => Some(vec![Err(ShellError::labeled_error_with_secondary( + "Expected a string from pipeline", + "requires string input", + name_span, + "value originates from here", + value_span, + ))]), } }) .flatten() diff --git a/crates/nu-command/src/commands/ls.rs b/crates/nu-command/src/commands/ls.rs index 7e646c7675..9b01b3b1d5 100644 --- a/crates/nu-command/src/commands/ls.rs +++ b/crates/nu-command/src/commands/ls.rs @@ -5,7 +5,6 @@ use nu_protocol::{Signature, SyntaxShape}; pub struct Ls; -#[async_trait] impl WholeStreamCommand for Ls { fn name(&self) -> &str { "ls" @@ -40,11 +39,11 @@ impl WholeStreamCommand for Ls { "View the contents of the current or given path." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let ctrl_c = args.ctrl_c.clone(); let shell_manager = args.shell_manager.clone(); - let (args, _) = args.process().await?; + let (args, _) = args.process()?; shell_manager.ls(args, name, ctrl_c) } diff --git a/crates/nu-command/src/commands/macros.rs b/crates/nu-command/src/commands/macros.rs index 8cd08c8add..e2e71fd501 100644 --- a/crates/nu-command/src/commands/macros.rs +++ b/crates/nu-command/src/commands/macros.rs @@ -33,7 +33,7 @@ macro_rules! command { fn command($args: EvaluatedCommandArgs, ( $($param_name),*, ): ( $($param_type),*, )) -> Result { let output = $body; - Ok(output.boxed().to_output_stream()) + Ok(output.to_output_stream()) } let $args = $args.evaluate_once(registry)?; diff --git a/crates/nu-command/src/commands/math/abs.rs b/crates/nu-command/src/commands/math/abs.rs index 00a9e1724f..96ba2edfcd 100644 --- a/crates/nu-command/src/commands/math/abs.rs +++ b/crates/nu-command/src/commands/math/abs.rs @@ -5,7 +5,6 @@ use nu_protocol::{Primitive, Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math abs" @@ -19,7 +18,7 @@ impl WholeStreamCommand for SubCommand { "Returns absolute values of a list of numbers" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let mapped = args.input.map(move |val| match val.value { UntaggedValue::Primitive(Primitive::Int(val)) => { UntaggedValue::int(val.magnitude().clone()).into() diff --git a/crates/nu-command/src/commands/math/avg.rs b/crates/nu-command/src/commands/math/avg.rs index edcc134621..3617d69c45 100644 --- a/crates/nu-command/src/commands/math/avg.rs +++ b/crates/nu-command/src/commands/math/avg.rs @@ -14,7 +14,6 @@ use bigdecimal::FromPrimitive; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math avg" @@ -28,8 +27,8 @@ impl WholeStreamCommand for SubCommand { "Finds the average of a list of numbers or tables" } - async fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), average).await + fn run(&self, args: CommandArgs) -> Result { + run_with_function(RunnableContext::from_command_args(args), average) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/ceil.rs b/crates/nu-command/src/commands/math/ceil.rs index ac62b6e261..27510ecb17 100644 --- a/crates/nu-command/src/commands/math/ceil.rs +++ b/crates/nu-command/src/commands/math/ceil.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math ceil" @@ -21,14 +20,13 @@ impl WholeStreamCommand for SubCommand { "Applies the ceil function to a list of numbers" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { run_with_numerical_functions_on_stream( RunnableContext::from_command_args(args), ceil_big_int, ceil_big_decimal, ceil_default, ) - .await } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/command.rs b/crates/nu-command/src/commands/math/command.rs index 8c3ea1fef1..718ae4d87a 100644 --- a/crates/nu-command/src/commands/math/command.rs +++ b/crates/nu-command/src/commands/math/command.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "math" @@ -19,7 +18,7 @@ impl WholeStreamCommand for Command { "Use mathematical functions as aggregate functions on a list of numbers or tables." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(Ok(ReturnSuccess::Value( UntaggedValue::string(get_full_help(&Command, &args.scope)).into_value(Tag::unknown()), )))) diff --git a/crates/nu-command/src/commands/math/eval.rs b/crates/nu-command/src/commands/math/eval.rs index 32bc9f9166..ee4239682e 100644 --- a/crates/nu-command/src/commands/math/eval.rs +++ b/crates/nu-command/src/commands/math/eval.rs @@ -11,7 +11,6 @@ pub struct SubCommandArgs { expression: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math eval" @@ -29,8 +28,8 @@ impl WholeStreamCommand for SubCommand { ) } - async fn run(&self, args: CommandArgs) -> Result { - eval(args).await + fn run(&self, args: CommandArgs) -> Result { + eval(args) } fn examples(&self) -> Vec { @@ -46,9 +45,9 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn eval(args: CommandArgs) -> Result { +pub fn eval(args: CommandArgs) -> Result { let name = args.call_info.name_tag.span; - let (SubCommandArgs { expression }, input) = args.process().await?; + let (SubCommandArgs { expression }, input) = args.process()?; if let Some(string) = expression { match parse(&string, &string.tag) { diff --git a/crates/nu-command/src/commands/math/floor.rs b/crates/nu-command/src/commands/math/floor.rs index 1b2736d3e6..e5dd027d48 100644 --- a/crates/nu-command/src/commands/math/floor.rs +++ b/crates/nu-command/src/commands/math/floor.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math floor" @@ -21,14 +20,13 @@ impl WholeStreamCommand for SubCommand { "Applies the floor function to a list of numbers" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { run_with_numerical_functions_on_stream( RunnableContext::from_command_args(args), floor_big_int, floor_big_decimal, floor_default, ) - .await } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/max.rs b/crates/nu-command/src/commands/math/max.rs index 1ed3802f98..0ce405ea79 100644 --- a/crates/nu-command/src/commands/math/max.rs +++ b/crates/nu-command/src/commands/math/max.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math max" @@ -21,8 +20,8 @@ impl WholeStreamCommand for SubCommand { "Finds the maximum within a list of numbers or tables" } - async fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), maximum).await + fn run(&self, args: CommandArgs) -> Result { + run_with_function(RunnableContext::from_command_args(args), maximum) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/median.rs b/crates/nu-command/src/commands/math/median.rs index 0211c4b9de..78c3ae98f9 100644 --- a/crates/nu-command/src/commands/math/median.rs +++ b/crates/nu-command/src/commands/math/median.rs @@ -11,7 +11,6 @@ use nu_protocol::{ pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math median" @@ -25,8 +24,8 @@ impl WholeStreamCommand for SubCommand { "Gets the median of a list of numbers" } - async fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), median).await + fn run(&self, args: CommandArgs) -> Result { + run_with_function(RunnableContext::from_command_args(args), median) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/min.rs b/crates/nu-command/src/commands/math/min.rs index e2580059b9..71918c5827 100644 --- a/crates/nu-command/src/commands/math/min.rs +++ b/crates/nu-command/src/commands/math/min.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math min" @@ -21,8 +20,8 @@ impl WholeStreamCommand for SubCommand { "Finds the minimum within a list of numbers or tables" } - async fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), minimum).await + fn run(&self, args: CommandArgs) -> Result { + run_with_function(RunnableContext::from_command_args(args), minimum) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/mode.rs b/crates/nu-command/src/commands/math/mode.rs index c10f4700ad..e765b2649b 100644 --- a/crates/nu-command/src/commands/math/mode.rs +++ b/crates/nu-command/src/commands/math/mode.rs @@ -7,7 +7,6 @@ use std::cmp::Ordering; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math mode" @@ -21,8 +20,8 @@ impl WholeStreamCommand for SubCommand { "Gets the most frequent element(s) from a list of numbers or tables" } - async fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), mode).await + fn run(&self, args: CommandArgs) -> Result { + run_with_function(RunnableContext::from_command_args(args), mode) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/product.rs b/crates/nu-command/src/commands/math/product.rs index 19778d43b4..66773813cb 100644 --- a/crates/nu-command/src/commands/math/product.rs +++ b/crates/nu-command/src/commands/math/product.rs @@ -7,7 +7,6 @@ use nu_protocol::{Primitive, Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math product" @@ -21,8 +20,8 @@ impl WholeStreamCommand for SubCommand { "Finds the product of a list of numbers or tables" } - async fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), product).await + fn run(&self, args: CommandArgs) -> Result { + run_with_function(RunnableContext::from_command_args(args), product) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/round.rs b/crates/nu-command/src/commands/math/round.rs index 0c36888d8c..a239af15b0 100644 --- a/crates/nu-command/src/commands/math/round.rs +++ b/crates/nu-command/src/commands/math/round.rs @@ -11,7 +11,6 @@ struct Arguments { precision: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math round" @@ -30,8 +29,8 @@ impl WholeStreamCommand for SubCommand { "Applies the round function to a list of numbers" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -58,8 +57,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { precision }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { precision }, input) = args.process()?; let precision = precision.map(|p| p.item).unwrap_or(0); let mapped = input.map(move |val| match val.value { diff --git a/crates/nu-command/src/commands/math/sqrt.rs b/crates/nu-command/src/commands/math/sqrt.rs index 3e5a9b0dc1..4f446b18f8 100644 --- a/crates/nu-command/src/commands/math/sqrt.rs +++ b/crates/nu-command/src/commands/math/sqrt.rs @@ -5,7 +5,6 @@ use nu_protocol::{Primitive, Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math sqrt" @@ -19,8 +18,8 @@ impl WholeStreamCommand for SubCommand { "Applies the square root function to a list of numbers" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + Ok(operate(args)) } fn examples(&self) -> Vec { @@ -35,13 +34,13 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> OutputStream { let mapped = args.input.map(move |val| match val.value { UntaggedValue::Primitive(Primitive::Int(val)) => sqrt_big_decimal(BigDecimal::from(val)), UntaggedValue::Primitive(Primitive::Decimal(val)) => sqrt_big_decimal(val), other => sqrt_default(other), }); - Ok(OutputStream::from_input(mapped)) + OutputStream::from_input(mapped) } fn sqrt_big_decimal(val: BigDecimal) -> Value { diff --git a/crates/nu-command/src/commands/math/stddev.rs b/crates/nu-command/src/commands/math/stddev.rs index 0766de9d2c..6f1723cdfb 100644 --- a/crates/nu-command/src/commands/math/stddev.rs +++ b/crates/nu-command/src/commands/math/stddev.rs @@ -13,7 +13,6 @@ struct Arguments { sample: Tagged, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math stddev" @@ -31,11 +30,11 @@ impl WholeStreamCommand for SubCommand { "Finds the stddev of a list of numbers or tables" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { sample }, mut input) = args.process().await?; + let (Arguments { sample }, mut input) = args.process()?; - let values: Vec = input.drain_vec().await; + let values: Vec = input.drain_vec(); let n = if let Tagged { item: true, .. } = sample { values.len() - 1 diff --git a/crates/nu-command/src/commands/math/sum.rs b/crates/nu-command/src/commands/math/sum.rs index 47a70db966..c94a4b71c2 100644 --- a/crates/nu-command/src/commands/math/sum.rs +++ b/crates/nu-command/src/commands/math/sum.rs @@ -8,7 +8,6 @@ use nu_protocol::{Primitive, Signature, UntaggedValue, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math sum" @@ -22,8 +21,8 @@ impl WholeStreamCommand for SubCommand { "Finds the sum of a list of numbers or tables" } - async fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), summation).await + fn run(&self, args: CommandArgs) -> Result { + run_with_function(RunnableContext::from_command_args(args), summation) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/utils.rs b/crates/nu-command/src/commands/math/utils.rs index 76ee0e5771..ab53854c10 100644 --- a/crates/nu-command/src/commands/math/utils.rs +++ b/crates/nu-command/src/commands/math/utils.rs @@ -6,13 +6,13 @@ use indexmap::map::IndexMap; pub type MathFunction = fn(values: &[Value], tag: &Tag) -> Result; -pub async fn run_with_function( +pub fn run_with_function( RunnableContext { mut input, name, .. }: RunnableContext, mf: MathFunction, ) -> Result { - let values: Vec = input.drain_vec().await; + let values: Vec = input.drain_vec(); let res = calculate(&values, &name, mf); match res { @@ -37,7 +37,7 @@ pub type DecimalFunction = fn(val: BigDecimal) -> Value; pub type DefaultFunction = fn(val: UntaggedValue) -> Value; -pub async fn run_with_numerical_functions_on_stream( +pub fn run_with_numerical_functions_on_stream( RunnableContext { input, .. }: RunnableContext, int_function: IntFunction, decimal_function: DecimalFunction, diff --git a/crates/nu-command/src/commands/math/variance.rs b/crates/nu-command/src/commands/math/variance.rs index 4ab6af858a..55e6cba00d 100644 --- a/crates/nu-command/src/commands/math/variance.rs +++ b/crates/nu-command/src/commands/math/variance.rs @@ -15,7 +15,6 @@ struct Arguments { sample: Tagged, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "math variance" @@ -29,11 +28,11 @@ impl WholeStreamCommand for SubCommand { "Finds the variance of a list of numbers or tables" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { sample }, mut input) = args.process().await?; + let (Arguments { sample }, mut input) = args.process()?; - let values: Vec = input.drain_vec().await; + let values: Vec = input.drain_vec(); let n = if let Tagged { item: true, .. } = sample { values.len() - 1 diff --git a/crates/nu-command/src/commands/merge.rs b/crates/nu-command/src/commands/merge.rs index 9122a1202b..ad150f9d22 100644 --- a/crates/nu-command/src/commands/merge.rs +++ b/crates/nu-command/src/commands/merge.rs @@ -15,7 +15,6 @@ pub struct MergeArgs { block: CapturedBlock, } -#[async_trait] impl WholeStreamCommand for Merge { fn name(&self) -> &str { "merge" @@ -33,8 +32,8 @@ impl WholeStreamCommand for Merge { "Merge a table." } - async fn run(&self, args: CommandArgs) -> Result { - merge(args).await + fn run(&self, args: CommandArgs) -> Result { + merge(args) } fn examples(&self) -> Vec { @@ -46,19 +45,19 @@ impl WholeStreamCommand for Merge { } } -async fn merge(raw_args: CommandArgs) -> Result { +fn merge(raw_args: CommandArgs) -> Result { let context = EvaluationContext::from_args(&raw_args); let name_tag = raw_args.call_info.name_tag.clone(); - let (merge_args, input): (MergeArgs, _) = raw_args.process().await?; + let (merge_args, input): (MergeArgs, _) = raw_args.process()?; let block = merge_args.block; context.scope.enter_scope(); context.scope.add_vars(&block.captured.entries); - let result = run_block(&block.block, &context, InputStream::empty()).await; + let result = run_block(&block.block, &context, InputStream::empty()); context.scope.exit_scope(); let table: Option> = match result { - Ok(mut stream) => Some(stream.drain_vec().await), + Ok(mut stream) => Some(stream.drain_vec()), Err(err) => { return Err(err); } diff --git a/crates/nu-command/src/commands/mkdir.rs b/crates/nu-command/src/commands/mkdir.rs index 8b420eb962..a58f0ff343 100644 --- a/crates/nu-command/src/commands/mkdir.rs +++ b/crates/nu-command/src/commands/mkdir.rs @@ -4,7 +4,6 @@ use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; pub struct Mkdir; -#[async_trait] impl WholeStreamCommand for Mkdir { fn name(&self) -> &str { "mkdir" @@ -23,8 +22,8 @@ impl WholeStreamCommand for Mkdir { "Make directories, creates intermediary directories as required." } - async fn run(&self, args: CommandArgs) -> Result { - mkdir(args).await + fn run(&self, args: CommandArgs) -> Result { + mkdir(args) } fn examples(&self) -> Vec { @@ -36,10 +35,10 @@ impl WholeStreamCommand for Mkdir { } } -async fn mkdir(args: CommandArgs) -> Result { +fn mkdir(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let shell_manager = args.shell_manager.clone(); - let (args, _) = args.process().await?; + let (args, _) = args.process()?; shell_manager.mkdir(args, name) } diff --git a/crates/nu-command/src/commands/move_/command.rs b/crates/nu-command/src/commands/move_/command.rs index 4b01b0f73d..320cd6483a 100644 --- a/crates/nu-command/src/commands/move_/command.rs +++ b/crates/nu-command/src/commands/move_/command.rs @@ -14,7 +14,6 @@ pub struct Arguments { before: Option, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "move" @@ -41,8 +40,8 @@ impl WholeStreamCommand for Command { "Move columns." } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -83,7 +82,7 @@ impl WholeStreamCommand for Command { } } -async fn operate(raw_args: CommandArgs) -> Result { +fn operate(raw_args: CommandArgs) -> Result { let name = raw_args.call_info.name_tag.clone(); let ( Arguments { @@ -92,7 +91,7 @@ async fn operate(raw_args: CommandArgs) -> Result { after, }, input, - ) = raw_args.process().await?; + ) = raw_args.process()?; if columns.is_empty() { return Err(ShellError::labeled_error( diff --git a/crates/nu-command/src/commands/move_/mv.rs b/crates/nu-command/src/commands/move_/mv.rs index 2f480021aa..12c6974408 100644 --- a/crates/nu-command/src/commands/move_/mv.rs +++ b/crates/nu-command/src/commands/move_/mv.rs @@ -5,7 +5,6 @@ use nu_protocol::{Signature, SyntaxShape}; pub struct Mv; -#[async_trait] impl WholeStreamCommand for Mv { fn name(&self) -> &str { "mv" @@ -29,8 +28,8 @@ impl WholeStreamCommand for Mv { "Move files or directories." } - async fn run(&self, args: CommandArgs) -> Result { - mv(args).await + fn run(&self, args: CommandArgs) -> Result { + mv(args) } fn examples(&self) -> Vec { @@ -54,10 +53,10 @@ impl WholeStreamCommand for Mv { } } -async fn mv(args: CommandArgs) -> Result { +fn mv(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let shell_manager = args.shell_manager.clone(); - let (args, _) = args.process().await?; + let (args, _) = args.process()?; shell_manager.mv(args, name) } diff --git a/crates/nu-command/src/commands/next.rs b/crates/nu-command/src/commands/next.rs index 0914dbd6a3..9c74b8647b 100644 --- a/crates/nu-command/src/commands/next.rs +++ b/crates/nu-command/src/commands/next.rs @@ -5,7 +5,6 @@ use nu_protocol::{CommandAction, ReturnSuccess, Signature}; pub struct Next; -#[async_trait] impl WholeStreamCommand for Next { fn name(&self) -> &str { "n" @@ -19,7 +18,7 @@ impl WholeStreamCommand for Next { "Go to next shell." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(next(args)) } } diff --git a/crates/nu-command/src/commands/nth.rs b/crates/nu-command/src/commands/nth.rs index a7550e35df..4df6a701fb 100644 --- a/crates/nu-command/src/commands/nth.rs +++ b/crates/nu-command/src/commands/nth.rs @@ -13,7 +13,6 @@ struct NthArgs { pub struct Nth; -#[async_trait] impl WholeStreamCommand for Nth { fn name(&self) -> &str { "nth" @@ -34,8 +33,8 @@ impl WholeStreamCommand for Nth { "Return or skip only the selected rows." } - async fn run(&self, args: CommandArgs) -> Result { - nth(args).await + fn run(&self, args: CommandArgs) -> Result { + nth(args) } fn examples(&self) -> Vec { @@ -59,7 +58,7 @@ impl WholeStreamCommand for Nth { } } -async fn nth(args: CommandArgs) -> Result { +fn nth(args: CommandArgs) -> Result { let ( NthArgs { row_number, @@ -67,7 +66,7 @@ async fn nth(args: CommandArgs) -> Result { skip, }, input, - ) = args.process().await?; + ) = args.process()?; let row_numbers = vec![vec![row_number], and_rows] .into_iter() @@ -78,11 +77,11 @@ async fn nth(args: CommandArgs) -> Result { Ok(input .enumerate() .filter_map(move |(idx, item)| { - futures::future::ready(if row_numbers.contains(&(idx as u64)) ^ skip { + if row_numbers.contains(&(idx as u64)) ^ skip { Some(ReturnSuccess::value(item)) } else { None - }) + } }) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/nu/plugin.rs b/crates/nu-command/src/commands/nu/plugin.rs index 1c5d53683a..3a69b037db 100644 --- a/crates/nu-command/src/commands/nu/plugin.rs +++ b/crates/nu-command/src/commands/nu/plugin.rs @@ -19,7 +19,6 @@ pub struct Arguments { pub load_path: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "nu plugin" @@ -46,10 +45,10 @@ impl WholeStreamCommand for SubCommand { }] } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let scope = args.scope.clone(); let shell_manager = args.shell_manager.clone(); - let (Arguments { load_path }, _) = args.process().await?; + let (Arguments { load_path }, _) = args.process()?; if let Some(Tagged { item: load_path, diff --git a/crates/nu-command/src/commands/open.rs b/crates/nu-command/src/commands/open.rs index 62b3c7f080..b08e74e6f0 100644 --- a/crates/nu-command/src/commands/open.rs +++ b/crates/nu-command/src/commands/open.rs @@ -1,7 +1,7 @@ use crate::commands::constants::BAT_LANGUAGES; use crate::prelude::*; use encoding_rs::{Encoding, UTF_8}; -use futures_util::StreamExt; + use log::debug; use nu_engine::StringOrBinary; use nu_engine::WholeStreamCommand; @@ -19,7 +19,6 @@ pub struct OpenArgs { encoding: Option>, } -#[async_trait] impl WholeStreamCommand for Open { fn name(&self) -> &str { "open" @@ -58,8 +57,8 @@ For a more complete list of encodings please refer to the encoding_rs documentation link at https://docs.rs/encoding_rs/0.8.28/encoding_rs/#statics"# } - async fn run(&self, args: CommandArgs) -> Result { - open(args).await + fn run(&self, args: CommandArgs) -> Result { + open(args) } fn examples(&self) -> Vec { @@ -100,7 +99,7 @@ pub fn get_encoding(opt: Option>) -> Result<&'static Encoding, Sh } } -async fn open(args: CommandArgs) -> Result { +fn open(args: CommandArgs) -> Result { let scope = args.scope.clone(); let cwd = PathBuf::from(args.shell_manager.path()); let shell_manager = args.shell_manager.clone(); @@ -114,7 +113,7 @@ async fn open(args: CommandArgs) -> Result { encoding, }, _, - ) = args.process().await?; + ) = args.process()?; if path.is_dir() { let args = nu_engine::shell::LsArgs { @@ -148,8 +147,7 @@ async fn open(args: CommandArgs) -> Result { &PathBuf::from(&path.item), path.tag.span, encoding, - ) - .await?; + )?; return Ok(OutputStream::one(ReturnSuccess::action( CommandAction::AutoConvert(tagged_contents, ext), ))); @@ -161,8 +159,7 @@ async fn open(args: CommandArgs) -> Result { &PathBuf::from(&path.item), path.tag.span, encoding, - ) - .await?; + )?; return Ok(OutputStream::one(ReturnSuccess::value(tagged_contents))); } } @@ -199,7 +196,7 @@ async fn open(args: CommandArgs) -> Result { // Note that we do not output a Stream in "fetch" since it is only used by "enter" command // Which we expect to use a concrete Value a not a Stream -pub async fn fetch( +pub fn fetch( cwd: &Path, location: &Path, span: Span, diff --git a/crates/nu-command/src/commands/parse/command.rs b/crates/nu-command/src/commands/parse/command.rs index a5e95f9177..4b0fd17134 100644 --- a/crates/nu-command/src/commands/parse/command.rs +++ b/crates/nu-command/src/commands/parse/command.rs @@ -14,7 +14,6 @@ struct Arguments { pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "parse" @@ -34,8 +33,8 @@ impl WholeStreamCommand for Command { "Parse columns from string data using a simple pattern." } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -50,9 +49,9 @@ impl WholeStreamCommand for Command { } } -pub async fn operate(args: CommandArgs) -> Result { +pub fn operate(args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); - let (Arguments { regex, pattern }, mut input) = args.process().await?; + let (Arguments { regex, pattern }, input) = args.process()?; let regex_pattern = if let Tagged { item: true, tag } = regex { Regex::new(&pattern.item) @@ -68,7 +67,7 @@ pub async fn operate(args: CommandArgs) -> Result { let columns = column_names(®ex_pattern); let mut parsed: VecDeque = VecDeque::new(); - while let Some(v) = input.next().await { + for v in input { match v.as_string() { Ok(s) => { let results = regex_pattern.captures_iter(&s); @@ -96,7 +95,7 @@ pub async fn operate(args: CommandArgs) -> Result { } } - Ok(futures::stream::iter(parsed).to_output_stream()) + Ok(parsed.into_iter().to_output_stream()) } fn build_regex(input: &str, tag: Tag) -> Result { diff --git a/crates/nu-command/src/commands/path/basename.rs b/crates/nu-command/src/commands/path/basename.rs index 7949a271df..6141b55653 100644 --- a/crates/nu-command/src/commands/path/basename.rs +++ b/crates/nu-command/src/commands/path/basename.rs @@ -20,7 +20,6 @@ impl PathSubcommandArguments for PathBasenameArguments { } } -#[async_trait] impl WholeStreamCommand for PathBasename { fn name(&self) -> &str { "path basename" @@ -41,11 +40,11 @@ impl WholeStreamCommand for PathBasename { "Gets the final component of a path" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (PathBasenameArguments { replace, rest }, input) = args.process().await?; + let (PathBasenameArguments { replace, rest }, input) = args.process()?; let args = Arc::new(PathBasenameArguments { replace, rest }); - operate(input, &action, tag.span, args).await + Ok(operate(input, &action, tag.span, args)) } #[cfg(windows)] diff --git a/crates/nu-command/src/commands/path/command.rs b/crates/nu-command/src/commands/path/command.rs index bc96a8346e..6f07b799c3 100644 --- a/crates/nu-command/src/commands/path/command.rs +++ b/crates/nu-command/src/commands/path/command.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; pub struct Path; -#[async_trait] impl WholeStreamCommand for Path { fn name(&self) -> &str { "path" @@ -19,7 +18,7 @@ impl WholeStreamCommand for Path { "Explore and manipulate paths." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(get_full_help(&Path, &args.scope)).into_value(Tag::unknown()), ))) diff --git a/crates/nu-command/src/commands/path/dirname.rs b/crates/nu-command/src/commands/path/dirname.rs index e21886e400..8faa973557 100644 --- a/crates/nu-command/src/commands/path/dirname.rs +++ b/crates/nu-command/src/commands/path/dirname.rs @@ -22,7 +22,6 @@ impl PathSubcommandArguments for PathDirnameArguments { } } -#[async_trait] impl WholeStreamCommand for PathDirname { fn name(&self) -> &str { "path dirname" @@ -49,7 +48,7 @@ impl WholeStreamCommand for PathDirname { "Gets the parent directory of a path" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ( PathDirnameArguments { @@ -58,13 +57,13 @@ impl WholeStreamCommand for PathDirname { rest, }, input, - ) = args.process().await?; + ) = args.process()?; let args = Arc::new(PathDirnameArguments { replace, num_levels, rest, }); - operate(input, &action, tag.span, args).await + Ok(operate(input, &action, tag.span, args)) } #[cfg(windows)] diff --git a/crates/nu-command/src/commands/path/exists.rs b/crates/nu-command/src/commands/path/exists.rs index 084e646554..fb6b2d15f6 100644 --- a/crates/nu-command/src/commands/path/exists.rs +++ b/crates/nu-command/src/commands/path/exists.rs @@ -18,7 +18,6 @@ impl PathSubcommandArguments for PathExistsArguments { } } -#[async_trait] impl WholeStreamCommand for PathExists { fn name(&self) -> &str { "path exists" @@ -33,11 +32,11 @@ impl WholeStreamCommand for PathExists { "Checks whether a path exists" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (PathExistsArguments { rest }, input) = args.process().await?; + let (PathExistsArguments { rest }, input) = args.process()?; let args = Arc::new(PathExistsArguments { rest }); - operate(input, &action, tag.span, args).await + Ok(operate(input, &action, tag.span, args)) } #[cfg(windows)] diff --git a/crates/nu-command/src/commands/path/expand.rs b/crates/nu-command/src/commands/path/expand.rs index 3d74c0dd8f..dab862afe4 100644 --- a/crates/nu-command/src/commands/path/expand.rs +++ b/crates/nu-command/src/commands/path/expand.rs @@ -18,7 +18,6 @@ impl PathSubcommandArguments for PathExpandArguments { } } -#[async_trait] impl WholeStreamCommand for PathExpand { fn name(&self) -> &str { "path expand" @@ -33,11 +32,11 @@ impl WholeStreamCommand for PathExpand { "Expands a path to its absolute form" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (PathExpandArguments { rest }, input) = args.process().await?; + let (PathExpandArguments { rest }, input) = args.process()?; let args = Arc::new(PathExpandArguments { rest }); - operate(input, &action, tag.span, args).await + Ok(operate(input, &action, tag.span, args)) } #[cfg(windows)] diff --git a/crates/nu-command/src/commands/path/extension.rs b/crates/nu-command/src/commands/path/extension.rs index ada2cd7689..69643fc612 100644 --- a/crates/nu-command/src/commands/path/extension.rs +++ b/crates/nu-command/src/commands/path/extension.rs @@ -20,7 +20,6 @@ impl PathSubcommandArguments for PathExtensionArguments { } } -#[async_trait] impl WholeStreamCommand for PathExtension { fn name(&self) -> &str { "path extension" @@ -41,11 +40,11 @@ impl WholeStreamCommand for PathExtension { "Gets the extension of a path" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (PathExtensionArguments { replace, rest }, input) = args.process().await?; + let (PathExtensionArguments { replace, rest }, input) = args.process()?; let args = Arc::new(PathExtensionArguments { replace, rest }); - operate(input, &action, tag.span, args).await + Ok(operate(input, &action, tag.span, args)) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/path/filestem.rs b/crates/nu-command/src/commands/path/filestem.rs index d14b5f3315..40ebc8cda4 100644 --- a/crates/nu-command/src/commands/path/filestem.rs +++ b/crates/nu-command/src/commands/path/filestem.rs @@ -22,7 +22,6 @@ impl PathSubcommandArguments for PathFilestemArguments { } } -#[async_trait] impl WholeStreamCommand for PathFilestem { fn name(&self) -> &str { "path filestem" @@ -55,7 +54,7 @@ impl WholeStreamCommand for PathFilestem { "Gets the file stem of a path" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ( PathFilestemArguments { @@ -65,14 +64,14 @@ impl WholeStreamCommand for PathFilestem { rest, }, input, - ) = args.process().await?; + ) = args.process()?; let args = Arc::new(PathFilestemArguments { prefix, suffix, replace, rest, }); - operate(input, &action, tag.span, args).await + Ok(operate(input, &action, tag.span, args)) } #[cfg(windows)] diff --git a/crates/nu-command/src/commands/path/join.rs b/crates/nu-command/src/commands/path/join.rs index bff5a01b3f..aff34843ec 100644 --- a/crates/nu-command/src/commands/path/join.rs +++ b/crates/nu-command/src/commands/path/join.rs @@ -20,7 +20,6 @@ impl PathSubcommandArguments for PathJoinArguments { } } -#[async_trait] impl WholeStreamCommand for PathJoin { fn name(&self) -> &str { "path join" @@ -36,11 +35,11 @@ impl WholeStreamCommand for PathJoin { "Joins an input path with another path" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (PathJoinArguments { path, rest }, input) = args.process().await?; + let (PathJoinArguments { path, rest }, input) = args.process()?; let args = Arc::new(PathJoinArguments { path, rest }); - operate(input, &action, tag.span, args).await + Ok(operate(input, &action, tag.span, args)) } #[cfg(windows)] diff --git a/crates/nu-command/src/commands/path/mod.rs b/crates/nu-command/src/commands/path/mod.rs index 227f731321..037d218c22 100644 --- a/crates/nu-command/src/commands/path/mod.rs +++ b/crates/nu-command/src/commands/path/mod.rs @@ -55,17 +55,17 @@ where Ok(v) } -async fn operate( +fn operate( input: crate::InputStream, action: &'static F, span: Span, args: Arc, -) -> Result +) -> OutputStream where T: PathSubcommandArguments + Send + Sync + 'static, F: Fn(&Path, &T) -> UntaggedValue + Send + Sync + 'static, { - Ok(input + input .map(move |v| { if args.get_column_paths().is_empty() { ReturnSuccess::value(handle_value(&action, &v, span, Arc::clone(&args))?) @@ -83,5 +83,5 @@ where ReturnSuccess::value(ret) } }) - .to_output_stream()) + .to_output_stream() } diff --git a/crates/nu-command/src/commands/path/type.rs b/crates/nu-command/src/commands/path/type.rs index 11dbcaa31f..197832531a 100644 --- a/crates/nu-command/src/commands/path/type.rs +++ b/crates/nu-command/src/commands/path/type.rs @@ -19,7 +19,6 @@ impl PathSubcommandArguments for PathTypeArguments { } } -#[async_trait] impl WholeStreamCommand for PathType { fn name(&self) -> &str { "path type" @@ -34,11 +33,11 @@ impl WholeStreamCommand for PathType { "Gives the type of the object a path refers to (e.g., file, dir, symlink)" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (PathTypeArguments { rest }, input) = args.process().await?; + let (PathTypeArguments { rest }, input) = args.process()?; let args = Arc::new(PathTypeArguments { rest }); - operate(input, &action, tag.span, args).await + Ok(operate(input, &action, tag.span, args)) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/pivot.rs b/crates/nu-command/src/commands/pivot.rs index dd282219a7..581cd9c8c9 100644 --- a/crates/nu-command/src/commands/pivot.rs +++ b/crates/nu-command/src/commands/pivot.rs @@ -18,7 +18,6 @@ pub struct PivotArgs { ignore_titles: bool, } -#[async_trait] impl WholeStreamCommand for Pivot { fn name(&self) -> &str { "pivot" @@ -46,15 +45,15 @@ impl WholeStreamCommand for Pivot { "Pivots the table contents so rows become columns and columns become rows." } - async fn run(&self, args: CommandArgs) -> Result { - pivot(args).await + fn run(&self, args: CommandArgs) -> Result { + pivot(args) } } -pub async fn pivot(args: CommandArgs) -> Result { +pub fn pivot(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (args, input): (PivotArgs, _) = args.process().await?; - let input = input.into_vec().await; + let (args, input): (PivotArgs, _) = args.process()?; + let input = input.into_vec(); let descs = merge_descriptors(&input); @@ -115,7 +114,7 @@ pub async fn pivot(args: CommandArgs) -> Result { descs }; - Ok(futures::stream::iter(descs.into_iter().map(move |desc| { + Ok((descs.into_iter().map(move |desc| { let mut column_num: usize = 0; let mut dict = TaggedDictBuilder::new(&name); diff --git a/crates/nu-command/src/commands/prepend.rs b/crates/nu-command/src/commands/prepend.rs index 932511293b..d4f8eacb25 100644 --- a/crates/nu-command/src/commands/prepend.rs +++ b/crates/nu-command/src/commands/prepend.rs @@ -10,7 +10,6 @@ struct PrependArgs { pub struct Prepend; -#[async_trait] impl WholeStreamCommand for Prepend { fn name(&self) -> &str { "prepend" @@ -28,8 +27,8 @@ impl WholeStreamCommand for Prepend { "Prepend the given row to the front of the table." } - async fn run(&self, args: CommandArgs) -> Result { - prepend(args).await + fn run(&self, args: CommandArgs) -> Result { + prepend(args) } fn examples(&self) -> Vec { @@ -46,10 +45,10 @@ impl WholeStreamCommand for Prepend { } } -async fn prepend(args: CommandArgs) -> Result { - let (PrependArgs { row }, input) = args.process().await?; +fn prepend(args: CommandArgs) -> Result { + let (PrependArgs { row }, input) = args.process()?; - let bos = futures::stream::iter(vec![row]); + let bos = vec![row].into_iter(); Ok(bos.chain(input).to_output_stream()) } diff --git a/crates/nu-command/src/commands/prev.rs b/crates/nu-command/src/commands/prev.rs index eababdfb63..d5eb23b766 100644 --- a/crates/nu-command/src/commands/prev.rs +++ b/crates/nu-command/src/commands/prev.rs @@ -6,7 +6,6 @@ use nu_engine::WholeStreamCommand; pub struct Previous; -#[async_trait] impl WholeStreamCommand for Previous { fn name(&self) -> &str { "p" @@ -20,7 +19,7 @@ impl WholeStreamCommand for Previous { "Go to previous shell." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(previous(args)) } } diff --git a/crates/nu-command/src/commands/pwd.rs b/crates/nu-command/src/commands/pwd.rs index f28c1c8ba4..cfecb0ed34 100644 --- a/crates/nu-command/src/commands/pwd.rs +++ b/crates/nu-command/src/commands/pwd.rs @@ -5,7 +5,6 @@ use nu_protocol::Signature; pub struct Pwd; -#[async_trait] impl WholeStreamCommand for Pwd { fn name(&self) -> &str { "pwd" @@ -19,8 +18,8 @@ impl WholeStreamCommand for Pwd { "Output the current working directory." } - async fn run(&self, args: CommandArgs) -> Result { - pwd(args).await + fn run(&self, args: CommandArgs) -> Result { + pwd(args) } fn examples(&self) -> Vec { @@ -32,9 +31,9 @@ impl WholeStreamCommand for Pwd { } } -pub async fn pwd(args: CommandArgs) -> Result { +pub fn pwd(args: CommandArgs) -> Result { let shell_manager = args.shell_manager.clone(); - let args = args.evaluate_once().await?; + let args = args.evaluate_once()?; shell_manager.pwd(args) } diff --git a/crates/nu-command/src/commands/random/bool.rs b/crates/nu-command/src/commands/random/bool.rs index 9f0fe83237..24e2e129bd 100644 --- a/crates/nu-command/src/commands/random/bool.rs +++ b/crates/nu-command/src/commands/random/bool.rs @@ -12,7 +12,6 @@ pub struct BoolArgs { bias: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "random bool" @@ -31,8 +30,8 @@ impl WholeStreamCommand for SubCommand { "Generate a random boolean value" } - async fn run(&self, args: CommandArgs) -> Result { - bool_command(args).await + fn run(&self, args: CommandArgs) -> Result { + bool_command(args) } fn examples(&self) -> Vec { @@ -51,8 +50,8 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn bool_command(args: CommandArgs) -> Result { - let (BoolArgs { bias }, _) = args.process().await?; +pub fn bool_command(args: CommandArgs) -> Result { + let (BoolArgs { bias }, _) = args.process()?; let mut probability = 0.5; diff --git a/crates/nu-command/src/commands/random/chars.rs b/crates/nu-command/src/commands/random/chars.rs index e2e73c3602..a4b63a4379 100644 --- a/crates/nu-command/src/commands/random/chars.rs +++ b/crates/nu-command/src/commands/random/chars.rs @@ -15,7 +15,6 @@ pub struct CharsArgs { const DEFAULT_CHARS_LENGTH: u32 = 25; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "random chars" @@ -34,8 +33,8 @@ impl WholeStreamCommand for SubCommand { "Generate random chars" } - async fn run(&self, args: CommandArgs) -> Result { - chars(args).await + fn run(&self, args: CommandArgs) -> Result { + chars(args) } fn examples(&self) -> Vec { @@ -54,8 +53,8 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn chars(args: CommandArgs) -> Result { - let (CharsArgs { length }, _) = args.process().await?; +pub fn chars(args: CommandArgs) -> Result { + let (CharsArgs { length }, _) = args.process()?; let chars_length = length.map_or(DEFAULT_CHARS_LENGTH, |l| l.item); diff --git a/crates/nu-command/src/commands/random/command.rs b/crates/nu-command/src/commands/random/command.rs index 7b0bdc87b3..f57b15568c 100644 --- a/crates/nu-command/src/commands/random/command.rs +++ b/crates/nu-command/src/commands/random/command.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "random" @@ -19,7 +18,7 @@ impl WholeStreamCommand for Command { "Generate random values." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(Ok(ReturnSuccess::Value( UntaggedValue::string(get_full_help(&Command, &args.scope)).into_value(Tag::unknown()), )))) diff --git a/crates/nu-command/src/commands/random/decimal.rs b/crates/nu-command/src/commands/random/decimal.rs index e3a17c1271..f695d15ef1 100644 --- a/crates/nu-command/src/commands/random/decimal.rs +++ b/crates/nu-command/src/commands/random/decimal.rs @@ -14,7 +14,6 @@ pub struct DecimalArgs { range: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "random decimal" @@ -28,8 +27,8 @@ impl WholeStreamCommand for SubCommand { "Generate a random decimal within a range [min..max]" } - async fn run(&self, args: CommandArgs) -> Result { - decimal(args).await + fn run(&self, args: CommandArgs) -> Result { + decimal(args) } fn examples(&self) -> Vec { @@ -58,8 +57,8 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn decimal(args: CommandArgs) -> Result { - let (DecimalArgs { range }, _) = args.process().await?; +pub fn decimal(args: CommandArgs) -> Result { + let (DecimalArgs { range }, _) = args.process()?; let (min, max) = if let Some(range) = &range { (range.item.min() as f64, range.item.max() as f64) diff --git a/crates/nu-command/src/commands/random/dice.rs b/crates/nu-command/src/commands/random/dice.rs index 868086147a..d34446967a 100644 --- a/crates/nu-command/src/commands/random/dice.rs +++ b/crates/nu-command/src/commands/random/dice.rs @@ -13,7 +13,6 @@ pub struct DiceArgs { sides: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "random dice" @@ -39,8 +38,8 @@ impl WholeStreamCommand for SubCommand { "Generate a random dice roll" } - async fn run(&self, args: CommandArgs) -> Result { - dice(args).await + fn run(&self, args: CommandArgs) -> Result { + dice(args) } fn examples(&self) -> Vec { @@ -59,9 +58,9 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn dice(args: CommandArgs) -> Result { +pub fn dice(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (DiceArgs { dice, sides }, _) = args.process().await?; + let (DiceArgs { dice, sides }, _) = args.process()?; let dice = if let Some(dice_tagged) = dice { *dice_tagged @@ -80,7 +79,7 @@ pub async fn dice(args: CommandArgs) -> Result { UntaggedValue::int(thread_rng.gen_range(1, sides + 1)).into_value(tag.clone()) }); - Ok(futures::stream::iter(iter).to_output_stream()) + Ok((iter).to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/random/integer.rs b/crates/nu-command/src/commands/random/integer.rs index 4aa8187152..18a9a69d97 100644 --- a/crates/nu-command/src/commands/random/integer.rs +++ b/crates/nu-command/src/commands/random/integer.rs @@ -14,7 +14,6 @@ pub struct IntegerArgs { range: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "random integer" @@ -28,8 +27,8 @@ impl WholeStreamCommand for SubCommand { "Generate a random integer [min..max]" } - async fn run(&self, args: CommandArgs) -> Result { - integer(args).await + fn run(&self, args: CommandArgs) -> Result { + integer(args) } fn examples(&self) -> Vec { @@ -58,8 +57,8 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn integer(args: CommandArgs) -> Result { - let (IntegerArgs { range }, _) = args.process().await?; +pub fn integer(args: CommandArgs) -> Result { + let (IntegerArgs { range }, _) = args.process()?; let (min, max) = if let Some(range) = &range { (range.item.min(), range.item.max()) diff --git a/crates/nu-command/src/commands/random/uuid.rs b/crates/nu-command/src/commands/random/uuid.rs index 9eca754316..ad2f38e172 100644 --- a/crates/nu-command/src/commands/random/uuid.rs +++ b/crates/nu-command/src/commands/random/uuid.rs @@ -6,7 +6,6 @@ use uuid_crate::Uuid; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "random uuid" @@ -20,8 +19,8 @@ impl WholeStreamCommand for SubCommand { "Generate a random uuid4 string" } - async fn run(&self, args: CommandArgs) -> Result { - uuid(args).await + fn run(&self, args: CommandArgs) -> Result { + uuid(args) } fn examples(&self) -> Vec { @@ -33,7 +32,7 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn uuid(_args: CommandArgs) -> Result { +pub fn uuid(_args: CommandArgs) -> Result { let uuid_4 = Uuid::new_v4().to_hyphenated().to_string(); Ok(OutputStream::one(ReturnSuccess::value(uuid_4))) diff --git a/crates/nu-command/src/commands/range.rs b/crates/nu-command/src/commands/range.rs index 7a980f4598..e4ee5e9d44 100644 --- a/crates/nu-command/src/commands/range.rs +++ b/crates/nu-command/src/commands/range.rs @@ -12,7 +12,6 @@ struct RangeArgs { pub struct Range; -#[async_trait] impl WholeStreamCommand for Range { fn name(&self) -> &str { "range" @@ -30,13 +29,13 @@ impl WholeStreamCommand for Range { "Return only the selected rows." } - async fn run(&self, args: CommandArgs) -> Result { - range(args).await + fn run(&self, args: CommandArgs) -> Result { + range(args) } } -async fn range(args: CommandArgs) -> Result { - let (RangeArgs { area }, input) = args.process().await?; +fn range(args: CommandArgs) -> Result { + let (RangeArgs { area }, input) = args.process()?; let range = area.item; let (from, left_inclusive) = range.from; let (to, right_inclusive) = range.to; diff --git a/crates/nu-command/src/commands/reduce.rs b/crates/nu-command/src/commands/reduce.rs index d4cfd8cb05..9496f0becf 100644 --- a/crates/nu-command/src/commands/reduce.rs +++ b/crates/nu-command/src/commands/reduce.rs @@ -1,6 +1,5 @@ use crate::commands::each; use crate::prelude::*; -use futures::stream::once; use nu_engine::run_block; use nu_engine::WholeStreamCommand; use nu_engine::{CommandArgs, Example}; @@ -19,7 +18,6 @@ pub struct ReduceArgs { numbered: Tagged, } -#[async_trait] impl WholeStreamCommand for Reduce { fn name(&self) -> &str { "reduce" @@ -49,8 +47,8 @@ impl WholeStreamCommand for Reduce { "Block must be (A, A) -> A unless --fold is selected, in which case it may be A, B -> A." } - async fn run(&self, args: CommandArgs) -> Result { - reduce(args).await + fn run(&self, args: CommandArgs) -> Result { + reduce(args) } fn examples(&self) -> Vec { @@ -79,32 +77,32 @@ impl WholeStreamCommand for Reduce { } } -async fn process_row( +fn process_row( block: Arc, context: &EvaluationContext, row: Value, ) -> Result { let row_clone = row.clone(); - let input_stream = once(async { Ok(row_clone) }).to_input_stream(); + let input_stream = vec![Ok(row_clone)].into_iter().to_input_stream(); context.scope.enter_scope(); context.scope.add_vars(&block.captured.entries); context.scope.add_var("$it", row); - let result = run_block(&block.block, context, input_stream).await; + let result = run_block(&block.block, context, input_stream); context.scope.exit_scope(); - Ok(result?) + result } -async fn reduce(raw_args: CommandArgs) -> Result { +fn reduce(raw_args: CommandArgs) -> Result { let span = raw_args.call_info.name_tag.span; let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let (reduce_args, mut input): (ReduceArgs, _) = raw_args.process().await?; + let (reduce_args, mut input): (ReduceArgs, _) = raw_args.process()?; let block = Arc::new(reduce_args.block); let (ioffset, start) = if !input.is_empty() { match reduce_args.fold { None => { - let first = input.next().await.expect("non-empty stream"); + let first = input.next().expect("non-empty stream"); (1, first) } @@ -132,29 +130,26 @@ async fn reduce(raw_args: CommandArgs) -> Result { let block = Arc::clone(&block); let row = each::make_indexed_item(input.0 + ioffset, input.1); - async move { - let values = acc?.drain_vec().await; + let values = acc?.drain_vec(); - let f = if values.len() == 1 { - let value = values - .get(0) - .ok_or_else(|| ShellError::unexpected("No value to update with"))?; - value.clone() - } else if values.is_empty() { - UntaggedValue::nothing().into_untagged_value() - } else { - UntaggedValue::table(&values).into_untagged_value() - }; + let f = if values.len() == 1 { + let value = values + .get(0) + .ok_or_else(|| ShellError::unexpected("No value to update with"))?; + value.clone() + } else if values.is_empty() { + UntaggedValue::nothing().into_untagged_value() + } else { + UntaggedValue::table(&values).into_untagged_value() + }; - context.scope.enter_scope(); - context.scope.add_var("$acc", f); - let result = process_row(block, &*context, row).await; - context.scope.exit_scope(); + context.scope.enter_scope(); + context.scope.add_var("$acc", f); + let result = process_row(block, &*context, row); + context.scope.exit_scope(); - result - } - }) - .await? + result + })? .to_output_stream()) } else { let initial = Ok(InputStream::one(start)); @@ -163,28 +158,25 @@ async fn reduce(raw_args: CommandArgs) -> Result { let block = Arc::clone(&block); let context = context.clone(); - async move { - let values = acc?.drain_vec().await; + let values = acc?.drain_vec(); - let f = if values.len() == 1 { - let value = values - .get(0) - .ok_or_else(|| ShellError::unexpected("No value to update with"))?; - value.clone() - } else if values.is_empty() { - UntaggedValue::nothing().into_untagged_value() - } else { - UntaggedValue::table(&values).into_untagged_value() - }; + let f = if values.len() == 1 { + let value = values + .get(0) + .ok_or_else(|| ShellError::unexpected("No value to update with"))?; + value.clone() + } else if values.is_empty() { + UntaggedValue::nothing().into_untagged_value() + } else { + UntaggedValue::table(&values).into_untagged_value() + }; - context.scope.enter_scope(); - context.scope.add_var("$acc", f); - let result = process_row(block, &*context, row).await; - context.scope.exit_scope(); - result - } - }) - .await? + context.scope.enter_scope(); + context.scope.add_var("$acc", f); + let result = process_row(block, &*context, row); + context.scope.exit_scope(); + result + })? .to_output_stream()) } } diff --git a/crates/nu-command/src/commands/reject.rs b/crates/nu-command/src/commands/reject.rs index 4bd259f19d..5d805e159d 100644 --- a/crates/nu-command/src/commands/reject.rs +++ b/crates/nu-command/src/commands/reject.rs @@ -12,7 +12,6 @@ pub struct RejectArgs { pub struct Reject; -#[async_trait] impl WholeStreamCommand for Reject { fn name(&self) -> &str { "reject" @@ -26,8 +25,8 @@ impl WholeStreamCommand for Reject { "Remove the given columns from the table. If you want to remove rows, try 'drop'." } - async fn run(&self, args: CommandArgs) -> Result { - reject(args).await + fn run(&self, args: CommandArgs) -> Result { + reject(args) } fn examples(&self) -> Vec { @@ -39,9 +38,9 @@ impl WholeStreamCommand for Reject { } } -async fn reject(args: CommandArgs) -> Result { +fn reject(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (RejectArgs { rest: fields }, input) = args.process().await?; + let (RejectArgs { rest: fields }, input) = args.process()?; if fields.is_empty() { return Err(ShellError::labeled_error( "Reject requires fields", diff --git a/crates/nu-command/src/commands/rename.rs b/crates/nu-command/src/commands/rename.rs index 82254a0532..d7f29aad71 100644 --- a/crates/nu-command/src/commands/rename.rs +++ b/crates/nu-command/src/commands/rename.rs @@ -13,7 +13,6 @@ pub struct Arguments { rest: Vec>, } -#[async_trait] impl WholeStreamCommand for Rename { fn name(&self) -> &str { "rename" @@ -33,8 +32,8 @@ impl WholeStreamCommand for Rename { "Creates a new table with columns renamed." } - async fn run(&self, args: CommandArgs) -> Result { - rename(args).await + fn run(&self, args: CommandArgs) -> Result { + rename(args) } fn examples(&self) -> Vec { @@ -62,9 +61,9 @@ impl WholeStreamCommand for Rename { } } -pub async fn rename(args: CommandArgs) -> Result { +pub fn rename(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { column_name, rest }, input) = args.process().await?; + let (Arguments { column_name, rest }, input) = args.process()?; let mut new_column_names = vec![vec![column_name]]; new_column_names.push(rest); diff --git a/crates/nu-command/src/commands/reverse.rs b/crates/nu-command/src/commands/reverse.rs index 1ac3659327..da6dd4384a 100644 --- a/crates/nu-command/src/commands/reverse.rs +++ b/crates/nu-command/src/commands/reverse.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; pub struct Reverse; -#[async_trait] impl WholeStreamCommand for Reverse { fn name(&self) -> &str { "reverse" @@ -19,8 +18,8 @@ impl WholeStreamCommand for Reverse { "Reverses the table." } - async fn run(&self, args: CommandArgs) -> Result { - reverse(args).await + fn run(&self, args: CommandArgs) -> Result { + reverse(args) } fn examples(&self) -> Vec { @@ -38,12 +37,12 @@ impl WholeStreamCommand for Reverse { } } -async fn reverse(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn reverse(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let (input, _args) = args.parts(); - let input = input.collect::>().await; - Ok(futures::stream::iter(input.into_iter().rev().map(ReturnSuccess::value)).to_output_stream()) + let input = input.collect::>(); + Ok((input.into_iter().rev().map(ReturnSuccess::value)).to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/rm.rs b/crates/nu-command/src/commands/rm.rs index 6fc7df6432..b9149c4526 100644 --- a/crates/nu-command/src/commands/rm.rs +++ b/crates/nu-command/src/commands/rm.rs @@ -6,7 +6,6 @@ use nu_protocol::{Signature, SyntaxShape}; pub struct Remove; -#[async_trait] impl WholeStreamCommand for Remove { fn name(&self) -> &str { "rm" @@ -33,8 +32,8 @@ impl WholeStreamCommand for Remove { "Remove file(s)." } - async fn run(&self, args: CommandArgs) -> Result { - rm(args).await + fn run(&self, args: CommandArgs) -> Result { + rm(args) } fn examples(&self) -> Vec { @@ -63,10 +62,10 @@ impl WholeStreamCommand for Remove { } } -async fn rm(args: CommandArgs) -> Result { +fn rm(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let shell_manager = args.shell_manager.clone(); - let (args, _): (RemoveArgs, _) = args.process().await?; + let (args, _): (RemoveArgs, _) = args.process()?; if args.trash.item && args.permanent.item { return Ok(OutputStream::one(Err(ShellError::labeled_error( diff --git a/crates/nu-command/src/commands/roll/column.rs b/crates/nu-command/src/commands/roll/column.rs index d8448d526f..759a84fdff 100644 --- a/crates/nu-command/src/commands/roll/column.rs +++ b/crates/nu-command/src/commands/roll/column.rs @@ -31,7 +31,6 @@ impl Arguments { } } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "roll column" @@ -48,24 +47,22 @@ impl WholeStreamCommand for SubCommand { "Rolls the table columns" } - async fn run(&self, args: CommandArgs) -> Result { - roll(args).await + fn run(&self, args: CommandArgs) -> Result { + roll(args) } } -pub async fn roll(args: CommandArgs) -> Result { - let (args, input) = args.process().await?; +pub fn roll(args: CommandArgs) -> Result { + let (args, input) = args.process()?; Ok(input .map(move |value| { - futures::stream::iter({ - let tag = value.tag(); + let tag = value.tag(); - roll_by(value, &args) - .unwrap_or_else(|| vec![UntaggedValue::nothing().into_value(tag)]) - .into_iter() - .map(ReturnSuccess::value) - }) + roll_by(value, &args) + .unwrap_or_else(|| vec![UntaggedValue::nothing().into_value(tag)]) + .into_iter() + .map(ReturnSuccess::value) }) .flatten() .to_output_stream()) diff --git a/crates/nu-command/src/commands/roll/command.rs b/crates/nu-command/src/commands/roll/command.rs index c4de00ad55..a470a065fb 100644 --- a/crates/nu-command/src/commands/roll/command.rs +++ b/crates/nu-command/src/commands/roll/command.rs @@ -13,7 +13,6 @@ pub struct Arguments { by: Option>, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "roll" @@ -27,23 +26,21 @@ impl WholeStreamCommand for Command { "Rolls the table rows." } - async fn run(&self, args: CommandArgs) -> Result { - roll(args).await + fn run(&self, args: CommandArgs) -> Result { + roll(args) } } -pub async fn roll(args: CommandArgs) -> Result { +pub fn roll(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (args, mut input) = args.process().await?; + let (args, mut input) = args.process()?; - let values = input.drain_vec().await; + let values = input.drain_vec(); - Ok(futures::stream::iter( - roll_down(values, &args) - .unwrap_or_else(|| vec![UntaggedValue::nothing().into_value(&name)]) - .into_iter() - .map(ReturnSuccess::value), - ) + Ok((roll_down(values, &args) + .unwrap_or_else(|| vec![UntaggedValue::nothing().into_value(&name)]) + .into_iter() + .map(ReturnSuccess::value)) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/roll/up.rs b/crates/nu-command/src/commands/roll/up.rs index d371e176a6..d012918414 100644 --- a/crates/nu-command/src/commands/roll/up.rs +++ b/crates/nu-command/src/commands/roll/up.rs @@ -13,7 +13,6 @@ pub struct Arguments { by: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "roll up" @@ -27,23 +26,21 @@ impl WholeStreamCommand for SubCommand { "Rolls the table rows" } - async fn run(&self, args: CommandArgs) -> Result { - roll(args).await + fn run(&self, args: CommandArgs) -> Result { + roll(args) } } -pub async fn roll(args: CommandArgs) -> Result { +pub fn roll(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (args, mut input) = args.process().await?; + let (args, mut input) = args.process()?; - let values = input.drain_vec().await; + let values = input.drain_vec(); - Ok(futures::stream::iter( - roll_up(values, &args) - .unwrap_or_else(|| vec![UntaggedValue::nothing().into_value(&name)]) - .into_iter() - .map(ReturnSuccess::value), - ) + Ok((roll_up(values, &args) + .unwrap_or_else(|| vec![UntaggedValue::nothing().into_value(&name)]) + .into_iter() + .map(ReturnSuccess::value)) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/rotate/command.rs b/crates/nu-command/src/commands/rotate/command.rs index 9006086c73..b3287d3870 100644 --- a/crates/nu-command/src/commands/rotate/command.rs +++ b/crates/nu-command/src/commands/rotate/command.rs @@ -15,7 +15,6 @@ pub struct Arguments { rest: Vec>, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "rotate" @@ -32,16 +31,16 @@ impl WholeStreamCommand for Command { "Rotates the table by 90 degrees clockwise." } - async fn run(&self, args: CommandArgs) -> Result { - rotate(args).await + fn run(&self, args: CommandArgs) -> Result { + rotate(args) } } -pub async fn rotate(args: CommandArgs) -> Result { +pub fn rotate(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { rest }, input) = args.process().await?; + let (Arguments { rest }, input) = args.process()?; - let input = input.into_vec().await; + let input = input.into_vec(); let total_rows = input.len(); let descs = merge_descriptors(&input); let total_descriptors = descs.len(); @@ -80,42 +79,39 @@ pub async fn rotate(args: CommandArgs) -> Result { &name, )?; - Ok(futures::stream::iter( - (0..total_descriptors) - .map(move |row_number| { - let mut row = TaggedDictBuilder::new(&name); + Ok(((0..total_descriptors) + .map(move |row_number| { + let mut row = TaggedDictBuilder::new(&name); - for (current_numbered_column, (column_name, _)) in values.row_entries().enumerate() - { - let raw_column_path = - format!("{}.0.{}", column_name, descs[row_number]).spanned_unknown(); - let path = ColumnPath::build(&raw_column_path); + for (current_numbered_column, (column_name, _)) in values.row_entries().enumerate() { + let raw_column_path = + format!("{}.0.{}", column_name, descs[row_number]).spanned_unknown(); + let path = ColumnPath::build(&raw_column_path); - match &values.get_data_by_column_path(&path, Box::new(move |_, _, error| error)) - { - Ok(x) => { - row.insert_value( - rest.get(current_numbered_column) - .map(|c| c.item.clone()) - .unwrap_or_else(|| column_name.to_string()), - x.clone(), - ); - } - Err(_) => {} + match &values.get_data_by_column_path(&path, Box::new(move |_, _, error| error)) { + Ok(x) => { + row.insert_value( + rest.get(current_numbered_column) + .map(|c| c.item.clone()) + .unwrap_or_else(|| column_name.to_string()), + x.clone(), + ); } + Err(_) => {} } + } - row.insert_value( - rest.get(total_rows) - .map(|c| c.item.clone()) - .unwrap_or_else(|| format!("Column{}", total_rows)), - UntaggedValue::string(&descs[row_number]).into_untagged_value(), - ); + row.insert_value( + rest.get(total_rows) + .map(|c| c.item.clone()) + .unwrap_or_else(|| format!("Column{}", total_rows)), + UntaggedValue::string(&descs[row_number]).into_untagged_value(), + ); - ReturnSuccess::value(row.into_value()) - }) - .rev() - .collect::>(), - ) + ReturnSuccess::value(row.into_value()) + }) + .rev() + .collect::>()) + .into_iter() .to_output_stream()) } diff --git a/crates/nu-command/src/commands/rotate/counter_clockwise.rs b/crates/nu-command/src/commands/rotate/counter_clockwise.rs index 64f123317c..17e7548a82 100644 --- a/crates/nu-command/src/commands/rotate/counter_clockwise.rs +++ b/crates/nu-command/src/commands/rotate/counter_clockwise.rs @@ -15,7 +15,6 @@ pub struct Arguments { rest: Vec>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "rotate counter-clockwise" @@ -32,16 +31,16 @@ impl WholeStreamCommand for SubCommand { "Rotates the table by 90 degrees counter clockwise." } - async fn run(&self, args: CommandArgs) -> Result { - rotate(args).await + fn run(&self, args: CommandArgs) -> Result { + rotate(args) } } -pub async fn rotate(args: CommandArgs) -> Result { +pub fn rotate(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { rest }, input) = args.process().await?; + let (Arguments { rest }, input) = args.process()?; - let input = input.into_vec().await; + let input = input.into_vec(); let descs = merge_descriptors(&input); let total_rows = input.len(); @@ -75,43 +74,40 @@ pub async fn rotate(args: CommandArgs) -> Result { &name, )?; - Ok(futures::stream::iter( - (0..descs.len()) - .rev() - .map(move |row_number| { - let mut row = TaggedDictBuilder::new(&name); + Ok(((0..descs.len()) + .rev() + .map(move |row_number| { + let mut row = TaggedDictBuilder::new(&name); - row.insert_value( - rest.get(0) - .map(|c| c.item.clone()) - .unwrap_or_else(|| String::from("Column0")), - UntaggedValue::string(descs.get(row_number).unwrap_or(&String::new())) - .into_untagged_value(), - ); + row.insert_value( + rest.get(0) + .map(|c| c.item.clone()) + .unwrap_or_else(|| String::from("Column0")), + UntaggedValue::string(descs.get(row_number).unwrap_or(&String::new())) + .into_untagged_value(), + ); - for (current_numbered_column, (column_name, _)) in values.row_entries().enumerate() - { - let raw_column_path = - format!("{}.0.{}", column_name, &descs[row_number]).spanned_unknown(); - let path = ColumnPath::build(&raw_column_path); + for (current_numbered_column, (column_name, _)) in values.row_entries().enumerate() { + let raw_column_path = + format!("{}.0.{}", column_name, &descs[row_number]).spanned_unknown(); + let path = ColumnPath::build(&raw_column_path); - match &values.get_data_by_column_path(&path, Box::new(move |_, _, error| error)) - { - Ok(x) => { - row.insert_value( - rest.get(current_numbered_column + 1) - .map(|c| c.item.clone()) - .unwrap_or_else(|| column_name.to_string()), - x.clone(), - ); - } - Err(_) => {} + match &values.get_data_by_column_path(&path, Box::new(move |_, _, error| error)) { + Ok(x) => { + row.insert_value( + rest.get(current_numbered_column + 1) + .map(|c| c.item.clone()) + .unwrap_or_else(|| column_name.to_string()), + x.clone(), + ); } + Err(_) => {} } + } - ReturnSuccess::value(row.into_value()) - }) - .collect::>(), - ) + ReturnSuccess::value(row.into_value()) + }) + .collect::>()) + .into_iter() .to_output_stream()) } diff --git a/crates/nu-command/src/commands/run_external.rs b/crates/nu-command/src/commands/run_external.rs index 8a5e404e94..666a23e2b9 100644 --- a/crates/nu-command/src/commands/run_external.rs +++ b/crates/nu-command/src/commands/run_external.rs @@ -37,7 +37,6 @@ fn spanned_expression_to_string(expr: SpannedExpression) -> Result &str { "run_external" @@ -63,7 +62,7 @@ impl WholeStreamCommand for RunExternalCommand { true } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let positionals = args.call_info.args.positional.clone().ok_or_else(|| { ShellError::untagged_runtime_error("positional arguments unexpectedly empty") })?; @@ -105,7 +104,7 @@ impl WholeStreamCommand for RunExternalCommand { // If we're in interactive mode, we will "auto cd". That is, instead of interpreting // this as an external command, we will see it as a path and `cd` into it. if is_interactive { - if let Some(path) = maybe_autocd_dir(&command, &mut external_context).await { + if let Some(path) = maybe_autocd_dir(&command, &mut external_context) { let cd_args = CdArgs { path: Some(Tagged { item: PathBuf::from(path), @@ -127,18 +126,14 @@ impl WholeStreamCommand for RunExternalCommand { &mut external_context, input, external_redirection, - ) - .await; + ); Ok(result?.to_output_stream()) } } #[allow(unused_variables)] -async fn maybe_autocd_dir<'a>( - cmd: &ExternalCommand, - ctx: &mut EvaluationContext, -) -> Option { +fn maybe_autocd_dir(cmd: &ExternalCommand, ctx: &mut EvaluationContext) -> Option { // We will "auto cd" if // - the command name ends in a path separator, or // - it's not a command on the path and no arguments were given. diff --git a/crates/nu-command/src/commands/save.rs b/crates/nu-command/src/commands/save.rs index 74b540be8f..59708c715b 100644 --- a/crates/nu-command/src/commands/save.rs +++ b/crates/nu-command/src/commands/save.rs @@ -132,7 +132,6 @@ pub struct SaveArgs { raw: bool, } -#[async_trait] impl WholeStreamCommand for Save { fn name(&self) -> &str { "save" @@ -156,12 +155,12 @@ impl WholeStreamCommand for Save { "Save the contents of the pipeline to a file." } - async fn run(&self, args: CommandArgs) -> Result { - save(args).await + fn run(&self, args: CommandArgs) -> Result { + save(args) } } -async fn save(raw_args: CommandArgs) -> Result { +fn save(raw_args: CommandArgs) -> Result { let mut full_path = PathBuf::from(raw_args.shell_manager.path()); let name_tag = raw_args.call_info.name_tag.clone(); let name = raw_args.call_info.name_tag.clone(); @@ -179,8 +178,8 @@ async fn save(raw_args: CommandArgs) -> Result { raw: save_raw, }, input, - ) = raw_args.process().await?; - let input: Vec = input.collect().await; + ) = raw_args.process()?; + let input: Vec = input.collect(); if path.is_none() { let mut should_return_file_path_error = true; @@ -198,7 +197,7 @@ async fn save(raw_args: CommandArgs) -> Result { return Err(ShellError::labeled_error( "Save requires a filepath", "needs path", - name_tag.clone(), + name_tag, )); } } else if let Some(file) = path { @@ -231,9 +230,8 @@ async fn save(raw_args: CommandArgs) -> Result { }, scope, }; - let mut result = converter.run(new_args.with_input(input)).await?; - let result_vec: Vec> = - result.drain_vec().await; + let mut result = converter.run(new_args.with_input(input))?; + let result_vec: Vec> = result.drain_vec(); if converter.is_binary() { process_binary_return_success!('scope, result_vec, name_tag) } else { diff --git a/crates/nu-command/src/commands/select.rs b/crates/nu-command/src/commands/select.rs index f8ddb49796..83947f5203 100644 --- a/crates/nu-command/src/commands/select.rs +++ b/crates/nu-command/src/commands/select.rs @@ -15,7 +15,6 @@ struct Arguments { pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "select" @@ -29,8 +28,8 @@ impl WholeStreamCommand for Command { "Down-select table to only these columns." } - async fn run(&self, args: CommandArgs) -> Result { - select(args).await + fn run(&self, args: CommandArgs) -> Result { + select(args) } fn examples(&self) -> Vec { @@ -49,9 +48,9 @@ impl WholeStreamCommand for Command { } } -async fn select(args: CommandArgs) -> Result { +fn select(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { mut rest }, mut input) = args.process().await?; + let (Arguments { mut rest }, input) = args.process()?; let (columns, _) = arguments(&mut rest)?; if columns.is_empty() { @@ -64,7 +63,7 @@ async fn select(args: CommandArgs) -> Result { let mut bring_back: indexmap::IndexMap> = indexmap::IndexMap::new(); - while let Some(value) = input.next().await { + for value in input { for path in &columns { let fetcher = get_data_by_column_path( &value, @@ -137,7 +136,7 @@ async fn select(args: CommandArgs) -> Result { let keys = bring_back.keys().cloned().collect::>(); - Ok(futures::stream::iter((0..max).map(move |current| { + Ok(((0..max).map(move |current| { let mut out = TaggedDictBuilder::new(name.clone()); for k in &keys { diff --git a/crates/nu-command/src/commands/seq.rs b/crates/nu-command/src/commands/seq.rs index 0735f9c37c..ce00257da1 100644 --- a/crates/nu-command/src/commands/seq.rs +++ b/crates/nu-command/src/commands/seq.rs @@ -16,7 +16,6 @@ pub struct SeqArgs { widths: Tagged, } -#[async_trait] impl WholeStreamCommand for Seq { fn name(&self) -> &str { "seq" @@ -48,8 +47,8 @@ impl WholeStreamCommand for Seq { "Print sequences of numbers." } - async fn run(&self, args: CommandArgs) -> Result { - seq(args).await + fn run(&self, args: CommandArgs) -> Result { + seq(args) } fn examples(&self) -> Vec { @@ -89,7 +88,7 @@ impl WholeStreamCommand for Seq { } } -async fn seq(args: CommandArgs) -> Result { +fn seq(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let ( @@ -100,7 +99,7 @@ async fn seq(args: CommandArgs) -> Result { widths, }, _, - ) = args.process().await?; + ) = args.process()?; if rest_nums.is_empty() { return Err(ShellError::labeled_error( @@ -318,5 +317,5 @@ fn print_seq( .lines() .map(|v| v.to_str_value_create_tag()) .collect(); - futures::stream::iter(rows.into_iter().map(ReturnSuccess::value)).to_output_stream() + (rows.into_iter().map(ReturnSuccess::value)).to_output_stream() } diff --git a/crates/nu-command/src/commands/seq_dates.rs b/crates/nu-command/src/commands/seq_dates.rs index c8e514202f..99a7c263ca 100644 --- a/crates/nu-command/src/commands/seq_dates.rs +++ b/crates/nu-command/src/commands/seq_dates.rs @@ -21,7 +21,6 @@ pub struct SeqDatesArgs { reverse: Tagged, } -#[async_trait] impl WholeStreamCommand for SeqDates { fn name(&self) -> &str { "seq date" @@ -73,8 +72,8 @@ impl WholeStreamCommand for SeqDates { "print sequences of dates" } - async fn run(&self, args: CommandArgs) -> Result { - seq_dates(args).await + fn run(&self, args: CommandArgs) -> Result { + seq_dates(args) } fn examples(&self) -> Vec { @@ -132,7 +131,7 @@ impl WholeStreamCommand for SeqDates { } } -async fn seq_dates(args: CommandArgs) -> Result { +fn seq_dates(args: CommandArgs) -> Result { let _name = args.call_info.name_tag.clone(); let ( @@ -147,7 +146,7 @@ async fn seq_dates(args: CommandArgs) -> Result { reverse, }, _, - ) = args.process().await?; + ) = args.process()?; let sep: String = match separator { Some(s) => { @@ -354,7 +353,7 @@ pub fn run_seq_dates( .lines() .map(|v| v.to_str_value_create_tag()) .collect(); - Ok(futures::stream::iter(rows.into_iter().map(ReturnSuccess::value)).to_output_stream()) + Ok((rows.into_iter().map(ReturnSuccess::value)).to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/shells.rs b/crates/nu-command/src/commands/shells.rs index 23c90f8324..b833a76df8 100644 --- a/crates/nu-command/src/commands/shells.rs +++ b/crates/nu-command/src/commands/shells.rs @@ -6,7 +6,6 @@ use std::sync::atomic::Ordering; pub struct Shells; -#[async_trait] impl WholeStreamCommand for Shells { fn name(&self) -> &str { "shells" @@ -20,7 +19,7 @@ impl WholeStreamCommand for Shells { "Display the list of current shells." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(shells(args)) } } diff --git a/crates/nu-command/src/commands/shuffle.rs b/crates/nu-command/src/commands/shuffle.rs index 629cdc69ea..ce1e5eca01 100644 --- a/crates/nu-command/src/commands/shuffle.rs +++ b/crates/nu-command/src/commands/shuffle.rs @@ -8,7 +8,6 @@ use rand::thread_rng; pub struct Shuffle; -#[async_trait] impl WholeStreamCommand for Shuffle { fn name(&self) -> &str { "shuffle" @@ -18,18 +17,21 @@ impl WholeStreamCommand for Shuffle { "Shuffle rows randomly." } - async fn run(&self, args: CommandArgs) -> Result { - shuffle(args).await + fn run(&self, args: CommandArgs) -> Result { + Ok(shuffle(args)) } } -async fn shuffle(args: CommandArgs) -> Result { +fn shuffle(args: CommandArgs) -> OutputStream { let input = args.input; - let mut values: Vec = input.collect().await; + let mut values: Vec = input.collect(); values.shuffle(&mut thread_rng()); - Ok(futures::stream::iter(values.into_iter().map(ReturnSuccess::value)).to_output_stream()) + values + .into_iter() + .map(ReturnSuccess::value) + .to_output_stream() } #[cfg(test)] diff --git a/crates/nu-command/src/commands/size.rs b/crates/nu-command/src/commands/size.rs index b8ee381ac3..16e7027bff 100644 --- a/crates/nu-command/src/commands/size.rs +++ b/crates/nu-command/src/commands/size.rs @@ -9,7 +9,6 @@ use unicode_segmentation::UnicodeSegmentation; pub struct Size; -#[async_trait] impl WholeStreamCommand for Size { fn name(&self) -> &str { "size" @@ -23,7 +22,7 @@ impl WholeStreamCommand for Size { "Gather word count statistics on the text." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(size(args)) } diff --git a/crates/nu-command/src/commands/skip/command.rs b/crates/nu-command/src/commands/skip/command.rs index 456425cc1e..5b98b2db6e 100644 --- a/crates/nu-command/src/commands/skip/command.rs +++ b/crates/nu-command/src/commands/skip/command.rs @@ -11,7 +11,6 @@ pub struct Arguments { rows: Option>, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "skip" @@ -25,8 +24,8 @@ impl WholeStreamCommand for Command { "Skip some number of rows." } - async fn run(&self, args: CommandArgs) -> Result { - skip(args).await + fn run(&self, args: CommandArgs) -> Result { + skip(args) } fn examples(&self) -> Vec { @@ -41,8 +40,8 @@ impl WholeStreamCommand for Command { } } -async fn skip(args: CommandArgs) -> Result { - let (Arguments { rows }, input) = args.process().await?; +fn skip(args: CommandArgs) -> Result { + let (Arguments { rows }, input) = args.process()?; let rows_desired = if let Some(quantity) = rows { *quantity } else { diff --git a/crates/nu-command/src/commands/skip/until.rs b/crates/nu-command/src/commands/skip/until.rs index 22cc52a7c3..bd209f7279 100644 --- a/crates/nu-command/src/commands/skip/until.rs +++ b/crates/nu-command/src/commands/skip/until.rs @@ -7,7 +7,6 @@ use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "skip until" @@ -27,9 +26,9 @@ impl WholeStreamCommand for SubCommand { "Skips rows until the condition matches." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); - let call_info = args.evaluate_once().await?; + let call_info = args.evaluate_once()?; let block = call_info.args.expect_nth(0)?.clone(); @@ -87,13 +86,11 @@ impl WholeStreamCommand for SubCommand { ctx.scope.add_vars(&captured.entries); trace!("ITEM = {:?}", item); - async move { - let result = evaluate_baseline_expr(&*condition, &*ctx).await; - ctx.scope.exit_scope(); - trace!("RESULT = {:?}", result); + let result = evaluate_baseline_expr(&*condition, &*ctx); + ctx.scope.exit_scope(); + trace!("RESULT = {:?}", result); - !matches!(result, Ok(ref v) if v.is_true()) - } + !matches!(result, Ok(ref v) if v.is_true()) }) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/skip/while_.rs b/crates/nu-command/src/commands/skip/while_.rs index 7cdd1320df..874ac3e23b 100644 --- a/crates/nu-command/src/commands/skip/while_.rs +++ b/crates/nu-command/src/commands/skip/while_.rs @@ -7,7 +7,6 @@ use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "skip while" @@ -27,9 +26,9 @@ impl WholeStreamCommand for SubCommand { "Skips rows while the condition matches." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&args)); - let call_info = args.evaluate_once().await?; + let call_info = args.evaluate_once()?; let block = call_info.args.expect_nth(0)?.clone(); @@ -88,13 +87,11 @@ impl WholeStreamCommand for SubCommand { ctx.scope.add_var("$it", item.clone()); trace!("ITEM = {:?}", item); - async move { - let result = evaluate_baseline_expr(&*condition, &*ctx).await; - ctx.scope.exit_scope(); - trace!("RESULT = {:?}", result); + let result = evaluate_baseline_expr(&*condition, &*ctx); + ctx.scope.exit_scope(); + trace!("RESULT = {:?}", result); - matches!(result, Ok(ref v) if v.is_true()) - } + matches!(result, Ok(ref v) if v.is_true()) }) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/sleep.rs b/crates/nu-command/src/commands/sleep.rs index 49de864c83..c040d739e2 100644 --- a/crates/nu-command/src/commands/sleep.rs +++ b/crates/nu-command/src/commands/sleep.rs @@ -1,16 +1,12 @@ use crate::prelude::*; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; -use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; +use nu_protocol::{ReturnValue, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; -use parking_lot::Mutex; use std::{ - future::Future, - pin::Pin, sync::atomic::Ordering, - task::{Poll, Waker}, thread, - time::Duration, + time::{Duration, Instant}, }; const CTRL_C_CHECK_INTERVAL: Duration = Duration::from_millis(100); @@ -23,7 +19,6 @@ pub struct SleepArgs { pub rest: Vec>, } -#[async_trait] impl WholeStreamCommand for Sleep { fn name(&self) -> &str { "sleep" @@ -39,10 +34,10 @@ impl WholeStreamCommand for Sleep { "Delay for a specified amount of time." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let ctrl_c = args.ctrl_c().clone(); - let (SleepArgs { duration, rest }, input) = args.process().await?; + let (SleepArgs { duration, rest }, _) = args.process()?; let total_dur = Duration::from_nanos(duration.item) + rest @@ -50,15 +45,18 @@ impl WholeStreamCommand for Sleep { .map(|val| Duration::from_nanos(val.item)) .sum::(); - SleepFuture::new(total_dur, ctrl_c).await; + //SleepHandler::new(total_dur, ctrl_c); // this is necessary because the following 2 commands gave different results: // `echo | sleep 1sec` - nothing // `sleep 1sec` - table with 0 elements - if input.is_empty() { - Ok(OutputStream::empty()) - } else { - Ok(input.into()) - } + + Ok(SleepIterator::new(total_dur, ctrl_c).to_output_stream()) + + // if input.is_empty() { + // Ok(OutputStream::empty()) + // } else { + // Ok(input.into()) + // } } fn examples(&self) -> Vec { @@ -74,101 +72,138 @@ impl WholeStreamCommand for Sleep { result: None, }, Example { - description: "Delay the output of another command by 1sec", - example: "echo [55 120] | sleep 1sec", - result: Some(vec![ - UntaggedValue::int(55).into(), - UntaggedValue::int(120).into(), - ]), + description: "Send output after 1sec", + example: "sleep 1sec; echo done", + result: Some(vec![UntaggedValue::string("done").into()]), }, ] } } -struct SleepFuture { - shared_state: Arc>, +struct SleepIterator { + total_dur: Duration, + ctrl_c: Arc, } -impl SleepFuture { - /// Create a new `SleepFuture` which will complete after the provided - /// timeout and check for Ctrl+C periodically. - pub fn new(duration: Duration, ctrl_c: Arc) -> Self { - let shared_state = Arc::new(Mutex::new(SharedState { - done: false, - waker: None, - })); - - // Spawn the main sleep thread - let thread_shared_state = shared_state.clone(); - thread::spawn(move || { - thread::sleep(duration); - let mut shared_state = thread_shared_state.lock(); - // Signal that the timer has completed and wake up the last - // task on which the future was polled, if one exists. - if !shared_state.done { - shared_state.done = true; - if let Some(waker) = shared_state.waker.take() { - waker.wake() - } - } - }); - - // Spawn the Ctrl+C-watching polling thread - let thread_shared_state = shared_state.clone(); - thread::spawn(move || { - loop { - { - let mut shared_state = thread_shared_state.lock(); - // exit if the main thread is done - if shared_state.done { - return; - } - // finish the future prematurely if Ctrl+C has been pressed - if ctrl_c.load(Ordering::SeqCst) { - shared_state.done = true; - if let Some(waker) = shared_state.waker.take() { - waker.wake() - } - return; - } - } - // sleep for a short time - thread::sleep(CTRL_C_CHECK_INTERVAL); - } - }); - - SleepFuture { shared_state } +impl SleepIterator { + pub fn new(total_dur: Duration, ctrl_c: Arc) -> Self { + Self { total_dur, ctrl_c } } } -struct SharedState { - done: bool, - waker: Option, -} +impl Iterator for SleepIterator { + type Item = ReturnValue; -impl Future for SleepFuture { - type Output = (); - - fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll { - // Look at the shared state to see if the timer has already completed. - let mut shared_state = self.shared_state.lock(); - if shared_state.done { - Poll::Ready(()) - } else { - // Set the waker if necessary - if shared_state - .waker - .as_ref() - .map(|waker| !waker.will_wake(&cx.waker())) - .unwrap_or(true) - { - shared_state.waker = Some(cx.waker().clone()); + fn next(&mut self) -> Option { + let start = Instant::now(); + loop { + thread::sleep(CTRL_C_CHECK_INTERVAL); + if start.elapsed() >= self.total_dur { + break; + } + + if self.ctrl_c.load(Ordering::SeqCst) { + break; } - Poll::Pending } + + None } } +// struct SleepHandler { +// shared_state: Arc>, +// } + +// impl SleepHandler { +// /// Create a new `SleepHandler` which will complete after the provided +// /// timeout and check for Ctrl+C periodically. +// pub fn new(duration: Duration, ctrl_c: Arc) -> Self { +// let shared_state = Arc::new(Mutex::new(SharedState { +// done: false, +// waker: None, +// })); + +// // Spawn the main sleep thread +// let thread_shared_state = shared_state.clone(); +// thread::spawn(move || { +// thread::sleep(duration); +// let mut shared_state = thread_shared_state.lock(); +// // Signal that the timer has completed and wake up the last +// // task on which the future was polled, if one exists. +// if !shared_state.done { +// shared_state.done = true; +// if let Some(waker) = shared_state.waker.take() { +// waker.wake() +// } +// } +// }); + +// // Spawn the Ctrl+C-watching polling thread +// let thread_shared_state = shared_state.clone(); +// thread::spawn(move || { +// loop { +// { +// let mut shared_state = thread_shared_state.lock(); +// // exit if the main thread is done +// if shared_state.done { +// return; +// } +// // finish the future prematurely if Ctrl+C has been pressed +// if ctrl_c.load(Ordering::SeqCst) { +// shared_state.done = true; +// if let Some(waker) = shared_state.waker.take() { +// waker.wake() +// } +// return; +// } +// } +// // sleep for a short time +// thread::sleep(CTRL_C_CHECK_INTERVAL); +// } +// }); + +// SleepHandler { shared_state } +// } +// } + +// struct SharedState { +// done: bool, +// } + +// impl Iterator for SleepHandler { +// type Item = (); + +// fn next(&mut self) -> Option { +// let mut shared_state = self.shared_state.lock(); +// loop { +// if shared_state.done { +// return None; +// } +// } +// } +// } +// impl Future for SleepHandler { +// type Output = (); + +// fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll { +// // Look at the shared state to see if the timer has already completed. +// Poll::Ready(()) +// } else { +// // Set the waker if necessary +// if shared_state +// .waker +// .as_ref() +// .map(|waker| !waker.will_wake(&cx.waker())) +// .unwrap_or(true) +// { +// shared_state.waker = Some(cx.waker().clone()); +// } +// Poll::Pending +// } +// } +// } + #[cfg(test)] mod tests { use super::Sleep; diff --git a/crates/nu-command/src/commands/sort_by.rs b/crates/nu-command/src/commands/sort_by.rs index 850958ca4f..5fa8d8f2b7 100644 --- a/crates/nu-command/src/commands/sort_by.rs +++ b/crates/nu-command/src/commands/sort_by.rs @@ -15,7 +15,6 @@ pub struct SortByArgs { reverse: bool, } -#[async_trait] impl WholeStreamCommand for SortBy { fn name(&self) -> &str { "sort-by" @@ -36,8 +35,8 @@ impl WholeStreamCommand for SortBy { "Sort by the given columns, in increasing order." } - async fn run(&self, args: CommandArgs) -> Result { - sort_by(args).await + fn run(&self, args: CommandArgs) -> Result { + sort_by(args) } fn examples(&self) -> Vec { @@ -112,7 +111,7 @@ impl WholeStreamCommand for SortBy { } } -async fn sort_by(args: CommandArgs) -> Result { +fn sort_by(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); let ( @@ -122,8 +121,8 @@ async fn sort_by(args: CommandArgs) -> Result { reverse, }, mut input, - ) = args.process().await?; - let mut vec = input.drain_vec().await; + ) = args.process()?; + let mut vec = input.drain_vec(); sort(&mut vec, &rest, &tag, insensitive)?; @@ -131,7 +130,7 @@ async fn sort_by(args: CommandArgs) -> Result { vec.reverse() } - Ok(futures::stream::iter(vec.into_iter()).to_output_stream()) + Ok((vec.into_iter()).to_output_stream()) } pub fn sort( diff --git a/crates/nu-command/src/commands/source.rs b/crates/nu-command/src/commands/source.rs index 09e7c25153..228ae00cae 100644 --- a/crates/nu-command/src/commands/source.rs +++ b/crates/nu-command/src/commands/source.rs @@ -13,7 +13,6 @@ pub struct SourceArgs { pub filename: Tagged, } -#[async_trait] impl WholeStreamCommand for Source { fn name(&self) -> &str { "source" @@ -31,8 +30,8 @@ impl WholeStreamCommand for Source { "Runs a script file in the current context." } - async fn run(&self, args: CommandArgs) -> Result { - source(args).await + fn run(&self, args: CommandArgs) -> Result { + source(args) } fn examples(&self) -> Vec { @@ -40,9 +39,9 @@ impl WholeStreamCommand for Source { } } -pub async fn source(args: CommandArgs) -> Result { +pub fn source(args: CommandArgs) -> Result { let ctx = EvaluationContext::from_args(&args); - let (SourceArgs { filename }, _) = args.process().await?; + let (SourceArgs { filename }, _) = args.process()?; // Note: this is a special case for setting the context from a command // In this case, if we don't set it now, we'll lose the scope that this @@ -50,7 +49,7 @@ pub async fn source(args: CommandArgs) -> Result { let contents = std::fs::read_to_string(expand_path(&filename.item).into_owned()); match contents { Ok(contents) => { - let result = script::run_script_standalone(contents, true, &ctx, false).await; + let result = script::run_script_standalone(contents, true, &ctx, false); if let Err(err) = result { ctx.error(err.into()); diff --git a/crates/nu-command/src/commands/split/chars.rs b/crates/nu-command/src/commands/split/chars.rs index 604fce9b85..86dcab38a9 100644 --- a/crates/nu-command/src/commands/split/chars.rs +++ b/crates/nu-command/src/commands/split/chars.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "split chars" @@ -19,8 +18,8 @@ impl WholeStreamCommand for SubCommand { "splits a string's characters into separate rows" } - async fn run(&self, args: CommandArgs) -> Result { - split_chars(args).await + fn run(&self, args: CommandArgs) -> Result { + Ok(split_chars(args)) } fn examples(&self) -> Vec { @@ -38,19 +37,17 @@ impl WholeStreamCommand for SubCommand { } } -async fn split_chars(args: CommandArgs) -> Result { +fn split_chars(args: CommandArgs) -> OutputStream { let name = args.call_info.name_tag.clone(); let input = args.input; - Ok(input + input .flat_map(move |v| { if let Ok(s) = v.as_string() { - futures::stream::iter( - s.chars() - .collect::>() - .into_iter() - .map(move |x| ReturnSuccess::value(Value::from(x.to_string()))), - ) - .to_output_stream() + s.chars() + .collect::>() + .into_iter() + .map(move |x| ReturnSuccess::value(Value::from(x.to_string()))) + .to_output_stream() } else { OutputStream::one(Err(ShellError::labeled_error_with_secondary( "Expected a string from pipeline", @@ -61,7 +58,7 @@ async fn split_chars(args: CommandArgs) -> Result { ))) } }) - .to_output_stream()) + .to_output_stream() } #[cfg(test)] diff --git a/crates/nu-command/src/commands/split/column.rs b/crates/nu-command/src/commands/split/column.rs index 29d343672a..2f8f7d4e33 100644 --- a/crates/nu-command/src/commands/split/column.rs +++ b/crates/nu-command/src/commands/split/column.rs @@ -17,7 +17,6 @@ struct SplitColumnArgs { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "split column" @@ -38,12 +37,12 @@ impl WholeStreamCommand for SubCommand { "splits contents across multiple columns via the separator." } - async fn run(&self, args: CommandArgs) -> Result { - split_column(args).await + fn run(&self, args: CommandArgs) -> Result { + split_column(args) } } -async fn split_column(args: CommandArgs) -> Result { +fn split_column(args: CommandArgs) -> Result { let name_span = args.call_info.name_tag.span; let ( SplitColumnArgs { @@ -52,7 +51,7 @@ async fn split_column(args: CommandArgs) -> Result { collapse_empty, }, input, - ) = args.process().await?; + ) = args.process()?; Ok(input .map(move |v| { diff --git a/crates/nu-command/src/commands/split/command.rs b/crates/nu-command/src/commands/split/command.rs index 90278e2912..d2fc9a0fbe 100644 --- a/crates/nu-command/src/commands/split/command.rs +++ b/crates/nu-command/src/commands/split/command.rs @@ -6,7 +6,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; #[derive(Clone)] pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "split" @@ -20,7 +19,7 @@ impl WholeStreamCommand for Command { "Split contents across desired subcommand (like row, column) via the separator." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(Ok(ReturnSuccess::Value( UntaggedValue::string(get_full_help(&Command, &args.scope)).into_value(Tag::unknown()), )))) diff --git a/crates/nu-command/src/commands/split/row.rs b/crates/nu-command/src/commands/split/row.rs index 24ca3d488b..4c4b7a9392 100644 --- a/crates/nu-command/src/commands/split/row.rs +++ b/crates/nu-command/src/commands/split/row.rs @@ -12,7 +12,6 @@ struct SplitRowArgs { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "split row" @@ -30,14 +29,14 @@ impl WholeStreamCommand for SubCommand { "splits contents over multiple rows via the separator." } - async fn run(&self, args: CommandArgs) -> Result { - split_row(args).await + fn run(&self, args: CommandArgs) -> Result { + split_row(args) } } -async fn split_row(args: CommandArgs) -> Result { +fn split_row(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (SplitRowArgs { separator }, input) = args.process().await?; + let (SplitRowArgs { separator }, input) = args.process()?; Ok(input .flat_map(move |v| { if let Ok(s) = v.as_string() { @@ -56,7 +55,7 @@ async fn split_row(args: CommandArgs) -> Result { trace!("split result = {:?}", split_result); - futures::stream::iter(split_result.into_iter().map(move |s| { + (split_result.into_iter().map(move |s| { ReturnSuccess::value( UntaggedValue::Primitive(Primitive::String(s)).into_value(&v.tag), ) diff --git a/crates/nu-command/src/commands/split_by.rs b/crates/nu-command/src/commands/split_by.rs index eeb7373491..4a2dcc50d5 100644 --- a/crates/nu-command/src/commands/split_by.rs +++ b/crates/nu-command/src/commands/split_by.rs @@ -13,7 +13,6 @@ pub struct SplitByArgs { column_name: Option>, } -#[async_trait] impl WholeStreamCommand for SplitBy { fn name(&self) -> &str { "split-by" @@ -31,15 +30,15 @@ impl WholeStreamCommand for SplitBy { "Creates a new table with the data from the inner tables split by the column given." } - async fn run(&self, args: CommandArgs) -> Result { - split_by(args).await + fn run(&self, args: CommandArgs) -> Result { + split_by(args) } } -pub async fn split_by(args: CommandArgs) -> Result { +pub fn split_by(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (SplitByArgs { column_name }, input) = args.process().await?; - let values: Vec = input.collect().await; + let (SplitByArgs { column_name }, input) = args.process()?; + let values: Vec = input.collect(); if values.len() > 1 || values.is_empty() { return Err(ShellError::labeled_error( diff --git a/crates/nu-command/src/commands/str_/capitalize.rs b/crates/nu-command/src/commands/str_/capitalize.rs index b944df2cc7..113baaa63e 100644 --- a/crates/nu-command/src/commands/str_/capitalize.rs +++ b/crates/nu-command/src/commands/str_/capitalize.rs @@ -15,7 +15,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str capitalize" @@ -32,8 +31,8 @@ impl WholeStreamCommand for SubCommand { "capitalizes text" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -45,8 +44,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { rest }, input) = args.process()?; let column_paths: Vec<_> = rest; diff --git a/crates/nu-command/src/commands/str_/case/camel_case.rs b/crates/nu-command/src/commands/str_/case/camel_case.rs index 34d21b1dea..f462a43789 100644 --- a/crates/nu-command/src/commands/str_/case/camel_case.rs +++ b/crates/nu-command/src/commands/str_/case/camel_case.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str camel-case" @@ -24,8 +23,8 @@ impl WholeStreamCommand for SubCommand { "converts a string to camelCase" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args, &to_camel_case).await + fn run(&self, args: CommandArgs) -> Result { + operate(args, &to_camel_case) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/str_/case/kebab_case.rs b/crates/nu-command/src/commands/str_/case/kebab_case.rs index c4a7007796..3642261af2 100644 --- a/crates/nu-command/src/commands/str_/case/kebab_case.rs +++ b/crates/nu-command/src/commands/str_/case/kebab_case.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str kebab-case" @@ -24,8 +23,8 @@ impl WholeStreamCommand for SubCommand { "converts a string to kebab-case" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args, &to_kebab_case).await + fn run(&self, args: CommandArgs) -> Result { + operate(args, &to_kebab_case) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/str_/case/mod.rs b/crates/nu-command/src/commands/str_/case/mod.rs index 2af30b2c00..a5003862e2 100644 --- a/crates/nu-command/src/commands/str_/case/mod.rs +++ b/crates/nu-command/src/commands/str_/case/mod.rs @@ -21,15 +21,11 @@ struct Arguments { rest: Vec, } -pub async fn operate( - args: CommandArgs, - - case_operation: &'static F, -) -> Result +pub fn operate(args: CommandArgs, case_operation: &'static F) -> Result where F: Fn(&str) -> String + Send + Sync + 'static, { - let (Arguments { rest }, input) = args.process().await?; + let (Arguments { rest }, input) = args.process()?; let column_paths: Vec<_> = rest; Ok(input diff --git a/crates/nu-command/src/commands/str_/case/pascal_case.rs b/crates/nu-command/src/commands/str_/case/pascal_case.rs index fc3ee48fc1..b085e96d35 100644 --- a/crates/nu-command/src/commands/str_/case/pascal_case.rs +++ b/crates/nu-command/src/commands/str_/case/pascal_case.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str pascal-case" @@ -24,8 +23,8 @@ impl WholeStreamCommand for SubCommand { "converts a string to PascalCase" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args, &to_pascal_case).await + fn run(&self, args: CommandArgs) -> Result { + operate(args, &to_pascal_case) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/str_/case/screaming_snake_case.rs b/crates/nu-command/src/commands/str_/case/screaming_snake_case.rs index 113ea96620..5f51209018 100644 --- a/crates/nu-command/src/commands/str_/case/screaming_snake_case.rs +++ b/crates/nu-command/src/commands/str_/case/screaming_snake_case.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str screaming-snake-case" @@ -24,8 +23,8 @@ impl WholeStreamCommand for SubCommand { "converts a string to SCREAMING_SNAKE_CASE" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args, &to_screaming_snake_case).await + fn run(&self, args: CommandArgs) -> Result { + operate(args, &to_screaming_snake_case) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/str_/case/snake_case.rs b/crates/nu-command/src/commands/str_/case/snake_case.rs index 56b06fec5d..43a4f12910 100644 --- a/crates/nu-command/src/commands/str_/case/snake_case.rs +++ b/crates/nu-command/src/commands/str_/case/snake_case.rs @@ -7,7 +7,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str snake-case" @@ -24,8 +23,8 @@ impl WholeStreamCommand for SubCommand { "converts a string to snake_case" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args, &to_snake_case).await + fn run(&self, args: CommandArgs) -> Result { + operate(args, &to_snake_case) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/str_/collect.rs b/crates/nu-command/src/commands/str_/collect.rs index e2cb6becc3..e78602cdac 100644 --- a/crates/nu-command/src/commands/str_/collect.rs +++ b/crates/nu-command/src/commands/str_/collect.rs @@ -11,7 +11,6 @@ pub struct SubCommandArgs { separator: Option>, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str collect" @@ -29,8 +28,8 @@ impl WholeStreamCommand for SubCommand { "collects a list of strings into a string" } - async fn run(&self, args: CommandArgs) -> Result { - collect(args).await + fn run(&self, args: CommandArgs) -> Result { + collect(args) } fn examples(&self) -> Vec { @@ -42,13 +41,12 @@ impl WholeStreamCommand for SubCommand { } } -pub async fn collect(args: CommandArgs) -> Result { +pub fn collect(args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (SubCommandArgs { separator }, input) = args.process().await?; + let (SubCommandArgs { separator }, input) = args.process()?; let separator = separator.map(|tagged| tagged.item).unwrap_or_default(); - let strings: Vec> = - input.map(|value| value.as_string()).collect().await; + let strings: Vec> = input.map(|value| value.as_string()).collect(); let strings: Result, _> = strings.into_iter().collect::>(); match strings { diff --git a/crates/nu-command/src/commands/str_/command.rs b/crates/nu-command/src/commands/str_/command.rs index 060eee9015..b235eb095d 100644 --- a/crates/nu-command/src/commands/str_/command.rs +++ b/crates/nu-command/src/commands/str_/command.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "str" @@ -22,7 +21,7 @@ impl WholeStreamCommand for Command { "Apply string function." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(get_full_help(&Command, &args.scope)).into_value(Tag::unknown()), ))) diff --git a/crates/nu-command/src/commands/str_/contains.rs b/crates/nu-command/src/commands/str_/contains.rs index 6821d444f2..9116a03da5 100644 --- a/crates/nu-command/src/commands/str_/contains.rs +++ b/crates/nu-command/src/commands/str_/contains.rs @@ -17,7 +17,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str contains" @@ -37,8 +36,8 @@ impl WholeStreamCommand for SubCommand { "Checks if string contains pattern" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -57,7 +56,7 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> Result { let ( Arguments { pattern, @@ -65,7 +64,7 @@ async fn operate(args: CommandArgs) -> Result { insensitive, }, input, - ) = args.process().await?; + ) = args.process()?; let column_paths: Vec<_> = rest; Ok(input diff --git a/crates/nu-command/src/commands/str_/downcase.rs b/crates/nu-command/src/commands/str_/downcase.rs index 309900bf6b..f350a486ee 100644 --- a/crates/nu-command/src/commands/str_/downcase.rs +++ b/crates/nu-command/src/commands/str_/downcase.rs @@ -15,7 +15,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str downcase" @@ -32,8 +31,8 @@ impl WholeStreamCommand for SubCommand { "downcases text" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -45,8 +44,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { rest }, input) = args.process()?; let column_paths: Vec<_> = rest; diff --git a/crates/nu-command/src/commands/str_/ends_with.rs b/crates/nu-command/src/commands/str_/ends_with.rs index 65979bcd67..45d4cf95a1 100644 --- a/crates/nu-command/src/commands/str_/ends_with.rs +++ b/crates/nu-command/src/commands/str_/ends_with.rs @@ -15,7 +15,6 @@ struct Arguments { } pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str ends-with" @@ -34,8 +33,8 @@ impl WholeStreamCommand for SubCommand { "checks if string ends with pattern" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -47,8 +46,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { pattern, rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { pattern, rest }, input) = args.process()?; let column_paths: Vec<_> = rest; diff --git a/crates/nu-command/src/commands/str_/find_replace.rs b/crates/nu-command/src/commands/str_/find_replace.rs index 32fb9b5ef1..2fd9266b35 100644 --- a/crates/nu-command/src/commands/str_/find_replace.rs +++ b/crates/nu-command/src/commands/str_/find_replace.rs @@ -19,7 +19,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str find-replace" @@ -40,8 +39,8 @@ impl WholeStreamCommand for SubCommand { "finds and replaces text" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -63,7 +62,7 @@ impl WholeStreamCommand for SubCommand { #[derive(Clone)] struct FindReplace(String, String); -async fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> Result { let ( Arguments { find, @@ -72,7 +71,7 @@ async fn operate(args: CommandArgs) -> Result { all, }, input, - ) = args.process().await?; + ) = args.process()?; let options = FindReplace(find.item, replace.item); let column_paths: Vec<_> = rest; diff --git a/crates/nu-command/src/commands/str_/from.rs b/crates/nu-command/src/commands/str_/from.rs index 410bac4959..34b167821e 100644 --- a/crates/nu-command/src/commands/str_/from.rs +++ b/crates/nu-command/src/commands/str_/from.rs @@ -22,7 +22,6 @@ struct Arguments { group_digits: bool, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str from" @@ -55,8 +54,8 @@ impl WholeStreamCommand for SubCommand { "Converts numeric types to strings. Trims trailing zeros unless decimals parameter is specified." } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -80,7 +79,7 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> Result { let ( Arguments { decimals, @@ -88,7 +87,7 @@ async fn operate(args: CommandArgs) -> Result { rest: column_paths, }, input, - ) = args.process().await?; + ) = args.process()?; let digits = decimals.map(|tagged| tagged.item); Ok(input diff --git a/crates/nu-command/src/commands/str_/index_of.rs b/crates/nu-command/src/commands/str_/index_of.rs index 5b40d68ea6..f98b90628e 100644 --- a/crates/nu-command/src/commands/str_/index_of.rs +++ b/crates/nu-command/src/commands/str_/index_of.rs @@ -21,7 +21,6 @@ pub struct SubCommand; #[derive(Clone)] pub struct IndexOfOptionalBounds(i32, i32); -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str index-of" @@ -51,8 +50,8 @@ impl WholeStreamCommand for SubCommand { "Returns starting index of given pattern in string counting from 0. Returns -1 when there are no results." } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -91,7 +90,7 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> Result { let ( Arguments { pattern, @@ -100,7 +99,7 @@ async fn operate(args: CommandArgs) -> Result { end, }, input, - ) = args.process().await?; + ) = args.process()?; let range = range.unwrap_or_else(|| { UntaggedValue::Primitive(Primitive::String("".to_string())).into_untagged_value() }); diff --git a/crates/nu-command/src/commands/str_/length.rs b/crates/nu-command/src/commands/str_/length.rs index 1e09a70995..fdb05b8912 100644 --- a/crates/nu-command/src/commands/str_/length.rs +++ b/crates/nu-command/src/commands/str_/length.rs @@ -13,7 +13,6 @@ struct Arguments { rest: Vec, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str length" @@ -30,8 +29,8 @@ impl WholeStreamCommand for SubCommand { "outputs the lengths of the strings in the pipeline" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -53,8 +52,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { rest }, input) = args.process()?; let column_paths: Vec<_> = rest; Ok(input diff --git a/crates/nu-command/src/commands/str_/lpad.rs b/crates/nu-command/src/commands/str_/lpad.rs index a4950e42a8..54610d692b 100644 --- a/crates/nu-command/src/commands/str_/lpad.rs +++ b/crates/nu-command/src/commands/str_/lpad.rs @@ -17,7 +17,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str lpad" @@ -42,8 +41,8 @@ impl WholeStreamCommand for SubCommand { "pad a string with a character a certain length" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -78,7 +77,7 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> Result { let ( Arguments { length, @@ -86,7 +85,7 @@ async fn operate(args: CommandArgs) -> Result { rest, }, input, - ) = args.process().await?; + ) = args.process()?; let column_paths: Vec<_> = rest; Ok(input diff --git a/crates/nu-command/src/commands/str_/reverse.rs b/crates/nu-command/src/commands/str_/reverse.rs index c804f2bc1f..9870388557 100644 --- a/crates/nu-command/src/commands/str_/reverse.rs +++ b/crates/nu-command/src/commands/str_/reverse.rs @@ -13,7 +13,6 @@ struct Arguments { rest: Vec, } -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str reverse" @@ -30,8 +29,8 @@ impl WholeStreamCommand for SubCommand { "outputs the reversals of the strings in the pipeline" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -43,8 +42,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { rest }, input) = args.process()?; let column_paths: Vec<_> = rest; Ok(input diff --git a/crates/nu-command/src/commands/str_/rpad.rs b/crates/nu-command/src/commands/str_/rpad.rs index 84bbf608e6..c304490413 100644 --- a/crates/nu-command/src/commands/str_/rpad.rs +++ b/crates/nu-command/src/commands/str_/rpad.rs @@ -17,7 +17,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str rpad" @@ -42,8 +41,8 @@ impl WholeStreamCommand for SubCommand { "pad a string with a character a certain length" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -78,7 +77,7 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> Result { let ( Arguments { length, @@ -86,7 +85,7 @@ async fn operate(args: CommandArgs) -> Result { rest, }, input, - ) = args.process().await?; + ) = args.process()?; let column_paths: Vec<_> = rest; Ok(input diff --git a/crates/nu-command/src/commands/str_/starts_with.rs b/crates/nu-command/src/commands/str_/starts_with.rs index da7987d4ef..1440924096 100644 --- a/crates/nu-command/src/commands/str_/starts_with.rs +++ b/crates/nu-command/src/commands/str_/starts_with.rs @@ -15,7 +15,6 @@ struct Arguments { } pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str starts-with" @@ -34,8 +33,8 @@ impl WholeStreamCommand for SubCommand { "checks if string starts with pattern" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -47,8 +46,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { pattern, rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { pattern, rest }, input) = args.process()?; let column_paths: Vec<_> = rest; diff --git a/crates/nu-command/src/commands/str_/substring.rs b/crates/nu-command/src/commands/str_/substring.rs index 1c1450e1ac..3cae367277 100644 --- a/crates/nu-command/src/commands/str_/substring.rs +++ b/crates/nu-command/src/commands/str_/substring.rs @@ -19,7 +19,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str substring" @@ -42,8 +41,8 @@ impl WholeStreamCommand for SubCommand { "substrings text" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -88,10 +87,10 @@ impl From<(isize, isize)> for Substring { struct SubstringText(String, String); -async fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (Arguments { range, rest }, input) = args.process().await?; + let (Arguments { range, rest }, input) = args.process()?; let column_paths: Vec<_> = rest; let options = process_arguments(range, name)?.into(); diff --git a/crates/nu-command/src/commands/str_/to_datetime.rs b/crates/nu-command/src/commands/str_/to_datetime.rs index 36d12d20f8..355e88ee6d 100644 --- a/crates/nu-command/src/commands/str_/to_datetime.rs +++ b/crates/nu-command/src/commands/str_/to_datetime.rs @@ -52,7 +52,6 @@ impl Zone { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str to-datetime" @@ -88,8 +87,8 @@ impl WholeStreamCommand for SubCommand { "converts text into datetime" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -127,7 +126,7 @@ impl WholeStreamCommand for SubCommand { #[derive(Clone)] struct DatetimeFormat(String); -async fn operate(args: CommandArgs) -> Result { +fn operate(args: CommandArgs) -> Result { let ( Arguments { timezone, @@ -136,7 +135,7 @@ async fn operate(args: CommandArgs) -> Result { rest, }, input, - ) = args.process().await?; + ) = args.process()?; let column_paths: Vec<_> = rest; diff --git a/crates/nu-command/src/commands/str_/to_decimal.rs b/crates/nu-command/src/commands/str_/to_decimal.rs index 50d31a944d..10363b858e 100644 --- a/crates/nu-command/src/commands/str_/to_decimal.rs +++ b/crates/nu-command/src/commands/str_/to_decimal.rs @@ -18,7 +18,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str to-decimal" @@ -35,8 +34,8 @@ impl WholeStreamCommand for SubCommand { "converts text into decimal" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -48,8 +47,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { rest }, input) = args.process()?; let column_paths: Vec<_> = rest; diff --git a/crates/nu-command/src/commands/str_/to_integer.rs b/crates/nu-command/src/commands/str_/to_integer.rs index 651aa88341..538dd8fcbe 100644 --- a/crates/nu-command/src/commands/str_/to_integer.rs +++ b/crates/nu-command/src/commands/str_/to_integer.rs @@ -18,7 +18,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str to-int" @@ -37,8 +36,8 @@ impl WholeStreamCommand for SubCommand { "converts text into integer" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -67,8 +66,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { mut rest, radix }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { mut rest, radix }, input) = args.process()?; let (column_paths, _) = arguments(&mut rest)?; let radix = radix.map(|r| r.item).unwrap_or(10); diff --git a/crates/nu-command/src/commands/str_/trim/mod.rs b/crates/nu-command/src/commands/str_/trim/mod.rs index c8531548ff..3d8e6c0580 100644 --- a/crates/nu-command/src/commands/str_/trim/mod.rs +++ b/crates/nu-command/src/commands/str_/trim/mod.rs @@ -21,15 +21,11 @@ struct Arguments { char_: Option>, } -pub async fn operate( - args: CommandArgs, - - trim_operation: &'static F, -) -> Result +pub fn operate(args: CommandArgs, trim_operation: &'static F) -> Result where F: Fn(&str, Option) -> String + Send + Sync + 'static, { - let (Arguments { rest, char_ }, input) = args.process().await?; + let (Arguments { rest, char_ }, input) = args.process()?; let column_paths: Vec<_> = rest; let to_trim = char_.map(|tagged| tagged.item); diff --git a/crates/nu-command/src/commands/str_/trim/trim_both_ends.rs b/crates/nu-command/src/commands/str_/trim/trim_both_ends.rs index cc04326f4e..abeb2c4b9a 100644 --- a/crates/nu-command/src/commands/str_/trim/trim_both_ends.rs +++ b/crates/nu-command/src/commands/str_/trim/trim_both_ends.rs @@ -6,7 +6,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str trim" @@ -30,8 +29,8 @@ impl WholeStreamCommand for SubCommand { "trims text" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args, &trim).await + fn run(&self, args: CommandArgs) -> Result { + operate(args, &trim) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/str_/trim/trim_left.rs b/crates/nu-command/src/commands/str_/trim/trim_left.rs index 83456e1205..3437f06cc1 100644 --- a/crates/nu-command/src/commands/str_/trim/trim_left.rs +++ b/crates/nu-command/src/commands/str_/trim/trim_left.rs @@ -6,7 +6,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str ltrim" @@ -30,8 +29,8 @@ impl WholeStreamCommand for SubCommand { "trims whitespace or character from the beginning of text" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args, &trim_left).await + fn run(&self, args: CommandArgs) -> Result { + operate(args, &trim_left) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/str_/trim/trim_right.rs b/crates/nu-command/src/commands/str_/trim/trim_right.rs index d68650084f..35077edb3b 100644 --- a/crates/nu-command/src/commands/str_/trim/trim_right.rs +++ b/crates/nu-command/src/commands/str_/trim/trim_right.rs @@ -6,7 +6,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str rtrim" @@ -30,8 +29,8 @@ impl WholeStreamCommand for SubCommand { "trims whitespace or character from the end of text" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args, &trim_right).await + fn run(&self, args: CommandArgs) -> Result { + operate(args, &trim_right) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/str_/upcase.rs b/crates/nu-command/src/commands/str_/upcase.rs index a00bad69c2..8d31a748c2 100644 --- a/crates/nu-command/src/commands/str_/upcase.rs +++ b/crates/nu-command/src/commands/str_/upcase.rs @@ -15,7 +15,6 @@ struct Arguments { pub struct SubCommand; -#[async_trait] impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { "str upcase" @@ -32,8 +31,8 @@ impl WholeStreamCommand for SubCommand { "upcases text" } - async fn run(&self, args: CommandArgs) -> Result { - operate(args).await + fn run(&self, args: CommandArgs) -> Result { + operate(args) } fn examples(&self) -> Vec { @@ -45,8 +44,8 @@ impl WholeStreamCommand for SubCommand { } } -async fn operate(args: CommandArgs) -> Result { - let (Arguments { rest }, input) = args.process().await?; +fn operate(args: CommandArgs) -> Result { + let (Arguments { rest }, input) = args.process()?; let column_paths: Vec<_> = rest; diff --git a/crates/nu-command/src/commands/table/command.rs b/crates/nu-command/src/commands/table/command.rs index b942fa3f8e..2a45258149 100644 --- a/crates/nu-command/src/commands/table/command.rs +++ b/crates/nu-command/src/commands/table/command.rs @@ -22,7 +22,6 @@ const STREAM_TIMEOUT_CHECK_INTERVAL: usize = 100; pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "table" @@ -41,8 +40,8 @@ impl WholeStreamCommand for Command { "View the contents of the pipeline as a table." } - async fn run(&self, args: CommandArgs) -> Result { - table(TableConfiguration::new(), args).await + fn run(&self, args: CommandArgs) -> Result { + table(TableConfiguration::new(), args) } } @@ -159,11 +158,8 @@ fn values_to_entries( entries } -async fn table( - configuration: TableConfiguration, - args: CommandArgs, -) -> Result { - let mut args = args.evaluate_once().await?; +fn table(configuration: TableConfiguration, args: CommandArgs) -> Result { + let mut args = args.evaluate_once()?; // Ideally, get_color_config would get all the colors configured in the config.toml // and create a style based on those settings. However, there are few places where @@ -203,7 +199,7 @@ async fn table( .set_input_handler(Box::new(input_handling::MinusInputHandler {})) .finish(); - let stream_data = async { + let stream_data = { let finished = Arc::new(AtomicBool::new(false)); // we are required to clone finished, for use within the callback, otherwise we get borrow errors #[cfg(feature = "table-pager")] @@ -213,7 +209,7 @@ async fn table( // This is called when the pager finishes, to indicate to the // while loop below to finish, in case of long running InputStream consumer // that doesn't finish by the time the user quits out of the pager - pager.lock().await.add_exit_callback(move || { + pager.lock().add_exit_callback(move || { finished_within_callback.store(true, Ordering::Relaxed); }); } @@ -226,7 +222,7 @@ async fn table( new_input.push_back(val); delay_slot = None; } else { - match args.input.next().await { + match args.input.next() { Some(a) => { if !new_input.is_empty() { if let Some(descs) = new_input.get(0) { @@ -274,7 +270,7 @@ async fn table( let output = draw_table(&t, term_width, &color_hm); #[cfg(feature = "table-pager")] { - let mut pager = pager.lock().await; + let mut pager = pager.lock(); writeln!(pager.lines, "{}", output).map_err(|_| { ShellError::untagged_runtime_error("Error writing to pager") })?; @@ -289,7 +285,7 @@ async fn table( #[cfg(feature = "table-pager")] { - let mut pager_lock = pager.lock().await; + let mut pager_lock = pager.lock(); pager_lock.data_finished(); } @@ -299,15 +295,13 @@ async fn table( #[cfg(feature = "table-pager")] { let (minus_result, streaming_result) = - join(minus::async_std_updating(pager.clone()), stream_data).await; + join(minus::async_std_updating(pager.clone()), stream_data); minus_result.map_err(|_| ShellError::untagged_runtime_error("Error paging data"))?; streaming_result?; } #[cfg(not(feature = "table-pager"))] - stream_data - .await - .map_err(|_| ShellError::untagged_runtime_error("Error streaming data"))?; + stream_data.map_err(|_| ShellError::untagged_runtime_error("Error streaming data"))?; Ok(OutputStream::empty()) } diff --git a/crates/nu-command/src/commands/tags.rs b/crates/nu-command/src/commands/tags.rs index 76a236419d..b2c5d72f99 100644 --- a/crates/nu-command/src/commands/tags.rs +++ b/crates/nu-command/src/commands/tags.rs @@ -5,7 +5,6 @@ use nu_protocol::{Signature, TaggedDictBuilder, UntaggedValue}; pub struct Tags; -#[async_trait] impl WholeStreamCommand for Tags { fn name(&self) -> &str { "tags" @@ -19,7 +18,7 @@ impl WholeStreamCommand for Tags { "Read the tags (metadata) for values." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(tags(args)) } } diff --git a/crates/nu-command/src/commands/termsize.rs b/crates/nu-command/src/commands/termsize.rs index 4c04d78ab8..2e7603d529 100644 --- a/crates/nu-command/src/commands/termsize.rs +++ b/crates/nu-command/src/commands/termsize.rs @@ -12,7 +12,6 @@ pub struct TermSizeArgs { tall: bool, } -#[async_trait] impl WholeStreamCommand for TermSize { fn name(&self) -> &str { "term size" @@ -28,9 +27,9 @@ impl WholeStreamCommand for TermSize { "Returns the terminal size as W H" } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let tag = args.call_info.name_tag.clone(); - let (TermSizeArgs { wide, tall }, _) = args.process().await?; + let (TermSizeArgs { wide, tall }, _) = args.process()?; let size = term_size::dimensions(); match size { diff --git a/crates/nu-command/src/commands/to.rs b/crates/nu-command/src/commands/to.rs index fd43e3ad1a..52aeeb9d1a 100644 --- a/crates/nu-command/src/commands/to.rs +++ b/crates/nu-command/src/commands/to.rs @@ -6,7 +6,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; #[derive(Clone)] pub struct To; -#[async_trait] impl WholeStreamCommand for To { fn name(&self) -> &str { "to" @@ -20,7 +19,7 @@ impl WholeStreamCommand for To { "Convert table into an output format (based on subcommand, like csv, html, json, yaml)." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(get_full_help(&To, &args.scope)).into_value(Tag::unknown()), ))) diff --git a/crates/nu-command/src/commands/to_csv.rs b/crates/nu-command/src/commands/to_csv.rs index 6c02dd4ac8..0e5d5e752e 100644 --- a/crates/nu-command/src/commands/to_csv.rs +++ b/crates/nu-command/src/commands/to_csv.rs @@ -12,7 +12,6 @@ pub struct ToCsvArgs { separator: Option, } -#[async_trait] impl WholeStreamCommand for ToCsv { fn name(&self) -> &str { "to csv" @@ -37,12 +36,12 @@ impl WholeStreamCommand for ToCsv { "Convert table into .csv text " } - async fn run(&self, args: CommandArgs) -> Result { - to_csv(args).await + fn run(&self, args: CommandArgs) -> Result { + to_csv(args) } } -async fn to_csv(args: CommandArgs) -> Result { +fn to_csv(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let ( ToCsvArgs { @@ -50,7 +49,7 @@ async fn to_csv(args: CommandArgs) -> Result { noheaders, }, input, - ) = args.process().await?; + ) = args.process()?; let sep = match separator { Some(Value { value: UntaggedValue::Primitive(Primitive::String(s)), @@ -74,7 +73,7 @@ async fn to_csv(args: CommandArgs) -> Result { _ => ',', }; - to_delimited_data(noheaders, sep, "CSV", input, name).await + to_delimited_data(noheaders, sep, "CSV", input, name) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/to_delimited_data.rs b/crates/nu-command/src/commands/to_delimited_data.rs index 4b717712b6..d070a44102 100644 --- a/crates/nu-command/src/commands/to_delimited_data.rs +++ b/crates/nu-command/src/commands/to_delimited_data.rs @@ -164,7 +164,7 @@ fn merge_descriptors(values: &[Value]) -> Vec> { ret } -pub async fn to_delimited_data( +pub fn to_delimited_data( noheaders: bool, sep: char, format_name: &'static str, @@ -174,7 +174,7 @@ pub async fn to_delimited_data( let name_tag = name; let name_span = name_tag.span; - let input: Vec = input.collect().await; + let input: Vec = input.collect(); let to_process_input = match input.len() { x if x > 1 => { @@ -188,36 +188,34 @@ pub async fn to_delimited_data( _ => vec![], }; - Ok( - futures::stream::iter(to_process_input.into_iter().map(move |value| { - match from_value_to_delimited_string(&clone_tagged_value(&value), sep) { - Ok(mut x) => { - if noheaders { - if let Some(second_line) = x.find('\n') { - let start = second_line + 1; - x.replace_range(0..start, ""); - } + Ok((to_process_input.into_iter().map(move |value| { + match from_value_to_delimited_string(&clone_tagged_value(&value), sep) { + Ok(mut x) => { + if noheaders { + if let Some(second_line) = x.find('\n') { + let start = second_line + 1; + x.replace_range(0..start, ""); } - ReturnSuccess::value( - UntaggedValue::Primitive(Primitive::String(x)).into_value(&name_tag), - ) - } - Err(_) => { - let expected = format!( - "Expected a table with {}-compatible structure from pipeline", - format_name - ); - let requires = format!("requires {}-compatible input", format_name); - Err(ShellError::labeled_error_with_secondary( - expected, - requires, - name_span, - "originates from here".to_string(), - value.tag.span, - )) } + ReturnSuccess::value( + UntaggedValue::Primitive(Primitive::String(x)).into_value(&name_tag), + ) } - })) - .to_output_stream(), - ) + Err(_) => { + let expected = format!( + "Expected a table with {}-compatible structure from pipeline", + format_name + ); + let requires = format!("requires {}-compatible input", format_name); + Err(ShellError::labeled_error_with_secondary( + expected, + requires, + name_span, + "originates from here".to_string(), + value.tag.span, + )) + } + } + })) + .to_output_stream()) } diff --git a/crates/nu-command/src/commands/to_html.rs b/crates/nu-command/src/commands/to_html.rs index cb413fbe5a..b792ae0557 100644 --- a/crates/nu-command/src/commands/to_html.rs +++ b/crates/nu-command/src/commands/to_html.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use futures::StreamExt; + use nu_data::value::format_leaf; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; @@ -91,7 +91,6 @@ pub struct ToHtmlArgs { list: bool, } -#[async_trait] impl WholeStreamCommand for ToHtml { fn name(&self) -> &str { "to html" @@ -124,8 +123,8 @@ impl WholeStreamCommand for ToHtml { "Convert table into simple HTML" } - async fn run(&self, args: CommandArgs) -> Result { - to_html(args).await + fn run(&self, args: CommandArgs) -> Result { + to_html(args) } } @@ -268,7 +267,7 @@ fn get_list_of_theme_names() -> Vec { theme_names } -async fn to_html(args: CommandArgs) -> Result { +fn to_html(args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); let ( ToHtmlArgs { @@ -280,8 +279,8 @@ async fn to_html(args: CommandArgs) -> Result { list, }, input, - ) = args.process().await?; - let input: Vec = input.collect().await; + ) = args.process()?; + let input: Vec = input.collect(); let headers = nu_protocol::merge_descriptors(&input); let headers = Some(headers) .filter(|headers| !headers.is_empty() && (headers.len() > 1 || !headers[0].is_empty())); diff --git a/crates/nu-command/src/commands/to_json.rs b/crates/nu-command/src/commands/to_json.rs index bf3e7faa0d..1062aa096d 100644 --- a/crates/nu-command/src/commands/to_json.rs +++ b/crates/nu-command/src/commands/to_json.rs @@ -14,7 +14,6 @@ pub struct ToJsonArgs { pretty: Option, } -#[async_trait] impl WholeStreamCommand for ToJson { fn name(&self) -> &str { "to json" @@ -33,8 +32,8 @@ impl WholeStreamCommand for ToJson { "Converts table data into JSON text." } - async fn run(&self, args: CommandArgs) -> Result { - to_json(args).await + fn run(&self, args: CommandArgs) -> Result { + to_json(args) } fn examples(&self) -> Vec { @@ -158,11 +157,11 @@ fn json_list(input: &[Value]) -> Result, ShellError> { Ok(out) } -async fn to_json(args: CommandArgs) -> Result { +fn to_json(args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); - let (ToJsonArgs { pretty }, input) = args.process().await?; + let (ToJsonArgs { pretty }, input) = args.process()?; let name_span = name_tag.span; - let input: Vec = input.collect().await; + let input: Vec = input.collect(); let to_process_input = match input.len() { x if x > 1 => { @@ -176,8 +175,9 @@ async fn to_json(args: CommandArgs) -> Result { _ => vec![], }; - Ok(futures::stream::iter(to_process_input.into_iter().map( - move |value| match value_to_json_value(&value) { + Ok((to_process_input + .into_iter() + .map(move |value| match value_to_json_value(&value) { Ok(json_value) => { let value_span = value.tag.span; @@ -248,8 +248,7 @@ async fn to_json(args: CommandArgs) -> Result { "requires JSON-compatible input", &name_tag, )), - }, - )) + })) .to_output_stream()) } diff --git a/crates/nu-command/src/commands/to_md.rs b/crates/nu-command/src/commands/to_md.rs index 2f533cde21..50aae08a66 100644 --- a/crates/nu-command/src/commands/to_md.rs +++ b/crates/nu-command/src/commands/to_md.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use futures::StreamExt; + use nu_data::value::format_leaf; use nu_engine::WholeStreamCommand; use nu_errors::ShellError; @@ -14,7 +14,6 @@ pub struct Arguments { per_element: bool, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "to md" @@ -38,8 +37,8 @@ impl WholeStreamCommand for Command { "Convert table into simple Markdown" } - async fn run(&self, args: CommandArgs) -> Result { - to_md(args).await + fn run(&self, args: CommandArgs) -> Result { + to_md(args) } fn examples(&self) -> Vec { @@ -83,11 +82,11 @@ impl WholeStreamCommand for Command { } } -async fn to_md(args: CommandArgs) -> Result { +fn to_md(args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); - let (arguments, input) = args.process().await?; + let (arguments, input) = args.process()?; - let input: Vec = input.collect().await; + let input: Vec = input.collect(); Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(process(&input, arguments)).into_value(if input.is_empty() { diff --git a/crates/nu-command/src/commands/to_toml.rs b/crates/nu-command/src/commands/to_toml.rs index 90406cfc6c..6210083f4b 100644 --- a/crates/nu-command/src/commands/to_toml.rs +++ b/crates/nu-command/src/commands/to_toml.rs @@ -5,7 +5,6 @@ use nu_protocol::{Primitive, ReturnSuccess, Signature, UnspannedPathMember, Unta pub struct ToToml; -#[async_trait] impl WholeStreamCommand for ToToml { fn name(&self) -> &str { "to toml" @@ -19,8 +18,8 @@ impl WholeStreamCommand for ToToml { "Convert table into .toml text" } - async fn run(&self, args: CommandArgs) -> Result { - to_toml(args).await + fn run(&self, args: CommandArgs) -> Result { + to_toml(args) } // TODO: add an example here. What commands to run to get a Row(Dictionary)? // fn examples(&self) -> Vec { @@ -140,11 +139,11 @@ fn collect_values(input: &[Value]) -> Result, ShellError> { Ok(out) } -async fn to_toml(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn to_toml(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let name_tag = args.name_tag(); let name_span = name_tag.span; - let input: Vec = args.input.collect().await; + let input: Vec = args.input.collect(); let to_process_input = match input.len() { x if x > 1 => { @@ -158,31 +157,29 @@ async fn to_toml(args: CommandArgs) -> Result { _ => vec![], }; - Ok( - futures::stream::iter(to_process_input.into_iter().map(move |value| { - let value_span = value.tag.span; - match value_to_toml_value(&value) { - Ok(toml_value) => match toml::to_string(&toml_value) { - Ok(x) => ReturnSuccess::value( - UntaggedValue::Primitive(Primitive::String(x)).into_value(&name_tag), - ), - _ => Err(ShellError::labeled_error_with_secondary( - "Expected a table with TOML-compatible structure.tag() from pipeline", - "requires TOML-compatible input", - name_span, - "originates from here".to_string(), - value_span, - )), - }, - _ => Err(ShellError::labeled_error( - "Expected a table with TOML-compatible structure from pipeline", + Ok((to_process_input.into_iter().map(move |value| { + let value_span = value.tag.span; + match value_to_toml_value(&value) { + Ok(toml_value) => match toml::to_string(&toml_value) { + Ok(x) => ReturnSuccess::value( + UntaggedValue::Primitive(Primitive::String(x)).into_value(&name_tag), + ), + _ => Err(ShellError::labeled_error_with_secondary( + "Expected a table with TOML-compatible structure.tag() from pipeline", "requires TOML-compatible input", - &name_tag, + name_span, + "originates from here".to_string(), + value_span, )), - } - })) - .to_output_stream(), - ) + }, + _ => Err(ShellError::labeled_error( + "Expected a table with TOML-compatible structure from pipeline", + "requires TOML-compatible input", + &name_tag, + )), + } + })) + .to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/to_tsv.rs b/crates/nu-command/src/commands/to_tsv.rs index 41d359b7dd..4341d3d275 100644 --- a/crates/nu-command/src/commands/to_tsv.rs +++ b/crates/nu-command/src/commands/to_tsv.rs @@ -11,7 +11,6 @@ pub struct ToTsvArgs { noheaders: bool, } -#[async_trait] impl WholeStreamCommand for ToTsv { fn name(&self) -> &str { "to tsv" @@ -29,16 +28,16 @@ impl WholeStreamCommand for ToTsv { "Convert table into .tsv text" } - async fn run(&self, args: CommandArgs) -> Result { - to_tsv(args).await + fn run(&self, args: CommandArgs) -> Result { + to_tsv(args) } } -async fn to_tsv(args: CommandArgs) -> Result { +fn to_tsv(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); - let (ToTsvArgs { noheaders }, input) = args.process().await?; + let (ToTsvArgs { noheaders }, input) = args.process()?; - to_delimited_data(noheaders, '\t', "TSV", input, name).await + to_delimited_data(noheaders, '\t', "TSV", input, name) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/to_url.rs b/crates/nu-command/src/commands/to_url.rs index 0f21cf1a26..4371633167 100644 --- a/crates/nu-command/src/commands/to_url.rs +++ b/crates/nu-command/src/commands/to_url.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value}; pub struct ToUrl; -#[async_trait] impl WholeStreamCommand for ToUrl { fn name(&self) -> &str { "to url" @@ -19,13 +18,13 @@ impl WholeStreamCommand for ToUrl { "Convert table into url-encoded text" } - async fn run(&self, args: CommandArgs) -> Result { - to_url(args).await + fn run(&self, args: CommandArgs) -> Result { + to_url(args) } } -async fn to_url(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn to_url(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let tag = args.name_tag(); let input = args.input; diff --git a/crates/nu-command/src/commands/to_xml.rs b/crates/nu-command/src/commands/to_xml.rs index c97fa2442c..c09b546c4e 100644 --- a/crates/nu-command/src/commands/to_xml.rs +++ b/crates/nu-command/src/commands/to_xml.rs @@ -15,7 +15,6 @@ pub struct ToXmlArgs { pretty: Option, } -#[async_trait] impl WholeStreamCommand for ToXml { fn name(&self) -> &str { "to xml" @@ -34,8 +33,8 @@ impl WholeStreamCommand for ToXml { "Convert table into .xml text" } - async fn run(&self, args: CommandArgs) -> Result { - to_xml(args).await + fn run(&self, args: CommandArgs) -> Result { + to_xml(args) } } @@ -132,11 +131,11 @@ pub fn write_xml_events( Ok(()) } -async fn to_xml(args: CommandArgs) -> Result { +fn to_xml(args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); let name_span = name_tag.span; - let (ToXmlArgs { pretty }, input) = args.process().await?; - let input: Vec = input.collect().await; + let (ToXmlArgs { pretty }, input) = args.process()?; + let input: Vec = input.collect(); let to_process_input = match input.len() { x if x > 1 => { @@ -150,40 +149,38 @@ async fn to_xml(args: CommandArgs) -> Result { _ => vec![], }; - Ok( - futures::stream::iter(to_process_input.into_iter().map(move |value| { - let mut w = pretty.as_ref().map_or_else( - || quick_xml::Writer::new(Cursor::new(Vec::new())), - |p| { - quick_xml::Writer::new_with_indent( - Cursor::new(Vec::new()), - b' ', - p.value.expect_int() as usize, - ) - }, - ); + Ok((to_process_input.into_iter().map(move |value| { + let mut w = pretty.as_ref().map_or_else( + || quick_xml::Writer::new(Cursor::new(Vec::new())), + |p| { + quick_xml::Writer::new_with_indent( + Cursor::new(Vec::new()), + b' ', + p.value.expect_int() as usize, + ) + }, + ); - let value_span = value.tag.span; + let value_span = value.tag.span; - match write_xml_events(&value, &mut w) { - Ok(_) => { - let b = w.into_inner().into_inner(); - let s = String::from_utf8(b)?; - ReturnSuccess::value( - UntaggedValue::Primitive(Primitive::String(s)).into_value(&name_tag), - ) - } - Err(_) => Err(ShellError::labeled_error_with_secondary( - "Expected a table with XML-compatible structure from pipeline", - "requires XML-compatible input", - name_span, - "originates from here".to_string(), - value_span, - )), + match write_xml_events(&value, &mut w) { + Ok(_) => { + let b = w.into_inner().into_inner(); + let s = String::from_utf8(b)?; + ReturnSuccess::value( + UntaggedValue::Primitive(Primitive::String(s)).into_value(&name_tag), + ) } - })) - .to_output_stream(), - ) + Err(_) => Err(ShellError::labeled_error_with_secondary( + "Expected a table with XML-compatible structure from pipeline", + "requires XML-compatible input", + name_span, + "originates from here".to_string(), + value_span, + )), + } + })) + .to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/to_yaml.rs b/crates/nu-command/src/commands/to_yaml.rs index caf9f275f3..46aa38847c 100644 --- a/crates/nu-command/src/commands/to_yaml.rs +++ b/crates/nu-command/src/commands/to_yaml.rs @@ -5,7 +5,6 @@ use nu_protocol::{Primitive, ReturnSuccess, Signature, UnspannedPathMember, Unta pub struct ToYaml; -#[async_trait] impl WholeStreamCommand for ToYaml { fn name(&self) -> &str { "to yaml" @@ -19,8 +18,8 @@ impl WholeStreamCommand for ToYaml { "Convert table into .yaml/.yml text" } - async fn run(&self, args: CommandArgs) -> Result { - to_yaml(args).await + fn run(&self, args: CommandArgs) -> Result { + to_yaml(args) } } @@ -114,12 +113,12 @@ pub fn value_to_yaml_value(v: &Value) -> Result { }) } -async fn to_yaml(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn to_yaml(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let name_tag = args.name_tag(); let name_span = name_tag.span; - let input: Vec = args.input.collect().await; + let input: Vec = args.input.collect(); let to_process_input = match input.len() { x if x > 1 => { @@ -133,32 +132,30 @@ async fn to_yaml(args: CommandArgs) -> Result { _ => vec![], }; - Ok( - futures::stream::iter(to_process_input.into_iter().map(move |value| { - let value_span = value.tag.span; + Ok((to_process_input.into_iter().map(move |value| { + let value_span = value.tag.span; - match value_to_yaml_value(&value) { - Ok(yaml_value) => match serde_yaml::to_string(&yaml_value) { - Ok(x) => ReturnSuccess::value( - UntaggedValue::Primitive(Primitive::String(x)).into_value(&name_tag), - ), - _ => Err(ShellError::labeled_error_with_secondary( - "Expected a table with YAML-compatible structure from pipeline", - "requires YAML-compatible input", - name_span, - "originates from here".to_string(), - value_span, - )), - }, - _ => Err(ShellError::labeled_error( + match value_to_yaml_value(&value) { + Ok(yaml_value) => match serde_yaml::to_string(&yaml_value) { + Ok(x) => ReturnSuccess::value( + UntaggedValue::Primitive(Primitive::String(x)).into_value(&name_tag), + ), + _ => Err(ShellError::labeled_error_with_secondary( "Expected a table with YAML-compatible structure from pipeline", "requires YAML-compatible input", - &name_tag, + name_span, + "originates from here".to_string(), + value_span, )), - } - })) - .to_output_stream(), - ) + }, + _ => Err(ShellError::labeled_error( + "Expected a table with YAML-compatible structure from pipeline", + "requires YAML-compatible input", + &name_tag, + )), + } + })) + .to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/touch.rs b/crates/nu-command/src/commands/touch.rs index f0594ece45..5757dc6b0e 100644 --- a/crates/nu-command/src/commands/touch.rs +++ b/crates/nu-command/src/commands/touch.rs @@ -14,7 +14,6 @@ pub struct TouchArgs { rest: Vec>, } -#[async_trait] impl WholeStreamCommand for Touch { fn name(&self) -> &str { "touch" @@ -31,8 +30,8 @@ impl WholeStreamCommand for Touch { fn usage(&self) -> &str { "Creates one or more files." } - async fn run(&self, args: CommandArgs) -> Result { - touch(args).await + fn run(&self, args: CommandArgs) -> Result { + touch(args) } fn examples(&self) -> Vec { @@ -51,8 +50,8 @@ impl WholeStreamCommand for Touch { } } -async fn touch(args: CommandArgs) -> Result { - let (TouchArgs { target, rest }, _) = args.process().await?; +fn touch(args: CommandArgs) -> Result { + let (TouchArgs { target, rest }, _) = args.process()?; for item in vec![target].into_iter().chain(rest.into_iter()) { match OpenOptions::new().write(true).create(true).open(&item) { diff --git a/crates/nu-command/src/commands/uniq.rs b/crates/nu-command/src/commands/uniq.rs index cb54c09d52..732d71a7f3 100644 --- a/crates/nu-command/src/commands/uniq.rs +++ b/crates/nu-command/src/commands/uniq.rs @@ -5,7 +5,6 @@ use nu_protocol::{Signature, UntaggedValue}; pub struct Uniq; -#[async_trait] impl WholeStreamCommand for Uniq { fn name(&self) -> &str { "uniq" @@ -19,8 +18,8 @@ impl WholeStreamCommand for Uniq { "Return the unique rows." } - async fn run(&self, args: CommandArgs) -> Result { - uniq(args).await + fn run(&self, args: CommandArgs) -> Result { + uniq(args) } fn examples(&self) -> Vec { @@ -54,13 +53,13 @@ impl WholeStreamCommand for Uniq { } } -async fn uniq(args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn uniq(args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let should_show_count = args.has("count"); let input = args.input; let uniq_values = { let mut counter = IndexMap::::new(); - for line in input.into_vec().await { + for line in input.into_vec() { *counter.entry(line).or_insert(0) += 1; } counter @@ -116,7 +115,7 @@ async fn uniq(args: CommandArgs) -> Result { } } - Ok(futures::stream::iter(values_vec_deque).to_output_stream()) + Ok(values_vec_deque.into_iter().to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/update.rs b/crates/nu-command/src/commands/update.rs index 434742181d..933ff44349 100644 --- a/crates/nu-command/src/commands/update.rs +++ b/crates/nu-command/src/commands/update.rs @@ -8,8 +8,6 @@ use nu_protocol::{ use nu_source::HasFallibleSpan; use nu_value_ext::ValueExt; -use futures::stream::once; - pub struct Command; #[derive(Deserialize)] @@ -18,7 +16,6 @@ pub struct Arguments { replacement: Value, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "update" @@ -42,8 +39,8 @@ impl WholeStreamCommand for Command { "Update an existing column to have a new value." } - async fn run(&self, args: CommandArgs) -> Result { - update(args).await + fn run(&self, args: CommandArgs) -> Result { + update(args) } fn examples(&self) -> Vec { @@ -67,7 +64,7 @@ impl WholeStreamCommand for Command { } } -async fn process_row( +fn process_row( context: Arc, input: Value, mut replacement: Arc, @@ -83,19 +80,19 @@ async fn process_row( tag: block_tag, } => { let for_block = input.clone(); - let input_stream = once(async { Ok(for_block) }).to_input_stream(); + let input_stream = vec![Ok(for_block)].into_iter().to_input_stream(); context.scope.enter_scope(); context.scope.add_var("$it", input.clone()); context.scope.add_vars(&captured_block.captured.entries); - let result = run_block(&captured_block.block, &*context, input_stream).await; + let result = run_block(&captured_block.block, &*context, input_stream); context.scope.exit_scope(); match result { Ok(mut stream) => { - let values = stream.drain_vec().await; + let values = stream.drain_vec(); let errors = context.get_errors(); if let Some(error) = errors.first() { @@ -172,25 +169,23 @@ async fn process_row( }) } -async fn update(raw_args: CommandArgs) -> Result { +fn update(raw_args: CommandArgs) -> Result { let name_tag = Arc::new(raw_args.call_info.name_tag.clone()); let context = Arc::new(EvaluationContext::from_args(&raw_args)); - let (Arguments { field, replacement }, input) = raw_args.process().await?; + let (Arguments { field, replacement }, input) = raw_args.process()?; let replacement = Arc::new(replacement); let field = Arc::new(field); Ok(input - .then(move |input| { + .map(move |input| { let tag = name_tag.clone(); let context = context.clone(); let replacement = replacement.clone(); let field = field.clone(); - async { - match process_row(context, input, replacement, field, tag).await { - Ok(s) => s, - Err(e) => OutputStream::one(Err(e)), - } + match process_row(context, input, replacement, field, tag) { + Ok(s) => s, + Err(e) => OutputStream::one(Err(e)), } }) .flatten() diff --git a/crates/nu-command/src/commands/url_/command.rs b/crates/nu-command/src/commands/url_/command.rs index 2de6681a1c..72a7566ff5 100644 --- a/crates/nu-command/src/commands/url_/command.rs +++ b/crates/nu-command/src/commands/url_/command.rs @@ -5,7 +5,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; pub struct Url; -#[async_trait] impl WholeStreamCommand for Url { fn name(&self) -> &str { "url" @@ -19,7 +18,7 @@ impl WholeStreamCommand for Url { "Apply url function." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(get_full_help(&Url, &args.scope)).into_value(Tag::unknown()), ))) diff --git a/crates/nu-command/src/commands/url_/host.rs b/crates/nu-command/src/commands/url_/host.rs index c3d2807077..ea519e9210 100644 --- a/crates/nu-command/src/commands/url_/host.rs +++ b/crates/nu-command/src/commands/url_/host.rs @@ -8,7 +8,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct UrlHost; -#[async_trait] impl WholeStreamCommand for UrlHost { fn name(&self) -> &str { "url host" @@ -23,9 +22,9 @@ impl WholeStreamCommand for UrlHost { "gets the host of a url" } - async fn run(&self, args: CommandArgs) -> Result { - let (DefaultArguments { rest }, input) = args.process().await?; - operate(input, rest, &host).await + fn run(&self, args: CommandArgs) -> Result { + let (DefaultArguments { rest }, input) = args.process()?; + Ok(operate(input, rest, &host)) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/url_/mod.rs b/crates/nu-command/src/commands/url_/mod.rs index 2b089673bd..0fd5462598 100644 --- a/crates/nu-command/src/commands/url_/mod.rs +++ b/crates/nu-command/src/commands/url_/mod.rs @@ -42,15 +42,11 @@ where Ok(v) } -async fn operate( - input: crate::InputStream, - paths: Vec, - action: &'static F, -) -> Result +fn operate(input: crate::InputStream, paths: Vec, action: &'static F) -> OutputStream where F: Fn(&Url) -> &str + Send + Sync + 'static, { - Ok(input + input .map(move |v| { if paths.is_empty() { ReturnSuccess::value(handle_value(&action, &v)?) @@ -67,5 +63,5 @@ where ReturnSuccess::value(ret) } }) - .to_output_stream()) + .to_output_stream() } diff --git a/crates/nu-command/src/commands/url_/path.rs b/crates/nu-command/src/commands/url_/path.rs index b31f4f5bf5..d9ca82d5d6 100644 --- a/crates/nu-command/src/commands/url_/path.rs +++ b/crates/nu-command/src/commands/url_/path.rs @@ -8,7 +8,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct UrlPath; -#[async_trait] impl WholeStreamCommand for UrlPath { fn name(&self) -> &str { "url path" @@ -23,9 +22,9 @@ impl WholeStreamCommand for UrlPath { "gets the path of a url" } - async fn run(&self, args: CommandArgs) -> Result { - let (DefaultArguments { rest }, input) = args.process().await?; - operate(input, rest, &Url::path).await + fn run(&self, args: CommandArgs) -> Result { + let (DefaultArguments { rest }, input) = args.process()?; + Ok(operate(input, rest, &Url::path)) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/url_/query.rs b/crates/nu-command/src/commands/url_/query.rs index 6378a0661e..05e256236e 100644 --- a/crates/nu-command/src/commands/url_/query.rs +++ b/crates/nu-command/src/commands/url_/query.rs @@ -8,7 +8,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct UrlQuery; -#[async_trait] impl WholeStreamCommand for UrlQuery { fn name(&self) -> &str { "url query" @@ -23,9 +22,9 @@ impl WholeStreamCommand for UrlQuery { "gets the query of a url" } - async fn run(&self, args: CommandArgs) -> Result { - let (DefaultArguments { rest }, input) = args.process().await?; - operate(input, rest, &query).await + fn run(&self, args: CommandArgs) -> Result { + let (DefaultArguments { rest }, input) = args.process()?; + Ok(operate(input, rest, &query)) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/url_/scheme.rs b/crates/nu-command/src/commands/url_/scheme.rs index da7ed52d55..4ff31d72bd 100644 --- a/crates/nu-command/src/commands/url_/scheme.rs +++ b/crates/nu-command/src/commands/url_/scheme.rs @@ -8,7 +8,6 @@ use nu_protocol::{Signature, SyntaxShape, Value}; pub struct UrlScheme; -#[async_trait] impl WholeStreamCommand for UrlScheme { fn name(&self) -> &str { "url scheme" @@ -22,9 +21,9 @@ impl WholeStreamCommand for UrlScheme { "gets the scheme (eg http, file) of a url" } - async fn run(&self, args: CommandArgs) -> Result { - let (DefaultArguments { rest }, input) = args.process().await?; - operate(input, rest, &Url::scheme).await + fn run(&self, args: CommandArgs) -> Result { + let (DefaultArguments { rest }, input) = args.process()?; + Ok(operate(input, rest, &Url::scheme)) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/version.rs b/crates/nu-command/src/commands/version.rs index 12f90d775d..a913ed81e2 100644 --- a/crates/nu-command/src/commands/version.rs +++ b/crates/nu-command/src/commands/version.rs @@ -10,7 +10,6 @@ pub mod shadow { pub struct Version; -#[async_trait] impl WholeStreamCommand for Version { fn name(&self) -> &str { "version" @@ -24,7 +23,7 @@ impl WholeStreamCommand for Version { "Display Nu version." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { version(args) } diff --git a/crates/nu-command/src/commands/where_.rs b/crates/nu-command/src/commands/where_.rs index a8f9bc707f..b1d6c6075c 100644 --- a/crates/nu-command/src/commands/where_.rs +++ b/crates/nu-command/src/commands/where_.rs @@ -13,7 +13,6 @@ pub struct Arguments { block: CapturedBlock, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "where" @@ -31,8 +30,8 @@ impl WholeStreamCommand for Command { "Filter table to match the condition." } - async fn run(&self, args: CommandArgs) -> Result { - where_command(args).await + fn run(&self, args: CommandArgs) -> Result { + where_command(args) } fn examples(&self) -> Vec { @@ -60,10 +59,10 @@ impl WholeStreamCommand for Command { ] } } -async fn where_command(raw_args: CommandArgs) -> Result { +fn where_command(raw_args: CommandArgs) -> Result { let ctx = Arc::new(EvaluationContext::from_args(&raw_args)); let tag = raw_args.call_info.name_tag.clone(); - let (Arguments { block }, input) = raw_args.process().await?; + let (Arguments { block }, input) = raw_args.process()?; let condition = { if block.block.block.len() != 1 { return Err(ShellError::labeled_error( @@ -102,24 +101,22 @@ async fn where_command(raw_args: CommandArgs) -> Result match condition.as_bool() { - Ok(b) => { - if b { - Some(Ok(ReturnSuccess::Value(input))) - } else { - None - } + match condition { + Ok(condition) => match condition.as_bool() { + Ok(b) => { + if b { + Some(Ok(ReturnSuccess::Value(input))) + } else { + None } - Err(e) => Some(Err(e)), - }, + } Err(e) => Some(Err(e)), - } + }, + Err(e) => Some(Err(e)), } }) .to_output_stream()) diff --git a/crates/nu-command/src/commands/which_.rs b/crates/nu-command/src/commands/which_.rs index 2afcadafcd..176dd7c0a1 100644 --- a/crates/nu-command/src/commands/which_.rs +++ b/crates/nu-command/src/commands/which_.rs @@ -8,7 +8,6 @@ use nu_source::Tagged; pub struct Which; -#[async_trait] impl WholeStreamCommand for Which { fn name(&self) -> &str { "which" @@ -25,8 +24,8 @@ impl WholeStreamCommand for Which { "Finds a program file, alias or custom command." } - async fn run(&self, args: CommandArgs) -> Result { - which(args).await + fn run(&self, args: CommandArgs) -> Result { + which(args) } } @@ -137,22 +136,24 @@ macro_rules! entry_path { } #[cfg(feature = "ichwh")] -async fn get_first_entry_in_path(item: &str, tag: Tag) -> Option { - ichwh::which(item) - .await +fn get_first_entry_in_path(item: &str, tag: Tag) -> Option { + use futures::executor::block_on; + + block_on(ichwh::which(item)) .unwrap_or(None) .map(|path| entry_path!(item, path.into(), tag)) } #[cfg(not(feature = "ichwh"))] -async fn get_first_entry_in_path(_: &str, _: Tag) -> Option { +fn get_first_entry_in_path(_: &str, _: Tag) -> Option { None } #[cfg(feature = "ichwh")] -async fn get_all_entries_in_path(item: &str, tag: Tag) -> Vec { - ichwh::which_all(&item) - .await +fn get_all_entries_in_path(item: &str, tag: Tag) -> Vec { + use futures::executor::block_on; + + block_on(ichwh::which_all(&item)) .unwrap_or_default() .into_iter() .map(|path| entry_path!(item, path.into(), tag.clone())) @@ -160,7 +161,7 @@ async fn get_all_entries_in_path(item: &str, tag: Tag) -> Vec { } #[cfg(not(feature = "ichwh"))] -async fn get_all_entries_in_path(_: &str, _: Tag) -> Vec { +fn get_all_entries_in_path(_: &str, _: Tag) -> Vec { vec![] } @@ -171,7 +172,7 @@ struct WhichArgs { all: bool, } -async fn which_single(application: Tagged, all: bool, scope: &Scope) -> Vec { +fn which_single(application: Tagged, all: bool, scope: &Scope) -> Vec { let (external, prog_name) = if application.starts_with('^') { (true, application.item[1..].to_string()) } else { @@ -183,9 +184,7 @@ async fn which_single(application: Tagged, all: bool, scope: &Scope) -> //program //This match handles all different cases match (all, external) { - (true, true) => { - return get_all_entries_in_path(&prog_name, application.tag.clone()).await; - } + (true, true) => get_all_entries_in_path(&prog_name, application.tag), (true, false) => { let mut output: Vec = vec![]; output.extend(get_entries_in_nu( @@ -194,31 +193,28 @@ async fn which_single(application: Tagged, all: bool, scope: &Scope) -> application.tag.clone(), false, )); - output.extend(get_all_entries_in_path(&prog_name, application.tag.clone()).await); - return output; + output.extend(get_all_entries_in_path(&prog_name, application.tag)); + output } (false, true) => { - if let Some(entry) = get_first_entry_in_path(&prog_name, application.tag.clone()).await - { + if let Some(entry) = get_first_entry_in_path(&prog_name, application.tag) { return vec![entry]; } - return vec![]; + vec![] } (false, false) => { let nu_entries = get_entries_in_nu(scope, &prog_name, application.tag.clone(), true); if !nu_entries.is_empty() { return vec![nu_entries[0].clone()]; - } else if let Some(entry) = - get_first_entry_in_path(&prog_name, application.tag.clone()).await - { + } else if let Some(entry) = get_first_entry_in_path(&prog_name, application.tag) { return vec![entry]; } - return vec![]; + vec![] } } } -async fn which(args: CommandArgs) -> Result { +fn which(args: CommandArgs) -> Result { let scope = args.scope.clone(); let ( @@ -228,16 +224,16 @@ async fn which(args: CommandArgs) -> Result { all, }, _, - ) = args.process().await?; + ) = args.process()?; let mut output = vec![]; for app in vec![application].into_iter().chain(rest.into_iter()) { - let values = which_single(app, all, &scope).await; + let values = which_single(app, all, &scope); output.extend(values); } - Ok(futures::stream::iter(output.into_iter().map(ReturnSuccess::value)).to_output_stream()) + Ok((output.into_iter().map(ReturnSuccess::value)).to_output_stream()) } #[cfg(test)] diff --git a/crates/nu-command/src/commands/with_env.rs b/crates/nu-command/src/commands/with_env.rs index 1a2c3c6a79..68dc4ade0e 100644 --- a/crates/nu-command/src/commands/with_env.rs +++ b/crates/nu-command/src/commands/with_env.rs @@ -14,7 +14,6 @@ struct WithEnvArgs { block: CapturedBlock, } -#[async_trait] impl WholeStreamCommand for WithEnv { fn name(&self) -> &str { "with-env" @@ -38,8 +37,8 @@ impl WholeStreamCommand for WithEnv { "Runs a block with an environment variable set." } - async fn run(&self, args: CommandArgs) -> Result { - with_env(args).await + fn run(&self, args: CommandArgs) -> Result { + with_env(args) } fn examples(&self) -> Vec { @@ -68,7 +67,7 @@ impl WholeStreamCommand for WithEnv { } } -async fn with_env(raw_args: CommandArgs) -> Result { +fn with_env(raw_args: CommandArgs) -> Result { let redirection = raw_args.call_info.args.external_redirection; let context = EvaluationContext::from_args(&raw_args); let ( @@ -77,7 +76,7 @@ async fn with_env(raw_args: CommandArgs) -> Result { mut block, }, input, - ) = raw_args.process().await?; + ) = raw_args.process()?; block.block.set_redirect(redirection); @@ -117,7 +116,7 @@ async fn with_env(raw_args: CommandArgs) -> Result { context.scope.add_env(env); context.scope.add_vars(&block.captured.entries); - let result = run_block(&block.block, &context, input).await; + let result = run_block(&block.block, &context, input); context.scope.exit_scope(); result.map(|x| x.to_output_stream()) diff --git a/crates/nu-command/src/commands/wrap.rs b/crates/nu-command/src/commands/wrap.rs index c8afab8784..fcbf5e9f47 100644 --- a/crates/nu-command/src/commands/wrap.rs +++ b/crates/nu-command/src/commands/wrap.rs @@ -14,7 +14,6 @@ struct WrapArgs { column: Option>, } -#[async_trait] impl WholeStreamCommand for Wrap { fn name(&self) -> &str { "wrap" @@ -32,8 +31,8 @@ impl WholeStreamCommand for Wrap { "Wraps the given data in a table." } - async fn run(&self, args: CommandArgs) -> Result { - wrap(args).await + fn run(&self, args: CommandArgs) -> Result { + wrap(args) } fn examples(&self) -> Vec { @@ -78,12 +77,12 @@ impl WholeStreamCommand for Wrap { } } -async fn wrap(args: CommandArgs) -> Result { - let (WrapArgs { column }, mut input) = args.process().await?; +fn wrap(args: CommandArgs) -> Result { + let (WrapArgs { column }, input) = args.process()?; let mut result_table = vec![]; let mut are_all_rows = true; - while let Some(value) = input.next().await { + for value in input { match value { Value { value: UntaggedValue::Row(_), @@ -122,10 +121,7 @@ async fn wrap(args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value(row))) } else { - Ok( - futures::stream::iter(result_table.into_iter().map(ReturnSuccess::value)) - .to_output_stream(), - ) + Ok((result_table.into_iter().map(ReturnSuccess::value)).to_output_stream()) } } diff --git a/crates/nu-command/src/examples.rs b/crates/nu-command/src/examples.rs index c44da09467..cd10723014 100644 --- a/crates/nu-command/src/examples.rs +++ b/crates/nu-command/src/examples.rs @@ -21,8 +21,6 @@ use crate::commands::{ use nu_engine::{run_block, whole_stream_command, Command, EvaluationContext, WholeStreamCommand}; use nu_stream::InputStream; -use futures::executor::block_on; - pub fn test_examples(cmd: Command) -> Result<(), ShellError> { let examples = cmd.examples(); @@ -56,7 +54,7 @@ pub fn test_examples(cmd: Command) -> Result<(), ShellError> { println!("{:#?}", block); if let Some(expected) = &sample_pipeline.result { - let result = block_on(evaluate_block(block, &mut ctx))?; + let result = evaluate_block(block, &mut ctx)?; ctx.with_errors(|reasons| reasons.iter().cloned().take(1).next()) .map_or(Ok(()), Err)?; @@ -113,7 +111,7 @@ pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> { let block = parse_line(sample_pipeline.example, &ctx)?; if let Some(expected) = &sample_pipeline.result { - let result = block_on(evaluate_block(block, &mut ctx))?; + let result = evaluate_block(block, &mut ctx)?; ctx.with_errors(|reasons| reasons.iter().cloned().take(1).next()) .map_or(Ok(()), Err)?; @@ -179,7 +177,7 @@ pub fn test_anchors(cmd: Command) -> Result<(), ShellError> { let block = parse_line(&pipeline_with_anchor, &ctx)?; if sample_pipeline.result.is_some() { - let result = block_on(evaluate_block(block, &mut ctx))?; + let result = evaluate_block(block, &mut ctx)?; ctx.with_errors(|reasons| reasons.iter().cloned().take(1).next()) .map_or(Ok(()), Err)?; @@ -225,7 +223,7 @@ fn parse_line(line: &str, ctx: &EvaluationContext) -> Result Result, ShellError> { @@ -233,11 +231,11 @@ async fn evaluate_block( ctx.scope.enter_scope(); - let result = run_block(&block.block, ctx, input_stream).await; + let result = run_block(&block.block, ctx, input_stream); ctx.scope.exit_scope(); - let result = result?.drain_vec().await; + let result = result?.drain_vec(); Ok(result) } diff --git a/crates/nu-command/src/examples/double_echo.rs b/crates/nu-command/src/examples/double_echo.rs index 2efcf0b470..02b0a594e2 100644 --- a/crates/nu-command/src/examples/double_echo.rs +++ b/crates/nu-command/src/examples/double_echo.rs @@ -4,8 +4,6 @@ use nu_engine::{CommandArgs, WholeStreamCommand}; use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_stream::{OutputStream, ToOutputStream}; -use async_trait::async_trait; -use futures::StreamExt; use serde::Deserialize; pub struct Command; @@ -15,7 +13,6 @@ struct Arguments { pub rest: Vec, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "echo" @@ -29,12 +26,12 @@ impl WholeStreamCommand for Command { "Mock echo." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); - let (Arguments { rest }, input) = args.process().await?; + let (Arguments { rest }, input) = args.process()?; let mut base_value = UntaggedValue::string("Yehuda Katz in Ecuador").into_value(name_tag); - let input: Vec = input.collect().await; + let input: Vec = input.collect(); if let Some(first) = input.get(0) { base_value = first.clone() @@ -63,18 +60,15 @@ impl WholeStreamCommand for Command { let subtable = vec![UntaggedValue::Table(values).into_value(base_value.tag())]; - futures::stream::iter(subtable.into_iter().map(ReturnSuccess::value)) - .to_output_stream() + (subtable.into_iter().map(ReturnSuccess::value)).to_output_stream() } else { - futures::stream::iter( - table - .into_iter() - .map(move |mut v| { - v.tag = base_value.tag(); - v - }) - .map(ReturnSuccess::value), - ) + (table + .into_iter() + .map(move |mut v| { + v.tag = base_value.tag(); + v + }) + .map(ReturnSuccess::value)) .to_output_stream() } } @@ -86,6 +80,6 @@ impl WholeStreamCommand for Command { } }); - Ok(futures::stream::iter(stream).flatten().to_output_stream()) + Ok((stream).flatten().to_output_stream()) } } diff --git a/crates/nu-command/src/examples/double_ls.rs b/crates/nu-command/src/examples/double_ls.rs index 382d493b37..256d04f06f 100644 --- a/crates/nu-command/src/examples/double_ls.rs +++ b/crates/nu-command/src/examples/double_ls.rs @@ -5,12 +5,8 @@ use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value}; use nu_stream::{OutputStream, ToOutputStream}; -use async_trait::async_trait; -use futures::StreamExt; - pub struct Command; -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "ls" @@ -24,28 +20,26 @@ impl WholeStreamCommand for Command { "Mock ls." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); let mut base_value = UntaggedValue::string("Andrés N. Robalino in Portland").into_value(name_tag); - let input: Vec = args.input.collect().await; + let input: Vec = args.input.collect(); if let Some(first) = input.get(0) { base_value = first.clone() } - Ok(futures::stream::iter( - file_listing() - .iter() - .map(|row| Value { - value: row.value.clone(), - tag: base_value.tag.clone(), - }) - .collect::>() - .into_iter() - .map(ReturnSuccess::value), - ) + Ok((file_listing() + .iter() + .map(|row| Value { + value: row.value.clone(), + tag: base_value.tag.clone(), + }) + .collect::>() + .into_iter() + .map(ReturnSuccess::value)) .to_output_stream()) } } diff --git a/crates/nu-command/src/examples/stub_generate.rs b/crates/nu-command/src/examples/stub_generate.rs index b55862b54e..abbe847f37 100644 --- a/crates/nu-command/src/examples/stub_generate.rs +++ b/crates/nu-command/src/examples/stub_generate.rs @@ -4,7 +4,6 @@ use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value}; use nu_source::{AnchorLocation, Tag}; use nu_stream::OutputStream; -use async_trait::async_trait; use serde::Deserialize; pub struct Command; @@ -14,7 +13,6 @@ struct Arguments { path: Option, } -#[async_trait] impl WholeStreamCommand for Command { fn name(&self) -> &str { "stub open" @@ -28,10 +26,10 @@ impl WholeStreamCommand for Command { "Generates tables and metadata that mimics behavior of real commands in controlled ways." } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let name_tag = args.call_info.name_tag.clone(); - let (Arguments { path: mocked_path }, _input) = args.process().await?; + let (Arguments { path: mocked_path }, _input) = args.process()?; let out = UntaggedValue::string("Yehuda Katz in Ecuador"); diff --git a/crates/nu-command/src/futures.rs b/crates/nu-command/src/futures.rs index 8bfc7203c7..aa1fce69b6 100644 --- a/crates/nu-command/src/futures.rs +++ b/crates/nu-command/src/futures.rs @@ -1,7 +1,5 @@ -use futures::stream::Stream; -use std::pin::Pin; use std::sync::{mpsc, Arc, Mutex}; -use std::task::{self, Poll, Waker}; +use std::task::Waker; use std::thread; #[allow(clippy::option_option)] @@ -76,20 +74,37 @@ impl ThreadedReceiver { } } -impl Stream for ThreadedReceiver { +// impl Stream for ThreadedReceiver { +// type Item = T; + +// fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll> { +// let mut shared_state = self +// .shared_state +// .lock() +// .expect("ThreadedFuture shared state shouldn't be poisoned"); + +// if let Some(result) = shared_state.result.take() { +// Poll::Ready(result) +// } else { +// shared_state.waker = Some(cx.waker().clone()); +// Poll::Pending +// } +// } +// } + +impl Iterator for ThreadedReceiver { type Item = T; - fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll> { - let mut shared_state = self - .shared_state - .lock() - .expect("ThreadedFuture shared state shouldn't be poisoned"); + fn next(&mut self) -> Option { + loop { + let mut shared_state = self + .shared_state + .lock() + .expect("ThreadedFuture shared state shouldn't be poisoned"); - if let Some(result) = shared_state.result.take() { - Poll::Ready(result) - } else { - shared_state.waker = Some(cx.waker().clone()); - Poll::Pending + if let Some(result) = shared_state.result.take() { + return result; + } } } } @@ -110,7 +125,6 @@ impl Drop for ThreadedReceiver { mod tests { mod threaded_receiver { use super::super::ThreadedReceiver; - use futures::executor::block_on_stream; use std::sync::mpsc; #[test] @@ -123,7 +137,7 @@ mod tests { }); let stream = ThreadedReceiver::new(rx); - let mut result = block_on_stream(stream); + let mut result = stream; assert_eq!(Some(1), result.next()); assert_eq!(Some(2), result.next()); assert_eq!(Some(3), result.next()); @@ -139,7 +153,7 @@ mod tests { { let stream = ThreadedReceiver::new(rx); - let mut result = block_on_stream(stream); + let mut result = stream; assert_eq!(Some(1), result.next()); } let result = th.join(); diff --git a/crates/nu-command/src/prelude.rs b/crates/nu-command/src/prelude.rs index 8bc256f10d..52017bb81a 100644 --- a/crates/nu-command/src/prelude.rs +++ b/crates/nu-command/src/prelude.rs @@ -21,34 +21,7 @@ macro_rules! stream { }} } -#[macro_export] -macro_rules! trace_out_stream { - (target: $target:tt, $desc:tt = $expr:expr) => {{ - if log::log_enabled!(target: $target, log::Level::Trace) { - use futures::stream::StreamExt; - - let objects = $expr.inspect(move |o| { - trace!( - target: $target, - "{} = {}", - $desc, - match o { - Err(err) => format!("{:?}", err), - Ok(value) => value.display(), - } - ); - }); - - nu_stream::OutputStream::new(objects) - } else { - $expr - } - }}; -} - -pub(crate) use async_trait::async_trait; pub(crate) use bigdecimal::BigDecimal; -pub(crate) use futures::{Stream, StreamExt}; pub(crate) use indexmap::{indexmap, IndexMap}; pub(crate) use itertools::Itertools; pub(crate) use nu_data::config; @@ -70,7 +43,6 @@ pub(crate) use num_bigint::BigInt; pub(crate) use num_traits::cast::ToPrimitive; pub(crate) use serde::Deserialize; pub(crate) use std::collections::VecDeque; -pub(crate) use std::future::Future; pub(crate) use std::sync::atomic::AtomicBool; pub(crate) use std::sync::Arc; @@ -81,11 +53,11 @@ pub trait FromInputStream { impl FromInputStream for T where - T: Stream + Send + 'static, + T: Iterator + Send + Sync + 'static, { fn from_input_stream(self) -> OutputStream { OutputStream { - values: self.map(nu_protocol::ReturnSuccess::value).boxed(), + values: Box::new(self.map(nu_protocol::ReturnSuccess::value)), } } } @@ -97,12 +69,12 @@ pub trait ToOutputStream { impl ToOutputStream for T where - T: Stream + Send + 'static, + T: Iterator + Send + Sync + 'static, U: Into, { fn to_output_stream(self) -> OutputStream { OutputStream { - values: self.map(|item| item.into()).boxed(), + values: Box::new(self.map(|item| item.into())), } } } diff --git a/crates/nu-engine/src/call_info.rs b/crates/nu-engine/src/call_info.rs index 25151e6b4b..d6822017a0 100644 --- a/crates/nu-engine/src/call_info.rs +++ b/crates/nu-engine/src/call_info.rs @@ -12,8 +12,8 @@ pub struct UnevaluatedCallInfo { } impl UnevaluatedCallInfo { - pub async fn evaluate(self, ctx: &EvaluationContext) -> Result { - let args = evaluate_args(&self.args, ctx).await?; + pub fn evaluate(self, ctx: &EvaluationContext) -> Result { + let args = evaluate_args(&self.args, ctx)?; Ok(CallInfo { args, diff --git a/crates/nu-engine/src/command_args.rs b/crates/nu-engine/src/command_args.rs index ba394c559c..ba8f7d077e 100644 --- a/crates/nu-engine/src/command_args.rs +++ b/crates/nu-engine/src/command_args.rs @@ -64,14 +64,14 @@ impl std::fmt::Debug for CommandArgs { } impl CommandArgs { - pub async fn evaluate_once(self) -> Result { + pub fn evaluate_once(self) -> Result { let ctx = EvaluationContext::from_args(&self); let host = self.host.clone(); let ctrl_c = self.ctrl_c.clone(); let configs = self.configs.clone(); let shell_manager = self.shell_manager.clone(); let input = self.input; - let call_info = self.call_info.evaluate(&ctx).await?; + let call_info = self.call_info.evaluate(&ctx)?; let scope = self.scope.clone(); Ok(EvaluatedWholeStreamCommandArgs::new( @@ -85,8 +85,8 @@ impl CommandArgs { )) } - pub async fn process<'de, T: Deserialize<'de>>(self) -> Result<(T, InputStream), ShellError> { - let args = self.evaluate_once().await?; + pub fn process<'de, T: Deserialize<'de>>(self) -> Result<(T, InputStream), ShellError> { + let args = self.evaluate_once()?; let call_info = args.call_info.clone(); let mut deserializer = ConfigDeserializer::from_call_info(call_info); diff --git a/crates/nu-engine/src/evaluate/block.rs b/crates/nu-engine/src/evaluate/block.rs index e2f4eab34e..328cc2b314 100644 --- a/crates/nu-engine/src/evaluate/block.rs +++ b/crates/nu-engine/src/evaluate/block.rs @@ -1,8 +1,6 @@ use crate::evaluate::expr::run_expression_block; use crate::evaluate::internal::run_internal_command; use crate::evaluation_context::EvaluationContext; -use async_recursion::async_recursion; -use futures::stream::TryStreamExt; use nu_errors::ShellError; use nu_parser::ParserScope; use nu_protocol::hir::{ @@ -14,8 +12,7 @@ use nu_stream::InputStream; use nu_stream::ToOutputStream; use std::sync::atomic::Ordering; -#[async_recursion] -pub async fn run_block( +pub fn run_block( block: &Block, ctx: &EvaluationContext, mut input: InputStream, @@ -32,34 +29,31 @@ pub async fn run_block( // Run autoview on the values we've seen so far // We may want to make this configurable for other kinds of hosting if let Some(autoview) = ctx.get_command("autoview") { - let mut output_stream = match ctx - .run_command( - autoview, - Tag::unknown(), - Call::new( - Box::new(SpannedExpression::new( - Expression::Synthetic(Synthetic::String("autoview".into())), - Span::unknown(), - )), + let mut output_stream = match ctx.run_command( + autoview, + Tag::unknown(), + Call::new( + Box::new(SpannedExpression::new( + Expression::Synthetic(Synthetic::String("autoview".into())), Span::unknown(), - ), - inp, - ) - .await - { + )), + Span::unknown(), + ), + inp, + ) { Ok(x) => x, Err(e) => { return Err(e); } }; - match output_stream.try_next().await { - Ok(Some(ReturnSuccess::Value(Value { + match output_stream.next() { + Some(Ok(ReturnSuccess::Value(Value { value: UntaggedValue::Error(e), .. }))) => { return Err(e); } - Ok(Some(_item)) => { + Some(Ok(_item)) => { if let Some(err) = ctx.get_errors().get(0) { ctx.clear_errors(); return Err(err.clone()); @@ -68,13 +62,13 @@ pub async fn run_block( return Ok(InputStream::empty()); } } - Ok(None) => { + None => { if let Some(err) = ctx.get_errors().get(0) { ctx.clear_errors(); return Err(err.clone()); } } - Err(e) => { + Some(Err(e)) => { return Err(e); } } @@ -91,14 +85,14 @@ pub async fn run_block( Ok(inp) => { let mut output_stream = inp.to_output_stream(); - match output_stream.try_next().await { - Ok(Some(ReturnSuccess::Value(Value { + match output_stream.next() { + Some(Ok(ReturnSuccess::Value(Value { value: UntaggedValue::Error(e), .. }))) => { return Err(e); } - Ok(Some(_item)) => { + Some(Ok(_item)) => { if let Some(err) = ctx.get_errors().get(0) { ctx.clear_errors(); return Err(err.clone()); @@ -112,13 +106,13 @@ pub async fn run_block( return Ok(InputStream::empty()); } } - Ok(None) => { + None => { if let Some(err) = ctx.get_errors().get(0) { ctx.clear_errors(); return Err(err.clone()); } } - Err(e) => { + Some(Err(e)) => { return Err(e); } } @@ -127,7 +121,7 @@ pub async fn run_block( return Err(e); } } - output = run_pipeline(pipeline, ctx, input).await; + output = run_pipeline(pipeline, ctx, input); input = InputStream::empty(); } @@ -136,8 +130,7 @@ pub async fn run_block( output } -#[async_recursion] -async fn run_pipeline( +fn run_pipeline( commands: &Pipeline, ctx: &EvaluationContext, mut input: InputStream, @@ -148,7 +141,7 @@ async fn run_pipeline( let mut args = vec![]; if let Some(positional) = call.positional { for pos in &positional { - let result = run_expression_block(pos, ctx).await?.into_vec().await; + let result = run_expression_block(pos, ctx)?.into_vec(); args.push(result); } } @@ -159,7 +152,7 @@ async fn run_pipeline( for (param, value) in block.params.positional.iter().zip(args.iter()) { ctx.scope.add_var(param.0.name(), value[0].clone()); } - let result = run_block(&block, ctx, input).await; + let result = run_block(&block, ctx, input); ctx.scope.exit_scope(); let result = result?; @@ -180,7 +173,7 @@ async fn run_pipeline( { ctx.scope.add_var(param.0.name(), value[0].clone()); } - let result = run_block(&captured_block.block, ctx, input).await; + let result = run_block(&captured_block.block, ctx, input); ctx.scope.exit_scope(); let result = result?; @@ -204,11 +197,11 @@ async fn run_pipeline( } } - ClassifiedCommand::Expr(expr) => run_expression_block(&*expr, ctx).await?, + ClassifiedCommand::Expr(expr) => run_expression_block(&*expr, ctx)?, ClassifiedCommand::Error(err) => return Err(err.into()), - ClassifiedCommand::Internal(left) => run_internal_command(left, ctx, input).await?, + ClassifiedCommand::Internal(left) => run_internal_command(left, ctx, input)?, }; } diff --git a/crates/nu-engine/src/evaluate/evaluate_args.rs b/crates/nu-engine/src/evaluate/evaluate_args.rs index 95dd57cbd3..c11d5e1c15 100644 --- a/crates/nu-engine/src/evaluate/evaluate_args.rs +++ b/crates/nu-engine/src/evaluate/evaluate_args.rs @@ -5,7 +5,7 @@ use indexmap::IndexMap; use nu_errors::ShellError; use nu_protocol::{hir, EvaluatedArgs, UntaggedValue, Value}; -pub(crate) async fn evaluate_args( +pub(crate) fn evaluate_args( call: &hir::Call, ctx: &EvaluationContext, ) -> Result { @@ -13,7 +13,7 @@ pub(crate) async fn evaluate_args( if let Some(positional) = &call.positional { for pos in positional { - let result = evaluate_baseline_expr(pos, ctx).await?; + let result = evaluate_baseline_expr(pos, ctx)?; positional_args.push(result); } } @@ -33,7 +33,7 @@ pub(crate) async fn evaluate_args( named_args.insert(name.clone(), UntaggedValue::boolean(true).into_value(tag)); } hir::NamedValue::Value(_, expr) => { - named_args.insert(name.clone(), evaluate_baseline_expr(expr, ctx).await?); + named_args.insert(name.clone(), evaluate_baseline_expr(expr, ctx)?); } _ => {} }; diff --git a/crates/nu-engine/src/evaluate/evaluator.rs b/crates/nu-engine/src/evaluate/evaluator.rs index c7c323c7b7..91c5c200b7 100644 --- a/crates/nu-engine/src/evaluate/evaluator.rs +++ b/crates/nu-engine/src/evaluate/evaluator.rs @@ -1,7 +1,6 @@ use crate::evaluate::block::run_block; use crate::evaluate::operator::apply_operator; use crate::evaluation_context::EvaluationContext; -use async_recursion::async_recursion; use indexmap::IndexMap; use log::trace; use nu_errors::{ArgumentError, ShellError}; @@ -17,8 +16,7 @@ use nu_source::{Span, SpannedItem, Tag}; use nu_stream::InputStream; use nu_value_ext::ValueExt; -#[async_recursion] -pub async fn evaluate_baseline_expr( +pub fn evaluate_baseline_expr( expr: &SpannedExpression, ctx: &EvaluationContext, ) -> Result { @@ -39,12 +37,12 @@ pub async fn evaluate_baseline_expr( } Expression::Variable(var, _) => evaluate_reference(&var, ctx, tag), Expression::Command => unimplemented!(), - Expression::Invocation(block) => evaluate_invocation(block, ctx).await, + Expression::Invocation(block) => evaluate_invocation(block, ctx), Expression::ExternalCommand(_) => unimplemented!(), Expression::Binary(binary) => { // TODO: If we want to add short-circuiting, we'll need to move these down - let left = evaluate_baseline_expr(&binary.left, ctx).await?; - let right = evaluate_baseline_expr(&binary.right, ctx).await?; + let left = evaluate_baseline_expr(&binary.left, ctx)?; + let right = evaluate_baseline_expr(&binary.right, ctx)?; trace!("left={:?} right={:?}", left.value, right.value); @@ -66,13 +64,13 @@ pub async fn evaluate_baseline_expr( } Expression::Range(range) => { let left = if let Some(left) = &range.left { - evaluate_baseline_expr(&left, ctx).await? + evaluate_baseline_expr(&left, ctx)? } else { Value::nothing() }; let right = if let Some(right) = &range.right { - evaluate_baseline_expr(&right, ctx).await? + evaluate_baseline_expr(&right, ctx)? } else { Value::nothing() }; @@ -98,7 +96,7 @@ pub async fn evaluate_baseline_expr( let mut output_headers = vec![]; for expr in headers { - let val = evaluate_baseline_expr(&expr, ctx).await?; + let val = evaluate_baseline_expr(&expr, ctx)?; let header = val.as_string()?; output_headers.push(header); @@ -126,7 +124,7 @@ pub async fn evaluate_baseline_expr( let mut row_output = IndexMap::new(); for cell in output_headers.iter().zip(row.iter()) { - let val = evaluate_baseline_expr(&cell.1, ctx).await?; + let val = evaluate_baseline_expr(&cell.1, ctx)?; row_output.insert(cell.0.clone(), val); } output_table.push(UntaggedValue::row(row_output).into_value(tag.clone())); @@ -138,7 +136,7 @@ pub async fn evaluate_baseline_expr( let mut exprs = vec![]; for expr in list { - let expr = evaluate_baseline_expr(&expr, ctx).await?; + let expr = evaluate_baseline_expr(&expr, ctx)?; exprs.push(expr); } @@ -165,7 +163,7 @@ pub async fn evaluate_baseline_expr( ) } Expression::Path(path) => { - let value = evaluate_baseline_expr(&path.head, ctx).await?; + let value = evaluate_baseline_expr(&path.head, ctx)?; let mut item = value; for member in &path.tail { @@ -271,10 +269,7 @@ fn evaluate_reference(name: &str, ctx: &EvaluationContext, tag: Tag) -> Result Result { +fn evaluate_invocation(block: &hir::Block, ctx: &EvaluationContext) -> Result { // FIXME: we should use a real context here let input = match ctx.scope.get_var("$it") { Some(it) => InputStream::one(it), @@ -284,9 +279,9 @@ async fn evaluate_invocation( let mut block = block.clone(); block.set_redirect(ExternalRedirection::Stdout); - let result = run_block(&block, ctx, input).await?; + let result = run_block(&block, ctx, input)?; - let output = result.into_vec().await; + let output = result.into_vec(); if let Some(e) = ctx.get_errors().get(0) { return Err(e.clone()); diff --git a/crates/nu-engine/src/evaluate/expr.rs b/crates/nu-engine/src/evaluate/expr.rs index 4b25437053..64ae7b5039 100644 --- a/crates/nu-engine/src/evaluate/expr.rs +++ b/crates/nu-engine/src/evaluate/expr.rs @@ -3,12 +3,11 @@ use crate::evaluate_baseline_expr; use log::{log_enabled, trace}; use crate::evaluation_context::EvaluationContext; -use futures::stream::once; use nu_errors::ShellError; use nu_protocol::hir::SpannedExpression; use nu_stream::{InputStream, ToInputStream}; -pub(crate) async fn run_expression_block( +pub(crate) fn run_expression_block( expr: &SpannedExpression, ctx: &EvaluationContext, ) -> Result { @@ -17,7 +16,7 @@ pub(crate) async fn run_expression_block( trace!(target: "nu::run::expr", "{:?}", expr); } - let output = evaluate_baseline_expr(expr, ctx).await?; + let output = evaluate_baseline_expr(expr, ctx)?; - Ok(once(async { Ok(output) }).to_input_stream()) + Ok(std::iter::once(Ok(output)).to_input_stream()) } diff --git a/crates/nu-engine/src/evaluate/internal.rs b/crates/nu-engine/src/evaluate/internal.rs index 97cfe67954..9dfd23adcc 100644 --- a/crates/nu-engine/src/evaluate/internal.rs +++ b/crates/nu-engine/src/evaluate/internal.rs @@ -3,16 +3,15 @@ use crate::command_args::RawCommandArgs; use crate::evaluation_context::EvaluationContext; use crate::filesystem::filesystem_shell::{FilesystemShell, FilesystemShellMode}; use crate::shell::value_shell::ValueShell; -use futures::StreamExt; use log::{log_enabled, trace}; use nu_errors::ShellError; use nu_protocol::hir::{ExternalRedirection, InternalCommand}; use nu_protocol::{CommandAction, ReturnSuccess, UntaggedValue, Value}; use nu_source::{PrettyDebug, Span, Tag}; -use nu_stream::{trace_stream, InputStream, ToInputStream}; +use nu_stream::{InputStream, ToInputStream}; use std::sync::Arc; -pub(crate) async fn run_internal_command( +pub(crate) fn run_internal_command( command: InternalCommand, context: &EvaluationContext, input: InputStream, @@ -22,19 +21,17 @@ pub(crate) async fn run_internal_command( trace!(target: "nu::run::internal", "{}", command.name); } - let objects: InputStream = trace_stream!(target: "nu::trace_stream::internal", "input" = input); + let objects: InputStream = input; let internal_command = context.scope.expect_command(&command.name); let result = { - context - .run_command( - internal_command?, - Tag::unknown_anchor(command.name_span), - command.args.clone(), - objects, - ) - .await? + context.run_command( + internal_command?, + Tag::unknown_anchor(command.name_span), + command.args.clone(), + objects, + )? }; let head = Arc::new(command.args.head.clone()); @@ -43,194 +40,188 @@ pub(crate) async fn run_internal_command( Ok(InputStream::from_stream( result - .then(move |item| { + .map(move |item| { let head = head.clone(); let command = command.clone(); let context = context.clone(); - async move { - match item { - Ok(ReturnSuccess::Action(action)) => match action { - CommandAction::ChangePath(path) => { - context.shell_manager.set_path(path); - InputStream::empty() - } - CommandAction::Exit(code) => std::process::exit(code), // TODO: save history.txt - CommandAction::Error(err) => { - context.error(err); - InputStream::empty() - } - CommandAction::AutoConvert(tagged_contents, extension) => { - let contents_tag = tagged_contents.tag.clone(); - let command_name = format!("from {}", extension); - let command = command.clone(); - if let Some(converter) = context.scope.get_command(&command_name) { - let new_args = RawCommandArgs { - host: context.host.clone(), - ctrl_c: context.ctrl_c.clone(), - configs: context.configs.clone(), - current_errors: context.current_errors.clone(), - shell_manager: context.shell_manager.clone(), - call_info: UnevaluatedCallInfo { - args: nu_protocol::hir::Call { - head: (&*head).clone(), - positional: None, - named: None, - span: Span::unknown(), - external_redirection: ExternalRedirection::Stdout, - }, - name_tag: Tag::unknown_anchor(command.name_span), + match item { + Ok(ReturnSuccess::Action(action)) => match action { + CommandAction::ChangePath(path) => { + context.shell_manager.set_path(path); + InputStream::empty() + } + CommandAction::Exit(code) => std::process::exit(code), // TODO: save history.txt + CommandAction::Error(err) => { + context.error(err); + InputStream::empty() + } + CommandAction::AutoConvert(tagged_contents, extension) => { + let contents_tag = tagged_contents.tag.clone(); + let command_name = format!("from {}", extension); + let command = command; + if let Some(converter) = context.scope.get_command(&command_name) { + let new_args = RawCommandArgs { + host: context.host.clone(), + ctrl_c: context.ctrl_c.clone(), + configs: context.configs.clone(), + current_errors: context.current_errors.clone(), + shell_manager: context.shell_manager.clone(), + call_info: UnevaluatedCallInfo { + args: nu_protocol::hir::Call { + head: (&*head).clone(), + positional: None, + named: None, + span: Span::unknown(), + external_redirection: ExternalRedirection::Stdout, }, - scope: context.scope.clone(), - }; - let result = converter - .run(new_args.with_input(vec![tagged_contents])) - .await; - - match result { - Ok(mut result) => { - let result_vec: Vec> = - result.drain_vec().await; - - let mut output = vec![]; - for res in result_vec { - match res { - Ok(ReturnSuccess::Value(Value { - value: UntaggedValue::Table(list), - .. - })) => { - for l in list { - output.push(Ok(l)); - } - } - Ok(ReturnSuccess::Value(Value { - value, - .. - })) => { - output - .push(Ok(value - .into_value(contents_tag.clone()))); - } - Err(e) => output.push(Err(e)), - _ => {} - } - } - - futures::stream::iter(output).to_input_stream() - } - Err(err) => { - context.error(err); - InputStream::empty() - } - } - } else { - InputStream::one(tagged_contents) - } - } - CommandAction::EnterValueShell(value) => { - context - .shell_manager - .insert_at_current(Box::new(ValueShell::new(value))); - InputStream::from_stream(futures::stream::iter(vec![])) - } - CommandAction::EnterShell(location) => { - let mode = if context.shell_manager.is_interactive() { - FilesystemShellMode::Cli - } else { - FilesystemShellMode::Script - }; - context.shell_manager.insert_at_current(Box::new( - match FilesystemShell::with_location(location, mode) { - Ok(v) => v, - Err(err) => { - context.error(err.into()); - return InputStream::empty(); - } + name_tag: Tag::unknown_anchor(command.name_span), }, - )); - InputStream::from_stream(futures::stream::iter(vec![])) - } - CommandAction::AddPlugins(path) => { - match crate::plugin::build_plugin::scan(vec![ - std::path::PathBuf::from(path), - ]) { - Ok(plugins) => { - context.add_commands( - plugins - .into_iter() - .filter(|p| { - !context.is_command_registered(p.name()) - }) - .collect(), - ); + scope: context.scope.clone(), + }; + let result = + converter.run(new_args.with_input(vec![tagged_contents])); - InputStream::empty() + match result { + Ok(mut result) => { + let result_vec: Vec> = + result.drain_vec(); + + let mut output = vec![]; + for res in result_vec { + match res { + Ok(ReturnSuccess::Value(Value { + value: UntaggedValue::Table(list), + .. + })) => { + for l in list { + output.push(Ok(l)); + } + } + Ok(ReturnSuccess::Value(Value { + value, .. + })) => { + output.push(Ok( + value.into_value(contents_tag.clone()) + )); + } + Err(e) => output.push(Err(e)), + _ => {} + } + } + + output.into_iter().to_input_stream() } - Err(reason) => { - context.error(reason); + Err(err) => { + context.error(err); InputStream::empty() } } + } else { + InputStream::one(tagged_contents) } - CommandAction::PreviousShell => { - context.shell_manager.prev(); - InputStream::empty() - } - CommandAction::NextShell => { - context.shell_manager.next(); - InputStream::empty() - } - CommandAction::LeaveShell(code) => { - context.shell_manager.remove_at_current(); - if context.shell_manager.is_empty() { - std::process::exit(code); // TODO: save history.txt + } + CommandAction::EnterValueShell(value) => { + context + .shell_manager + .insert_at_current(Box::new(ValueShell::new(value))); + InputStream::from_stream(std::iter::empty()) + } + CommandAction::EnterShell(location) => { + let mode = if context.shell_manager.is_interactive() { + FilesystemShellMode::Cli + } else { + FilesystemShellMode::Script + }; + context.shell_manager.insert_at_current(Box::new( + match FilesystemShell::with_location(location, mode) { + Ok(v) => v, + Err(err) => { + context.error(err.into()); + return InputStream::empty(); + } + }, + )); + InputStream::from_stream(std::iter::empty()) + } + CommandAction::AddPlugins(path) => { + match crate::plugin::build_plugin::scan(vec![std::path::PathBuf::from( + path, + )]) { + Ok(plugins) => { + context.add_commands( + plugins + .into_iter() + .filter(|p| !context.is_command_registered(p.name())) + .collect(), + ); + + InputStream::empty() } - InputStream::empty() - } - CommandAction::UnloadConfig(cfg_path) => { - context.unload_config(&cfg_path).await; - InputStream::empty() - } - CommandAction::LoadConfig(cfg_path) => { - if let Err(e) = context.load_config(&cfg_path).await { - InputStream::one(UntaggedValue::Error(e).into_untagged_value()) - } else { + Err(reason) => { + context.error(reason); InputStream::empty() } } - }, - - Ok(ReturnSuccess::Value(Value { - value: UntaggedValue::Error(err), - .. - })) => { - context.error(err); + } + CommandAction::PreviousShell => { + context.shell_manager.prev(); InputStream::empty() } - - Ok(ReturnSuccess::Value(v)) => InputStream::one(v), - - Ok(ReturnSuccess::DebugValue(v)) => { - let doc = PrettyDebug::pretty_doc(&v); - let mut buffer = termcolor::Buffer::ansi(); - - let _ = doc.render_raw( - context.with_host(|host| host.width() - 5), - &mut nu_source::TermColored::new(&mut buffer), - ); - - let value = String::from_utf8_lossy(buffer.as_slice()); - - InputStream::one(UntaggedValue::string(value).into_untagged_value()) - } - - Err(err) => { - context.error(err); + CommandAction::NextShell => { + context.shell_manager.next(); InputStream::empty() } + CommandAction::LeaveShell(code) => { + context.shell_manager.remove_at_current(); + if context.shell_manager.is_empty() { + std::process::exit(code); // TODO: save history.txt + } + InputStream::empty() + } + CommandAction::UnloadConfig(cfg_path) => { + context.unload_config(&cfg_path); + InputStream::empty() + } + CommandAction::LoadConfig(cfg_path) => { + if let Err(e) = context.load_config(&cfg_path) { + InputStream::one(UntaggedValue::Error(e).into_untagged_value()) + } else { + InputStream::empty() + } + } + }, + + Ok(ReturnSuccess::Value(Value { + value: UntaggedValue::Error(err), + .. + })) => { + context.error(err); + InputStream::empty() + } + + Ok(ReturnSuccess::Value(v)) => InputStream::one(v), + + Ok(ReturnSuccess::DebugValue(v)) => { + let doc = PrettyDebug::pretty_doc(&v); + let mut buffer = termcolor::Buffer::ansi(); + + let _ = doc.render_raw( + context.with_host(|host| host.width() - 5), + &mut nu_source::TermColored::new(&mut buffer), + ); + + let value = String::from_utf8_lossy(buffer.as_slice()); + + InputStream::one(UntaggedValue::string(value).into_untagged_value()) + } + + Err(err) => { + context.error(err); + InputStream::empty() } } }) .flatten() - .take_while(|x| futures::future::ready(!x.is_error())), + .take_while(|x| !x.is_error()), )) } diff --git a/crates/nu-engine/src/evaluation_context.rs b/crates/nu-engine/src/evaluation_context.rs index aa5ca9926c..bf33b8975a 100644 --- a/crates/nu-engine/src/evaluation_context.rs +++ b/crates/nu-engine/src/evaluation_context.rs @@ -87,7 +87,7 @@ impl EvaluationContext { self.scope.has_command(name) } - pub(crate) async fn run_command( + pub(crate) fn run_command( &self, command: Command, name_tag: Tag, @@ -95,7 +95,7 @@ impl EvaluationContext { input: InputStream, ) -> Result { let command_args = self.command_args(args, input, name_tag); - command.run(command_args).await + command.run(command_args) } fn call_info(&self, args: hir::Call, name_tag: Tag) -> UnevaluatedCallInfo { @@ -127,7 +127,7 @@ impl EvaluationContext { // The rational here is that, we should not partially load any config // that might be damaged. However, startup scripts might fail for various reasons. // A failure there is not as crucial as wrong config files. - pub async fn load_config(&self, cfg_path: &ConfigPath) -> Result<(), ShellError> { + pub fn load_config(&self, cfg_path: &ConfigPath) -> Result<(), ShellError> { trace!("Loading cfg {:?}", cfg_path); let cfg = NuConfig::load(Some(cfg_path.get_path().clone()))?; @@ -178,8 +178,7 @@ impl EvaluationContext { } if !startup_scripts.is_empty() { - self.run_scripts(startup_scripts, cfg_path.get_path().parent()) - .await; + self.run_scripts(startup_scripts, cfg_path.get_path().parent()); } Ok(()) @@ -189,7 +188,7 @@ impl EvaluationContext { /// If an error occurs while reloading the config: /// The config is not reloaded /// The error is returned - pub async fn reload_config(&self, cfg_path: &ConfigPath) -> Result<(), ShellError> { + pub fn reload_config(&self, cfg_path: &ConfigPath) -> Result<(), ShellError> { trace!("Reloading cfg {:?}", cfg_path); let mut configs = self.configs.lock(); @@ -263,15 +262,14 @@ impl EvaluationContext { /// If an error occurs while running exit scripts: /// The error is added to `self.current_errors` /// If no config with path of `cfg_path` is present, this method does nothing - pub async fn unload_config(&self, cfg_path: &ConfigPath) { + pub fn unload_config(&self, cfg_path: &ConfigPath) { trace!("UnLoading cfg {:?}", cfg_path); let tag = config::cfg_path_to_scope_tag(cfg_path); //Run exitscripts with scope frame and cfg still applied if let Some(scripts) = self.scope.get_exitscripts_of_frame_with_tag(&tag) { - self.run_scripts(scripts, cfg_path.get_path().parent()) - .await; + self.run_scripts(scripts, cfg_path.get_path().parent()); } //Unload config @@ -281,10 +279,10 @@ impl EvaluationContext { /// Runs scripts with cwd of dir. If dir is None, this method does nothing. /// Each error is added to `self.current_errors` - pub async fn run_scripts(&self, scripts: Vec, dir: Option<&Path>) { + pub fn run_scripts(&self, scripts: Vec, dir: Option<&Path>) { if let Some(dir) = dir { for script in scripts { - match script::run_script_in_dir(script.clone(), dir, &self).await { + match script::run_script_in_dir(script.clone(), dir, &self) { Ok(_) => {} Err(e) => { let err = ShellError::untagged_runtime_error(format!( diff --git a/crates/nu-engine/src/filesystem/filesystem_shell.rs b/crates/nu-engine/src/filesystem/filesystem_shell.rs index 64ace8cfe8..b7320826a9 100644 --- a/crates/nu-engine/src/filesystem/filesystem_shell.rs +++ b/crates/nu-engine/src/filesystem/filesystem_shell.rs @@ -1,25 +1,21 @@ -use crate::command_args::EvaluatedWholeStreamCommandArgs; use crate::filesystem::dir_info::{DirBuilder, DirInfo}; use crate::filesystem::path::canonicalize; use crate::filesystem::utils::FileStructure; use crate::maybe_text_codec::{MaybeTextCodec, StringOrBinary}; use crate::shell::shell_args::{CdArgs, CopyArgs, LsArgs, MkdirArgs, MvArgs, RemoveArgs}; use crate::shell::Shell; +use crate::{command_args::EvaluatedWholeStreamCommandArgs, BufCodecReader}; use encoding_rs::Encoding; -use futures::stream::BoxStream; -use futures::StreamExt; -use futures_codec::FramedRead; -use futures_util::TryStreamExt; use nu_data::config::LocalConfigDiff; use nu_protocol::{CommandAction, ConfigPath, TaggedDictBuilder, Value}; use nu_source::{Span, Tag}; use nu_stream::{Interruptible, OutputStream, ToOutputStream}; -use std::collections::HashMap; use std::collections::VecDeque; use std::io::{Error, ErrorKind}; use std::path::{Path, PathBuf}; use std::sync::atomic::AtomicBool; use std::sync::Arc; +use std::{collections::HashMap, io::BufReader}; #[cfg(unix)] use std::os::unix::fs::PermissionsExt; @@ -183,49 +179,50 @@ impl Shell for FilesystemShell { // Generated stream: impl Stream - Ok(futures::stream::iter(paths.filter_map(move |path| { - let path = match path.map_err(|e| ShellError::from(e.into_error())) { - Ok(path) => path, - Err(err) => return Some(Err(err)), - }; + Ok(paths + .filter_map(move |path| { + let path = match path.map_err(|e| ShellError::from(e.into_error())) { + Ok(path) => path, + Err(err) => return Some(Err(err)), + }; - if path_contains_hidden_folder(&path, &hidden_dirs) { - return None; - } - - if !all && !hidden_dir_specified && is_hidden_dir(&path) { - if path.is_dir() { - hidden_dirs.push(path); + if path_contains_hidden_folder(&path, &hidden_dirs) { + return None; } - return None; - } - let metadata = match std::fs::symlink_metadata(&path) { - Ok(metadata) => Some(metadata), - Err(e) => { - if e.kind() == ErrorKind::PermissionDenied || e.kind() == ErrorKind::Other { - None - } else { - return Some(Err(e.into())); + if !all && !hidden_dir_specified && is_hidden_dir(&path) { + if path.is_dir() { + hidden_dirs.push(path); } + return None; } - }; - let entry = dir_entry_dict( - &path, - metadata.as_ref(), - name_tag.clone(), - long, - short_names, - du, - ctrl_c.clone(), - ) - .map(ReturnSuccess::Value); + let metadata = match std::fs::symlink_metadata(&path) { + Ok(metadata) => Some(metadata), + Err(e) => { + if e.kind() == ErrorKind::PermissionDenied || e.kind() == ErrorKind::Other { + None + } else { + return Some(Err(e.into())); + } + } + }; - Some(entry) - })) - .interruptible(ctrl_c_copy) - .to_output_stream()) + let entry = dir_entry_dict( + &path, + metadata.as_ref(), + name_tag.clone(), + long, + short_names, + du, + ctrl_c.clone(), + ) + .map(ReturnSuccess::Value); + + Some(entry) + }) + .interruptible(ctrl_c_copy) + .to_output_stream()) } fn cd(&self, args: CdArgs, name: Tag) -> Result { @@ -665,8 +662,9 @@ impl Shell for FilesystemShell { )); } - Ok( - futures::stream::iter(all_targets.into_iter().map(move |(f, tag)| { + Ok(all_targets + .into_iter() + .map(move |(f, tag)| { let is_empty = || match f.read_dir() { Ok(mut p) => p.next().is_none(), Err(_) => false, @@ -741,9 +739,8 @@ impl Shell for FilesystemShell { tag, )) } - })) - .to_output_stream(), - ) + }) + .to_output_stream()) } fn path(&self) -> String { @@ -794,7 +791,10 @@ impl Shell for FilesystemShell { path: &Path, name: Span, with_encoding: Option<&'static Encoding>, - ) -> Result>, ShellError> { + ) -> Result< + Box> + Sync + Send>, + ShellError, + > { let metadata = std::fs::metadata(&path); let read_full = if let Ok(metadata) = metadata { @@ -805,8 +805,6 @@ impl Shell for FilesystemShell { }; if read_full { - use futures_codec::Decoder; - // We should, in theory, be able to read in the whole file as one chunk let buffer = std::fs::read(&path).map_err(|e| { ShellError::labeled_error( @@ -816,15 +814,15 @@ impl Shell for FilesystemShell { ) })?; - let mut bytes_mut = bytes::BytesMut::from(&buffer[..]); + let bytes_mut = bytes::BytesMut::from(&buffer[..]); let mut codec = MaybeTextCodec::new(with_encoding); - match codec.decode(&mut bytes_mut).map_err(|_| { + match codec.decode(&bytes_mut).map_err(|_| { ShellError::labeled_error("Error opening file", "error opening file", name) })? { - Some(sb) => Ok(futures::stream::iter(vec![Ok(sb)].into_iter()).boxed()), - None => Ok(futures::stream::iter(vec![].into_iter()).boxed()), + Some(sb) => Ok(Box::new(vec![Ok(sb)].into_iter())), + None => Ok(Box::new(vec![].into_iter())), } } else { // We don't know that this is a finite file, so treat it as a stream @@ -835,14 +833,10 @@ impl Shell for FilesystemShell { name, ) })?; - let async_reader = futures::io::AllowStdIo::new(f); - let sob_stream = FramedRead::new(async_reader, MaybeTextCodec::new(with_encoding)) - .map_err(move |_| { - ShellError::labeled_error("Error opening file", "error opening file", name) - }) - .into_stream(); + let buf_reader = BufReader::new(f); + let buf_codec = BufCodecReader::new(buf_reader, MaybeTextCodec::new(with_encoding)); - Ok(sob_stream.boxed()) + Ok(Box::new(buf_codec)) } } diff --git a/crates/nu-engine/src/lib.rs b/crates/nu-engine/src/lib.rs index 158a10b547..ef8cd33355 100644 --- a/crates/nu-engine/src/lib.rs +++ b/crates/nu-engine/src/lib.rs @@ -36,7 +36,7 @@ pub use crate::example::Example; pub use crate::filesystem::dir_info::{DirBuilder, DirInfo, FileInfo}; pub use crate::filesystem::filesystem_shell::FilesystemShell; pub use crate::filesystem::path; -pub use crate::maybe_text_codec::{MaybeTextCodec, StringOrBinary}; +pub use crate::maybe_text_codec::{BufCodecReader, MaybeTextCodec, StringOrBinary}; pub use crate::print::maybe_print_errors; pub use crate::runnable_context::RunnableContext; pub use crate::shell::painter::Painter; diff --git a/crates/nu-engine/src/maybe_text_codec.rs b/crates/nu-engine/src/maybe_text_codec.rs index 3cffef2119..0d2c7ed934 100644 --- a/crates/nu-engine/src/maybe_text_codec.rs +++ b/crates/nu-engine/src/maybe_text_codec.rs @@ -1,4 +1,4 @@ -use bytes::{BufMut, Bytes, BytesMut}; +use std::io::{BufRead, BufReader, Read}; use nu_errors::ShellError; @@ -19,6 +19,39 @@ pub struct MaybeTextCodec { decoder: Decoder, } +pub struct BufCodecReader { + maybe_text_codec: MaybeTextCodec, + input: BufReader, +} + +impl BufCodecReader { + pub fn new(input: BufReader, maybe_text_codec: MaybeTextCodec) -> Self { + BufCodecReader { + input, + maybe_text_codec, + } + } +} + +impl Iterator for BufCodecReader { + type Item = Result; + + fn next(&mut self) -> Option { + let buffer = self.input.fill_buf(); + match buffer { + Ok(s) => { + let result = self.maybe_text_codec.decode(&s).transpose(); + + let buffer_len = s.len(); + self.input.consume(buffer_len); + + result + } + Err(e) => Some(Err(ShellError::untagged_runtime_error(e.to_string()))), + } + } +} + impl MaybeTextCodec { // The constructor takes an Option<&'static Encoding>, because an absence of an encoding indicates that we want BOM sniffing enabled pub fn new(encoding: Option<&'static Encoding>) -> Self { @@ -38,31 +71,21 @@ impl Default for MaybeTextCodec { } } -impl futures_codec::Encoder for MaybeTextCodec { - type Item = StringOrBinary; - type Error = std::io::Error; +// impl MaybeTextCodec { +// fn encode(&mut self, item: StringOrBinary, mut dst: &mut [u8]) { +// match item { +// StringOrBinary::String(s) => { +// dst.put(s.as_bytes()); +// } +// StringOrBinary::Binary(b) => { +// dst.put(Bytes::from(b)); +// } +// } +// } +// } - fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> { - match item { - StringOrBinary::String(s) => { - dst.reserve(s.len()); - dst.put(s.as_bytes()); - Ok(()) - } - StringOrBinary::Binary(b) => { - dst.reserve(b.len()); - dst.put(Bytes::from(b)); - Ok(()) - } - } - } -} - -impl futures_codec::Decoder for MaybeTextCodec { - type Item = StringOrBinary; - type Error = ShellError; - - fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { +impl MaybeTextCodec { + pub fn decode(&mut self, src: &[u8]) -> Result, ShellError> { if src.is_empty() { return Ok(None); } @@ -96,31 +119,8 @@ impl futures_codec::Decoder for MaybeTextCodec { StringOrBinary::String(s) }; - src.clear(); + // src.clear(); Ok(Some(result)) } } - -#[cfg(test)] -mod tests { - use super::{MaybeTextCodec, StringOrBinary}; - use bytes::BytesMut; - use futures_codec::Decoder; - - // TODO: Write some more tests - - #[test] - fn should_consume_all_bytes_from_source_when_temporary_buffer_overflows() { - let mut maybe_text = MaybeTextCodec::new(None); - let mut bytes = BytesMut::from("0123456789"); - - let text = maybe_text.decode(&mut bytes); - - assert_eq!( - Ok(Some(StringOrBinary::String("0123456789".to_string()))), - text - ); - assert!(bytes.is_empty()); - } -} diff --git a/crates/nu-engine/src/plugin/run_plugin.rs b/crates/nu-engine/src/plugin/run_plugin.rs index d31f8d5d20..2ebbf38741 100644 --- a/crates/nu-engine/src/plugin/run_plugin.rs +++ b/crates/nu-engine/src/plugin/run_plugin.rs @@ -1,8 +1,7 @@ use crate::command_args::CommandArgs; use crate::whole_stream_command::{whole_stream_command, WholeStreamCommand}; -use async_trait::async_trait; use derive_new::new; -use futures::StreamExt; + use log::trace; use nu_errors::ShellError; use nu_plugin::jsonrpc::JsonRpc; @@ -94,7 +93,6 @@ pub struct PluginFilter { config: Signature, } -#[async_trait] impl WholeStreamCommand for PluginFilter { fn name(&self) -> &str { &self.name @@ -108,22 +106,20 @@ impl WholeStreamCommand for PluginFilter { &self.config.usage } - async fn run(&self, args: CommandArgs) -> Result { - run_filter(self.path.clone(), args).await + fn run(&self, args: CommandArgs) -> Result { + run_filter(self.path.clone(), args) } } -async fn run_filter(path: String, args: CommandArgs) -> Result { +fn run_filter(path: String, args: CommandArgs) -> Result { trace!("filter_plugin :: {}", path); - let bos = futures::stream::iter(vec![ - UntaggedValue::Primitive(Primitive::BeginningOfStream).into_untagged_value() - ]); - let eos = futures::stream::iter(vec![ - UntaggedValue::Primitive(Primitive::EndOfStream).into_untagged_value() - ]); + let bos = vec![UntaggedValue::Primitive(Primitive::BeginningOfStream).into_untagged_value()] + .into_iter(); + let eos = + vec![UntaggedValue::Primitive(Primitive::EndOfStream).into_untagged_value()].into_iter(); - let args = args.evaluate_once().await?; + let args = args.evaluate_once()?; let real_path = Path::new(&path); let ext = real_path.extension(); @@ -205,9 +201,10 @@ async fn run_filter(path: String, args: CommandArgs) -> Result match params { - Ok(params) => futures::stream::iter(params).to_output_stream(), - Err(e) => futures::stream::iter(vec![ReturnValue::Err(e)]) - .to_output_stream(), + Ok(params) => params.into_iter().to_output_stream(), + Err(e) => { + vec![ReturnValue::Err(e)].into_iter().to_output_stream() + } }, Err(e) => OutputStream::one(Err( @@ -265,25 +262,25 @@ async fn run_filter(path: String, args: CommandArgs) -> Result match params { - Ok(params) => futures::stream::iter(params).to_output_stream(), - Err(e) => futures::stream::iter(vec![ReturnValue::Err(e)]) - .to_output_stream(), + Ok(params) => params.into_iter().to_output_stream(), + Err(e) => { + vec![ReturnValue::Err(e)].into_iter().to_output_stream() + } }, - Err(e) => futures::stream::iter(vec![Err( - ShellError::untagged_runtime_error(format!( - "Error while processing end_filter response: {:?} {}", - e, input - )), - )]) + Err(e) => vec![Err(ShellError::untagged_runtime_error(format!( + "Error while processing end_filter response: {:?} {}", + e, input + )))] + .into_iter() .to_output_stream(), } } - Err(e) => { - futures::stream::iter(vec![Err(ShellError::untagged_runtime_error( - format!("Error while reading end_filter response: {:?}", e), - ))]) - .to_output_stream() - } + Err(e) => vec![Err(ShellError::untagged_runtime_error(format!( + "Error while reading end_filter response: {:?}", + e + )))] + .into_iter() + .to_output_stream(), }; let stdin = child.stdin.as_mut().expect("Failed to open stdin"); @@ -339,9 +336,10 @@ async fn run_filter(path: String, args: CommandArgs) -> Result match params { - Ok(params) => futures::stream::iter(params).to_output_stream(), - Err(e) => futures::stream::iter(vec![ReturnValue::Err(e)]) - .to_output_stream(), + Ok(params) => params.into_iter().to_output_stream(), + Err(e) => { + vec![ReturnValue::Err(e)].into_iter().to_output_stream() + } }, Err(e) => OutputStream::one(Err( ShellError::untagged_runtime_error(format!( @@ -369,7 +367,6 @@ pub struct PluginSink { config: Signature, } -#[async_trait] impl WholeStreamCommand for PluginSink { fn name(&self) -> &str { &self.name @@ -383,18 +380,18 @@ impl WholeStreamCommand for PluginSink { &self.config.usage } - async fn run(&self, args: CommandArgs) -> Result { - run_sink(self.path.clone(), args).await + fn run(&self, args: CommandArgs) -> Result { + run_sink(self.path.clone(), args) } } -async fn run_sink(path: String, args: CommandArgs) -> Result { - let args = args.evaluate_once().await?; +fn run_sink(path: String, args: CommandArgs) -> Result { + let args = args.evaluate_once()?; let call_info = args.call_info.clone(); - let input: Vec = args.input.collect().await; + let input: Vec = args.input.into_vec(); - let request = JsonRpc::new("sink", (call_info.clone(), input)); + let request = JsonRpc::new("sink", (call_info, input)); let request_raw = serde_json::to_string(&request); if let Ok(request_raw) = request_raw { if let Ok(mut tmpfile) = tempfile::NamedTempFile::new() { diff --git a/crates/nu-engine/src/script.rs b/crates/nu-engine/src/script.rs index 29ad593302..7999c59b22 100644 --- a/crates/nu-engine/src/script.rs +++ b/crates/nu-engine/src/script.rs @@ -1,7 +1,5 @@ use crate::{maybe_print_errors, path::canonicalize, run_block}; -use crate::{MaybeTextCodec, StringOrBinary}; -use futures::StreamExt; -use futures_codec::FramedRead; +use crate::{BufCodecReader, MaybeTextCodec, StringOrBinary}; use nu_errors::ShellError; use nu_protocol::hir::{ Call, ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments, @@ -13,9 +11,9 @@ use nu_stream::{InputStream, ToInputStream}; use crate::EvaluationContext; use log::{debug, trace}; use nu_source::{Span, Tag, Text}; -use std::iter::Iterator; use std::path::Path; use std::{error::Error, sync::atomic::Ordering}; +use std::{io::BufReader, iter::Iterator}; #[derive(Debug)] pub enum LineResult { @@ -35,7 +33,7 @@ fn chomp_newline(s: &str) -> &str { } } -pub async fn run_script_in_dir( +pub fn run_script_in_dir( script: String, dir: &Path, ctx: &EvaluationContext, @@ -45,14 +43,14 @@ pub async fn run_script_in_dir( ctx.shell_manager .set_path(dir.to_string_lossy().to_string()); - run_script_standalone(script, false, ctx, false).await?; + run_script_standalone(script, false, ctx, false)?; ctx.shell_manager.set_path(path_before); Ok(()) } /// Process the line by parsing the text to turn it into commands, classify those commands so that we understand what is being called in the pipeline, and then run this pipeline -pub async fn process_script( +pub fn process_script( script_text: &str, ctx: &EvaluationContext, redirect_stdin: bool, @@ -161,8 +159,10 @@ pub async fn process_script( } let input_stream = if redirect_stdin { - let file = futures::io::AllowStdIo::new(std::io::stdin()); - let stream = FramedRead::new(file, MaybeTextCodec::default()).map(|line| { + let file = std::io::stdin(); + let buf_reader = BufReader::new(file); + let buf_codec = BufCodecReader::new(buf_reader, MaybeTextCodec::default()); + let stream = buf_codec.map(|line| { if let Ok(line) = line { let primitive = match line { StringOrBinary::String(s) => Primitive::String(s), @@ -184,47 +184,43 @@ pub async fn process_script( trace!("{:#?}", block); - let result = run_block(&block, ctx, input_stream).await; + let result = run_block(&block, ctx, input_stream); match result { Ok(input) => { // Running a pipeline gives us back a stream that we can then // work through. At the top level, we just want to pull on the // values to compute them. - use futures::stream::TryStreamExt; let autoview_cmd = ctx .get_command("autoview") .expect("Could not find autoview command"); - if let Ok(mut output_stream) = ctx - .run_command( - autoview_cmd, - Tag::unknown(), - Call::new( - Box::new(SpannedExpression::new( - Expression::string("autoview".to_string()), - Span::unknown(), - )), + if let Ok(mut output_stream) = ctx.run_command( + autoview_cmd, + Tag::unknown(), + Call::new( + Box::new(SpannedExpression::new( + Expression::string("autoview".to_string()), Span::unknown(), - ), - input, - ) - .await - { + )), + Span::unknown(), + ), + input, + ) { loop { - match output_stream.try_next().await { - Ok(Some(ReturnSuccess::Value(Value { + match output_stream.next() { + Some(Ok(ReturnSuccess::Value(Value { value: UntaggedValue::Error(e), .. }))) => return LineResult::Error(line.to_string(), e), - Ok(Some(_item)) => { + Some(Ok(_item)) => { if ctx.ctrl_c.load(Ordering::SeqCst) { break; } } - Ok(None) => break, - Err(e) => return LineResult::Error(line.to_string(), e), + None => break, + Some(Err(e)) => return LineResult::Error(line.to_string(), e), } } } @@ -236,7 +232,7 @@ pub async fn process_script( } } -pub async fn run_script_standalone( +pub fn run_script_standalone( script_text: String, redirect_stdin: bool, context: &EvaluationContext, @@ -246,7 +242,7 @@ pub async fn run_script_standalone( .shell_manager .enter_script_mode() .map_err(Box::new)?; - let line = process_script(&script_text, context, redirect_stdin, 0, false).await; + let line = process_script(&script_text, context, redirect_stdin, 0, false); match line { LineResult::Success(line) => { diff --git a/crates/nu-engine/src/shell/mod.rs b/crates/nu-engine/src/shell/mod.rs index 9159619c6e..fb2e631d0f 100644 --- a/crates/nu-engine/src/shell/mod.rs +++ b/crates/nu-engine/src/shell/mod.rs @@ -4,7 +4,6 @@ use crate::command_args::EvaluatedWholeStreamCommandArgs; use crate::maybe_text_codec::StringOrBinary; pub use crate::shell::shell_args::{CdArgs, CopyArgs, LsArgs, MkdirArgs, MvArgs, RemoveArgs}; use encoding_rs::Encoding; -use futures::stream::BoxStream; use nu_errors::ShellError; use nu_source::{Span, Tag}; use std::path::{Path, PathBuf}; @@ -41,7 +40,10 @@ pub trait Shell: std::fmt::Debug { path: &Path, name: Span, with_encoding: Option<&'static Encoding>, - ) -> Result>, ShellError>; + ) -> Result< + Box> + Send + Sync>, + ShellError, + >; fn save( &mut self, path: &Path, diff --git a/crates/nu-engine/src/shell/shell_manager.rs b/crates/nu-engine/src/shell/shell_manager.rs index af3e16418b..a408a377df 100644 --- a/crates/nu-engine/src/shell/shell_manager.rs +++ b/crates/nu-engine/src/shell/shell_manager.rs @@ -1,7 +1,6 @@ use crate::shell::Shell; use crate::{command_args::EvaluatedWholeStreamCommandArgs, FilesystemShell}; use crate::{filesystem::filesystem_shell::FilesystemShellMode, maybe_text_codec::StringOrBinary}; -use futures::Stream; use nu_stream::OutputStream; use crate::shell::shell_args::{CdArgs, CopyArgs, LsArgs, MkdirArgs, MvArgs, RemoveArgs}; @@ -85,8 +84,10 @@ impl ShellManager { full_path: &Path, name: Span, with_encoding: Option<&'static Encoding>, - ) -> Result> + Send + 'static, ShellError> - { + ) -> Result< + Box> + Send + Sync>, + ShellError, + > { self.shells.lock()[self.current_shell()].open(full_path, name, with_encoding) } diff --git a/crates/nu-engine/src/shell/value_shell.rs b/crates/nu-engine/src/shell/value_shell.rs index 85fd849f41..9f24fc3eae 100644 --- a/crates/nu-engine/src/shell/value_shell.rs +++ b/crates/nu-engine/src/shell/value_shell.rs @@ -3,7 +3,6 @@ use crate::maybe_text_codec::StringOrBinary; use crate::shell::shell_args::{CdArgs, CopyArgs, LsArgs, MkdirArgs, MvArgs, RemoveArgs}; use crate::shell::Shell; use encoding_rs::Encoding; -use futures::stream::BoxStream; use nu_errors::ShellError; use nu_protocol::ValueStructure; use nu_protocol::{ReturnSuccess, ShellTypeName, UntaggedValue, Value}; @@ -238,7 +237,10 @@ impl Shell for ValueShell { _path: &Path, _name: Span, _with_encoding: Option<&'static Encoding>, - ) -> Result>, ShellError> { + ) -> Result< + Box> + Send + Sync>, + ShellError, + > { Err(ShellError::unimplemented( "open on help shell is not supported", )) diff --git a/crates/nu-engine/src/whole_stream_command.rs b/crates/nu-engine/src/whole_stream_command.rs index e05e9185d4..189027b86b 100644 --- a/crates/nu-engine/src/whole_stream_command.rs +++ b/crates/nu-engine/src/whole_stream_command.rs @@ -3,7 +3,6 @@ use crate::documentation::get_full_help; use crate::evaluate::block::run_block; use crate::evaluation_context::EvaluationContext; use crate::example::Example; -use async_trait::async_trait; use nu_errors::ShellError; use nu_parser::ParserScope; use nu_protocol::hir::Block; @@ -12,7 +11,6 @@ use nu_source::{DbgDocBldr, DebugDocBuilder, PrettyDebugWithSource, Span, Tag}; use nu_stream::{OutputStream, ToOutputStream}; use std::sync::Arc; -#[async_trait] pub trait WholeStreamCommand: Send + Sync { fn name(&self) -> &str; @@ -26,7 +24,7 @@ pub trait WholeStreamCommand: Send + Sync { "" } - async fn run(&self, args: CommandArgs) -> Result; + fn run(&self, args: CommandArgs) -> Result; fn is_binary(&self) -> bool { false @@ -45,7 +43,7 @@ pub trait WholeStreamCommand: Send + Sync { // Custom commands are blocks, so we can use the information in the block to also // implement a WholeStreamCommand #[allow(clippy::suspicious_else_formatting)] -#[async_trait] + impl WholeStreamCommand for Block { fn name(&self) -> &str { &self.params.name @@ -59,14 +57,14 @@ impl WholeStreamCommand for Block { &self.params.usage } - async fn run(&self, args: CommandArgs) -> Result { + fn run(&self, args: CommandArgs) -> Result { let call_info = args.call_info.clone(); let mut block = self.clone(); block.set_redirect(call_info.args.external_redirection); let ctx = EvaluationContext::from_args(&args); - let evaluated = call_info.evaluate(&ctx).await?; + let evaluated = call_info.evaluate(&ctx)?; let input = args.input; ctx.scope.enter_scope(); @@ -153,7 +151,7 @@ impl WholeStreamCommand for Block { } } } - let result = run_block(&block, &ctx, input).await; + let result = run_block(&block, &ctx, input); ctx.scope.exit_scope(); result.map(|x| x.to_output_stream()) } @@ -210,14 +208,14 @@ impl Command { self.0.examples() } - pub async fn run(&self, args: CommandArgs) -> Result { + pub fn run(&self, args: CommandArgs) -> Result { if args.call_info.switch_present("help") { let cl = self.0.clone(); Ok(OutputStream::one(Ok(ReturnSuccess::Value( UntaggedValue::string(get_full_help(&*cl, &args.scope)).into_value(Tag::unknown()), )))) } else { - self.0.run(args).await + self.0.run(args) } } diff --git a/crates/nu-stream/src/input.rs b/crates/nu-stream/src/input.rs index 72eff8fe57..9207eb1410 100644 --- a/crates/nu-stream/src/input.rs +++ b/crates/nu-stream/src/input.rs @@ -1,20 +1,27 @@ use crate::prelude::*; -use futures::stream::iter; use nu_errors::ShellError; use nu_protocol::{Primitive, Type, UntaggedValue, Value}; use nu_source::{HasFallibleSpan, PrettyDebug, Tag, Tagged, TaggedItem}; pub struct InputStream { - values: BoxStream<'static, Value>, + values: Box + Send + Sync>, // Whether or not an empty stream was explicitly requested via InputStream::empty empty: bool, } +impl Iterator for InputStream { + type Item = Value; + + fn next(&mut self) -> Option { + self.values.next() + } +} + impl InputStream { pub fn empty() -> InputStream { InputStream { - values: futures::stream::empty().boxed(), + values: Box::new(std::iter::empty()), empty: true, } } @@ -25,7 +32,7 @@ impl InputStream { v.into() } - pub fn into_vec(self) -> impl Future> { + pub fn into_vec(self) -> Vec { self.values.collect() } @@ -33,26 +40,27 @@ impl InputStream { self.empty } - pub fn drain_vec(&mut self) -> impl Future> { - let mut values: BoxStream<'static, Value> = iter(VecDeque::new()).boxed(); - std::mem::swap(&mut values, &mut self.values); - - values.collect() + pub fn drain_vec(&mut self) -> Vec { + let mut output = vec![]; + while let Some(x) = self.values.next() { + output.push(x); + } + output } - pub fn from_stream(input: impl Stream + Send + 'static) -> InputStream { + pub fn from_stream(input: impl Iterator + Send + Sync + 'static) -> InputStream { InputStream { - values: input.boxed(), + values: Box::new(input), empty: false, } } - pub async fn collect_string(mut self, tag: Tag) -> Result, ShellError> { + pub fn collect_string(mut self, tag: Tag) -> Result, ShellError> { let mut bytes = vec![]; let mut value_tag = tag.clone(); loop { - match self.values.next().await { + match self.values.next() { Some(Value { value: UntaggedValue::Primitive(Primitive::String(s)), tag: value_t, @@ -93,7 +101,7 @@ impl InputStream { } match String::from_utf8(bytes) { - Ok(s) => Ok(s.tagged(value_tag.clone())), + Ok(s) => Ok(s.tagged(value_tag)), Err(_) => Err(ShellError::labeled_error_with_secondary( "Expected a string from pipeline", "requires string input", @@ -104,12 +112,12 @@ impl InputStream { } } - pub async fn collect_binary(mut self, tag: Tag) -> Result>, ShellError> { + pub fn collect_binary(mut self, tag: Tag) -> Result>, ShellError> { let mut bytes = vec![]; let mut value_tag = tag.clone(); loop { - match self.values.next().await { + match self.values.next() { Some(Value { value: UntaggedValue::Primitive(Primitive::Binary(b)), tag: value_t, @@ -137,30 +145,10 @@ impl InputStream { } } -impl Stream for InputStream { - type Item = Value; - - fn poll_next( - mut self: std::pin::Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - ) -> core::task::Poll> { - Stream::poll_next(std::pin::Pin::new(&mut self.values), cx) - } -} - -impl From> for InputStream { - fn from(input: BoxStream<'static, Value>) -> InputStream { - InputStream { - values: input, - empty: false, - } - } -} - impl From> for InputStream { fn from(input: VecDeque) -> InputStream { InputStream { - values: futures::stream::iter(input).boxed(), + values: Box::new(input.into_iter()), empty: false, } } @@ -169,7 +157,7 @@ impl From> for InputStream { impl From> for InputStream { fn from(input: Vec) -> InputStream { InputStream { - values: futures::stream::iter(input).boxed(), + values: Box::new(input.into_iter()), empty: false, } } @@ -182,16 +170,19 @@ pub trait ToInputStream { impl ToInputStream for T where - T: Stream + Send + 'static, + T: Iterator + Send + Sync + 'static, U: Into>, { fn to_input_stream(self) -> InputStream { - InputStream::from_stream(self.map(|item| match item.into() { - Ok(result) => result, - Err(err) => match HasFallibleSpan::maybe_span(&err) { - Some(span) => nu_protocol::UntaggedValue::Error(err).into_value(span), - None => nu_protocol::UntaggedValue::Error(err).into_untagged_value(), - }, - })) + InputStream { + empty: false, + values: Box::new(self.map(|item| match item.into() { + Ok(result) => result, + Err(err) => match HasFallibleSpan::maybe_span(&err) { + Some(span) => nu_protocol::UntaggedValue::Error(err).into_value(span), + None => nu_protocol::UntaggedValue::Error(err).into_untagged_value(), + }, + })), + } } } diff --git a/crates/nu-stream/src/interruptible.rs b/crates/nu-stream/src/interruptible.rs index a16dd6224d..43b8d6bbe4 100644 --- a/crates/nu-stream/src/interruptible.rs +++ b/crates/nu-stream/src/interruptible.rs @@ -1,35 +1,31 @@ use crate::prelude::*; -use futures::task::Poll; use std::sync::atomic::{AtomicBool, Ordering}; pub struct InterruptibleStream { - inner: BoxStream<'static, V>, + inner: Box + Send + Sync>, interrupt_signal: Arc, } impl InterruptibleStream { pub fn new(inner: S, interrupt_signal: Arc) -> InterruptibleStream where - S: Stream + Send + 'static, + S: Iterator + Send + Sync + 'static, { InterruptibleStream { - inner: inner.boxed(), + inner: Box::new(inner), interrupt_signal, } } } -impl Stream for InterruptibleStream { +impl Iterator for InterruptibleStream { type Item = V; - fn poll_next( - mut self: std::pin::Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - ) -> core::task::Poll> { + fn next(&mut self) -> Option { if self.interrupt_signal.load(Ordering::SeqCst) { - Poll::Ready(None) + None } else { - Stream::poll_next(std::pin::Pin::new(&mut self.inner), cx) + self.inner.next() } } } @@ -40,7 +36,7 @@ pub trait Interruptible { impl Interruptible for S where - S: Stream + Send + 'static, + S: Iterator + Send + Sync + 'static, { fn interruptible(self, ctrl_c: Arc) -> InterruptibleStream { InterruptibleStream::new(self, ctrl_c) diff --git a/crates/nu-stream/src/output.rs b/crates/nu-stream/src/output.rs index e4fd0e394d..f934109472 100644 --- a/crates/nu-stream/src/output.rs +++ b/crates/nu-stream/src/output.rs @@ -1,16 +1,23 @@ use crate::prelude::*; -use futures::stream::iter; use nu_protocol::{ReturnSuccess, ReturnValue, Value}; use std::iter::IntoIterator; pub struct OutputStream { - pub values: BoxStream<'static, ReturnValue>, + pub values: Box + Send + Sync>, +} + +impl Iterator for OutputStream { + type Item = ReturnValue; + + fn next(&mut self) -> Option { + self.values.next() + } } impl OutputStream { - pub fn new(values: impl Stream + Send + 'static) -> OutputStream { + pub fn new(values: impl Iterator + Send + Sync + 'static) -> OutputStream { OutputStream { - values: values.boxed(), + values: Box::new(values), } } @@ -21,60 +28,52 @@ impl OutputStream { pub fn one(item: impl Into) -> OutputStream { let item = item.into(); - futures::stream::once(async move { item }).to_output_stream() - } - - pub fn from_input(input: impl Stream + Send + 'static) -> OutputStream { OutputStream { - values: input.map(ReturnSuccess::value).boxed(), + values: Box::new(std::iter::once(item)), } } - pub fn drain_vec(&mut self) -> impl Future> { - let mut values: BoxStream<'static, ReturnValue> = iter(VecDeque::new()).boxed(); - std::mem::swap(&mut values, &mut self.values); - - values.collect() + pub fn from_input(input: impl Iterator + Send + Sync + 'static) -> OutputStream { + OutputStream { + values: Box::new(input.map(ReturnSuccess::value)), + } } -} -impl Stream for OutputStream { - type Item = ReturnValue; - - fn poll_next( - mut self: std::pin::Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - ) -> core::task::Poll> { - Stream::poll_next(std::pin::Pin::new(&mut self.values), cx) + pub fn drain_vec(&mut self) -> Vec { + let mut output = vec![]; + while let Some(x) = self.values.next() { + output.push(x); + } + output } } impl From for OutputStream { fn from(input: InputStream) -> OutputStream { OutputStream { - values: input.map(ReturnSuccess::value).boxed(), + values: Box::new(input.into_iter().map(ReturnSuccess::value)), } } } -impl From> for OutputStream { - fn from(input: BoxStream<'static, Value>) -> OutputStream { - OutputStream { - values: input.map(ReturnSuccess::value).boxed(), - } - } -} +// impl From + Send + Sync + 'static> for OutputStream { +// fn from(input: impl Iterator + Send + Sync + 'static) -> OutputStream { +// OutputStream { +// values: Box::new(input.map(ReturnSuccess::value)), +// } +// } +// } -impl From> for OutputStream { - fn from(input: BoxStream<'static, ReturnValue>) -> OutputStream { - OutputStream { values: input } - } -} +// impl From> for OutputStream { +// fn from(input: BoxStream<'static, ReturnValue>) -> OutputStream { +// OutputStream { values: input } +// } +// } impl From> for OutputStream { fn from(input: VecDeque) -> OutputStream { OutputStream { - values: futures::stream::iter(input).boxed(), + values: Box::new(input.into_iter()), } } } @@ -83,7 +82,7 @@ impl From> for OutputStream { fn from(input: VecDeque) -> OutputStream { let stream = input.into_iter().map(ReturnSuccess::value); OutputStream { - values: futures::stream::iter(stream).boxed(), + values: Box::new(stream), } } } @@ -91,7 +90,7 @@ impl From> for OutputStream { impl From> for OutputStream { fn from(input: Vec) -> OutputStream { OutputStream { - values: futures::stream::iter(input).boxed(), + values: Box::new(input.into_iter()), } } } @@ -100,7 +99,7 @@ impl From> for OutputStream { fn from(input: Vec) -> OutputStream { let stream = input.into_iter().map(ReturnSuccess::value); OutputStream { - values: futures::stream::iter(stream).boxed(), + values: Box::new(stream), } } } diff --git a/crates/nu-stream/src/prelude.rs b/crates/nu-stream/src/prelude.rs index f0bef4630a..57c9cc9f02 100644 --- a/crates/nu-stream/src/prelude.rs +++ b/crates/nu-stream/src/prelude.rs @@ -21,34 +21,10 @@ macro_rules! stream { }} } -#[macro_export] -macro_rules! trace_stream { - (target: $target:tt, $desc:tt = $expr:expr) => {{ - if log::log_enabled!(target: $target, log::Level::Trace) { - use futures::stream::StreamExt; - - let objects = $expr.inspect(move |o| { - trace!( - target: $target, - "{} = {}", - $desc, - nu_source::PrettyDebug::plain_string(o, 70) - ); - }); - - nu_stream::InputStream::from_stream(objects.boxed()) - } else { - $expr - } - }}; -} - #[macro_export] macro_rules! trace_out_stream { (target: $target:tt, $desc:tt = $expr:expr) => {{ if log::log_enabled!(target: $target, log::Level::Trace) { - use futures::stream::StreamExt; - let objects = $expr.inspect(move |o| { trace!( target: $target, @@ -68,10 +44,7 @@ macro_rules! trace_out_stream { }}; } -pub(crate) use futures::stream::BoxStream; -pub(crate) use futures::{Stream, StreamExt}; pub(crate) use std::collections::VecDeque; -pub(crate) use std::future::Future; pub(crate) use std::sync::Arc; pub(crate) use crate::{InputStream, OutputStream}; @@ -83,12 +56,12 @@ pub trait ToOutputStream { impl ToOutputStream for T where - T: Stream + Send + 'static, + T: Iterator + Send + Sync + 'static, U: Into, { fn to_output_stream(self) -> OutputStream { OutputStream { - values: self.map(|item| item.into()).boxed(), + values: Box::new(self.map(|item| item.into())), } } } diff --git a/src/main.rs b/src/main.rs index 3f0f8a51fb..737a04acd9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -150,7 +150,7 @@ fn main() -> Result<(), Box> { Some(values) => { options.scripts = vec![NuScript::code(values)?]; - futures::executor::block_on(nu_cli::run_script_file(options))?; + nu_cli::run_script_file(options)?; return Ok(()); } } @@ -161,7 +161,7 @@ fn main() -> Result<(), Box> { options.scripts = vec![NuScript::source_file(filepath.as_os_str())?]; - futures::executor::block_on(nu_cli::run_script_file(options))?; + nu_cli::run_script_file(options)?; return Ok(()); } @@ -174,7 +174,7 @@ fn main() -> Result<(), Box> { #[cfg(feature = "rustyline-support")] { - futures::executor::block_on(nu_cli::cli(context, options))?; + nu_cli::cli(context, options)?; } #[cfg(not(feature = "rustyline-support"))] diff --git a/tests/shell/environment/nu_env.rs b/tests/shell/environment/nu_env.rs index 0001ed0a4f..5df472c29a 100644 --- a/tests/shell/environment/nu_env.rs +++ b/tests/shell/environment/nu_env.rs @@ -533,7 +533,7 @@ fn autoenv_test_entry_scripts() { cd bar ls .. | where name == "../hello.txt" | length"# ); - assert!(actual.out.contains("0")); + assert!(actual.out.contains('0')); }); }