diff options
author | Alex Auvolat <alex@adnab.me> | 2022-07-22 13:23:42 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-07-22 13:23:42 +0200 |
commit | f9db9a4b696569bbc56c40b9170320307ebcdd81 (patch) | |
tree | 548bb5d1bac583daadddbaea461a81db9e4d4bf4 /src/stream.rs | |
parent | 5da59ebec5f3072d0b6c3b1ffc90eb8923c50ad9 (diff) | |
download | netapp-f9db9a4b696569bbc56c40b9170320307ebcdd81.tar.gz netapp-f9db9a4b696569bbc56c40b9170320307ebcdd81.zip |
Simplify send.rs
Diffstat (limited to 'src/stream.rs')
-rw-r--r-- | src/stream.rs | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/src/stream.rs b/src/stream.rs index 6c23f4a..ae57d62 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -82,6 +82,23 @@ impl ByteStreamReader { } } + pub fn take_buffer(&mut self) -> Bytes { + let bytes = Bytes::from( + self .buf + .iter() + .map(|x| &x[..]) + .collect::<Vec<_>>() + .concat(), + ); + self.buf.clear(); + self.buf_len = 0; + bytes + } + + pub fn eos(&self) -> bool { + self.buf.is_empty() && self.eos + } + fn try_get(&mut self, read_len: usize) -> Option<Bytes> { if self.buf_len >= read_len { let mut slices = Vec::with_capacity(self.buf.len()); @@ -144,17 +161,7 @@ impl<'a> Future for ByteStreamReadExact<'a> { if *this.fail_on_eos { return Poll::Ready(Err(ReadExactError::UnexpectedEos)); } else { - let bytes = Bytes::from( - this.reader - .buf - .iter() - .map(|x| &x[..]) - .collect::<Vec<_>>() - .concat(), - ); - this.reader.buf.clear(); - this.reader.buf_len = 0; - return Poll::Ready(Ok(bytes)); + return Poll::Ready(Ok(this.reader.take_buffer())); } } |