diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 4b1e3eb275583c31eaa9823f067540027feae082..a69ad3fce082694ce57e66c20b97c2092931374e 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<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 } } diff --git a/src/lib.rs b/src/lib.rs index aa1f80a564422df408dd56f7bbd5a1d71743bd17..f79ea9b230c3bd6a99a82225136e56f621119e96 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 693b88925451cbab1f65f2d65a573c4429138217..6ba3e3582be33dd4057135847da1149ed6b4581e 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 b36456ad1d513d265c931bb4cacf4c84d41b3a70..831869bd8588a27a724ffa2fda42af3357141e9b 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 1b4db54ecfc1e428084e1856497f11284f3cf62d..1843a9541d7c913cd9db6544dd66602e57f26eef 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 24644761b7d0d553d3002e6dfce7ad98be338630..a59872f86f26316bcdfe28b8f3941b07093b82f6 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 76709c44b4c1d4130dcf6add0b7a81fe817a4b2d..93e5453f7029e140f77daaf5fcbfab1c78c0c189 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 37d7b10e88a337fcc2e5d692b26f98382bf4f847..1e3446b1155015772b1824a8eb5a2c4d2481b6b6 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 74dc6bc433feea55e428b51494facf9b9ac31ed2..0ef9850c1c34b47d399a2f522206355541afc00d 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 f9a2f0ca04f4b7c012942d4742526ce4548d5ca0..ffd85e20f220e31e669a964533953405b40c3e13 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 0a8dd8139ca972b3f31b85e315a3fbd33e0aaec4..b546714c39d2ebe48d56d83abd659fa217d431a8 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 07820a8df55ee259d44609b4a366691e3a425e17..6740e0976a0e5e77611c5fec857af8a50c51d9cc 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); }