use bytes::Buf;
use futures::{Async, Poll};
use std::io as std_io;
use AsyncRead;
pub trait AsyncWrite: std_io::Write {
fn poll_write(&mut self, buf: &[u8]) -> Poll<usize, std_io::Error> {
match self.write(buf) {
Ok(t) => Ok(Async::Ready(t)),
Err(ref e) if e.kind() == std_io::ErrorKind::WouldBlock => return Ok(Async::NotReady),
Err(e) => return Err(e.into()),
}
}
fn poll_flush(&mut self) -> Poll<(), std_io::Error> {
match self.flush() {
Ok(t) => Ok(Async::Ready(t)),
Err(ref e) if e.kind() == std_io::ErrorKind::WouldBlock => return Ok(Async::NotReady),
Err(e) => return Err(e.into()),
}
}
fn shutdown(&mut self) -> Poll<(), std_io::Error>;
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> Poll<usize, std_io::Error>
where
Self: Sized,
{
if !buf.has_remaining() {
return Ok(Async::Ready(0));
}
let n = try_ready!(self.poll_write(buf.bytes()));
buf.advance(n);
Ok(Async::Ready(n))
}
}
impl<T: ?Sized + AsyncWrite> AsyncWrite for Box<T> {
fn shutdown(&mut self) -> Poll<(), std_io::Error> {
(**self).shutdown()
}
}
impl<'a, T: ?Sized + AsyncWrite> AsyncWrite for &'a mut T {
fn shutdown(&mut self) -> Poll<(), std_io::Error> {
(**self).shutdown()
}
}
impl AsyncRead for std_io::Repeat {
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
false
}
}
impl AsyncWrite for std_io::Sink {
fn shutdown(&mut self) -> Poll<(), std_io::Error> {
Ok(().into())
}
}
impl<T: AsyncRead> AsyncRead for std_io::Take<T> {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
self.get_ref().prepare_uninitialized_buffer(buf)
}
}
impl<T, U> AsyncRead for std_io::Chain<T, U>
where
T: AsyncRead,
U: AsyncRead,
{
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
let (t, u) = self.get_ref();
t.prepare_uninitialized_buffer(buf) || u.prepare_uninitialized_buffer(buf)
}
}
impl<T: AsyncWrite> AsyncWrite for std_io::BufWriter<T> {
fn shutdown(&mut self) -> Poll<(), std_io::Error> {
try_ready!(self.poll_flush());
self.get_mut().shutdown()
}
}
impl<T: AsyncRead> AsyncRead for std_io::BufReader<T> {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
self.get_ref().prepare_uninitialized_buffer(buf)
}
}
impl<T: AsRef<[u8]>> AsyncRead for std_io::Cursor<T> {}
impl<'a> AsyncWrite for std_io::Cursor<&'a mut [u8]> {
fn shutdown(&mut self) -> Poll<(), std_io::Error> {
Ok(().into())
}
}
impl AsyncWrite for std_io::Cursor<Vec<u8>> {
fn shutdown(&mut self) -> Poll<(), std_io::Error> {
Ok(().into())
}
}
impl AsyncWrite for std_io::Cursor<Box<[u8]>> {
fn shutdown(&mut self) -> Poll<(), std_io::Error> {
Ok(().into())
}
}