forked from extern/nushell
Move timeit to use blocks. Make match vars immutable (#8592)
# Description This does a couple random changes/fixes: * Moves `timeit` to use a block instead of a closure. This makes it a bit more flexible. * Moves var bindings in patterns to be immutable # User-Facing Changes `timeit` now takes a block and no arguments. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
parent
2c3aade057
commit
546c753d1e
@ -1,7 +1,7 @@
|
||||
use nu_engine::{eval_block, CallExt};
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Closure, Command, EngineState, Stack},
|
||||
engine::{Block, Command, EngineState, Stack},
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
|
||||
Value,
|
||||
};
|
||||
@ -16,16 +16,12 @@ impl Command for TimeIt {
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Time the running time of a closure."
|
||||
"Time the running time of a block."
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("timeit")
|
||||
.required(
|
||||
"closure",
|
||||
SyntaxShape::Closure(Some(vec![SyntaxShape::Any])),
|
||||
"the closure to run",
|
||||
)
|
||||
.required("block", SyntaxShape::Block, "the block to run")
|
||||
.input_output_types(vec![
|
||||
(Type::Any, Type::Duration),
|
||||
(Type::Nothing, Type::Duration),
|
||||
@ -45,13 +41,13 @@ impl Command for TimeIt {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let capture_block: Closure = call.req(engine_state, stack, 0)?;
|
||||
let capture_block: Block = call.req(engine_state, stack, 0)?;
|
||||
let block = engine_state.get_block(capture_block.block_id);
|
||||
|
||||
let redirect_stdout = call.redirect_stdout;
|
||||
let redirect_stderr = call.redirect_stderr;
|
||||
|
||||
let mut stack = stack.captures_to_stack(&capture_block.captures);
|
||||
// let mut stack = stack.captures_to_stack(&capture_block.captures);
|
||||
|
||||
// In order to provide the pipeline as a positional, it must be converted into a value.
|
||||
// But because pipelines do not have Clone, this one has to be cloned as a value
|
||||
@ -70,7 +66,7 @@ impl Command for TimeIt {
|
||||
let start_time = Instant::now();
|
||||
eval_block(
|
||||
engine_state,
|
||||
&mut stack,
|
||||
stack,
|
||||
block,
|
||||
input_val.into_pipeline_data_with_metadata(input_metadata),
|
||||
redirect_stdout,
|
||||
@ -107,11 +103,11 @@ impl Command for TimeIt {
|
||||
#[test]
|
||||
// Due to difficulty in observing side-effects from time closures,
|
||||
// checks that the closures have run correctly must use the filesystem.
|
||||
fn test_time_closure() {
|
||||
fn test_time_block() {
|
||||
use nu_test_support::{nu, nu_repl_code, playground::Playground};
|
||||
Playground::setup("test_time_closure", |dirs, _| {
|
||||
Playground::setup("test_time_block", |dirs, _| {
|
||||
let inp = [
|
||||
r#"[2 3 4] | timeit {|_| to nuon | save foo.txt }"#,
|
||||
r#"[2 3 4] | timeit {to nuon | save foo.txt }"#,
|
||||
"open foo.txt",
|
||||
];
|
||||
let actual_repl = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
||||
@ -121,11 +117,11 @@ fn test_time_closure() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time_closure_2() {
|
||||
fn test_time_block_2() {
|
||||
use nu_test_support::{nu, nu_repl_code, playground::Playground};
|
||||
Playground::setup("test_time_closure", |dirs, _| {
|
||||
Playground::setup("test_time_block", |dirs, _| {
|
||||
let inp = [
|
||||
r#"[2 3 4] | timeit {|e| {result: $e} | to nuon | save foo.txt }"#,
|
||||
r#"[2 3 4] | timeit {{result: $in} | to nuon | save foo.txt }"#,
|
||||
"open foo.txt",
|
||||
];
|
||||
let actual_repl = nu!(cwd: dirs.test(), nu_repl_code(&inp));
|
||||
|
@ -88,7 +88,7 @@ pub fn parse_variable_pattern(
|
||||
None,
|
||||
)
|
||||
} else {
|
||||
let var_id = working_set.add_variable(bytes.to_vec(), span, Type::Any, true);
|
||||
let var_id = working_set.add_variable(bytes.to_vec(), span, Type::Any, false);
|
||||
|
||||
(
|
||||
MatchPattern {
|
||||
|
Loading…
Reference in New Issue
Block a user