feat(Position): add getter for position
Signed-off-by: Freyskeyd <simon.paitrault@gmail.com>
This commit is contained in:
parent
00bab6d45e
commit
e88629085b
38
src/lib.rs
38
src/lib.rs
|
@ -78,6 +78,29 @@ impl Buffer {
|
||||||
cnt
|
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) {
|
pub fn reset(&mut self) {
|
||||||
self.position = 0;
|
self.position = 0;
|
||||||
self.end = 0;
|
self.end = 0;
|
||||||
|
@ -229,7 +252,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn delete() {
|
fn delete() {
|
||||||
let mut b = Buffer::with_capacity(10);
|
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_data(), 8);
|
||||||
assert_eq!(b.available_space(), 2);
|
assert_eq!(b.available_space(), 2);
|
||||||
|
|
||||||
|
@ -245,7 +268,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn replace() {
|
fn replace() {
|
||||||
let mut b = Buffer::with_capacity(10);
|
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_data(), 8);
|
||||||
assert_eq!(b.available_space(), 2);
|
assert_eq!(b.available_space(), 2);
|
||||||
|
|
||||||
|
@ -267,4 +290,15 @@ mod tests {
|
||||||
assert_eq!(b.available_space(), 2);
|
assert_eq!(b.available_space(), 2);
|
||||||
assert_eq!(b.data(), &b"ab123Zgh"[..]);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue