Another batch of removing async_stream (#1981)

This commit is contained in:
Jonathan Turner 2020-06-13 16:54:35 -07:00 committed by GitHub
parent 86b316e930
commit d7b1480ad0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 266 additions and 278 deletions

View File

@ -3,9 +3,7 @@ use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*; use crate::prelude::*;
use log::trace; use log::trace;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
hir::ClassifiedCommand, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
pub struct KeepUntil; pub struct KeepUntil;
@ -34,80 +32,81 @@ impl WholeStreamCommand for KeepUntil {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let registry = registry.clone(); let registry = Arc::new(registry.clone());
let scope = args.call_info.scope.clone(); let scope = Arc::new(args.call_info.scope.clone());
let stream = async_stream! {
let mut call_info = args.evaluate_once(&registry).await?;
let block = call_info.args.expect_nth(0)?.clone(); let call_info = args.evaluate_once(&registry).await?;
let condition = match block { let block = call_info.args.expect_nth(0)?.clone();
Value {
value: UntaggedValue::Block(block), let condition = Arc::new(match block {
tag, Value {
} => { value: UntaggedValue::Block(block),
if block.block.len() != 1 { tag,
yield Err(ShellError::labeled_error( } => {
"Expected a condition", if block.block.len() != 1 {
"expected a condition", return Err(ShellError::labeled_error(
tag,
));
return;
}
match block.block[0].list.get(0) {
Some(item) => match item {
ClassifiedCommand::Expr(expr) => expr.clone(),
_ => {
yield Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
},
None => {
yield Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
}
}
Value { tag, .. } => {
yield Err(ShellError::labeled_error(
"Expected a condition", "Expected a condition",
"expected a condition", "expected a condition",
tag, tag,
)); ));
return;
} }
}; match block.block[0].list.get(0) {
Some(item) => match item {
while let Some(item) = call_info.input.next().await { ClassifiedCommand::Expr(expr) => expr.clone(),
let condition = condition.clone(); _ => {
trace!("ITEM = {:?}", item); return Err(ShellError::labeled_error(
let result = "Expected a condition",
evaluate_baseline_expr(&*condition, &registry, &item, &scope.vars, &scope.env) "expected a condition",
.await; tag,
trace!("RESULT = {:?}", result); ));
}
let return_value = match result { },
Ok(ref v) if v.is_true() => false, None => {
_ => true, return Err(ShellError::labeled_error(
}; "Expected a condition",
"expected a condition",
if return_value { tag,
yield ReturnSuccess::value(item); ));
} else { }
break;
} }
} }
}; Value { tag, .. } => {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
});
Ok(stream.to_output_stream()) Ok(call_info
.input
.take_while(move |item| {
let condition = condition.clone();
let registry = registry.clone();
let scope = scope.clone();
let item = item.clone();
trace!("ITEM = {:?}", item);
async move {
let result = evaluate_baseline_expr(
&*condition,
&registry,
&item,
&scope.vars,
&scope.env,
)
.await;
trace!("RESULT = {:?}", result);
match result {
Ok(ref v) if v.is_true() => false,
_ => true,
}
}
})
.to_output_stream())
} }
} }

View File

@ -3,9 +3,7 @@ use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*; use crate::prelude::*;
use log::trace; use log::trace;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
hir::ClassifiedCommand, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
pub struct KeepWhile; pub struct KeepWhile;
@ -34,80 +32,81 @@ impl WholeStreamCommand for KeepWhile {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let registry = registry.clone(); let registry = Arc::new(registry.clone());
let scope = args.call_info.scope.clone(); let scope = Arc::new(args.call_info.scope.clone());
let stream = async_stream! { let call_info = args.evaluate_once(&registry).await?;
let mut call_info = args.evaluate_once(&registry).await?;
let block = call_info.args.expect_nth(0)?.clone(); let block = call_info.args.expect_nth(0)?.clone();
let condition = match block { let condition = Arc::new(match block {
Value { Value {
value: UntaggedValue::Block(block), value: UntaggedValue::Block(block),
tag, tag,
} => { } => {
if block.block.len() != 1 { if block.block.len() != 1 {
yield Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
match block.block[0].list.get(0) {
Some(item) => match item {
ClassifiedCommand::Expr(expr) => expr.clone(),
_ => {
yield Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
},
None => {
yield Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
}
}
Value { tag, .. } => {
yield Err(ShellError::labeled_error(
"Expected a condition", "Expected a condition",
"expected a condition", "expected a condition",
tag, tag,
)); ));
return;
} }
}; match block.block[0].list.get(0) {
Some(item) => match item {
while let Some(item) = call_info.input.next().await { ClassifiedCommand::Expr(expr) => expr.clone(),
let condition = condition.clone(); _ => {
trace!("ITEM = {:?}", item); return Err(ShellError::labeled_error(
let result = "Expected a condition",
evaluate_baseline_expr(&*condition, &registry, &item, &scope.vars, &scope.env) "expected a condition",
.await; tag,
trace!("RESULT = {:?}", result); ));
}
let return_value = match result { },
Ok(ref v) if v.is_true() => true, None => {
_ => false, return Err(ShellError::labeled_error(
}; "Expected a condition",
"expected a condition",
if return_value { tag,
yield ReturnSuccess::value(item); ));
} else { }
break;
} }
} }
}; Value { tag, .. } => {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
});
Ok(stream.to_output_stream()) Ok(call_info
.input
.take_while(move |item| {
let condition = condition.clone();
let registry = registry.clone();
let scope = scope.clone();
let item = item.clone();
trace!("ITEM = {:?}", item);
async move {
let result = evaluate_baseline_expr(
&*condition,
&registry,
&item,
&scope.vars,
&scope.env,
)
.await;
trace!("RESULT = {:?}", result);
match result {
Ok(ref v) if v.is_true() => true,
_ => false,
}
}
})
.to_output_stream())
} }
} }

View File

@ -3,9 +3,7 @@ use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*; use crate::prelude::*;
use log::trace; use log::trace;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
hir::ClassifiedCommand, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
pub struct SkipUntil; pub struct SkipUntil;
@ -34,83 +32,80 @@ impl WholeStreamCommand for SkipUntil {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let registry = registry.clone(); let registry = Arc::new(registry.clone());
let scope = args.call_info.scope.clone(); let scope = Arc::new(args.call_info.scope.clone());
let stream = async_stream! { let call_info = args.evaluate_once(&registry).await?;
let mut call_info = args.evaluate_once(&registry).await?;
let block = call_info.args.expect_nth(0)?.clone(); let block = call_info.args.expect_nth(0)?.clone();
let condition = match block { let condition = Arc::new(match block {
Value { Value {
value: UntaggedValue::Block(block), value: UntaggedValue::Block(block),
tag, tag,
} => { } => {
if block.block.len() != 1 { if block.block.len() != 1 {
yield Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
match block.block[0].list.get(0) {
Some(item) => match item {
ClassifiedCommand::Expr(expr) => expr.clone(),
_ => {
yield Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
},
None => {
yield Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
}
}
Value { tag, .. } => {
yield Err(ShellError::labeled_error(
"Expected a condition", "Expected a condition",
"expected a condition", "expected a condition",
tag, tag,
)); ));
return;
} }
}; match block.block[0].list.get(0) {
Some(item) => match item {
let mut skipping = true; ClassifiedCommand::Expr(expr) => expr.clone(),
while let Some(item) = call_info.input.next().await { _ => {
let condition = condition.clone(); return Err(ShellError::labeled_error(
trace!("ITEM = {:?}", item); "Expected a condition",
let result = "expected a condition",
evaluate_baseline_expr(&*condition, &registry, &item, &scope.vars, &scope.env) tag,
.await; ));
trace!("RESULT = {:?}", result); }
},
let return_value = match result { None => {
Ok(ref v) if v.is_true() => true, return Err(ShellError::labeled_error(
_ => false, "Expected a condition",
}; "expected a condition",
tag,
if return_value { ));
skipping = false; }
}
if !skipping {
yield ReturnSuccess::value(item);
} }
} }
}; Value { tag, .. } => {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
});
Ok(stream.to_output_stream()) Ok(call_info
.input
.skip_while(move |item| {
let condition = condition.clone();
let registry = registry.clone();
let scope = scope.clone();
let item = item.clone();
trace!("ITEM = {:?}", item);
async move {
let result = evaluate_baseline_expr(
&*condition,
&registry,
&item,
&scope.vars,
&scope.env,
)
.await;
trace!("RESULT = {:?}", result);
match result {
Ok(ref v) if v.is_true() => true,
_ => false,
}
}
})
.to_output_stream())
} }
} }

View File

@ -3,9 +3,7 @@ use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*; use crate::prelude::*;
use log::trace; use log::trace;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
hir::ClassifiedCommand, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
pub struct SkipWhile; pub struct SkipWhile;
@ -34,83 +32,80 @@ impl WholeStreamCommand for SkipWhile {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let registry = registry.clone(); let registry = Arc::new(registry.clone());
let scope = args.call_info.scope.clone(); let scope = Arc::new(args.call_info.scope.clone());
let stream = async_stream! { let call_info = args.evaluate_once(&registry).await?;
let mut call_info = args.evaluate_once(&registry).await?;
let block = call_info.args.expect_nth(0)?.clone(); let block = call_info.args.expect_nth(0)?.clone();
let condition = match block { let condition = Arc::new(match block {
Value { Value {
value: UntaggedValue::Block(block), value: UntaggedValue::Block(block),
tag, tag,
} => { } => {
if block.block.len() != 1 { if block.block.len() != 1 {
yield Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
match block.block[0].list.get(0) {
Some(item) => match item {
ClassifiedCommand::Expr(expr) => expr.clone(),
_ => {
yield Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
},
None => {
yield Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
return;
}
}
}
Value { tag, .. } => {
yield Err(ShellError::labeled_error(
"Expected a condition", "Expected a condition",
"expected a condition", "expected a condition",
tag, tag,
)); ));
return;
} }
}; match block.block[0].list.get(0) {
Some(item) => match item {
let mut skipping = true; ClassifiedCommand::Expr(expr) => expr.clone(),
while let Some(item) = call_info.input.next().await { _ => {
let condition = condition.clone(); return Err(ShellError::labeled_error(
trace!("ITEM = {:?}", item); "Expected a condition",
let result = "expected a condition",
evaluate_baseline_expr(&*condition, &registry, &item, &scope.vars, &scope.env) tag,
.await; ));
trace!("RESULT = {:?}", result); }
},
let return_value = match result { None => {
Ok(ref v) if v.is_true() => false, return Err(ShellError::labeled_error(
_ => true, "Expected a condition",
}; "expected a condition",
tag,
if return_value { ));
skipping = false; }
}
if !skipping {
yield ReturnSuccess::value(item);
} }
} }
}; Value { tag, .. } => {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
});
Ok(stream.to_output_stream()) Ok(call_info
.input
.skip_while(move |item| {
let item = item.clone();
let condition = condition.clone();
let registry = registry.clone();
let scope = scope.clone();
trace!("ITEM = {:?}", item);
async move {
let result = evaluate_baseline_expr(
&*condition,
&registry,
&item,
&scope.vars,
&scope.env,
)
.await;
trace!("RESULT = {:?}", result);
match result {
Ok(ref v) if v.is_true() => true,
_ => false,
}
}
})
.to_output_stream())
} }
} }

View File

@ -4,17 +4,17 @@ use std::sync::atomic::{AtomicBool, Ordering};
pub struct InterruptibleStream<V> { pub struct InterruptibleStream<V> {
inner: BoxStream<'static, V>, inner: BoxStream<'static, V>,
ctrl_c: Arc<AtomicBool>, interrupt_signal: Arc<AtomicBool>,
} }
impl<V> InterruptibleStream<V> { impl<V> InterruptibleStream<V> {
pub fn new<S>(inner: S, ctrl_c: Arc<AtomicBool>) -> InterruptibleStream<V> pub fn new<S>(inner: S, interrupt_signal: Arc<AtomicBool>) -> InterruptibleStream<V>
where where
S: Stream<Item = V> + Send + 'static, S: Stream<Item = V> + Send + 'static,
{ {
InterruptibleStream { InterruptibleStream {
inner: inner.boxed(), inner: inner.boxed(),
ctrl_c, interrupt_signal,
} }
} }
} }
@ -26,7 +26,7 @@ impl<V> Stream for InterruptibleStream<V> {
mut self: std::pin::Pin<&mut Self>, mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>, cx: &mut std::task::Context<'_>,
) -> core::task::Poll<Option<Self::Item>> { ) -> core::task::Poll<Option<Self::Item>> {
if self.ctrl_c.load(Ordering::SeqCst) { if self.interrupt_signal.load(Ordering::SeqCst) {
Poll::Ready(None) Poll::Ready(None)
} else { } else {
Stream::poll_next(std::pin::Pin::new(&mut self.inner), cx) Stream::poll_next(std::pin::Pin::new(&mut self.inner), cx)