Tweak Sink / Source

This commit is contained in:
Carl Lerche
2016-10-07 15:37:12 -07:00
parent 6f97d04077
commit a4bfc63de7
4 changed files with 26 additions and 43 deletions
+19 -38
View File
@@ -27,10 +27,10 @@ pub trait Buf {
self.remaining() > 0 self.remaining() > 0
} }
fn copy_to<S: Sink>(&mut self, dst: S) -> usize fn copy_to<S: Sink + ?Sized>(&mut self, dst: &mut S) -> usize
where Self: Sized { where Self: Sized {
let rem = self.remaining(); let rem = self.remaining();
dst.copy_from(self); dst.sink(self);
rem - self.remaining() rem - self.remaining()
} }
@@ -206,7 +206,7 @@ pub trait MutBuf {
fn copy_from<S: Source>(&mut self, src: S) -> usize fn copy_from<S: Source>(&mut self, src: S) -> usize
where Self: Sized { where Self: Sized {
let rem = self.remaining(); let rem = self.remaining();
src.copy_to(self); src.source(self);
rem - self.remaining() rem - self.remaining()
} }
@@ -440,36 +440,37 @@ impl<'a> IntoBuf for &'a () {
/// 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 {
fn copy_to<B: MutBuf>(self, buf: &mut B); /// Copy data from self into destination buffer
fn source<B: MutBuf>(self, buf: &mut B);
} }
impl<'a> Source for &'a [u8] { impl<'a> Source for &'a [u8] {
fn copy_to<B: MutBuf>(self, buf: &mut B) { fn source<B: MutBuf>(self, buf: &mut B) {
buf.write_slice(self); buf.write_slice(self);
} }
} }
impl Source for u8 { impl Source for u8 {
fn copy_to<B: MutBuf>(self, buf: &mut B) { fn source<B: MutBuf>(self, buf: &mut B) {
let src = [self]; let src = [self];
buf.write_slice(&src); buf.write_slice(&src);
} }
} }
impl Source for Bytes { impl Source for Bytes {
fn copy_to<B: MutBuf>(self, buf: &mut B) { fn source<B: MutBuf>(self, buf: &mut B) {
Source::copy_to(&self, buf); Source::source(&self, buf);
} }
} }
impl<'a> Source for &'a Bytes { impl<'a> Source for &'a Bytes {
fn copy_to<B: MutBuf>(self, buf: &mut B) { fn source<B: MutBuf>(self, buf: &mut B) {
Source::copy_to(self.buf(), buf); Source::source(&mut self.buf(), buf);
} }
} }
impl<T: Buf> Source for T { impl<'a, T: Buf> Source for &'a mut T {
fn copy_to<B: MutBuf>(mut self, buf: &mut B) { fn source<B: MutBuf>(mut self, buf: &mut B) {
while self.has_remaining() && buf.has_remaining() { while self.has_remaining() && buf.has_remaining() {
let l; let l;
@@ -491,38 +492,18 @@ impl<T: Buf> Source for T {
} }
pub trait Sink { pub trait Sink {
fn copy_from<B: Buf>(self, buf: &mut B); fn sink<B: Buf>(&mut self, buf: &mut B);
} }
impl<'a> Sink for &'a mut [u8] { impl Sink for [u8] {
fn copy_from<B: Buf>(self, buf: &mut B) { fn sink<B: Buf>(&mut self, buf: &mut B) {
buf.read_slice(self); buf.read_slice(self);
} }
} }
impl<'a> Sink for &'a mut Vec<u8> { impl<T: MutBuf> Sink for T {
fn copy_from<B: Buf>(self, buf: &mut B) { fn sink<B: Buf>(&mut self, buf: &mut B) {
use std::slice; Source::source(buf, self)
self.clear();
let rem = buf.remaining();
// Ensure that the vec is big enough
if rem > self.capacity() {
// current length is 0, so reserve completely
self.reserve(rem);
}
debug_assert!(rem <= self.capacity());
unsafe {
{
let dst = &mut self[..];
buf.read_slice(slice::from_raw_parts_mut(dst.as_mut_ptr(), rem));
}
self.set_len(rem);
}
} }
} }
+4 -4
View File
@@ -285,11 +285,11 @@ impl Node {
} }
impl<'a> Source for &'a Node { impl<'a> Source for &'a Node {
fn copy_to<B: MutBuf>(self, buf: &mut B) { fn source<B: MutBuf>(self, buf: &mut B) {
match *self { match *self {
Node::Seq(ref b) => b.as_slice().copy_to(buf), Node::Seq(ref b) => b.as_slice().source(buf),
Node::Small(ref b) => b.as_ref().copy_to(buf), Node::Small(ref b) => b.as_ref().source(buf),
Node::Rope(ref b) => b.buf().copy_to(buf), Node::Rope(ref b) => b.buf().source(buf),
Node::Empty => unreachable!(), Node::Empty => unreachable!(),
} }
} }
+1 -1
View File
@@ -54,6 +54,6 @@ fn test_vec_sink_capacity() {
sink.reserve(16); sink.reserve(16);
assert!(sink.capacity() >= 16, "Capacity {} must be at least 16", sink.capacity()); assert!(sink.capacity() >= 16, "Capacity {} must be at least 16", sink.capacity());
let mut source = Cursor::new(b"0123456789abcdef0123456789abcdef"); let mut source = Cursor::new(b"0123456789abcdef0123456789abcdef");
sink.copy_from(&mut source); sink.sink(&mut source);
assert!(sink.len() <= sink.capacity(), "Length {} must be less than or equal to capacity {}", sink.len(), sink.capacity()); assert!(sink.len() <= sink.capacity(), "Length {} must be less than or equal to capacity {}", sink.len(), sink.capacity());
} }
+2
View File
@@ -38,12 +38,14 @@ pub fn test_rope_slice() {
let left = bytes.slice_to(250); let left = bytes.slice_to(250);
assert_eq!(250, left.len()); assert_eq!(250, left.len());
dst.clear();
left.buf().copy_to(&mut dst); left.buf().copy_to(&mut dst);
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());
dst.clear();
right.buf().copy_to(&mut dst); right.buf().copy_to(&mut dst);
// assert_eq!(dst, &TEST_BYTES_1[250..]); // assert_eq!(dst, &TEST_BYTES_1[250..]);
} }