-
Notifications
You must be signed in to change notification settings - Fork 332
Adds Buf impl for VecDeque<impl Buf>
#822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 8 commits
80647bb
94f0950
ea8d166
c1e91a4
ee4ce5d
2aa7fb0
de6884b
16bd51e
2d094c4
9ce4dee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,3 +38,113 @@ impl Buf for VecDeque<u8> { | |
| self.drain(..cnt); | ||
| } | ||
| } | ||
|
|
||
| impl<T: Buf> Buf for VecDeque<T> { | ||
| fn remaining(&self) -> usize { | ||
| self.iter().map(|b| b.remaining()).sum() | ||
| } | ||
|
|
||
| fn chunk(&self) -> &[u8] { | ||
| self.iter() | ||
| .find(|b| b.has_remaining()) | ||
| .map(|b| b.chunk()) | ||
| .unwrap_or_default() | ||
| } | ||
|
Comment on lines
+47
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you mean, let me try to make it follow the contract.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we need a bugfix to existing code, it should be a separate PR. |
||
|
|
||
| #[cfg(feature = "std")] | ||
| fn chunks_vectored<'a>(&'a self, dst: &mut [io::IoSlice<'a>]) -> usize { | ||
| let mut n = 0; | ||
| for buf in self { | ||
| if n >= dst.len() { | ||
| break; | ||
| } | ||
|
|
||
| let old_n = n; | ||
| n += buf.chunks_vectored(&mut dst[n..]); | ||
|
Comment on lines
+57
to
+63
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop must exit if the chunks written by
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The access issue should be fixed now. Let me know if I missed anything. |
||
|
|
||
| let total_length: usize = dst[old_n..n].iter().map(|s| s.len()).sum(); | ||
| if total_length < buf.remaining() { | ||
| // * we don't gather all the remaining data of the current buffer, | ||
| // must stop here to preserve the correct data ordering. | ||
| break; | ||
| } | ||
| } | ||
| n | ||
| } | ||
|
|
||
| fn advance(&mut self, mut cnt: usize) { | ||
| while cnt > 0 { | ||
| let b = self | ||
| .front_mut() | ||
| .expect("advance called with cnt > remaining"); | ||
| let rem = b.remaining(); | ||
| if cnt < rem { | ||
| b.advance(cnt); | ||
| return; | ||
| } else { | ||
| cnt -= rem; | ||
| self.pop_front(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests go in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now under |
||
| use super::*; | ||
| use crate::Bytes; | ||
| use std::io::IoSlice; | ||
|
|
||
| #[test] | ||
| fn test_vec_deque_buf_sequential_integrity() { | ||
| /// A mock Buf that simulates a "fragmented" memory layout. | ||
| /// It claims to have 10 bytes remaining, but its `chunks_vectored` | ||
| /// implementation only exposes the first 4 bytes. | ||
| struct MockBuf(Bytes); | ||
| impl Buf for MockBuf { | ||
| fn remaining(&self) -> usize { | ||
| self.0.remaining() | ||
| } | ||
| fn chunk(&self) -> &[u8] { | ||
| &self.0.chunk()[..1] | ||
| } | ||
| fn advance(&mut self, cnt: usize) { | ||
| self.0.advance(cnt); | ||
| } | ||
| /// Purposefully return fewer bytes than `remaining()` to test | ||
| /// if the caller correctly stops at the first gap. | ||
| fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize { | ||
| if dst.is_empty() || self.0.is_empty() { | ||
| return 0; | ||
| } | ||
| let limit = std::cmp::min(self.0.len(), 4); | ||
| dst[0] = IoSlice::new(&self.0.chunk()[..limit]); | ||
| 1 | ||
| } | ||
| } | ||
|
|
||
| let buf1 = MockBuf(Bytes::from("0123456789")); // 10 bytes | ||
| let buf2 = MockBuf(Bytes::from("ABCDEFGHIJ")); // 10 bytes | ||
|
|
||
| let mut deque = VecDeque::new(); | ||
| deque.push_back(buf1); | ||
| deque.push_back(buf2); | ||
|
|
||
| let mut slices = [IoSlice::new(&[]); 16]; | ||
| let n = deque.chunks_vectored(&mut slices); | ||
|
|
||
| let total_len: usize = slices[..n].iter().map(|s| s.len()).sum(); | ||
|
|
||
| // Verification Logic: | ||
| // A correct implementation must stop gathering if a buffer cannot | ||
| // expose its entire remaining content in a continuous vector of slices. | ||
| // If total_len > 4, it means the implementation skipped the tail of | ||
| // buf1 ("456789") and jumped straight to buf2, which breaks data ordering. | ||
| assert!( | ||
| total_len <= 4, | ||
| "Error: Implementation gathered data from subsequent buffers before | ||
| exhausting the current buffer's remaining data! Total len: {}", | ||
| total_len | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.