use super::backend::StreamBackend;
use super::Stream;
use crate::Error;
pub struct StreamBuilder<B: StreamBackend> {
backend: B,
buffer_size: Option<usize>,
lazy_open: bool,
disown: bool,
}
impl<B: StreamBackend> StreamBuilder<B> {
pub fn new(backend: B) -> Self {
Self {
backend,
buffer_size: None,
lazy_open: false,
disown: false,
}
}
pub fn buffer_size(mut self, size: usize) -> Self {
self.buffer_size = Some(size);
self
}
pub fn lazy_open(mut self) -> Self {
self.lazy_open = true;
self
}
pub fn disown(mut self) -> Self {
self.disown = true;
self
}
pub fn build(self) -> Result<Stream, Error<'static>> {
let stream = Stream::from_backend(self.backend)?;
if self.buffer_size.is_some() {
}
Ok(stream)
}
}
pub trait IntoStream {
fn into_stream(self) -> Result<Stream, Error<'static>>;
}
impl<T> IntoStream for T
where
T: StreamBackend,
{
fn into_stream(self) -> Result<Stream, Error<'static>> {
Stream::from_backend(self)
}
}
pub fn buffered_stream<B: StreamBackend>(
backend: B,
buffer_size: usize,
) -> Result<Stream, Error<'static>> {
StreamBuilder::new(backend).buffer_size(buffer_size).build()
}
pub fn lazy_stream<B: StreamBackend>(backend: B) -> Result<Stream, Error<'static>> {
StreamBuilder::new(backend).lazy_open().build()
}