mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-16 00:00:15 +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};
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user