use std::fmt::Debug;
use std::io::{Error, ErrorKind};
use futures::{Future, Sink, Stream};
pub type IoFuture<T> = Box<Future<Item = T, Error = Error> + Send>;
pub type IoStream<T> = Box<Stream<Item = T, Error = Error> + Send>;
pub fn send_to<T: Send + 'static, Tx, E: Debug>(tx: &Tx, v: T) -> IoFuture<()>
where Tx: Sink<SinkItem = T, SinkError = E> + Send + Clone + 'static
{
Box::new(tx
.clone() .send(v)
.map(|_tx| ()) .map_err(|e| {
debug!("Send to a sink error {:?}", e);
Error::from(ErrorKind::UnexpectedEof)
})
)
}
pub fn send_all_to<T: Send + 'static, S, Tx, E: Debug>(tx: &Tx, s: S) -> IoFuture<()>
where S: Stream<Item = T, Error = E> + Send + 'static,
Tx: Sink<SinkItem = T, SinkError = E> + Send + Clone + 'static
{
Box::new(tx
.clone() .send_all(s)
.map(|_tx| ()) .map_err(|e| {
debug!("Send to a sink error {:?}", e);
Error::from(ErrorKind::UnexpectedEof)
})
)
}