Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of Buf for VecDeque (#249)
- Loading branch information
1 parent
e13d2a7
commit 0e8b440
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ mod into_buf; | |
mod iter; | ||
mod reader; | ||
mod take; | ||
mod vec_deque; | ||
mod writer; | ||
|
||
pub use self::buf::Buf; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>>()[..]); | ||
} | ||
} |