mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-25 00:12:59 +01:00
31 lines
754 B
Rust
31 lines
754 B
Rust
use std::io;
|
|
use bytes::{BytesMut, BufMut};
|
|
use futures::{Async, Poll};
|
|
|
|
use super::IoStream;
|
|
|
|
const LW_BUFFER_SIZE: usize = 4096;
|
|
const HW_BUFFER_SIZE: usize = 16_384;
|
|
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|