Remove buf::Source in favor of buf::IntoBuf

The `Source` trait was essentially covering the same case as `IntoBuf`,
so remove it.

While technically a breaking change, this should not have any impact due
to:

1) There are no reverse dependencies that currently depend on `bytes`
2) Source was not supposed to be implemented externally
3) IntoBuf provides the same implementations as `Source`

Given these points, the change should be safe to apply.
This commit is contained in:
Carl Lerche
2017-03-07 11:30:08 -08:00
parent d70f575afd
commit 06b94c55b0
6 changed files with 123 additions and 154 deletions
+71 -3
View File
@@ -1,4 +1,4 @@
use super::{Source, Writer};
use super::{IntoBuf, Writer};
use byteorder::ByteOrder;
use iovec::IoVec;
@@ -219,8 +219,30 @@ pub trait BufMut {
/// # Panics
///
/// Panics if `self` does not have enough capacity to contain `src`.
fn put<S: Source>(&mut self, src: S) where Self: Sized {
src.copy_to_buf(self);
fn put<T: IntoBuf>(&mut self, src: T) where Self: Sized {
use super::Buf;
let mut src = src.into_buf();
assert!(self.remaining_mut() >= src.remaining());
while src.has_remaining() {
let l;
unsafe {
let s = src.bytes();
let d = self.bytes_mut();
l = cmp::min(s.len(), d.len());
ptr::copy_nonoverlapping(
s.as_ptr(),
d.as_mut_ptr(),
l);
}
src.advance(l);
unsafe { self.advance_mut(l); }
}
}
/// Transfer bytes into `self` from `src` and advance the cursor by the
@@ -268,6 +290,52 @@ pub trait BufMut {
}
}
/// Writes an unsigned 8 bit integer to `self`.
///
/// The current position is advanced by 1.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_u8(0x01);
/// assert_eq!(buf, b"\x01");
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining capacity in
/// `self`.
fn put_u8(&mut self, n: u8) {
let src = [n];
self.put_slice(&src);
}
/// Writes a signed 8 bit integer to `self`.
///
/// The current position is advanced by 1.
///
/// # Examples
///
/// ```
/// use bytes::BufMut;
///
/// let mut buf = vec![];
/// buf.put_i8(0x01);
/// assert_eq!(buf, b"\x01");
/// ```
///
/// # Panics
///
/// This function panics if there is not enough remaining capacity in
/// `self`.
fn put_i8(&mut self, n: i8) {
let src = [n as u8];
self.put_slice(&src)
}
/// Writes an unsigned 16 bit integer to `self` in the specified byte order.
///
/// The current position is advanced by 2.