diff options
author | 2017-05-04 22:20:21 +0200 | |
---|---|---|
committer | 2017-05-05 10:47:27 +0200 | |
commit | e88629085ba51f344ec2a141421d2ce829ba4dc0 (patch) | |
tree | 79ed18c7ba76eb98793dc1c5e39f525fa1324b9b | |
parent | 00bab6d45e7dd31a58f760643bf30baaf695c686 (diff) | |
download | circular-e88629085ba51f344ec2a141421d2ce829ba4dc0.tar.gz circular-e88629085ba51f344ec2a141421d2ce829ba4dc0.tar.bz2 circular-e88629085ba51f344ec2a141421d2ce829ba4dc0.zip |
feat(Position): add getter for position
Signed-off-by: Freyskeyd <simon.paitrault@gmail.com>
-rw-r--r-- | src/lib.rs | 38 |
1 files changed, 36 insertions, 2 deletions
@@ -78,6 +78,29 @@ impl Buffer { cnt } + /// Get the current position + /// + /// # Examples + /// ``` + /// use circular::Buffer; + /// use std::io::{Read,Write}; + /// + /// let mut output = [0;5]; + /// + /// let mut b = Buffer::with_capacity(10); + /// + /// let res = b.write(&b"abcdefgh"[..]); + /// + /// b.read(&mut output); + /// + /// // Position must be 5 + /// assert_eq!(b.position(), 5); + /// assert_eq!(b.available_data(), 3); + /// ``` + pub fn position(&self) -> usize { + self.position + } + pub fn reset(&mut self) { self.position = 0; self.end = 0; @@ -229,7 +252,7 @@ mod tests { #[test] fn delete() { let mut b = Buffer::with_capacity(10); - let res = b.write(&b"abcdefgh"[..]); + let _ = b.write(&b"abcdefgh"[..]); assert_eq!(b.available_data(), 8); assert_eq!(b.available_space(), 2); @@ -245,7 +268,7 @@ mod tests { #[test] fn replace() { let mut b = Buffer::with_capacity(10); - let res = b.write(&b"abcdefgh"[..]); + let _ = b.write(&b"abcdefgh"[..]); assert_eq!(b.available_data(), 8); assert_eq!(b.available_space(), 2); @@ -267,4 +290,15 @@ mod tests { assert_eq!(b.available_space(), 2); assert_eq!(b.data(), &b"ab123Zgh"[..]); } + + use std::str; + #[test] + fn set_position() { + let mut output = [0;5]; + let mut b = Buffer::with_capacity(10); + let _ = b.write(&b"abcdefgh"[..]); + let _ = b.read(&mut output); + assert_eq!(b.available_data(), 3); + println!("{:?}", b.position()); + } } |