Skip to content
Snippets Groups Projects
Commit b2efe63c authored by Carl Lerche's avatar Carl Lerche
Browse files

Get rid of SliceBuf

parent 16b4266c
No related branches found
No related tags found
No related merge requests found
...@@ -2,14 +2,12 @@ mod append; ...@@ -2,14 +2,12 @@ mod append;
mod byte; mod byte;
mod ring; mod ring;
mod sink; mod sink;
mod slice;
mod source; mod source;
mod take; mod take;
pub use self::append::AppendBuf; pub use self::append::AppendBuf;
pub use self::byte::{ByteBuf, MutByteBuf, ROByteBuf}; pub use self::byte::{ByteBuf, MutByteBuf, ROByteBuf};
pub use self::ring::RingBuf; pub use self::ring::RingBuf;
pub use self::slice::{SliceBuf, MutSliceBuf};
pub use self::take::Take; pub use self::take::Take;
use {ByteStr, RopeBuf}; use {ByteStr, RopeBuf};
...@@ -43,9 +41,10 @@ pub trait Buf { ...@@ -43,9 +41,10 @@ pub trait Buf {
/// Returns the number of bytes read. /// Returns the number of bytes read.
/// ///
/// ``` /// ```
/// use bytes::{SliceBuf, Buf}; /// use std::io::Cursor;
/// use bytes::Buf;
/// ///
/// let mut buf = SliceBuf::wrap(b"hello world"); /// let mut buf = Cursor::new(b"hello world");
/// let mut dst = [0; 5]; /// let mut dst = [0; 5];
/// ///
/// buf.read_slice(&mut dst); /// buf.read_slice(&mut dst);
...@@ -117,12 +116,13 @@ pub trait MutBuf : Sized { ...@@ -117,12 +116,13 @@ pub trait MutBuf : Sized {
/// Returns the number of bytes written. /// Returns the number of bytes written.
/// ///
/// ``` /// ```
/// use bytes::{MutSliceBuf, Buf, MutBuf}; /// use bytes::MutBuf;
/// use std::io::Cursor;
/// ///
/// let mut dst = [0; 6]; /// let mut dst = [0; 6];
/// ///
/// { /// {
/// let mut buf = MutSliceBuf::wrap(&mut dst); /// let mut buf = Cursor::new(&mut dst);
/// buf.write_slice(b"hello"); /// buf.write_slice(b"hello");
/// ///
/// assert_eq!(1, buf.remaining()); /// assert_eq!(1, buf.remaining());
......
use std::cmp;
use {Buf, MutBuf};
// TODO: Rename -> Cursor. Use as buf for various byte strings
pub struct SliceBuf<'a> {
bytes: &'a [u8],
pos: usize
}
impl<'a> SliceBuf<'a> {
pub fn wrap(bytes: &'a [u8]) -> SliceBuf<'a> {
SliceBuf { bytes: bytes, pos: 0 }
}
}
impl<'a> Buf for SliceBuf<'a> {
fn remaining(&self) -> usize {
self.bytes.len() - self.pos
}
fn bytes<'b>(&'b self) -> &'b [u8] {
&self.bytes[self.pos..]
}
fn advance(&mut self, mut cnt: usize) {
cnt = cmp::min(cnt, self.remaining());
self.pos += cnt;
}
}
pub struct MutSliceBuf<'a> {
bytes: &'a mut [u8],
pos: usize
}
impl<'a> MutSliceBuf<'a> {
pub fn wrap(bytes: &'a mut [u8]) -> MutSliceBuf<'a> {
MutSliceBuf {
bytes: bytes,
pos: 0
}
}
}
impl<'a> MutBuf for MutSliceBuf<'a> {
fn remaining(&self) -> usize {
self.bytes.len() - self.pos
}
unsafe fn advance(&mut self, mut cnt: usize) {
cnt = cmp::min(cnt, self.remaining());
self.pos += cnt;
}
unsafe fn mut_bytes<'b>(&'b mut self) -> &'b mut [u8] {
&mut self.bytes[self.pos..]
}
}
...@@ -14,8 +14,6 @@ pub use buf::{ ...@@ -14,8 +14,6 @@ pub use buf::{
MutByteBuf, MutByteBuf,
RingBuf, RingBuf,
ROByteBuf, ROByteBuf,
SliceBuf,
MutSliceBuf,
Take, Take,
ReadExt, ReadExt,
WriteExt, WriteExt,
......
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