aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Freyskeyd <simon.paitrault@gmail.com>2017-05-04 22:20:21 +0200
committerLibravatar Freyskeyd <simon.paitrault@gmail.com>2017-05-05 10:47:27 +0200
commite88629085ba51f344ec2a141421d2ce829ba4dc0 (patch)
tree79ed18c7ba76eb98793dc1c5e39f525fa1324b9b
parent00bab6d45e7dd31a58f760643bf30baaf695c686 (diff)
downloadcircular-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.rs38
1 files changed, 36 insertions, 2 deletions
diff --git a/src/lib.rs b/src/lib.rs
index ecfb057..8025be5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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());
+ }
}