summaryrefslogtreecommitdiffstats
path: root/third_party/rust/bytes-0.4.12/src/buf/vec_deque.rs
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/bytes-0.4.12/src/buf/vec_deque.rs')
-rw-r--r--third_party/rust/bytes-0.4.12/src/buf/vec_deque.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/third_party/rust/bytes-0.4.12/src/buf/vec_deque.rs b/third_party/rust/bytes-0.4.12/src/buf/vec_deque.rs
new file mode 100644
index 0000000000..1cd650f51d
--- /dev/null
+++ b/third_party/rust/bytes-0.4.12/src/buf/vec_deque.rs
@@ -0,0 +1,39 @@
+use std::collections::VecDeque;
+
+use super::Buf;
+
+impl Buf for VecDeque<u8> {
+ fn remaining(&self) -> usize {
+ self.len()
+ }
+
+ fn bytes(&self) -> &[u8] {
+ let (s1, s2) = self.as_slices();
+ if s1.is_empty() {
+ s2
+ } else {
+ s1
+ }
+ }
+
+ fn advance(&mut self, cnt: usize) {
+ self.drain(..cnt);
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn hello_world() {
+ let mut buffer: VecDeque<u8> = VecDeque::new();
+ buffer.extend(b"hello world");
+ assert_eq!(11, buffer.remaining());
+ assert_eq!(b"hello world", buffer.bytes());
+ buffer.advance(6);
+ assert_eq!(b"world", buffer.bytes());
+ buffer.extend(b" piece");
+ assert_eq!(b"world piece" as &[u8], &buffer.collect::<Vec<u8>>()[..]);
+ }
+}