Skip to content
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

Implementation of Buf for VecDeque #249

Merged
merged 1 commit into from Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/buf/mod.rs
Expand Up @@ -24,6 +24,7 @@ mod into_buf;
mod iter;
mod reader;
mod take;
mod vec_deque;
mod writer;

pub use self::buf::Buf;
Expand Down
39 changes: 39 additions & 0 deletions 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>>()[..]);
}
}