From 17c033030ba6deda3f32755f06e4da04f46ab532 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 22 Jun 2018 08:55:19 +0600 Subject: [PATCH] Revert "remove unnecessary use of unsafe in read_from_io" This reverts commit da237611cb429da5fb42ab9d6be52837d1de3dda. --- src/server/utils.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/server/utils.rs b/src/server/utils.rs index b0470d76..e0e7e7f6 100644 --- a/src/server/utils.rs +++ b/src/server/utils.rs @@ -1,5 +1,5 @@ use bytes::{BufMut, BytesMut}; -use futures::Poll; +use futures::{Async, Poll}; use std::io; use super::IoStream; @@ -10,8 +10,22 @@ const HW_BUFFER_SIZE: usize = 32_768; pub fn read_from_io( io: &mut T, buf: &mut BytesMut, ) -> Poll { - if buf.remaining_mut() < LW_BUFFER_SIZE { - buf.reserve(HW_BUFFER_SIZE); + 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) + } + } + } } - io.read_buf(buf) }