1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-02-20 03:14:21 +01:00

simplify h1 dispatcher (#1899)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
fakeshadow 2021-01-11 08:13:56 +08:00 committed by GitHub
parent 46b2f7eaaf
commit 7affc6878e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -287,42 +287,35 @@ where
self: Pin<&mut Self>, self: Pin<&mut Self>,
cx: &mut Context<'_>, cx: &mut Context<'_>,
) -> Result<bool, DispatchError> { ) -> Result<bool, DispatchError> {
if self.write_buf.is_empty() { let len = self.write_buf.len();
if len == 0 {
return Ok(false); return Ok(false);
} }
let len = self.write_buf.len();
let mut written = 0;
let InnerDispatcherProj { io, write_buf, .. } = self.project(); let InnerDispatcherProj { io, write_buf, .. } = self.project();
let mut io = Pin::new(io.as_mut().unwrap()); let mut io = Pin::new(io.as_mut().unwrap());
let mut written = 0;
while written < len { while written < len {
match io.as_mut().poll_write(cx, &write_buf[written..]) { match io.as_mut().poll_write(cx, &write_buf[written..]) {
Poll::Ready(Ok(0)) => { Poll::Ready(Ok(0)) => {
return Err(DispatchError::Io(io::Error::new( return Err(DispatchError::Io(io::Error::new(
io::ErrorKind::WriteZero, io::ErrorKind::WriteZero,
"", "",
))); )))
}
Poll::Ready(Ok(n)) => {
written += n;
} }
Poll::Ready(Ok(n)) => written += n,
Poll::Pending => { Poll::Pending => {
if written > 0 { write_buf.advance(written);
write_buf.advance(written);
}
return Ok(true); return Ok(true);
} }
Poll::Ready(Err(err)) => return Err(DispatchError::Io(err)), Poll::Ready(Err(err)) => return Err(DispatchError::Io(err)),
} }
} }
if written == write_buf.len() { // SAFETY: setting length to 0 is safe
// SAFETY: setting length to 0 is safe // skips one length check vs truncate
// skips one length check vs truncate unsafe { write_buf.set_len(0) }
unsafe { write_buf.set_len(0) }
} else {
write_buf.advance(written);
}
Ok(false) Ok(false)
} }
@ -766,19 +759,12 @@ where
} else { } else {
// flush buffer // flush buffer
inner.as_mut().poll_flush(cx)?; inner.as_mut().poll_flush(cx)?;
if !inner.write_buf.is_empty() || inner.io.is_none() { if !inner.write_buf.is_empty() {
Poll::Pending Poll::Pending
} else { } else {
match Pin::new(inner.project().io) Pin::new(inner.project().io.as_mut().unwrap())
.as_pin_mut()
.unwrap()
.poll_shutdown(cx) .poll_shutdown(cx)
{ .map_err(DispatchError::from)
Poll::Ready(res) => {
Poll::Ready(res.map_err(DispatchError::from))
}
Poll::Pending => Poll::Pending,
}
} }
} }
} else { } else {
@ -920,7 +906,7 @@ where
buf.reserve(HW_BUFFER_SIZE - remaining); buf.reserve(HW_BUFFER_SIZE - remaining);
} }
match read(cx, io, buf) { match actix_codec::poll_read_buf(Pin::new(io), cx, buf) {
Poll::Pending => { Poll::Pending => {
return if read_some { Ok(Some(false)) } else { Ok(None) }; return if read_some { Ok(Some(false)) } else { Ok(None) };
} }
@ -948,17 +934,6 @@ where
} }
} }
fn read<T>(
cx: &mut Context<'_>,
io: &mut T,
buf: &mut BytesMut,
) -> Poll<Result<usize, io::Error>>
where
T: AsyncRead + Unpin,
{
actix_codec::poll_read_buf(Pin::new(io), cx, buf)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::str; use std::str;