feat(Position): add getter for position

Signed-off-by: Freyskeyd <simon.paitrault@gmail.com>
This commit is contained in:
Freyskeyd 2017-05-04 22:20:21 +02:00
parent 00bab6d45e
commit e88629085b
No known key found for this signature in database
GPG Key ID: 5A645DEDF790F113
1 changed files with 36 additions and 2 deletions

View File

@ -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());
}
}