use crate::*; use std::{cell::RefCell, fmt::Debug, rc::Rc}; #[derive(Clone)] pub struct ValueStream(pub Rc>>); impl ValueStream { pub fn into_string(self) -> String { format!( "[{}]", self.map(|x: Value| x.into_string()) .collect::>() .join(", ") ) } pub fn from_stream(input: impl Iterator + 'static) -> ValueStream { ValueStream(Rc::new(RefCell::new(input))) } } impl Debug for ValueStream { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("ValueStream").finish() } } impl Iterator for ValueStream { type Item = Value; fn next(&mut self) -> Option { { self.0.borrow_mut().next() } } } pub trait IntoValueStream { fn into_value_stream(self) -> ValueStream; } impl IntoValueStream for T where T: Iterator + 'static, { fn into_value_stream(self) -> ValueStream { ValueStream::from_stream(self) } }