2018-04-14 01:02:01 +02:00
|
|
|
use bytes::{BufMut, BytesMut};
|
2018-01-12 06:48:36 +01:00
|
|
|
use futures::{Async, Poll};
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::io;
|
2018-01-12 06:48:36 +01:00
|
|
|
|
|
|
|
use super::IoStream;
|
|
|
|
|
2018-03-02 05:13:50 +01:00
|
|
|
const LW_BUFFER_SIZE: usize = 4096;
|
2018-03-02 05:01:25 +01:00
|
|
|
const HW_BUFFER_SIZE: usize = 32_768;
|
2018-01-12 06:48:36 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn read_from_io<T: IoStream>(
|
2018-04-29 07:55:47 +02:00
|
|
|
io: &mut T, buf: &mut BytesMut,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> Poll<usize, io::Error> {
|
2018-01-12 06:48:36 +01:00
|
|
|
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))
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2018-01-12 06:48:36 +01:00
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == io::ErrorKind::WouldBlock {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
} else {
|
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|