Get rid of BufError

This commit is contained in:
Carl Lerche
2016-08-05 21:54:29 -07:00
parent 6529f6392a
commit b6a424d892
12 changed files with 61 additions and 78 deletions
+15 -15
View File
@@ -12,7 +12,7 @@ pub use self::ring::RingBuf;
pub use self::slice::{SliceBuf, MutSliceBuf};
pub use self::take::Take;
use {BufError, ByteStr, RopeBuf};
use {ByteStr, RopeBuf};
use std::{cmp, fmt, io, ptr, usize};
/// A trait for values that provide sequential read access to bytes.
@@ -33,7 +33,7 @@ pub trait Buf {
self.remaining() > 0
}
fn copy_to<S: Sink>(&mut self, dst: S) -> Result<usize, BufError>
fn copy_to<S: Sink>(&mut self, dst: S) -> usize
where Self: Sized {
dst.copy_from(self)
}
@@ -107,7 +107,7 @@ pub trait MutBuf : Sized {
/// The returned byte slice may represent uninitialized memory.
unsafe fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8];
fn copy_from<S: Source>(&mut self, src: S) -> Result<usize, BufError>
fn copy_from<S: Source>(&mut self, src: S) -> usize
where Self: Sized {
src.copy_to(self)
}
@@ -166,24 +166,24 @@ pub trait MutBuf : Sized {
/// A value that writes bytes from itself into a `MutBuf`.
pub trait Source {
fn copy_to<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError>;
fn copy_to<B: MutBuf>(self, buf: &mut B) -> usize;
}
impl<'a> Source for &'a [u8] {
fn copy_to<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
Ok(buf.write_slice(self))
fn copy_to<B: MutBuf>(self, buf: &mut B) -> usize {
buf.write_slice(self)
}
}
impl Source for u8 {
fn copy_to<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
fn copy_to<B: MutBuf>(self, buf: &mut B) -> usize {
let src = [self];
Ok(buf.write_slice(&src))
buf.write_slice(&src)
}
}
impl<'a, T: ByteStr> Source for &'a T {
fn copy_to<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
fn copy_to<B: MutBuf>(self, buf: &mut B) -> usize {
let mut src = ByteStr::buf(self);
let mut res = 0;
@@ -207,22 +207,22 @@ impl<'a, T: ByteStr> Source for &'a T {
res += l;
}
Ok(res)
res
}
}
pub trait Sink {
fn copy_from<B: Buf>(self, buf: &mut B) -> Result<usize, BufError>;
fn copy_from<B: Buf>(self, buf: &mut B) -> usize;
}
impl<'a> Sink for &'a mut [u8] {
fn copy_from<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
Ok(buf.read_slice(self))
fn copy_from<B: Buf>(self, buf: &mut B) -> usize {
buf.read_slice(self)
}
}
impl<'a> Sink for &'a mut Vec<u8> {
fn copy_from<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
fn copy_from<B: Buf>(self, buf: &mut B) -> usize {
use std::slice;
self.clear();
@@ -246,7 +246,7 @@ impl<'a> Sink for &'a mut Vec<u8> {
self.set_len(rem);
}
Ok(rem)
rem
}
}
-13
View File
@@ -34,16 +34,3 @@ pub use str::{
use std::u32;
const MAX_CAPACITY: usize = u32::MAX as usize;
/*
*
* ===== BufError =====
*
*/
#[derive(Copy, Clone, Debug)]
pub enum BufError {
Underflow,
Overflow,
}
+2 -2
View File
@@ -259,8 +259,8 @@ fn concat(left: Bytes, right: Bytes) -> Rope {
fn concat_bytes(left: &Bytes, right: &Bytes, len: usize) -> Rope {
let mut buf = ByteBuf::mut_with_capacity(len);
buf.copy_from(left).ok().expect("unexpected error");
buf.copy_from(right).ok().expect("unexpected error");
buf.copy_from(left);
buf.copy_from(right);
return Rope::of(buf.flip().to_bytes());
}
+1 -5
View File
@@ -13,11 +13,7 @@ impl SeqByteStr {
/// The contents of the byte slice will be copied.
pub fn from_slice(bytes: &[u8]) -> SeqByteStr {
let mut buf = ByteBuf::mut_with_capacity(bytes.len());
if let Err(e) = buf.copy_from(bytes) {
panic!("failed to copy bytes from slice; err={:?}", e);
}
buf.copy_from(bytes);
buf.flip().to_seq_byte_str()
}
+2 -2
View File
@@ -17,7 +17,7 @@ pub fn test_initial_buf_empty() {
assert_eq!(buf.bytes(), b"hello world");
let view1 = buf.slice(0, 11);
view1.buf().copy_to(&mut dst).unwrap();
view1.buf().copy_to(&mut dst);
assert_eq!(dst, b"hello world");
assert_eq!(view1, buf.slice(0, 11));
@@ -47,7 +47,7 @@ pub fn test_append_buf_from_pool() {
assert_eq!(buf.bytes(), b"hello world");
let view1 = buf.slice(0, 11);
view1.buf().copy_to(&mut dst).unwrap();
view1.buf().copy_to(&mut dst);
assert_eq!(dst, b"hello world");
assert_eq!(view1, buf.slice(0, 11));
+2 -2
View File
@@ -35,7 +35,7 @@ pub fn test_vec_as_mut_buf() {
assert!(buf.mut_bytes().len() >= 64);
}
buf.copy_from(&b"zomg"[..]).unwrap();
buf.copy_from(&b"zomg"[..]);
assert_eq!(&buf, b"zomg");
@@ -43,7 +43,7 @@ pub fn test_vec_as_mut_buf() {
assert_eq!(buf.capacity(), 64);
for _ in 0..16 {
buf.copy_from(&b"zomg"[..]).unwrap();
buf.copy_from(&b"zomg"[..]);
}
assert_eq!(buf.len(), 68);
+10 -10
View File
@@ -20,10 +20,10 @@ pub fn test_initial_buf_empty() {
#[test]
pub fn test_byte_buf_bytes() {
let mut buf = ByteBuf::mut_with_capacity(32);
buf.copy_from(&b"hello "[..]).unwrap();
buf.copy_from(&b"hello "[..]);
assert_eq!(&b"hello "[..], buf.bytes());
buf.copy_from(&b"world"[..]).unwrap();
buf.copy_from(&b"world"[..]);
assert_eq!(&b"hello world"[..], buf.bytes());
let buf = buf.flip();
assert_eq!(&b"hello world"[..], buf.bytes());
@@ -33,37 +33,37 @@ pub fn test_byte_buf_bytes() {
pub fn test_byte_buf_read_write() {
let mut buf = ByteBuf::mut_with_capacity(32);
buf.copy_from(&b"hello world"[..]).unwrap();
buf.copy_from(&b"hello world"[..]);
assert_eq!(21, buf.remaining());
buf.copy_from(&b" goodbye"[..]).unwrap();
buf.copy_from(&b" goodbye"[..]);
assert_eq!(13, buf.remaining());
let mut buf = buf.flip();
let mut dst = [0; 5];
buf.mark();
assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
assert_eq!(5, buf.copy_to(&mut dst[..]));
assert_eq!(b"hello", &dst);
buf.reset();
assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
assert_eq!(5, buf.copy_to(&mut dst[..]));
assert_eq!(b"hello", &dst);
assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
assert_eq!(5, buf.copy_to(&mut dst[..]));
assert_eq!(b" worl", &dst);
let mut dst = [0; 2];
assert_eq!(2, buf.copy_to(&mut dst[..]).unwrap());
assert_eq!(2, buf.copy_to(&mut dst[..]));
assert_eq!(b"d ", &dst);
let mut dst = [0; 7];
assert_eq!(7, buf.copy_to(&mut dst[..]).unwrap());
assert_eq!(7, buf.copy_to(&mut dst[..]));
assert_eq!(b"goodbye", &dst);
let mut buf = buf.resume();
assert_eq!(13, buf.remaining());
buf.copy_from(&b" have fun"[..]).unwrap();
buf.copy_from(&b" have fun"[..]);
assert_eq!(4, buf.remaining());
let buf = buf.flip();
+1 -1
View File
@@ -26,7 +26,7 @@ fn test_pool_with_one_capacity() {
let mut dst = vec![];
buf.copy_to(&mut dst).unwrap();
buf.copy_to(&mut dst);
assert_eq!(&dst[..], b"Hello World");
+21 -21
View File
@@ -6,10 +6,10 @@ pub fn test_initial_buf_empty() {
assert_eq!(MutBuf::remaining(&buf), 16);
assert_eq!(Buf::remaining(&buf), 0);
let bytes_written = buf.copy_from(&[1, 2, 3][..]).unwrap();
let bytes_written = buf.copy_from(&[1, 2, 3][..]);
assert_eq!(bytes_written, 3);
let bytes_written = buf.copy_from(&[][..]).unwrap();
let bytes_written = buf.copy_from(&[][..]);
assert_eq!(bytes_written, 0);
assert_eq!(MutBuf::remaining(&buf), 13);
assert_eq!(Buf::remaining(&buf), 3);
@@ -18,11 +18,11 @@ pub fn test_initial_buf_empty() {
let mut out = [0u8; 3];
buf.mark();
let bytes_read = buf.copy_to(&mut out[..]).unwrap();;
let bytes_read = buf.copy_to(&mut out[..]);
assert_eq!(bytes_read, 3);
assert_eq!(out, [1, 2, 3]);
buf.reset();
let bytes_read = buf.copy_to(&mut out[..]).unwrap();;
let bytes_read = buf.copy_to(&mut out[..]);
assert_eq!(bytes_read, 3);
assert_eq!(out, [1, 2, 3]);
@@ -35,19 +35,19 @@ fn test_wrapping_write() {
let mut buf = RingBuf::new(16);
let mut out = [0;10];
buf.copy_from(&[42;12][..]).unwrap();
let bytes_read = buf.copy_to(&mut out[..]).unwrap();
buf.copy_from(&[42;12][..]);
let bytes_read = buf.copy_to(&mut out[..]);
assert_eq!(bytes_read, 10);
let bytes_written = buf.copy_from(&[23;8][..]).unwrap();
let bytes_written = buf.copy_from(&[23;8][..]);
assert_eq!(bytes_written, 8);
buf.mark();
let bytes_read = buf.copy_to(&mut out[..]).unwrap();
let bytes_read = buf.copy_to(&mut out[..]);
assert_eq!(bytes_read, 10);
assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
buf.reset();
let bytes_read = buf.copy_to(&mut out[..]).unwrap();
let bytes_read = buf.copy_to(&mut out[..]);
assert_eq!(bytes_read, 10);
assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
}
@@ -57,16 +57,16 @@ fn test_io_write_and_read() {
let mut buf = RingBuf::new(16);
let mut out = [0u8;8];
let written = buf.copy_from(&[1;8][..]).unwrap();
let written = buf.copy_from(&[1;8][..]);
assert_eq!(written, 8);
buf.copy_to(&mut out[..]).unwrap();
buf.copy_to(&mut out[..]);
assert_eq!(out, [1;8]);
let written = buf.copy_from(&[2;8][..]).unwrap();
let written = buf.copy_from(&[2;8][..]);
assert_eq!(written, 8);
let bytes_read = buf.copy_to(&mut out[..]).unwrap();
let bytes_read = buf.copy_to(&mut out[..]);
assert_eq!(bytes_read, 8);
assert_eq!(out, [2;8]);
}
@@ -75,10 +75,10 @@ fn test_io_write_and_read() {
#[should_panic]
fn test_wrap_reset() {
let mut buf = RingBuf::new(8);
buf.copy_from(&[1, 2, 3, 4, 5, 6, 7][..]).unwrap();
buf.copy_from(&[1, 2, 3, 4, 5, 6, 7][..]);
buf.mark();
buf.copy_to(&mut [0; 4][..]).unwrap();
buf.copy_from(&[1, 2, 3, 4][..]).unwrap();
buf.copy_to(&mut [0; 4][..]);
buf.copy_from(&[1, 2, 3, 4][..]);
buf.reset();
}
@@ -86,13 +86,13 @@ fn test_wrap_reset() {
// Test that writes across a mark/reset are preserved.
fn test_mark_write() {
let mut buf = RingBuf::new(8);
buf.copy_from(&[1, 2, 3, 4, 5, 6, 7][..]).unwrap();
buf.copy_from(&[1, 2, 3, 4, 5, 6, 7][..]);
buf.mark();
buf.copy_from(&[8][..]).unwrap();
buf.copy_from(&[8][..]);
buf.reset();
let mut buf2 = [0; 8];
buf.copy_to(&mut buf2[..]).unwrap();
buf.copy_to(&mut buf2[..]);
assert_eq!(buf2, [1, 2, 3, 4, 5, 6, 7, 8]);
}
@@ -101,7 +101,7 @@ fn test_mark_write() {
// full buffer to zero.
fn test_reset_full() {
let mut buf = RingBuf::new(8);
buf.copy_from(&[1, 2, 3, 4, 5, 6, 7, 8][..]).unwrap();
buf.copy_from(&[1, 2, 3, 4, 5, 6, 7, 8][..]);
assert_eq!(MutBuf::remaining(&buf), 0);
buf.mark();
buf.reset();
@@ -113,7 +113,7 @@ fn test_reset_full() {
// Test that "RingBuf::clear" does the full reset
fn test_clear() {
let mut buf = RingBuf::new(8);
buf.copy_from(&[0; 8][..]).unwrap();
buf.copy_from(&[0; 8][..]);
assert_eq!(MutBuf::remaining(&buf), 0);
assert_eq!(Buf::remaining(&buf), 8);
buf.clear();
+5 -5
View File
@@ -33,7 +33,7 @@ pub fn test_rope_round_trip() {
assert_eq!(4, rope.len());
let mut dst = vec![];
rope.buf().copy_to(&mut dst).unwrap();
rope.buf().copy_to(&mut dst);
assert_eq!(b"zomg", &dst[..]);
}
@@ -45,19 +45,19 @@ pub fn test_rope_slice() {
let bytes = Rope::from_slice(TEST_BYTES_1);
assert_eq!(TEST_BYTES_1.len(), bytes.len());
bytes.buf().copy_to(&mut dst).unwrap();
bytes.buf().copy_to(&mut dst);
assert_eq!(dst, TEST_BYTES_1);
let left = bytes.slice_to(250);
assert_eq!(250, left.len());
left.buf().copy_to(&mut dst).unwrap();
left.buf().copy_to(&mut dst);
assert_eq!(dst, &TEST_BYTES_1[..250]);
let right = bytes.slice_from(250);
assert_eq!(TEST_BYTES_1.len() - 250, right.len());
right.buf().copy_to(&mut dst).unwrap();
right.buf().copy_to(&mut dst);
assert_eq!(dst, &TEST_BYTES_1[250..]);
}
@@ -72,7 +72,7 @@ pub fn test_rope_concat_two_byte_str() {
assert_eq!(both.len(), TEST_BYTES_1.len() + TEST_BYTES_2.len());
both.buf().copy_to(&mut dst).unwrap();
both.buf().copy_to(&mut dst);
let mut expected = Vec::new();
expected.extend(TEST_BYTES_1.iter().cloned());
expected.extend(TEST_BYTES_2.iter().cloned());
+1 -1
View File
@@ -9,7 +9,7 @@ pub fn test_slice_round_trip() {
let s = SeqByteStr::from_slice(&src);
assert_eq!(2000, s.len());
s.buf().copy_to(&mut dst).unwrap();
s.buf().copy_to(&mut dst);
assert_eq!(dst, src);
}
+1 -1
View File
@@ -9,7 +9,7 @@ pub fn test_slice_round_trip() {
let s = SmallByteStr::from_slice(&src).unwrap();
assert_eq!(3, s.len());
s.buf().copy_to(&mut dst).unwrap();
s.buf().copy_to(&mut dst);
assert_eq!(dst, src);
}