This commit is contained in:
sunli 2020-04-23 22:54:34 +08:00
parent a4a1db4836
commit 11ddf0ae29

View File

@ -7,7 +7,7 @@ use std::pin::Pin;
/// An Adapter for bytes stream to `AsyncRead` /// An Adapter for bytes stream to `AsyncRead`
pub struct StreamBody<S> { pub struct StreamBody<S> {
s: S, s: S,
remain_bytes: Option<Bytes>, remaining_bytes: Option<Bytes>,
} }
impl<S> StreamBody<S> { impl<S> StreamBody<S> {
@ -15,7 +15,7 @@ impl<S> StreamBody<S> {
pub fn new(s: S) -> Self { pub fn new(s: S) -> Self {
Self { Self {
s, s,
remain_bytes: None, remaining_bytes: None,
} }
} }
} }
@ -31,17 +31,17 @@ where
buf: &mut [u8], buf: &mut [u8],
) -> Poll<Result<usize>> { ) -> Poll<Result<usize>> {
loop { loop {
if let Some(bytes) = &mut self.remain_bytes { if let Some(bytes) = &mut self.remaining_bytes {
let data = bytes.split_to(buf.len().min(bytes.len())); let data = bytes.split_to(buf.len().min(bytes.len()));
buf[..data.len()].copy_from_slice(&data); buf[..data.len()].copy_from_slice(&data);
if !bytes.has_remaining() { if !bytes.has_remaining() {
self.remain_bytes = None; self.remaining_bytes = None;
} }
return Poll::Ready(Ok(data.len())); return Poll::Ready(Ok(data.len()));
} else { } else {
match self.s.poll_next_unpin(cx) { match self.s.poll_next_unpin(cx) {
Poll::Ready(Some(Ok(mut bytes))) => { Poll::Ready(Some(Ok(mut bytes))) => {
self.remain_bytes = Some(bytes.to_bytes()); self.remaining_bytes = Some(bytes.to_bytes());
} }
Poll::Ready(Some(Err(_))) => { Poll::Ready(Some(Err(_))) => {
return Poll::Ready(Err(Error::from(ErrorKind::InvalidData))) return Poll::Ready(Err(Error::from(ErrorKind::InvalidData)))