use bytes::{BufMut, BytesMut};
use futures::Poll;
use std::io;

use super::IoStream;

const LW_BUFFER_SIZE: usize = 4096;
const HW_BUFFER_SIZE: usize = 32_768;

pub fn read_from_io<T: IoStream>(
    io: &mut T, buf: &mut BytesMut,
) -> Poll<usize, io::Error> {
    if buf.remaining_mut() < LW_BUFFER_SIZE {
        buf.reserve(HW_BUFFER_SIZE);
    }
    io.read_buf(buf)
}

use bytes::{BufMut, BytesMut};
use futures::{Async, Poll};
use std::io;

use super::IoStream;

const LW_BUFFER_SIZE: usize = 4096;
const HW_BUFFER_SIZE: usize = 32_768;

pub fn read_from_io<T: IoStream>(
    io: &mut T, buf: &mut BytesMut,
) -> Poll<usize, io::Error> {
    unsafe {
        if buf.remaining_mut() < LW_BUFFER_SIZE {
            buf.reserve(HW_BUFFER_SIZE);
        }
        match io.read(buf.bytes_mut()) {
            Ok(n) => {
                buf.advance_mut(n);
                Ok(Async::Ready(n))
            }
            Err(e) => {
                if e.kind() == io::ErrorKind::WouldBlock {
                    Ok(Async::NotReady)
                } else {
                    Err(e)
                }
            }
        }
    }
}

Revert "remove unnecessary use of unsafe in read_from_io"
Revert "remove unnecessary use of unsafe in read_from_io"

This reverts commit da237611cb429da5fb42ab9d6be52837d1de3dda.
