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 {