Skip to content
Snippets Groups Projects
Commit 7b63fa03 authored by Jamie Turner's avatar Jamie Turner
Browse files

Add resume() method to ByteBuf that restores prior write position.

parent d2b4cc38
No related branches found
No related tags found
No related merge requests found
......@@ -88,6 +88,14 @@ impl ByteBuf {
buf
}
/// Flips the buffer back to mutable, resetting the write position
/// to the byte after the previous write.
pub fn resume(mut self) -> MutByteBuf {
self.pos = self.lim;
self.lim = self.cap;
MutByteBuf { buf: self }
}
pub fn read_slice(&mut self, dst: &mut [u8]) -> usize {
let len = cmp::min(dst.len(), self.remaining());
let cnt = len as u32;
......
......@@ -47,4 +47,13 @@ pub fn test_byte_buf_read_write() {
let mut dst = [0; 7];
assert_eq!(7, buf.read(&mut dst[..]).unwrap());
assert_eq!(b"goodbye", &dst);
let mut buf = buf.resume();
assert_eq!(13, buf.remaining());
buf.write(&b" have fun"[..]).unwrap();
assert_eq!(4, buf.remaining());
let buf = buf.flip();
assert_eq!(buf.bytes(), b"hello world goodbye have fun");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment