use async_std::prelude::*;
use std::borrow::BorrowMut;
#[derive(Debug)]
pub struct AsyncToBlockStream {
async_stream: async_std::net::TcpStream,
}
impl AsyncToBlockStream {
pub fn new(async_stream: async_std::net::TcpStream) -> Self {
Self {
async_stream
}
}
}
impl std::io::Read for AsyncToBlockStream {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
async_std::task::block_on(async {
self.async_stream.read(buf).await
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
})
}
}
impl std::io::Write for AsyncToBlockStream {
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
async_std::task::block_on(async {
self.async_stream.write(buf).await
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
})
}
fn flush(&mut self) -> Result<(), std::io::Error> {
async_std::task::block_on(async {
self.async_stream.flush().await
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
})
}
}