From ea22c319b6a98fefc310c26495c8264989448820 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 30 Jul 2024 08:53:41 -0500 Subject: [PATCH] make `math sqrt` const (#13487) # Description Just a quick PR to demonstrate one way to make commands const. related to https://github.com/nushell/nushell/issues/13482 # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-command/src/math/sqrt.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/nu-command/src/math/sqrt.rs b/crates/nu-command/src/math/sqrt.rs index a3ab5fbbd8..d20978222c 100644 --- a/crates/nu-command/src/math/sqrt.rs +++ b/crates/nu-command/src/math/sqrt.rs @@ -29,6 +29,10 @@ impl Command for SubCommand { vec!["square", "root"] } + fn is_const(&self) -> bool { + true + } + fn run( &self, engine_state: &EngineState, @@ -44,6 +48,23 @@ impl Command for SubCommand { input.map(move |value| operate(value, head), engine_state.signals()) } + fn run_const( + &self, + working_set: &StateWorkingSet, + call: &Call, + input: PipelineData, + ) -> Result { + let head = call.head; + // This doesn't match explicit nulls + if matches!(input, PipelineData::Empty) { + return Err(ShellError::PipelineEmpty { dst_span: head }); + } + input.map( + move |value| operate(value, head), + working_set.permanent().signals(), + ) + } + fn examples(&self) -> Vec { vec![Example { description: "Compute the square root of each number in a list",