From b6a424d8921cb53b1426623b5734a4dc604924e5 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 5 Aug 2016 21:54:29 -0700 Subject: [PATCH] Get rid of BufError --- src/buf/mod.rs | 30 +++++++++++++------------- src/lib.rs | 13 ------------ src/str/rope.rs | 4 ++-- src/str/seq.rs | 6 +----- test/test_append.rs | 4 ++-- test/test_buf.rs | 4 ++-- test/test_byte_buf.rs | 20 +++++++++--------- test/test_pool.rs | 2 +- test/test_ring.rs | 42 ++++++++++++++++++------------------- test/test_rope.rs | 10 ++++----- test/test_seq_byte_str.rs | 2 +- test/test_small_byte_str.rs | 2 +- 12 files changed, 61 insertions(+), 78 deletions(-) diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 4b1e3eb..a69ad3f 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -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(&mut self, dst: S) -> Result + fn copy_to(&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(&mut self, src: S) -> Result + fn copy_from(&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(self, buf: &mut B) -> Result; + fn copy_to(self, buf: &mut B) -> usize; } impl<'a> Source for &'a [u8] { - fn copy_to(self, buf: &mut B) -> Result { - Ok(buf.write_slice(self)) + fn copy_to(self, buf: &mut B) -> usize { + buf.write_slice(self) } } impl Source for u8 { - fn copy_to(self, buf: &mut B) -> Result { + fn copy_to(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(self, buf: &mut B) -> Result { + fn copy_to(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(self, buf: &mut B) -> Result; + fn copy_from(self, buf: &mut B) -> usize; } impl<'a> Sink for &'a mut [u8] { - fn copy_from(self, buf: &mut B) -> Result { - Ok(buf.read_slice(self)) + fn copy_from(self, buf: &mut B) -> usize { + buf.read_slice(self) } } impl<'a> Sink for &'a mut Vec { - fn copy_from(self, buf: &mut B) -> Result { + fn copy_from(self, buf: &mut B) -> usize { use std::slice; self.clear(); @@ -246,7 +246,7 @@ impl<'a> Sink for &'a mut Vec { self.set_len(rem); } - Ok(rem) + rem } } diff --git a/src/lib.rs b/src/lib.rs index aa1f80a..f79ea9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, -} diff --git a/src/str/rope.rs b/src/str/rope.rs index 693b889..6ba3e35 100644 --- a/src/str/rope.rs +++ b/src/str/rope.rs @@ -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()); } diff --git a/src/str/seq.rs b/src/str/seq.rs index b36456a..831869b 100644 --- a/src/str/seq.rs +++ b/src/str/seq.rs @@ -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() } diff --git a/test/test_append.rs b/test/test_append.rs index 1b4db54..1843a95 100644 --- a/test/test_append.rs +++ b/test/test_append.rs @@ -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)); diff --git a/test/test_buf.rs b/test/test_buf.rs index 2464476..a59872f 100644 --- a/test/test_buf.rs +++ b/test/test_buf.rs @@ -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); diff --git a/test/test_byte_buf.rs b/test/test_byte_buf.rs index 76709c4..93e5453 100644 --- a/test/test_byte_buf.rs +++ b/test/test_byte_buf.rs @@ -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(); diff --git a/test/test_pool.rs b/test/test_pool.rs index 37d7b10..1e3446b 100644 --- a/test/test_pool.rs +++ b/test/test_pool.rs @@ -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"); diff --git a/test/test_ring.rs b/test/test_ring.rs index 74dc6bc..0ef9850 100644 --- a/test/test_ring.rs +++ b/test/test_ring.rs @@ -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(); diff --git a/test/test_rope.rs b/test/test_rope.rs index f9a2f0c..ffd85e2 100644 --- a/test/test_rope.rs +++ b/test/test_rope.rs @@ -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()); diff --git a/test/test_seq_byte_str.rs b/test/test_seq_byte_str.rs index 0a8dd81..b546714 100644 --- a/test/test_seq_byte_str.rs +++ b/test/test_seq_byte_str.rs @@ -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); } diff --git a/test/test_small_byte_str.rs b/test/test_small_byte_str.rs index 07820a8..6740e09 100644 --- a/test/test_small_byte_str.rs +++ b/test/test_small_byte_str.rs @@ -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); }