Freshen up buf API

This commit is contained in:
Carl Lerche
2016-06-08 14:48:49 -07:00
parent 12dfc417ce
commit 270d2e2844
16 changed files with 156 additions and 230 deletions
+2 -3
View File
@@ -1,5 +1,4 @@
use {alloc, Bytes, SeqByteStr, MAX_CAPACITY}; use {alloc, Buf, Bytes, MutBuf, SeqByteStr, MAX_CAPACITY};
use traits::{Buf, MutBuf, MutBufExt, ByteStr};
use std::{cmp, fmt, ptr}; use std::{cmp, fmt, ptr};
/* /*
@@ -24,7 +23,7 @@ impl ByteBuf {
/// Create a new `ByteBuf` by copying the contents of the given slice. /// Create a new `ByteBuf` by copying the contents of the given slice.
pub fn from_slice(bytes: &[u8]) -> ByteBuf { pub fn from_slice(bytes: &[u8]) -> ByteBuf {
let mut buf = ByteBuf::mut_with_capacity(bytes.len()); let mut buf = ByteBuf::mut_with_capacity(bytes.len());
buf.write(bytes).ok().expect("unexpected failure"); buf.write_slice(bytes);
buf.flip() buf.flip()
} }
+92 -90
View File
@@ -10,7 +10,7 @@ pub use self::ring::RingBuf;
pub use self::slice::{SliceBuf, MutSliceBuf}; pub use self::slice::{SliceBuf, MutSliceBuf};
pub use self::take::Take; pub use self::take::Take;
use {BufError, RopeBuf}; use {BufError, ByteStr, RopeBuf};
use std::{cmp, fmt, io, ptr, usize}; use std::{cmp, fmt, io, ptr, usize};
/// A trait for values that provide sequential read access to bytes. /// A trait for values that provide sequential read access to bytes.
@@ -31,6 +31,11 @@ pub trait Buf {
self.remaining() > 0 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 /// Read bytes from the `Buf` into the given slice and advance the cursor by
/// the number of bytes read. /// the number of bytes read.
/// Returns 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. /// A trait for values that provide sequential write access to bytes.
pub trait MutBuf : Sized { pub trait MutBuf : Sized {
@@ -108,6 +105,11 @@ pub trait MutBuf : Sized {
/// The returned byte slice may represent uninitialized memory. /// The returned byte slice may represent uninitialized memory.
unsafe fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8]; 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 /// Write bytes from the given slice into the `MutBuf` and advance the
/// cursor by the number of bytes written. /// cursor by the number of bytes written.
/// Returns the number of bytes written. /// Returns the number of bytes written.
@@ -151,43 +153,6 @@ pub trait MutBuf : Sized {
len 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 =====
*
*/
impl<B: Buf> BufExt for B {
fn read<S: Sink>(&mut self, dst: S) -> Result<usize, S::Error> {
dst.sink(self)
}
}
impl<B: MutBuf> MutBufExt for B {
fn write<S: Source>(&mut self, src: S) -> Result<usize, S::Error> {
src.fill(self)
}
} }
/* /*
@@ -196,32 +161,66 @@ impl<B: MutBuf> MutBufExt for B {
* *
*/ */
/// A value that reads bytes from a Buf into itself
pub trait Sink {
type Error;
fn sink<B: Buf>(self, buf: &mut B) -> Result<usize, Self::Error>;
}
/// A value that writes bytes from itself into a `MutBuf`. /// A value that writes bytes from itself into a `MutBuf`.
pub trait Source { pub trait Source {
type Error; fn copy_to<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError>;
}
fn fill<B: MutBuf>(self, buf: &mut B) -> Result<usize, Self::Error>; impl<'a> Source for &'a [u8] {
fn copy_to<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
Ok(buf.write_slice(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))
}
}
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;
while src.has_remaining() && buf.has_remaining() {
let l;
unsafe {
let s = src.bytes();
let d = buf.mut_bytes();
l = cmp::min(s.len(), d.len());
ptr::copy_nonoverlapping(
s.as_ptr(),
d.as_mut_ptr(),
l);
}
src.advance(l);
unsafe { buf.advance(l); }
res += l;
}
Ok(res)
}
}
pub trait Sink {
fn copy_from<B: Buf>(self, buf: &mut B) -> Result<usize, BufError>;
} }
impl<'a> Sink for &'a mut [u8] { impl<'a> Sink for &'a mut [u8] {
type Error = BufError; fn copy_from<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
fn sink<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
Ok(buf.read_slice(self)) Ok(buf.read_slice(self))
} }
} }
impl<'a> Sink for &'a mut Vec<u8> { impl<'a> Sink for &'a mut Vec<u8> {
type Error = BufError; fn copy_from<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
fn sink<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
use std::slice; use std::slice;
self.clear(); 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> { pub trait ReadExt {
Ok(buf.write_slice(self)) fn read_buf<B: MutBuf>(&mut self, buf: &mut B) -> io::Result<usize>;
}
} }
impl<'a> Source for &'a Vec<u8> { impl<T: io::Read> ReadExt for T {
type Error = BufError; fn read_buf<B: MutBuf>(&mut self, buf: &mut B) -> io::Result<usize> {
if !buf.has_remaining() {
fn fill<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> { return Ok(0);
Ok(buf.write_slice(self.as_ref()))
}
}
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;
} }
Ok(cnt) unsafe {
let i = try!(self.read(buf.mut_bytes()));
buf.advance(i);
Ok(i)
}
}
}
pub trait WriteExt {
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> io::Result<usize>;
}
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);
}
let i = try!(self.write(buf.bytes()));
buf.advance(i);
Ok(i)
} }
} }
+1 -25
View File
@@ -1,5 +1,5 @@
use {alloc, Buf, MutBuf}; use {alloc, Buf, MutBuf};
use std::{cmp, fmt, io, ptr}; use std::{cmp, fmt, ptr};
enum Mark { enum Mark {
NoMark, 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 { } unsafe impl Send for RingBuf { }
+3 -5
View File
@@ -7,18 +7,16 @@ pub mod str;
pub use buf::{ pub use buf::{
Buf, Buf,
BufExt,
MutBuf, MutBuf,
MutBufExt,
ByteBuf, ByteBuf,
MutByteBuf, MutByteBuf,
RingBuf, RingBuf,
ROByteBuf, ROByteBuf,
SliceBuf, SliceBuf,
MutSliceBuf, MutSliceBuf,
Source,
Sink,
Take, Take,
ReadExt,
WriteExt,
}; };
pub use str::{ pub use str::{
ByteStr, ByteStr,
@@ -35,7 +33,7 @@ use std::u32;
pub mod traits { pub mod traits {
//! All traits are re-exported here to allow glob imports. //! 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; const MAX_CAPACITY: usize = u32::MAX as usize;
+3 -34
View File
@@ -1,7 +1,7 @@
use {ByteBuf, MutBuf, SmallByteStr, Source, BufError}; use {ByteBuf, SmallByteStr};
use traits::{Buf, ByteStr, ToBytes}; use traits::{Buf, ByteStr, ToBytes};
use std::{cmp, fmt, mem, ops, ptr}; use std::{fmt, mem, ops, ptr};
use std::any::{Any, TypeId}; use std::any::{TypeId};
const INLINE: usize = 1; const INLINE: usize = 1;
@@ -198,37 +198,6 @@ impl Drop for Bytes {
unsafe impl Send for Bytes { } unsafe impl Send for Bytes { }
unsafe impl Sync 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 { trait ByteStrPriv {
fn buf(&self) -> Box<Buf+'static>; fn buf(&self) -> Box<Buf+'static>;
+4 -12
View File
@@ -1,5 +1,5 @@
use {Bytes, ByteBuf, Source, BufError}; use {Bytes, ByteBuf};
use traits::{Buf, ByteStr, MutBuf, MutBufExt, ToBytes}; use traits::{Buf, ByteStr, MutBuf, ToBytes};
use std::{cmp, mem, ops}; use std::{cmp, mem, ops};
use std::sync::Arc; 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 ===== * ===== Helper Fns =====
@@ -266,8 +258,8 @@ fn concat(left: Bytes, right: Bytes) -> Rope {
fn concat_bytes(left: &Bytes, right: &Bytes, len: usize) -> Rope { fn concat_bytes(left: &Bytes, right: &Bytes, len: usize) -> Rope {
let mut buf = ByteBuf::mut_with_capacity(len); let mut buf = ByteBuf::mut_with_capacity(len);
buf.write(left).ok().expect("unexpected error"); buf.copy_from(left).ok().expect("unexpected error");
buf.write(right).ok().expect("unexpected error"); buf.copy_from(right).ok().expect("unexpected error");
return Rope::of(buf.flip().to_bytes()); return Rope::of(buf.flip().to_bytes());
} }
+2 -2
View File
@@ -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; use std::ops;
pub struct SeqByteStr { pub struct SeqByteStr {
@@ -14,7 +14,7 @@ impl SeqByteStr {
pub fn from_slice(bytes: &[u8]) -> SeqByteStr { pub fn from_slice(bytes: &[u8]) -> SeqByteStr {
let mut buf = ByteBuf::mut_with_capacity(bytes.len()); 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); panic!("failed to copy bytes from slice; err={:?}", e);
} }
+1 -1
View File
@@ -1,5 +1,5 @@
use {Bytes, Rope}; use {Bytes, Rope};
use traits::{Buf, MutBuf, ByteStr, ToBytes}; use traits::{Buf, ByteStr, ToBytes};
use std::{cmp, ops}; use std::{cmp, ops};
/* /*
+3 -3
View File
@@ -1,4 +1,4 @@
use bytes::{Buf, MutBuf, MutBufExt}; use bytes::{Buf, MutBuf};
use std::usize; use std::usize;
use std::io::{Cursor}; use std::io::{Cursor};
@@ -35,7 +35,7 @@ pub fn test_vec_as_mut_buf() {
assert!(buf.mut_bytes().len() >= 64); assert!(buf.mut_bytes().len() >= 64);
} }
buf.write(&b"zomg"[..]).unwrap(); buf.copy_from(&b"zomg"[..]).unwrap();
assert_eq!(&buf, b"zomg"); assert_eq!(&buf, b"zomg");
@@ -43,7 +43,7 @@ pub fn test_vec_as_mut_buf() {
assert_eq!(buf.capacity(), 64); assert_eq!(buf.capacity(), 64);
for _ in 0..16 { for _ in 0..16 {
buf.write(&b"zomg"[..]).unwrap(); buf.copy_from(&b"zomg"[..]).unwrap();
} }
assert_eq!(buf.len(), 68); assert_eq!(buf.len(), 68);
+3 -3
View File
@@ -2,12 +2,12 @@ use bytes::*;
use std::io; use std::io;
#[test] #[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 reader = chunks(vec![b"foo", b"bar", b"baz"]);
let mut buf = ByteBuf::mut_with_capacity(1024); let mut buf = ByteBuf::mut_with_capacity(1024);
assert_eq!(9, buf.write(&mut reader).unwrap()); assert_eq!(3, reader.read_buf(&mut buf).unwrap());
assert_eq!(b"foobarbaz".to_bytes(), buf.flip().to_bytes()); assert_eq!(b"foo".to_bytes(), buf.flip().to_bytes());
} }
fn chunks(chunks: Vec<&'static [u8]>) -> Chunked { fn chunks(chunks: Vec<&'static [u8]>) -> Chunked {
+1 -1
View File
@@ -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 buf = Take::new(Cursor::new(b"hello world".to_vec()), 5);
let mut res = vec![]; let mut res = vec![];
buf.read_to_end(&mut res); buf.read_to_end(&mut res).unwrap();
assert_eq!(&res, b"hello"); assert_eq!(&res, b"hello");
} }
+10 -10
View File
@@ -20,10 +20,10 @@ pub fn test_initial_buf_empty() {
#[test] #[test]
pub fn test_byte_buf_bytes() { pub fn test_byte_buf_bytes() {
let mut buf = ByteBuf::mut_with_capacity(32); 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()); assert_eq!(&b"hello "[..], buf.bytes());
buf.write(&b"world"[..]).unwrap(); buf.copy_from(&b"world"[..]).unwrap();
assert_eq!(&b"hello world"[..], buf.bytes()); assert_eq!(&b"hello world"[..], buf.bytes());
let buf = buf.flip(); let buf = buf.flip();
assert_eq!(&b"hello world"[..], buf.bytes()); assert_eq!(&b"hello world"[..], buf.bytes());
@@ -33,37 +33,37 @@ pub fn test_byte_buf_bytes() {
pub fn test_byte_buf_read_write() { pub fn test_byte_buf_read_write() {
let mut buf = ByteBuf::mut_with_capacity(32); 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()); assert_eq!(21, buf.remaining());
buf.write(&b" goodbye"[..]).unwrap(); buf.copy_from(&b" goodbye"[..]).unwrap();
assert_eq!(13, buf.remaining()); assert_eq!(13, buf.remaining());
let mut buf = buf.flip(); let mut buf = buf.flip();
let mut dst = [0; 5]; let mut dst = [0; 5];
buf.mark(); buf.mark();
assert_eq!(5, buf.read(&mut dst[..]).unwrap()); assert_eq!(5, buf.copy_to(&mut dst[..]).unwrap());
assert_eq!(b"hello", &dst); assert_eq!(b"hello", &dst);
buf.reset(); 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!(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); assert_eq!(b" worl", &dst);
let mut dst = [0; 2]; 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); assert_eq!(b"d ", &dst);
let mut dst = [0; 7]; 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); assert_eq!(b"goodbye", &dst);
let mut buf = buf.resume(); let mut buf = buf.resume();
assert_eq!(13, buf.remaining()); assert_eq!(13, buf.remaining());
buf.write(&b" have fun"[..]).unwrap(); buf.copy_from(&b" have fun"[..]).unwrap();
assert_eq!(4, buf.remaining()); assert_eq!(4, buf.remaining());
let buf = buf.flip(); let buf = buf.flip();
+24 -34
View File
@@ -1,18 +1,17 @@
use bytes::RingBuf; use bytes::{RingBuf, Buf, MutBuf};
#[test] #[test]
pub fn test_initial_buf_empty() { pub fn test_initial_buf_empty() {
use bytes::traits::{Buf, BufExt, MutBuf, MutBufExt}; use bytes::traits::{Buf, MutBuf};
let mut buf = RingBuf::new(16); let mut buf = RingBuf::new(16);
assert_eq!(MutBuf::remaining(&buf), 16); assert_eq!(MutBuf::remaining(&buf), 16);
assert_eq!(Buf::remaining(&buf), 0); 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); 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!(bytes_written, 0);
assert_eq!(MutBuf::remaining(&buf), 13); assert_eq!(MutBuf::remaining(&buf), 13);
assert_eq!(Buf::remaining(&buf), 3); assert_eq!(Buf::remaining(&buf), 3);
@@ -21,11 +20,11 @@ pub fn test_initial_buf_empty() {
let mut out = [0u8; 3]; let mut out = [0u8; 3];
buf.mark(); 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!(bytes_read, 3);
assert_eq!(out, [1, 2, 3]); assert_eq!(out, [1, 2, 3]);
buf.reset(); 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!(bytes_read, 3);
assert_eq!(out, [1, 2, 3]); assert_eq!(out, [1, 2, 3]);
@@ -35,44 +34,41 @@ pub fn test_initial_buf_empty() {
#[test] #[test]
fn test_wrapping_write() { fn test_wrapping_write() {
use bytes::traits::{BufExt, MutBufExt};
let mut buf = RingBuf::new(16); let mut buf = RingBuf::new(16);
let mut out = [0;10]; let mut out = [0;10];
buf.write(&[42;12][..]).unwrap(); buf.copy_from(&[42;12][..]).unwrap();
let bytes_read = buf.read(&mut out[..]).unwrap(); let bytes_read = buf.copy_to(&mut out[..]).unwrap();
assert_eq!(bytes_read, 10); 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); assert_eq!(bytes_written, 8);
buf.mark(); 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!(bytes_read, 10);
assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]); assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
buf.reset(); 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!(bytes_read, 10);
assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]); assert_eq!(out, [42, 42, 23, 23, 23, 23, 23, 23, 23, 23]);
} }
#[test] #[test]
fn test_io_write_and_read() { fn test_io_write_and_read() {
use std::io::{Read, Write};
let mut buf = RingBuf::new(16); 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); assert_eq!(written, 8);
buf.read(&mut out).unwrap(); buf.copy_to(&mut out[..]).unwrap();
assert_eq!(out, [1;8]); assert_eq!(out, [1;8]);
let written = buf.write(&[2;8][..]).unwrap(); let written = buf.copy_from(&[2;8][..]).unwrap();
assert_eq!(written, 8); 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!(bytes_read, 8);
assert_eq!(out, [2;8]); assert_eq!(out, [2;8]);
} }
@@ -80,29 +76,25 @@ fn test_io_write_and_read() {
#[test] #[test]
#[should_panic] #[should_panic]
fn test_wrap_reset() { fn test_wrap_reset() {
use std::io::{Read, Write};
let mut buf = RingBuf::new(8); 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.mark();
buf.read(&mut [0; 4]).unwrap(); buf.copy_to(&mut [0; 4][..]).unwrap();
buf.write(&[1, 2, 3, 4]).unwrap(); buf.copy_from(&[1, 2, 3, 4][..]).unwrap();
buf.reset(); buf.reset();
} }
#[test] #[test]
// Test that writes across a mark/reset are preserved. // Test that writes across a mark/reset are preserved.
fn test_mark_write() { fn test_mark_write() {
use std::io::{Read, Write};
let mut buf = RingBuf::new(8); 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.mark();
buf.write(&[8]).unwrap(); buf.copy_from(&[8][..]).unwrap();
buf.reset(); buf.reset();
let mut buf2 = [0; 8]; 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]); assert_eq!(buf2, [1, 2, 3, 4, 5, 6, 7, 8]);
} }
@@ -111,10 +103,9 @@ fn test_mark_write() {
// full buffer to zero. // full buffer to zero.
fn test_reset_full() { fn test_reset_full() {
use bytes::traits::MutBuf; use bytes::traits::MutBuf;
use std::io::Write;
let mut buf = RingBuf::new(8); 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); assert_eq!(MutBuf::remaining(&buf), 0);
buf.mark(); buf.mark();
buf.reset(); buf.reset();
@@ -126,10 +117,9 @@ fn test_reset_full() {
// Test that "RingBuf::clear" does the full reset // Test that "RingBuf::clear" does the full reset
fn test_clear() { fn test_clear() {
use bytes::traits::{Buf, MutBuf}; use bytes::traits::{Buf, MutBuf};
use std::io::Write;
let mut buf = RingBuf::new(8); 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!(MutBuf::remaining(&buf), 0);
assert_eq!(Buf::remaining(&buf), 8); assert_eq!(Buf::remaining(&buf), 8);
buf.clear(); buf.clear();
+5 -5
View File
@@ -34,7 +34,7 @@ pub fn test_rope_round_trip() {
assert_eq!(4, rope.len()); assert_eq!(4, rope.len());
let mut dst = vec![]; let mut dst = vec![];
rope.buf().read(&mut dst).unwrap(); rope.buf().copy_to(&mut dst).unwrap();
assert_eq!(b"zomg", &dst[..]); assert_eq!(b"zomg", &dst[..]);
} }
@@ -46,19 +46,19 @@ pub fn test_rope_slice() {
let bytes = Rope::from_slice(TEST_BYTES_1); let bytes = Rope::from_slice(TEST_BYTES_1);
assert_eq!(TEST_BYTES_1.len(), bytes.len()); 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); assert_eq!(dst, TEST_BYTES_1);
let left = bytes.slice_to(250); let left = bytes.slice_to(250);
assert_eq!(250, left.len()); 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]); assert_eq!(dst, &TEST_BYTES_1[..250]);
let right = bytes.slice_from(250); let right = bytes.slice_from(250);
assert_eq!(TEST_BYTES_1.len() - 250, right.len()); 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..]); 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()); 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(); let mut expected = Vec::new();
expected.extend(TEST_BYTES_1.iter().cloned()); expected.extend(TEST_BYTES_1.iter().cloned());
expected.extend(TEST_BYTES_2.iter().cloned()); expected.extend(TEST_BYTES_2.iter().cloned());
+1 -1
View File
@@ -10,7 +10,7 @@ pub fn test_slice_round_trip() {
let s = SeqByteStr::from_slice(&src); let s = SeqByteStr::from_slice(&src);
assert_eq!(2000, s.len()); assert_eq!(2000, s.len());
s.buf().read(&mut dst).unwrap(); s.buf().copy_to(&mut dst).unwrap();
assert_eq!(dst, src); assert_eq!(dst, src);
} }
+1 -1
View File
@@ -10,7 +10,7 @@ pub fn test_slice_round_trip() {
let s = SmallByteStr::from_slice(&src).unwrap(); let s = SmallByteStr::from_slice(&src).unwrap();
assert_eq!(3, s.len()); assert_eq!(3, s.len());
s.buf().read(&mut dst).unwrap(); s.buf().copy_to(&mut dst).unwrap();
assert_eq!(dst, src); assert_eq!(dst, src);
} }