mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-10 00:00:09 +02:00
Tweak Sink / Source
This commit is contained in:
+19
-38
@@ -27,10 +27,10 @@ pub trait Buf {
|
||||
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 {
|
||||
let rem = self.remaining();
|
||||
dst.copy_from(self);
|
||||
dst.sink(self);
|
||||
rem - self.remaining()
|
||||
}
|
||||
|
||||
@@ -206,7 +206,7 @@ pub trait MutBuf {
|
||||
fn copy_from<S: Source>(&mut self, src: S) -> usize
|
||||
where Self: Sized {
|
||||
let rem = self.remaining();
|
||||
src.copy_to(self);
|
||||
src.source(self);
|
||||
rem - self.remaining()
|
||||
}
|
||||
|
||||
@@ -440,36 +440,37 @@ impl<'a> IntoBuf for &'a () {
|
||||
|
||||
/// A value that writes bytes from itself into a `MutBuf`.
|
||||
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] {
|
||||
fn copy_to<B: MutBuf>(self, buf: &mut B) {
|
||||
fn source<B: MutBuf>(self, buf: &mut B) {
|
||||
buf.write_slice(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for u8 {
|
||||
fn copy_to<B: MutBuf>(self, buf: &mut B) {
|
||||
fn source<B: MutBuf>(self, buf: &mut B) {
|
||||
let src = [self];
|
||||
buf.write_slice(&src);
|
||||
}
|
||||
}
|
||||
|
||||
impl Source for Bytes {
|
||||
fn copy_to<B: MutBuf>(self, buf: &mut B) {
|
||||
Source::copy_to(&self, buf);
|
||||
fn source<B: MutBuf>(self, buf: &mut B) {
|
||||
Source::source(&self, buf);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Source for &'a Bytes {
|
||||
fn copy_to<B: MutBuf>(self, buf: &mut B) {
|
||||
Source::copy_to(self.buf(), buf);
|
||||
fn source<B: MutBuf>(self, buf: &mut B) {
|
||||
Source::source(&mut self.buf(), buf);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Buf> Source for T {
|
||||
fn copy_to<B: MutBuf>(mut self, buf: &mut B) {
|
||||
impl<'a, T: Buf> Source for &'a mut T {
|
||||
fn source<B: MutBuf>(mut self, buf: &mut B) {
|
||||
while self.has_remaining() && buf.has_remaining() {
|
||||
let l;
|
||||
|
||||
@@ -491,38 +492,18 @@ impl<T: Buf> Source for T {
|
||||
}
|
||||
|
||||
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] {
|
||||
fn copy_from<B: Buf>(self, buf: &mut B) {
|
||||
impl Sink for [u8] {
|
||||
fn sink<B: Buf>(&mut self, buf: &mut B) {
|
||||
buf.read_slice(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Sink for &'a mut Vec<u8> {
|
||||
fn copy_from<B: Buf>(self, buf: &mut B) {
|
||||
use std::slice;
|
||||
|
||||
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);
|
||||
}
|
||||
impl<T: MutBuf> Sink for T {
|
||||
fn sink<B: Buf>(&mut self, buf: &mut B) {
|
||||
Source::source(buf, self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -285,11 +285,11 @@ impl 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 {
|
||||
Node::Seq(ref b) => b.as_slice().copy_to(buf),
|
||||
Node::Small(ref b) => b.as_ref().copy_to(buf),
|
||||
Node::Rope(ref b) => b.buf().copy_to(buf),
|
||||
Node::Seq(ref b) => b.as_slice().source(buf),
|
||||
Node::Small(ref b) => b.as_ref().source(buf),
|
||||
Node::Rope(ref b) => b.buf().source(buf),
|
||||
Node::Empty => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -54,6 +54,6 @@ fn test_vec_sink_capacity() {
|
||||
sink.reserve(16);
|
||||
assert!(sink.capacity() >= 16, "Capacity {} must be at least 16", sink.capacity());
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -38,12 +38,14 @@ pub fn test_rope_slice() {
|
||||
let left = bytes.slice_to(250);
|
||||
assert_eq!(250, left.len());
|
||||
|
||||
dst.clear();
|
||||
left.buf().copy_to(&mut dst);
|
||||
assert_eq!(dst, &TEST_BYTES_1[..250]);
|
||||
|
||||
let right = bytes.slice_from(250);
|
||||
assert_eq!(TEST_BYTES_1.len() - 250, right.len());
|
||||
|
||||
dst.clear();
|
||||
right.buf().copy_to(&mut dst);
|
||||
// assert_eq!(dst, &TEST_BYTES_1[250..]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user