use crate::prelude::*; pub struct InputStream { crate values: BoxStream<'static, Tagged>, } impl InputStream { pub fn into_vec(self) -> impl Future>> { self.values.collect() } pub fn drain_vec(&mut self) -> impl Future>> { let mut values: BoxStream<'static, Tagged> = VecDeque::new().boxed(); std::mem::swap(&mut values, &mut self.values); values.collect() } pub fn from_stream(input: impl Stream> + Send + 'static) -> InputStream { InputStream { values: input.boxed(), } } } impl From>> for InputStream { fn from(input: BoxStream<'static, Tagged>) -> InputStream { InputStream { values: input } } } impl From>> for InputStream { fn from(input: VecDeque>) -> InputStream { InputStream { values: input.boxed(), } } } impl From>> for InputStream { fn from(input: Vec>) -> InputStream { let mut list = VecDeque::default(); list.extend(input); InputStream { values: list.boxed(), } } } pub struct OutputStream { crate values: BoxStream<'static, ReturnValue>, } impl OutputStream { pub fn new(values: impl Stream + Send + 'static) -> OutputStream { OutputStream { values: values.boxed(), } } pub fn empty() -> OutputStream { let v: VecDeque = VecDeque::new(); v.into() } pub fn one(item: impl Into) -> OutputStream { let mut v: VecDeque = VecDeque::new(); v.push_back(item.into()); v.into() } pub fn from_input(input: impl Stream> + Send + 'static) -> OutputStream { OutputStream { values: input.map(ReturnSuccess::value).boxed(), } } pub fn drain_vec(&mut self) -> impl Future> { let mut values: BoxStream<'static, ReturnValue> = VecDeque::new().boxed(); std::mem::swap(&mut values, &mut self.values); values.collect() } } impl std::ops::Try for OutputStream { type Ok = OutputStream; type Error = ShellError; fn into_result(self) -> Result { Ok(self) } fn from_error(v: Self::Error) -> Self { OutputStream::one(Err(v)) } fn from_ok(v: Self::Ok) -> Self { v } } impl Stream for OutputStream { type Item = ReturnValue; fn poll_next( mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> core::task::Poll> { Stream::poll_next(std::pin::Pin::new(&mut self.values), cx) } } impl From for OutputStream { fn from(input: InputStream) -> OutputStream { OutputStream { values: input.values.map(ReturnSuccess::value).boxed(), } } } impl From>> for OutputStream { fn from(input: BoxStream<'static, Tagged>) -> OutputStream { OutputStream { values: input.map(ReturnSuccess::value).boxed(), } } } impl From> for OutputStream { fn from(input: BoxStream<'static, ReturnValue>) -> OutputStream { OutputStream { values: input } } } impl From> for OutputStream { fn from(input: VecDeque) -> OutputStream { OutputStream { values: input.boxed(), } } } impl From>> for OutputStream { fn from(input: VecDeque>) -> OutputStream { OutputStream { values: input .into_iter() .map(|i| ReturnSuccess::value(i)) .collect::>() .boxed(), } } } impl From> for OutputStream { fn from(input: Vec) -> OutputStream { let mut list = VecDeque::default(); list.extend(input); OutputStream { values: list.boxed(), } } } impl From>> for OutputStream { fn from(input: Vec>) -> OutputStream { let mut list = VecDeque::default(); list.extend(input.into_iter().map(ReturnSuccess::value)); OutputStream { values: list.boxed(), } } }