add mark/reset feature to ByteBuf and RingBuf

This commit is contained in:
Dan Burkert
2015-04-11 15:18:32 -07:00
parent 40f1d1aa77
commit 4d645c7f53
4 changed files with 110 additions and 5 deletions
+47 -2
View File
@@ -13,7 +13,8 @@ pub struct ByteBuf {
mem: alloc::MemRef,
cap: u32,
pos: u32,
lim: u32
lim: u32,
mark: Option<u32>,
}
impl ByteBuf {
@@ -35,6 +36,7 @@ impl ByteBuf {
cap: 0,
pos: 0,
lim: 0,
mark: None,
}
}
@@ -46,6 +48,7 @@ impl ByteBuf {
cap: cap,
pos: pos,
lim: lim,
mark: None,
}
}
@@ -70,7 +73,8 @@ impl ByteBuf {
mem: mem,
cap: capacity,
pos: 0,
lim: capacity
lim: capacity,
mark: None,
}
}
@@ -112,6 +116,27 @@ impl ByteBuf {
Bytes::of(self.to_seq_byte_str())
}
/// Marks the current read location.
///
/// Together with `reset`, this can be used to read from a section of the
/// buffer multiple times. The marked location will be cleared when the
/// buffer is flipped.
pub fn mark(&mut self) {
self.mark = Some(self.pos);
}
/// Resets the read position to the previously marked position.
///
/// Together with `mark`, this can be used to read from a section of the
/// buffer multiple times.
///
/// # Panics
///
/// This method will panic if no mark has been set.
pub fn reset(&mut self) {
self.pos = self.mark.take().expect("no mark set");
}
#[inline]
fn pos(&self) -> usize {
self.pos as usize
@@ -177,6 +202,26 @@ impl ROByteBuf {
pub fn to_bytes(self) -> Bytes {
self.buf.to_bytes()
}
/// Marks the current read location.
///
/// Together with `reset`, this can be used to read from a section of the
/// buffer multiple times.
pub fn mark(&mut self) {
self.buf.mark = Some(self.buf.pos);
}
/// Resets the read position to the previously marked position.
///
/// Together with `mark`, this can be used to read from a section of the
/// buffer multiple times.
///
/// # Panics
///
/// This method will panic if no mark has been set.
pub fn reset(&mut self) {
self.buf.pos = self.buf.mark.take().expect("no mark set");
}
}
impl Buf for ROByteBuf {
+34 -3
View File
@@ -8,7 +8,8 @@ pub struct RingBuf {
ptr: alloc::MemRef, // Pointer to the memory
cap: usize, // Capacity of the buffer
pos: usize, // Offset of read cursor
len: usize // Number of bytes to read
len: usize, // Number of bytes to read
mark: Option<usize>, // Marked read position
}
// TODO: There are most likely many optimizations that can be made
@@ -20,7 +21,8 @@ impl RingBuf {
ptr: alloc::MemRef::none(),
cap: 0,
pos: 0,
len: 0
len: 0,
mark: None,
}
}
@@ -33,7 +35,8 @@ impl RingBuf {
ptr: mem,
cap: capacity,
pos: 0,
len: 0
len: 0,
mark: None,
}
}
@@ -49,6 +52,29 @@ impl RingBuf {
self.cap
}
/// Marks the current read location.
///
/// Together with `reset`, this can be used to read from a section of the
/// buffer multiple times. The mark will be cleared if it is overwritten
/// during a write.
pub fn mark(&mut self) {
self.mark = Some(self.pos);
}
/// Resets the read position to the previously marked position.
///
/// Together with `mark`, this can be used to read from a section of the
/// buffer multiple times.
///
/// # Panics
///
/// This method will panic if no mark has been set,
pub fn reset(&mut self){
let mark = self.mark.take().expect("no mark set");
self.len = (self.len + self.pos + self.cap - mark) % self.cap;
self.pos = mark;
}
fn read_remaining(&self) -> usize {
self.len
}
@@ -71,6 +97,11 @@ impl RingBuf {
fn advance_writer(&mut self, mut cnt: usize) {
cnt = cmp::min(cnt, self.write_remaining());
self.len += cnt;
if let Some(mark) = self.mark {
if (self.pos + self.len) % self.cap > mark {
self.mark = None;
}
}
}
}