nu_stream/
prelude.rs

1#[macro_export]
2macro_rules! return_err {
3    ($expr:expr) => {
4        match $expr {
5            Err(_) => return,
6            Ok(expr) => expr,
7        };
8    };
9}
10
11#[macro_export]
12macro_rules! trace_out_stream {
13    (target: $target:tt, $desc:tt = $expr:expr) => {{
14        if log::log_enabled!(target: $target, log::Level::Trace) {
15            let objects = $expr.inspect(move |o| {
16                trace!(
17                    target: $target,
18                    "{} = {}",
19                    $desc,
20                    match o {
21                        Err(err) => format!("{:?}", err),
22                        Ok(value) => value.display(),
23                    }
24                );
25            });
26
27            $crate::stream::OutputStream::new(objects)
28        } else {
29            $expr
30        }
31    }};
32}
33
34pub(crate) use std::collections::VecDeque;
35pub(crate) use std::sync::Arc;
36
37use nu_protocol::Value;
38
39pub(crate) use crate::{ActionStream, InputStream, OutputStream};
40
41pub trait IntoOutputStream {
42    fn into_output_stream(self) -> OutputStream;
43}
44
45impl<T> IntoOutputStream for T
46where
47    T: Iterator<Item = Value> + Send + Sync + 'static,
48{
49    fn into_output_stream(self) -> OutputStream {
50        OutputStream::from_stream(self)
51    }
52}
53
54pub trait IntoActionStream {
55    fn into_action_stream(self) -> ActionStream;
56}
57
58impl<T, U> IntoActionStream for T
59where
60    T: Iterator<Item = U> + Send + Sync + 'static,
61    U: Into<nu_protocol::ReturnValue>,
62{
63    fn into_action_stream(self) -> ActionStream {
64        ActionStream {
65            values: Box::new(self.map(|item| item.into())),
66        }
67    }
68}