From 834522d0022ef538ce8a7d96b380a4316c6e58ba Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Thu, 27 Oct 2022 23:45:45 +0200 Subject: [PATCH] Fix `each while` behavior when printing. (#6897) Fixes #6895 Warning: `Iterator::map_while` does not return a `FusedIterator`! Depending on the consuming adaptor or code (e.g. for loop) the iteration may be stopped but this is not guaranteed. Adds a test example but Examples handle iterators like `FusedIterator` and thus don't catch the regression Cleanup the message on another test --- crates/nu-command/src/filters/each_while.rs | 28 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/nu-command/src/filters/each_while.rs b/crates/nu-command/src/filters/each_while.rs index 468e961d4c..f0459cff29 100644 --- a/crates/nu-command/src/filters/each_while.rs +++ b/crates/nu-command/src/filters/each_while.rs @@ -36,24 +36,42 @@ impl Command for EachWhile { fn examples(&self) -> Vec { let stream_test_1 = vec![ Value::Int { - val: 1, + val: 2, span: Span::test_data(), }, Value::Int { - val: 2, + val: 4, + span: Span::test_data(), + }, + ]; + let stream_test_2 = vec![ + Value::String { + val: "Output: 1".into(), + span: Span::test_data(), + }, + Value::String { + val: "Output: 2".into(), span: Span::test_data(), }, ]; vec![ Example { - example: "[1 2 3] | each while { |it| if $it < 3 { $it } else { null } }", - description: "Multiplies elements in list", + example: "[1 2 3] | each while { |it| if $it < 3 { $it * 2 } else { null } }", + description: "Multiplies elements below three by two", result: Some(Value::List { vals: stream_test_1, span: Span::test_data(), }), }, + Example { + example: r#"[1 2 stop 3 4] | each while { |it| if $it == 'stop' { null } else { $"Output: ($it)" } }"#, + description: "Output elements till reaching 'stop'", + result: Some(Value::List { + vals: stream_test_2, + span: Span::test_data(), + }), + }, Example { example: r#"[1 2 3] | each while -n { |it| if $it.item < 2 { $"value ($it.item) at ($it.index)!"} else { null } }"#, description: "Iterate over each element, print the matching value and its index", @@ -140,6 +158,7 @@ impl Command for EachWhile { Err(_) => None, } }) + .fuse() .into_pipeline_data(ctrlc)), PipelineData::ExternalStream { stdout: None, .. } => Ok(PipelineData::new(call.head)), PipelineData::ExternalStream { @@ -198,6 +217,7 @@ impl Command for EachWhile { Err(_) => None, } }) + .fuse() .into_pipeline_data(ctrlc)), PipelineData::Value(x, ..) => { if let Some(var) = block.signature.get_positional(0) {