From 270d2e2844f167fade46325585fcf6c16aa41704 Mon Sep 17 00:00:00 2001
From: Carl Lerche <me@carllerche.com>
Date: Wed, 8 Jun 2016 14:48:49 -0700
Subject: [PATCH] Freshen up buf API

---
 src/buf/byte.rs             |   5 +-
 src/buf/mod.rs              | 162 ++++++++++++++++++------------------
 src/buf/ring.rs             |  26 +-----
 src/lib.rs                  |   8 +-
 src/str/bytes.rs            |  37 +-------
 src/str/rope.rs             |  16 +---
 src/str/seq.rs              |   4 +-
 src/str/small.rs            |   2 +-
 test/test_buf.rs            |   6 +-
 test/test_buf_fill.rs       |   6 +-
 test/test_buf_take.rs       |   2 +-
 test/test_byte_buf.rs       |  20 ++---
 test/test_ring.rs           |  58 ++++++-------
 test/test_rope.rs           |  10 +--
 test/test_seq_byte_str.rs   |   2 +-
 test/test_small_byte_str.rs |   2 +-
 16 files changed, 146 insertions(+), 220 deletions(-)

diff --git a/src/buf/byte.rs b/src/buf/byte.rs
index 46c2e6d..e536ab2 100644
--- a/src/buf/byte.rs
+++ b/src/buf/byte.rs
@@ -1,5 +1,4 @@
-use {alloc, Bytes, SeqByteStr, MAX_CAPACITY};
-use traits::{Buf, MutBuf, MutBufExt, ByteStr};
+use {alloc, Buf, Bytes, MutBuf, SeqByteStr, MAX_CAPACITY};
 use std::{cmp, fmt, ptr};
 
 /*
@@ -24,7 +23,7 @@ impl ByteBuf {
     /// Create a new `ByteBuf` by copying the contents of the given slice.
     pub fn from_slice(bytes: &[u8]) -> ByteBuf {
         let mut buf = ByteBuf::mut_with_capacity(bytes.len());
-        buf.write(bytes).ok().expect("unexpected failure");
+        buf.write_slice(bytes);
         buf.flip()
     }
 
diff --git a/src/buf/mod.rs b/src/buf/mod.rs
index 9acb915..b52e99a 100644
--- a/src/buf/mod.rs
+++ b/src/buf/mod.rs
@@ -10,7 +10,7 @@ pub use self::ring::RingBuf;
 pub use self::slice::{SliceBuf, MutSliceBuf};
 pub use self::take::Take;
 
-use {BufError, RopeBuf};
+use {BufError, ByteStr, RopeBuf};
 use std::{cmp, fmt, io, ptr, usize};
 
 /// A trait for values that provide sequential read access to bytes.
@@ -31,6 +31,11 @@ pub trait Buf {
         self.remaining() > 0
     }
 
+    fn copy_to<S: Sink>(&mut self, dst: S) -> Result<usize, BufError>
+            where Self: Sized {
+        dst.copy_from(self)
+    }
+
     /// Read bytes from the `Buf` into the given slice and advance the cursor by
     /// the number of bytes read.
     /// Returns the number of bytes read.
@@ -80,14 +85,6 @@ pub trait Buf {
     }
 }
 
-/// An extension trait providing extra functions applicable to all `Buf` values.
-pub trait BufExt {
-
-    /// Read bytes from this Buf into the given sink and advance the cursor by
-    /// the number of bytes read.
-    fn read<S: Sink>(&mut self, dst: S) -> Result<usize, S::Error>;
-}
-
 /// A trait for values that provide sequential write access to bytes.
 pub trait MutBuf : Sized {
 
@@ -108,6 +105,11 @@ 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>
+            where Self: Sized {
+        src.copy_to(self)
+    }
+
     /// Write bytes from the given slice into the `MutBuf` and advance the
     /// cursor by the number of bytes written.
     /// Returns the number of bytes written.
@@ -151,77 +153,74 @@ pub trait MutBuf : Sized {
 
         len
     }
-
-    /// Write a single byte to the `MuBuf`
-    fn write_byte(&mut self, byte: u8) -> bool {
-        let src = [byte];
-
-        if self.write_slice(&src) == 0 {
-            return false;
-        }
-
-        true
-    }
-}
-
-/// An extension trait providing extra functions applicable to all `MutBuf` values.
-pub trait MutBufExt {
-
-    /// Write bytes from the given source into the current `MutBuf` and advance
-    /// the cursor by the number of bytes written.
-    fn write<S: Source>(&mut self, src: S) -> Result<usize, S::Error>;
 }
 
 /*
  *
- * ===== *Ext impls =====
+ * ===== Sink / Source =====
  *
  */
 
-impl<B: Buf> BufExt for B {
-    fn read<S: Sink>(&mut self, dst: S) -> Result<usize, S::Error> {
-        dst.sink(self)
+
+/// 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>;
+}
+
+impl<'a> Source for &'a [u8] {
+    fn copy_to<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
+        Ok(buf.write_slice(self))
     }
 }
 
-impl<B: MutBuf> MutBufExt for B {
-    fn write<S: Source>(&mut self, src: S) -> Result<usize, S::Error> {
-        src.fill(self)
+impl Source for u8 {
+    fn copy_to<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
+        let src = [self];
+        Ok(buf.write_slice(&src))
     }
 }
 
-/*
- *
- * ===== Sink / Source =====
- *
- */
+impl<'a, T: ByteStr> Source for &'a T {
+    fn copy_to<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
+        let mut src = ByteStr::buf(self);
+        let mut res = 0;
 
-/// A value that reads bytes from a Buf into itself
-pub trait Sink {
-    type Error;
+        while src.has_remaining() && buf.has_remaining() {
+            let l;
 
-    fn sink<B: Buf>(self, buf: &mut B) -> Result<usize, Self::Error>;
-}
+            unsafe {
+                let s = src.bytes();
+                let d = buf.mut_bytes();
+                l = cmp::min(s.len(), d.len());
 
-/// A value that writes bytes from itself into a `MutBuf`.
-pub trait Source {
-    type Error;
+                ptr::copy_nonoverlapping(
+                    s.as_ptr(),
+                    d.as_mut_ptr(),
+                    l);
+            }
+
+            src.advance(l);
+            unsafe { buf.advance(l); }
+
+            res += l;
+        }
 
-    fn fill<B: MutBuf>(self, buf: &mut B) -> Result<usize, Self::Error>;
+        Ok(res)
+    }
 }
 
-impl<'a> Sink for &'a mut [u8] {
-    type Error = BufError;
+pub trait Sink {
+    fn copy_from<B: Buf>(self, buf: &mut B) -> Result<usize, BufError>;
+}
 
-    fn sink<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
+impl<'a> Sink for &'a mut [u8] {
+    fn copy_from<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
         Ok(buf.read_slice(self))
     }
 }
 
 impl<'a> Sink for &'a mut Vec<u8> {
-    type Error = BufError;
-
-    fn sink<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
+    fn copy_from<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
         use std::slice;
 
         self.clear();
@@ -249,41 +248,44 @@ impl<'a> Sink for &'a mut Vec<u8> {
     }
 }
 
-impl<'a> Source for &'a [u8] {
-    type Error = BufError;
+/*
+ *
+ * ===== Read / Write =====
+ *
+ */
 
-    fn fill<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
-        Ok(buf.write_slice(self))
-    }
+pub trait ReadExt {
+    fn read_buf<B: MutBuf>(&mut self, buf: &mut B) -> io::Result<usize>;
 }
 
-impl<'a> Source for &'a Vec<u8> {
-    type Error = BufError;
+impl<T: io::Read> ReadExt for T {
+    fn read_buf<B: MutBuf>(&mut self, buf: &mut B) -> io::Result<usize> {
+        if !buf.has_remaining() {
+            return Ok(0);
+        }
+
+        unsafe {
+            let i = try!(self.read(buf.mut_bytes()));
 
-    fn fill<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
-        Ok(buf.write_slice(self.as_ref()))
+            buf.advance(i);
+            Ok(i)
+        }
     }
 }
 
+pub trait WriteExt {
+    fn write_buf<B: Buf>(&mut self, buf: &mut B) -> io::Result<usize>;
+}
 
-impl<'a, R: io::Read+'a> Source for &'a mut R {
-    type Error = io::Error;
-
-    fn fill<B: MutBuf>(self, buf: &mut B) -> Result<usize, io::Error> {
-        let mut cnt = 0;
-
-        while buf.has_remaining() {
-            let i = try!(self.read(unsafe { buf.mut_bytes() }));
-
-            if i == 0 {
-                break;
-            }
-
-            unsafe { buf.advance(i); }
-            cnt += i;
+impl<T: io::Write> WriteExt for T {
+    fn write_buf<B: Buf>(&mut self, buf: &mut B) -> io::Result<usize> {
+        if !buf.has_remaining() {
+            return Ok(0);
         }
 
-        Ok(cnt)
+        let i = try!(self.write(buf.bytes()));
+        buf.advance(i);
+        Ok(i)
     }
 }
 
diff --git a/src/buf/ring.rs b/src/buf/ring.rs
index 7308095..75ab492 100644
--- a/src/buf/ring.rs
+++ b/src/buf/ring.rs
@@ -1,5 +1,5 @@
 use {alloc, Buf, MutBuf};
-use std::{cmp, fmt, io, ptr};
+use std::{cmp, fmt, ptr};
 
 enum Mark {
     NoMark,
@@ -223,28 +223,4 @@ impl MutBuf for RingBuf {
     }
 }
 
-impl io::Read for RingBuf {
-    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        if !Buf::has_remaining(self) {
-            return Ok(0);
-        }
-
-        Ok(self.read_slice(buf))
-    }
-}
-
-impl io::Write for RingBuf {
-    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
-        if !MutBuf::has_remaining(self) {
-            return Ok(0);
-        }
-
-        Ok(self.write_slice(buf))
-    }
-
-    fn flush(&mut self) -> io::Result<()> {
-        Ok(())
-    }
-}
-
 unsafe impl Send for RingBuf { }
diff --git a/src/lib.rs b/src/lib.rs
index 6fa8922..d5eca9b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -7,18 +7,16 @@ pub mod str;
 
 pub use buf::{
     Buf,
-    BufExt,
     MutBuf,
-    MutBufExt,
     ByteBuf,
     MutByteBuf,
     RingBuf,
     ROByteBuf,
     SliceBuf,
     MutSliceBuf,
-    Source,
-    Sink,
     Take,
+    ReadExt,
+    WriteExt,
 };
 pub use str::{
     ByteStr,
@@ -35,7 +33,7 @@ use std::u32;
 
 pub mod traits {
     //! All traits are re-exported here to allow glob imports.
-    pub use {Buf, BufExt, MutBuf, MutBufExt, ByteStr, ToBytes};
+    pub use {Buf, MutBuf, ByteStr, ToBytes};
 }
 
 const MAX_CAPACITY: usize = u32::MAX as usize;
diff --git a/src/str/bytes.rs b/src/str/bytes.rs
index d5bf409..902e930 100644
--- a/src/str/bytes.rs
+++ b/src/str/bytes.rs
@@ -1,7 +1,7 @@
-use {ByteBuf, MutBuf, SmallByteStr, Source, BufError};
+use {ByteBuf, SmallByteStr};
 use traits::{Buf, ByteStr, ToBytes};
-use std::{cmp, fmt, mem, ops, ptr};
-use std::any::{Any, TypeId};
+use std::{fmt, mem, ops, ptr};
+use std::any::{TypeId};
 
 const INLINE: usize = 1;
 
@@ -198,37 +198,6 @@ impl Drop for Bytes {
 unsafe impl Send for Bytes { }
 unsafe impl Sync for Bytes { }
 
-impl<'a> Source for &'a Bytes {
-    type Error = BufError;
-
-    fn fill<B: MutBuf>(self, dst: &mut B) -> Result<usize, BufError> {
-        let mut src = ByteStr::buf(self);
-        let mut res = 0;
-
-        while src.has_remaining() && dst.has_remaining() {
-            let l;
-
-            unsafe {
-                let s = src.bytes();
-                let d = dst.mut_bytes();
-                l = cmp::min(s.len(), d.len());
-
-                ptr::copy_nonoverlapping(
-                    s.as_ptr(),
-                    d.as_mut_ptr(),
-                    l);
-            }
-
-            src.advance(l);
-            unsafe { dst.advance(l); }
-
-            res += l;
-        }
-
-        Ok(res)
-    }
-}
-
 trait ByteStrPriv {
 
     fn buf(&self) -> Box<Buf+'static>;
diff --git a/src/str/rope.rs b/src/str/rope.rs
index ca724df..d379855 100644
--- a/src/str/rope.rs
+++ b/src/str/rope.rs
@@ -1,5 +1,5 @@
-use {Bytes, ByteBuf, Source, BufError};
-use traits::{Buf, ByteStr, MutBuf, MutBufExt, ToBytes};
+use {Bytes, ByteBuf};
+use traits::{Buf, ByteStr, MutBuf, ToBytes};
 use std::{cmp, mem, ops};
 use std::sync::Arc;
 
@@ -176,14 +176,6 @@ impl Clone for Rope {
     }
 }
 
-impl<'a> Source for &'a Rope {
-    type Error = BufError;
-
-    fn fill<B: MutBuf>(self, _buf: &mut B) -> Result<usize, BufError> {
-        unimplemented!();
-    }
-}
-
 /*
  *
  * ===== Helper Fns =====
@@ -266,8 +258,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.write(left).ok().expect("unexpected error");
-    buf.write(right).ok().expect("unexpected error");
+    buf.copy_from(left).ok().expect("unexpected error");
+    buf.copy_from(right).ok().expect("unexpected error");
 
     return Rope::of(buf.flip().to_bytes());
 }
diff --git a/src/str/seq.rs b/src/str/seq.rs
index a8ab256..2b67e6b 100644
--- a/src/str/seq.rs
+++ b/src/str/seq.rs
@@ -1,4 +1,4 @@
-use {alloc, ByteBuf, MutBufExt, ByteStr, ROByteBuf, Rope, Bytes, ToBytes};
+use {alloc, ByteBuf, ByteStr, MutBuf, ROByteBuf, Rope, Bytes, ToBytes};
 use std::ops;
 
 pub struct SeqByteStr {
@@ -14,7 +14,7 @@ impl SeqByteStr {
     pub fn from_slice(bytes: &[u8]) -> SeqByteStr {
         let mut buf = ByteBuf::mut_with_capacity(bytes.len());
 
-        if let Err(e) = buf.write(bytes) {
+        if let Err(e) = buf.copy_from(bytes) {
             panic!("failed to copy bytes from slice; err={:?}", e);
         }
 
diff --git a/src/str/small.rs b/src/str/small.rs
index 949ac1b..9128a95 100644
--- a/src/str/small.rs
+++ b/src/str/small.rs
@@ -1,5 +1,5 @@
 use {Bytes, Rope};
-use traits::{Buf, MutBuf, ByteStr, ToBytes};
+use traits::{Buf, ByteStr, ToBytes};
 use std::{cmp, ops};
 
 /*
diff --git a/test/test_buf.rs b/test/test_buf.rs
index ecdf1db..2464476 100644
--- a/test/test_buf.rs
+++ b/test/test_buf.rs
@@ -1,4 +1,4 @@
-use bytes::{Buf, MutBuf, MutBufExt};
+use bytes::{Buf, MutBuf};
 use std::usize;
 use std::io::{Cursor};
 
@@ -35,7 +35,7 @@ pub fn test_vec_as_mut_buf() {
         assert!(buf.mut_bytes().len() >= 64);
     }
 
-    buf.write(&b"zomg"[..]).unwrap();
+    buf.copy_from(&b"zomg"[..]).unwrap();
 
     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.write(&b"zomg"[..]).unwrap();
+        buf.copy_from(&b"zomg"[..]).unwrap();
     }
 
     assert_eq!(buf.len(), 68);
diff --git a/test/test_buf_fill.rs b/test/test_buf_fill.rs
index d32a1ed..5f6bc94 100644
--- a/test/test_buf_fill.rs
+++ b/test/test_buf_fill.rs
@@ -2,12 +2,12 @@ use bytes::*;
 use std::io;
 
 #[test]
-pub fn test_filling_buf_from_reader() {
+pub fn test_readijng_buf_from_reader() {
     let mut reader = chunks(vec![b"foo", b"bar", b"baz"]);
     let mut buf = ByteBuf::mut_with_capacity(1024);
 
-    assert_eq!(9, buf.write(&mut reader).unwrap());
-    assert_eq!(b"foobarbaz".to_bytes(), buf.flip().to_bytes());
+    assert_eq!(3, reader.read_buf(&mut buf).unwrap());
+    assert_eq!(b"foo".to_bytes(), buf.flip().to_bytes());
 }
 
 fn chunks(chunks: Vec<&'static [u8]>) -> Chunked {
diff --git a/test/test_buf_take.rs b/test/test_buf_take.rs
index b2300c1..efb3744 100644
--- a/test/test_buf_take.rs
+++ b/test/test_buf_take.rs
@@ -6,7 +6,7 @@ pub fn test_take_from_buf() {
     let mut buf = Take::new(Cursor::new(b"hello world".to_vec()), 5);
     let mut res = vec![];
 
-    buf.read_to_end(&mut res);
+    buf.read_to_end(&mut res).unwrap();
 
     assert_eq!(&res, b"hello");
 }
diff --git a/test/test_byte_buf.rs b/test/test_byte_buf.rs
index a0e6937..110da8a 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.write(&b"hello "[..]).unwrap();
+    buf.copy_from(&b"hello "[..]).unwrap();
     assert_eq!(&b"hello "[..], buf.bytes());
 
-    buf.write(&b"world"[..]).unwrap();
+    buf.copy_from(&b"world"[..]).unwrap();
     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.write(&b"hello world"[..]).unwrap();
+    buf.copy_from(&b"hello world"[..]).unwrap();
     assert_eq!(21, buf.remaining());
 
-    buf.write(&b" goodbye"[..]).unwrap();
+    buf.copy_from(&b" goodbye"[..]).unwrap();
     assert_eq!(13, buf.remaining());
 
     let mut buf = buf.flip();
     let mut dst = [0; 5];
 
     buf.mark();
-    assert_eq!(5, buf.read(&mut dst[..]).unwrap());
+    assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
     assert_eq!(b"hello", &dst);
     buf.reset();
-    assert_eq!(5, buf.read(&mut dst[..]).unwrap());
+    assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
     assert_eq!(b"hello", &dst);
 
-    assert_eq!(5, buf.read(&mut dst[..]).unwrap());
+    assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
     assert_eq!(b" worl", &dst);
 
     let mut dst = [0; 2];
-    assert_eq!(2, buf.read(&mut dst[..]).unwrap());
+    assert_eq!(2, buf.copy_to(&mut dst[..]).unwrap());
     assert_eq!(b"d ", &dst);
 
     let mut dst = [0; 7];
-    assert_eq!(7, buf.read(&mut dst[..]).unwrap());
+    assert_eq!(7, buf.copy_to(&mut dst[..]).unwrap());
     assert_eq!(b"goodbye", &dst);
 
     let mut buf = buf.resume();
     assert_eq!(13, buf.remaining());
 
-    buf.write(&b" have fun"[..]).unwrap();
+    buf.copy_from(&b" have fun"[..]).unwrap();
     assert_eq!(4, buf.remaining());
 
     let buf = buf.flip();
diff --git a/test/test_ring.rs b/test/test_ring.rs
index c54423c..f42219f 100644
--- a/test/test_ring.rs
+++ b/test/test_ring.rs
@@ -1,18 +1,17 @@
-use bytes::RingBuf;
-
+use bytes::{RingBuf, Buf, MutBuf};
 
 #[test]
 pub fn test_initial_buf_empty() {
-    use bytes::traits::{Buf, BufExt, MutBuf, MutBufExt};
+    use bytes::traits::{Buf, MutBuf};
 
     let mut buf = RingBuf::new(16);
     assert_eq!(MutBuf::remaining(&buf), 16);
     assert_eq!(Buf::remaining(&buf), 0);
 
-    let bytes_written = buf.write(&[1, 2, 3][..]).unwrap();
+    let bytes_written = buf.copy_from(&[1, 2, 3][..]).unwrap();
     assert_eq!(bytes_written, 3);
 
-    let bytes_written = buf.write(&[][..]).unwrap();
+    let bytes_written = buf.copy_from(&[][..]).unwrap();
     assert_eq!(bytes_written, 0);
     assert_eq!(MutBuf::remaining(&buf), 13);
     assert_eq!(Buf::remaining(&buf), 3);
@@ -21,11 +20,11 @@ pub fn test_initial_buf_empty() {
     let mut out = [0u8; 3];
 
     buf.mark();
-    let bytes_read = buf.read(&mut out[..]).unwrap();;
+    let bytes_read = buf.copy_to(&mut out[..]).unwrap();;
     assert_eq!(bytes_read, 3);
     assert_eq!(out, [1, 2, 3]);
     buf.reset();
-    let bytes_read = buf.read(&mut out[..]).unwrap();;
+    let bytes_read = buf.copy_to(&mut out[..]).unwrap();;
     assert_eq!(bytes_read, 3);
     assert_eq!(out, [1, 2, 3]);
 
@@ -35,44 +34,41 @@ pub fn test_initial_buf_empty() {
 
 #[test]
 fn test_wrapping_write() {
-    use bytes::traits::{BufExt, MutBufExt};
     let mut buf = RingBuf::new(16);
     let mut out = [0;10];
 
-    buf.write(&[42;12][..]).unwrap();
-    let bytes_read = buf.read(&mut out[..]).unwrap();
+    buf.copy_from(&[42;12][..]).unwrap();
+    let bytes_read = buf.copy_to(&mut out[..]).unwrap();
     assert_eq!(bytes_read, 10);
 
-    let bytes_written = buf.write(&[23;8][..]).unwrap();
+    let bytes_written = buf.copy_from(&[23;8][..]).unwrap();
     assert_eq!(bytes_written, 8);
 
     buf.mark();
-    let bytes_read = buf.read(&mut out[..]).unwrap();
+    let bytes_read = buf.copy_to(&mut out[..]).unwrap();
     assert_eq!(bytes_read, 10);
     assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
     buf.reset();
-    let bytes_read = buf.read(&mut out[..]).unwrap();
+    let bytes_read = buf.copy_to(&mut out[..]).unwrap();
     assert_eq!(bytes_read, 10);
     assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
 }
 
 #[test]
 fn test_io_write_and_read() {
-    use std::io::{Read, Write};
-
     let mut buf = RingBuf::new(16);
-    let mut out = [0;8];
+    let mut out = [0u8;8];
 
-    let written = buf.write(&[1;8][..]).unwrap();
+    let written = buf.copy_from(&[1;8][..]).unwrap();
     assert_eq!(written, 8);
 
-    buf.read(&mut out).unwrap();
+    buf.copy_to(&mut out[..]).unwrap();
     assert_eq!(out, [1;8]);
 
-    let written = buf.write(&[2;8][..]).unwrap();
+    let written = buf.copy_from(&[2;8][..]).unwrap();
     assert_eq!(written, 8);
 
-    let bytes_read = buf.read(&mut out).unwrap();
+    let bytes_read = buf.copy_to(&mut out[..]).unwrap();
     assert_eq!(bytes_read, 8);
     assert_eq!(out, [2;8]);
 }
@@ -80,29 +76,25 @@ fn test_io_write_and_read() {
 #[test]
 #[should_panic]
 fn test_wrap_reset() {
-    use std::io::{Read, Write};
-
     let mut buf = RingBuf::new(8);
-    buf.write(&[1, 2, 3, 4, 5, 6, 7]).unwrap();
+    buf.copy_from(&[1, 2, 3, 4, 5, 6, 7][..]).unwrap();
     buf.mark();
-    buf.read(&mut [0; 4]).unwrap();
-    buf.write(&[1, 2, 3, 4]).unwrap();
+    buf.copy_to(&mut [0; 4][..]).unwrap();
+    buf.copy_from(&[1, 2, 3, 4][..]).unwrap();
     buf.reset();
 }
 
 #[test]
 // Test that writes across a mark/reset are preserved.
 fn test_mark_write() {
-    use std::io::{Read, Write};
-
     let mut buf = RingBuf::new(8);
-    buf.write(&[1, 2, 3, 4, 5, 6, 7]).unwrap();
+    buf.copy_from(&[1, 2, 3, 4, 5, 6, 7][..]).unwrap();
     buf.mark();
-    buf.write(&[8]).unwrap();
+    buf.copy_from(&[8][..]).unwrap();
     buf.reset();
 
     let mut buf2 = [0; 8];
-    buf.read(&mut buf2).unwrap();
+    buf.copy_to(&mut buf2[..]).unwrap();
     assert_eq!(buf2, [1, 2, 3, 4, 5, 6, 7, 8]);
 }
 
@@ -111,10 +103,9 @@ fn test_mark_write() {
 // full buffer to zero.
 fn test_reset_full() {
     use bytes::traits::MutBuf;
-    use std::io::Write;
 
     let mut buf = RingBuf::new(8);
-    buf.write(&[1, 2, 3, 4, 5, 6, 7, 8]).unwrap();
+    buf.copy_from(&[1, 2, 3, 4, 5, 6, 7, 8][..]).unwrap();
     assert_eq!(MutBuf::remaining(&buf), 0);
     buf.mark();
     buf.reset();
@@ -126,10 +117,9 @@ fn test_reset_full() {
 // Test that "RingBuf::clear" does the full reset
 fn test_clear() {
     use bytes::traits::{Buf, MutBuf};
-    use std::io::Write;
 
     let mut buf = RingBuf::new(8);
-    buf.write(&[0; 8]).unwrap();
+    buf.copy_from(&[0; 8][..]).unwrap();
     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 f478f0e..ded1cd5 100644
--- a/test/test_rope.rs
+++ b/test/test_rope.rs
@@ -34,7 +34,7 @@ pub fn test_rope_round_trip() {
     assert_eq!(4, rope.len());
 
     let mut dst = vec![];
-    rope.buf().read(&mut dst).unwrap();
+    rope.buf().copy_to(&mut dst).unwrap();
 
     assert_eq!(b"zomg", &dst[..]);
 }
@@ -46,19 +46,19 @@ pub fn test_rope_slice() {
     let bytes = Rope::from_slice(TEST_BYTES_1);
     assert_eq!(TEST_BYTES_1.len(), bytes.len());
 
-    bytes.buf().read(&mut dst).unwrap();
+    bytes.buf().copy_to(&mut dst).unwrap();
     assert_eq!(dst, TEST_BYTES_1);
 
     let left = bytes.slice_to(250);
     assert_eq!(250, left.len());
 
-    left.buf().read(&mut dst).unwrap();
+    left.buf().copy_to(&mut dst).unwrap();
     assert_eq!(dst, &TEST_BYTES_1[..250]);
 
     let right = bytes.slice_from(250);
     assert_eq!(TEST_BYTES_1.len() - 250, right.len());
 
-    right.buf().read(&mut dst).unwrap();
+    right.buf().copy_to(&mut dst).unwrap();
     assert_eq!(dst, &TEST_BYTES_1[250..]);
 }
 
@@ -73,7 +73,7 @@ pub fn test_rope_concat_two_byte_str() {
 
     assert_eq!(both.len(), TEST_BYTES_1.len() + TEST_BYTES_2.len());
 
-    both.buf().read(&mut dst).unwrap();
+    both.buf().copy_to(&mut dst).unwrap();
     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 23e959a..9e5508e 100644
--- a/test/test_seq_byte_str.rs
+++ b/test/test_seq_byte_str.rs
@@ -10,7 +10,7 @@ pub fn test_slice_round_trip() {
     let s = SeqByteStr::from_slice(&src);
     assert_eq!(2000, s.len());
 
-    s.buf().read(&mut dst).unwrap();
+    s.buf().copy_to(&mut dst).unwrap();
     assert_eq!(dst, src);
 }
 
diff --git a/test/test_small_byte_str.rs b/test/test_small_byte_str.rs
index 938b732..454e2d5 100644
--- a/test/test_small_byte_str.rs
+++ b/test/test_small_byte_str.rs
@@ -10,7 +10,7 @@ pub fn test_slice_round_trip() {
     let s = SmallByteStr::from_slice(&src).unwrap();
     assert_eq!(3, s.len());
 
-    s.buf().read(&mut dst).unwrap();
+    s.buf().copy_to(&mut dst).unwrap();
     assert_eq!(dst, src);
 }
 
-- 
GitLab