Get rid of BufError

This commit is contained in:
Carl Lerche
2016-08-05 21:54:29 -07:00
parent 6529f6392a
commit b6a424d892
12 changed files with 61 additions and 78 deletions
+15 -15
View File
@@ -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
}
}
-13
View File
@@ -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,
}
+2 -2
View File
@@ -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());
}
+1 -5
View File
@@ -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()
}