Skip to content

Commit

Permalink
Implement Buf::chunks_vectored for VecDeque<u8> (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini authored Jan 10, 2025
1 parent 0e9e4fc commit 16cc574
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/buf/vec_deque.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use alloc::collections::VecDeque;
#[cfg(feature = "std")]
use std::io;

use super::Buf;

Expand All @@ -16,6 +18,22 @@ impl Buf for VecDeque<u8> {
}
}

#[cfg(feature = "std")]
fn chunks_vectored<'a>(&'a self, dst: &mut [io::IoSlice<'a>]) -> usize {
if self.is_empty() || dst.is_empty() {
return 0;
}

let (s1, s2) = self.as_slices();
dst[0] = io::IoSlice::new(s1);
if s2.is_empty() || dst.len() == 1 {
return 1;
}

dst[1] = io::IoSlice::new(s2);
2
}

fn advance(&mut self, cnt: usize) {
self.drain(..cnt);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ mod vec_deque {
deque
}

buf_tests!(make_input, /* `VecDeque` does not do `chucks_vectored */ false);
buf_tests!(make_input, true);
}

#[cfg(feature = "std")]
Expand Down

0 comments on commit 16cc574

Please sign in to comment.