mirror of
https://github.com/nushell/nushell.git
synced 2025-07-02 23:51:49 +02:00
move value into its own folder
This commit is contained in:
50
crates/nu-protocol/src/value/stream.rs
Normal file
50
crates/nu-protocol/src/value/stream.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use crate::*;
|
||||
use std::{cell::RefCell, fmt::Debug, rc::Rc};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ValueStream(pub Rc<RefCell<dyn Iterator<Item = Value>>>);
|
||||
|
||||
impl ValueStream {
|
||||
pub fn into_string(self) -> String {
|
||||
format!(
|
||||
"[{}]",
|
||||
self.map(|x: Value| x.into_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
)
|
||||
}
|
||||
|
||||
pub fn from_stream(input: impl Iterator<Item = Value> + '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::Item> {
|
||||
{
|
||||
let mut iter = self.0.borrow_mut();
|
||||
iter.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoValueStream {
|
||||
fn into_value_stream(self) -> ValueStream;
|
||||
}
|
||||
|
||||
impl<T> IntoValueStream for T
|
||||
where
|
||||
T: Iterator<Item = Value> + 'static,
|
||||
{
|
||||
fn into_value_stream(self) -> ValueStream {
|
||||
ValueStream::from_stream(self)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user