mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-11 00:00:23 +02:00
Freshen up buf API
This commit is contained in:
+2
-3
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
+92
-90
@@ -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,43 +153,6 @@ 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 =====
|
||||
*
|
||||
*/
|
||||
|
||||
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`.
|
||||
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] {
|
||||
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> {
|
||||
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;
|
||||
|
||||
fn fill<B: MutBuf>(self, buf: &mut B) -> Result<usize, BufError> {
|
||||
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;
|
||||
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);
|
||||
}
|
||||
|
||||
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
@@ -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 { }
|
||||
|
||||
+3
-5
@@ -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;
|
||||
|
||||
+3
-34
@@ -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>;
|
||||
|
||||
+4
-12
@@ -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());
|
||||
}
|
||||
|
||||
+2
-2
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
use {Bytes, Rope};
|
||||
use traits::{Buf, MutBuf, ByteStr, ToBytes};
|
||||
use traits::{Buf, ByteStr, ToBytes};
|
||||
use std::{cmp, ops};
|
||||
|
||||
/*
|
||||
|
||||
+3
-3
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
+10
-10
@@ -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();
|
||||
|
||||
+24
-34
@@ -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();
|
||||
|
||||
+5
-5
@@ -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());
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user