Remove IntoBuf/FromBuf (#288)

As consequence Buf::collect is removed as well, which is replaced with `Buf::into_bytes`. The advantage of `Buf::into_bytes` is that it can be optimized in cases where converting a `T: Buf` into a `Bytes` instance is efficient.
This commit is contained in:
Douman
2019-08-27 13:09:43 -07:00
committed by Carl Lerche
parent 79e4b2847f
commit b6cb346adf
16 changed files with 97 additions and 373 deletions
+1
View File
@@ -37,6 +37,7 @@ jobs:
- script: | - script: |
git clone https://github.com/rust-embedded/cross.git git clone https://github.com/rust-embedded/cross.git
cd cross cd cross
git reset --hard fb1cb1d7288151f4349f1cb4c990e0e2281764da #Is broken after this commit (images are not uploaded to new docker hub)
git apply ../ci/cross-patch git apply ../ci/cross-patch
cargo install --path . cargo install --path .
rm -rf cross rm -rf cross
+4 -4
View File
@@ -1,5 +1,5 @@
diff --git a/src/docker.rs b/src/docker.rs diff --git a/src/docker.rs b/src/docker.rs
index 1525b87..5c9cd54 100644 index 6ea745d..15fef81 100644
--- a/src/docker.rs --- a/src/docker.rs
+++ b/src/docker.rs +++ b/src/docker.rs
@@ -62,7 +62,7 @@ pub fn register(target: &Target, verbose: bool) -> Result<()> { @@ -62,7 +62,7 @@ pub fn register(target: &Target, verbose: bool) -> Result<()> {
@@ -12,8 +12,8 @@ index 1525b87..5c9cd54 100644
.args(&["sh", "-c", cmd]) .args(&["sh", "-c", cmd])
.run(verbose) .run(verbose)
@@ -160,7 +160,7 @@ pub fn run(target: &Target, @@ -160,7 +160,7 @@ pub fn run(target: &Target,
.args(&["-v", &format!("{}:/rust:ro", sysroot.display())]) .args(&["-v", &format!("{}:/rust:Z,ro", sysroot.display())])
.args(&["-v", &format!("{}:/target", target_dir.display())]) .args(&["-v", &format!("{}:/target:Z", target_dir.display())])
.args(&["-w", "/project"]) .args(&["-w", "/project"])
- .args(&["-it", &image(toml, target)?]) - .args(&["-it", &image(toml, target)?])
+ .args(&["-i", &image(toml, target)?]) + .args(&["-i", &image(toml, target)?])
+43 -34
View File
@@ -1,4 +1,4 @@
use super::{IntoBuf, Take, Reader, FromBuf, Chain}; use super::{Take, Reader, Chain};
use std::{cmp, io::IoSlice, ptr, mem}; use std::{cmp, io::IoSlice, ptr, mem};
@@ -787,30 +787,6 @@ pub trait Buf {
f64::from_bits(Self::get_u64_le(self)) f64::from_bits(Self::get_u64_le(self))
} }
/// Transforms a `Buf` into a concrete buffer.
///
/// `collect()` can operate on any value that implements `Buf`, and turn it
/// into the relevant concrete buffer type.
///
/// # Examples
///
/// Collecting a buffer and loading the contents into a `Vec<u8>`.
///
/// ```
/// use bytes::Buf;
///
/// let buf = &b"hello world"[..];
/// let vec: Vec<u8> = buf.collect();
///
/// assert_eq!(vec, b"hello world");
/// ```
fn collect<B>(self) -> B
where Self: Sized,
B: FromBuf,
{
B::from_buf(self)
}
/// Creates an adaptor which will read at most `limit` bytes from `self`. /// Creates an adaptor which will read at most `limit` bytes from `self`.
/// ///
/// This function returns a new instance of `Buf` which will read at most /// This function returns a new instance of `Buf` which will read at most
@@ -848,16 +824,15 @@ pub trait Buf {
/// ``` /// ```
/// use bytes::Buf; /// use bytes::Buf;
/// ///
/// let chain = b"hello "[..].chain(&b"world"[..]); /// let mut chain = b"hello "[..].chain(&b"world"[..]);
/// ///
/// let full: Vec<u8> = chain.collect(); /// let full = chain.to_bytes();
/// assert_eq!(full, b"hello world"); /// assert_eq!(full.bytes(), b"hello world");
/// ``` /// ```
fn chain<U>(self, next: U) -> Chain<Self, U::Buf> fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
where U: IntoBuf, where Self: Sized
Self: Sized,
{ {
Chain::new(self, next.into_buf()) Chain::new(self, next)
} }
/// Creates a "by reference" adaptor for this instance of `Buf`. /// Creates a "by reference" adaptor for this instance of `Buf`.
@@ -896,10 +871,10 @@ pub trait Buf {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use bytes::{Buf, IntoBuf, Bytes}; /// use bytes::{Buf, Bytes};
/// use std::io::Read; /// use std::io::Read;
/// ///
/// let buf = Bytes::from("hello world").into_buf(); /// let buf = Bytes::from("hello world");
/// ///
/// let mut reader = buf.reader(); /// let mut reader = buf.reader();
/// let mut dst = [0; 1024]; /// let mut dst = [0; 1024];
@@ -912,6 +887,23 @@ pub trait Buf {
fn reader(self) -> Reader<Self> where Self: Sized { fn reader(self) -> Reader<Self> where Self: Sized {
super::reader::new(self) super::reader::new(self)
} }
/// Consumes remaining bytes inside self and returns new instance of `Bytes`
///
/// # Examples
///
/// ```
/// use bytes::{Buf};
///
/// let bytes = "hello world".to_bytes();
/// assert_eq!(&bytes[..], &b"hello world"[..]);
/// ```
fn to_bytes(&mut self) -> crate::Bytes {
use super::BufMut;
let mut ret = crate::BytesMut::with_capacity(self.remaining());
ret.put(self);
ret.freeze()
}
} }
impl<T: Buf + ?Sized> Buf for &mut T { impl<T: Buf + ?Sized> Buf for &mut T {
@@ -967,6 +959,23 @@ impl Buf for &[u8] {
} }
} }
impl Buf for &str {
#[inline]
fn remaining(&self) -> usize {
self.len()
}
#[inline]
fn bytes(&self) -> &[u8] {
self.as_bytes()
}
#[inline]
fn advance(&mut self, cnt: usize) {
*self = &self[cnt..];
}
}
impl Buf for Option<[u8; 1]> { impl Buf for Option<[u8; 1]> {
fn remaining(&self) -> usize { fn remaining(&self) -> usize {
if self.is_some() { if self.is_some() {
+3 -7
View File
@@ -1,4 +1,4 @@
use super::{IntoBuf, Writer}; use super::{Writer};
use std::{mem, cmp, io::IoSliceMut, ptr, usize}; use std::{mem, cmp, io::IoSliceMut, ptr, usize};
@@ -208,7 +208,7 @@ pub trait BufMut {
/// ///
/// let mut buf = vec![]; /// let mut buf = vec![];
/// ///
/// buf.put(b'h'); /// buf.put_u8(b'h');
/// buf.put(&b"ello"[..]); /// buf.put(&b"ello"[..]);
/// buf.put(" world"); /// buf.put(" world");
/// ///
@@ -218,11 +218,7 @@ pub trait BufMut {
/// # Panics /// # Panics
/// ///
/// Panics if `self` does not have enough capacity to contain `src`. /// Panics if `self` does not have enough capacity to contain `src`.
fn put<T: IntoBuf>(&mut self, src: T) where Self: Sized { fn put<T: super::Buf>(&mut self, mut src: T) where Self: Sized {
use super::Buf;
let mut src = src.into_buf();
assert!(self.remaining_mut() >= src.remaining()); assert!(self.remaining_mut() >= src.remaining());
while src.has_remaining() { while src.has_remaining() {
+21 -13
View File
@@ -14,13 +14,13 @@ use std::io::{IoSlice, IoSliceMut};
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use bytes::{Bytes, Buf, IntoBuf}; /// use bytes::{Bytes, Buf};
/// use bytes::buf::Chain; /// use bytes::buf::Chain;
/// ///
/// let buf = Bytes::from(&b"hello "[..]).into_buf() /// let mut buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..])); /// .chain(Bytes::from(&b"world"[..]));
/// ///
/// let full: Bytes = buf.collect(); /// let full: Bytes = buf.to_bytes();
/// assert_eq!(full[..], b"hello world"[..]); /// assert_eq!(full[..], b"hello world"[..]);
/// ``` /// ```
/// ///
@@ -60,9 +60,9 @@ impl<T, U> Chain<T, U> {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use bytes::{Bytes, Buf, IntoBuf}; /// use bytes::{Bytes, Buf};
/// ///
/// let buf = Bytes::from(&b"hello"[..]).into_buf() /// let buf = Bytes::from(&b"hello"[..])
/// .chain(Bytes::from(&b"world"[..])); /// .chain(Bytes::from(&b"world"[..]));
/// ///
/// assert_eq!(buf.first_ref()[..], b"hello"[..]); /// assert_eq!(buf.first_ref()[..], b"hello"[..]);
@@ -76,14 +76,14 @@ impl<T, U> Chain<T, U> {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use bytes::{Bytes, Buf, IntoBuf}; /// use bytes::{Bytes, Buf};
/// ///
/// let mut buf = Bytes::from(&b"hello "[..]).into_buf() /// let mut buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..])); /// .chain(Bytes::from(&b"world"[..]));
/// ///
/// buf.first_mut().advance(1); /// buf.first_mut().advance(1);
/// ///
/// let full: Bytes = buf.collect(); /// let full: Bytes = buf.to_bytes();
/// assert_eq!(full[..], b"ello world"[..]); /// assert_eq!(full[..], b"ello world"[..]);
/// ``` /// ```
pub fn first_mut(&mut self) -> &mut T { pub fn first_mut(&mut self) -> &mut T {
@@ -95,9 +95,9 @@ impl<T, U> Chain<T, U> {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use bytes::{Bytes, Buf, IntoBuf}; /// use bytes::{Bytes, Buf};
/// ///
/// let buf = Bytes::from(&b"hello"[..]).into_buf() /// let buf = Bytes::from(&b"hello"[..])
/// .chain(Bytes::from(&b"world"[..])); /// .chain(Bytes::from(&b"world"[..]));
/// ///
/// assert_eq!(buf.last_ref()[..], b"world"[..]); /// assert_eq!(buf.last_ref()[..], b"world"[..]);
@@ -111,14 +111,14 @@ impl<T, U> Chain<T, U> {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use bytes::{Bytes, Buf, IntoBuf}; /// use bytes::{Bytes, Buf};
/// ///
/// let mut buf = Bytes::from(&b"hello "[..]).into_buf() /// let mut buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..])); /// .chain(Bytes::from(&b"world"[..]));
/// ///
/// buf.last_mut().advance(1); /// buf.last_mut().advance(1);
/// ///
/// let full: Bytes = buf.collect(); /// let full: Bytes = buf.to_bytes();
/// assert_eq!(full[..], b"hello orld"[..]); /// assert_eq!(full[..], b"hello orld"[..]);
/// ``` /// ```
pub fn last_mut(&mut self) -> &mut U { pub fn last_mut(&mut self) -> &mut U {
@@ -183,6 +183,14 @@ impl<T, U> Buf for Chain<T, U>
n += self.b.bytes_vectored(&mut dst[n..]); n += self.b.bytes_vectored(&mut dst[n..]);
n n
} }
fn to_bytes(&mut self) -> crate::Bytes {
let mut bytes: crate::BytesMut = self.a.to_bytes().try_mut()
.unwrap_or_else(|bytes| bytes.into());
bytes.put(&mut self.b);
bytes.freeze()
}
} }
impl<T, U> BufMut for Chain<T, U> impl<T, U> BufMut for Chain<T, U>
-117
View File
@@ -1,117 +0,0 @@
use crate::{Buf, BufMut, IntoBuf, Bytes, BytesMut};
/// Conversion from a [`Buf`]
///
/// Implementing `FromBuf` for a type defines how it is created from a buffer.
/// This is common for types which represent byte storage of some kind.
///
/// [`FromBuf::from_buf`] is rarely called explicitly, and it is instead used
/// through [`Buf::collect`]. See [`Buf::collect`] documentation for more examples.
///
/// See also [`IntoBuf`].
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use bytes::{Bytes, IntoBuf};
/// use bytes::buf::FromBuf;
///
/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
/// let vec = Vec::from_buf(buf);
///
/// assert_eq!(vec, &b"hello world"[..]);
/// ```
///
/// Using [`Buf::collect`] to implicitly use `FromBuf`:
///
/// ```
/// use bytes::{Buf, Bytes, IntoBuf};
///
/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
/// let vec: Vec<u8> = buf.collect();
///
/// assert_eq!(vec, &b"hello world"[..]);
/// ```
///
/// Implementing `FromBuf` for your type:
///
/// ```
/// use bytes::{BufMut, Bytes};
/// use bytes::buf::{IntoBuf, FromBuf};
///
/// // A sample buffer, that's just a wrapper over Vec<u8>
/// struct MyBuffer(Vec<u8>);
///
/// impl FromBuf for MyBuffer {
/// fn from_buf<B>(buf: B) -> Self where B: IntoBuf {
/// let mut v = Vec::new();
/// v.put(buf.into_buf());
/// MyBuffer(v)
/// }
/// }
///
/// // Now we can make a new buf
/// let buf = Bytes::from(&b"hello world"[..]);
///
/// // And make a MyBuffer out of it
/// let my_buf = MyBuffer::from_buf(buf);
///
/// assert_eq!(my_buf.0, &b"hello world"[..]);
/// ```
///
/// [`Buf`]: trait.Buf.html
/// [`FromBuf::from_buf`]: #method.from_buf
/// [`Buf::collect`]: trait.Buf.html#method.collect
/// [`IntoBuf`]: trait.IntoBuf.html
pub trait FromBuf {
/// Creates a value from a buffer.
///
/// See the [type-level documentation](#) for more details.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use bytes::{Bytes, IntoBuf};
/// use bytes::buf::FromBuf;
///
/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
/// let vec = Vec::from_buf(buf);
///
/// assert_eq!(vec, &b"hello world"[..]);
/// ```
fn from_buf<T>(buf: T) -> Self where T: IntoBuf;
}
impl FromBuf for Vec<u8> {
fn from_buf<T>(buf: T) -> Self
where T: IntoBuf
{
let buf = buf.into_buf();
let mut ret = Vec::with_capacity(buf.remaining());
ret.put(buf);
ret
}
}
impl FromBuf for Bytes {
fn from_buf<T>(buf: T) -> Self
where T: IntoBuf
{
BytesMut::from_buf(buf).freeze()
}
}
impl FromBuf for BytesMut {
fn from_buf<T>(buf: T) -> Self
where T: IntoBuf
{
let buf = buf.into_buf();
let mut ret = BytesMut::with_capacity(buf.remaining());
ret.put(buf);
ret
}
}
-129
View File
@@ -1,129 +0,0 @@
use super::{Buf};
use crate::BytesMut;
/// Conversion into a `Buf`
///
/// An `IntoBuf` implementation defines how to convert a value into a `Buf`.
/// This is common for types that represent byte storage of some kind. `IntoBuf`
/// may be implemented directly for types or on references for those types.
///
/// # Examples
///
/// ```
/// use bytes::{Buf, IntoBuf};
///
/// let bytes = b"\x00\x01hello world";
/// let mut buf = bytes.into_buf();
///
/// assert_eq!(1, buf.get_u16());
///
/// let mut rest = [0; 11];
/// buf.copy_to_slice(&mut rest);
///
/// assert_eq!(b"hello world", &rest);
/// ```
pub trait IntoBuf {
/// The `Buf` type that `self` is being converted into
type Buf: Buf;
/// Creates a `Buf` from a value.
///
/// # Examples
///
/// ```
/// use bytes::{Buf, IntoBuf};
///
/// let bytes = b"\x00\x01hello world";
/// let mut buf = bytes.into_buf();
///
/// assert_eq!(1, buf.get_u16());
///
/// let mut rest = [0; 11];
/// buf.copy_to_slice(&mut rest);
///
/// assert_eq!(b"hello world", &rest);
/// ```
fn into_buf(self) -> Self::Buf;
}
impl<T: Buf> IntoBuf for T {
type Buf = Self;
fn into_buf(self) -> Self {
self
}
}
impl<'a> IntoBuf for &'a str {
type Buf = &'a [u8];
fn into_buf(self) -> Self::Buf {
self.as_bytes()
}
}
impl IntoBuf for Vec<u8> {
type Buf = BytesMut;
fn into_buf(self) -> Self::Buf {
self.into()
}
}
impl<'a> IntoBuf for &'a Vec<u8> {
type Buf = &'a [u8];
fn into_buf(self) -> Self::Buf {
self.as_slice()
}
}
// Kind of annoying... but this impl is required to allow passing `&'static
// [u8]` where for<'a> &'a T: IntoBuf is required.
impl<'a> IntoBuf for &'a &'static [u8] {
type Buf = &'static [u8];
fn into_buf(self) -> Self::Buf {
*self
}
}
impl<'a> IntoBuf for &'a &'static str {
type Buf = &'static [u8];
fn into_buf(self) -> Self::Buf {
self.as_bytes().into_buf()
}
}
impl IntoBuf for String {
type Buf = BytesMut;
fn into_buf(self) -> Self::Buf {
self.into_bytes().into_buf()
}
}
impl<'a> IntoBuf for &'a String {
type Buf = &'a [u8];
fn into_buf(self) -> Self::Buf {
self.as_bytes().into_buf()
}
}
impl IntoBuf for u8 {
type Buf = Option<[u8; 1]>;
fn into_buf(self) -> Self::Buf {
Some([self])
}
}
impl IntoBuf for i8 {
type Buf = Option<[u8; 1]>;
fn into_buf(self) -> Self::Buf {
Some([self as u8; 1])
}
}
+7 -7
View File
@@ -9,9 +9,9 @@ use crate::Buf;
/// Basic usage: /// Basic usage:
/// ///
/// ``` /// ```
/// use bytes::{Buf, IntoBuf, Bytes}; /// use bytes::{Buf, Bytes};
/// ///
/// let buf = Bytes::from(&b"abc"[..]).into_buf(); /// let buf = Bytes::from(&b"abc"[..]);
/// let mut iter = buf.into_iter(); /// let mut iter = buf.into_iter();
/// ///
/// assert_eq!(iter.next(), Some(b'a')); /// assert_eq!(iter.next(), Some(b'a'));
@@ -52,9 +52,9 @@ impl<T> IntoIter<T> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use bytes::{Buf, IntoBuf, Bytes}; /// use bytes::{Buf, Bytes};
/// ///
/// let buf = Bytes::from(&b"abc"[..]).into_buf(); /// let buf = Bytes::from(&b"abc"[..]);
/// let mut iter = buf.into_iter(); /// let mut iter = buf.into_iter();
/// ///
/// assert_eq!(iter.next(), Some(b'a')); /// assert_eq!(iter.next(), Some(b'a'));
@@ -73,9 +73,9 @@ impl<T> IntoIter<T> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use bytes::{Buf, IntoBuf, Bytes}; /// use bytes::{Buf, Bytes};
/// ///
/// let buf = Bytes::from(&b"abc"[..]).into_buf(); /// let buf = Bytes::from(&b"abc"[..]);
/// let mut iter = buf.into_iter(); /// let mut iter = buf.into_iter();
/// ///
/// assert_eq!(iter.next(), Some(b'a')); /// assert_eq!(iter.next(), Some(b'a'));
@@ -93,7 +93,7 @@ impl<T> IntoIter<T> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use bytes::{Buf, IntoBuf, BytesMut}; /// use bytes::{Buf, BytesMut};
/// ///
/// let buf = BytesMut::from(&b"abc"[..]); /// let buf = BytesMut::from(&b"abc"[..]);
/// let mut iter = buf.into_iter(); /// let mut iter = buf.into_iter();
-4
View File
@@ -18,9 +18,7 @@
mod buf; mod buf;
mod buf_mut; mod buf_mut;
mod from_buf;
mod chain; mod chain;
mod into_buf;
mod iter; mod iter;
mod reader; mod reader;
mod take; mod take;
@@ -29,9 +27,7 @@ mod writer;
pub use self::buf::Buf; pub use self::buf::Buf;
pub use self::buf_mut::BufMut; pub use self::buf_mut::BufMut;
pub use self::from_buf::FromBuf;
pub use self::chain::Chain; pub use self::chain::Chain;
pub use self::into_buf::IntoBuf;
pub use self::iter::IntoIter; pub use self::iter::IntoIter;
pub use self::reader::Reader; pub use self::reader::Reader;
pub use self::take::Take; pub use self::take::Take;
+3 -1
View File
@@ -34,6 +34,8 @@ mod tests {
buffer.advance(6); buffer.advance(6);
assert_eq!(b"world", buffer.bytes()); assert_eq!(b"world", buffer.bytes());
buffer.extend(b" piece"); buffer.extend(b" piece");
assert_eq!(b"world piece" as &[u8], &buffer.collect::<Vec<u8>>()[..]); let mut out = [0; 11];
buffer.copy_to_slice(&mut out);
assert_eq!(b"world piece", &out[..]);
} }
} }
+5 -13
View File
@@ -1,4 +1,4 @@
use crate::{Buf, BufMut, IntoBuf}; use crate::{Buf, BufMut};
use crate::buf::IntoIter; use crate::buf::IntoIter;
use crate::debug; use crate::debug;
@@ -133,8 +133,8 @@ pub struct Bytes {
/// ///
/// let mut buf = BytesMut::with_capacity(64); /// let mut buf = BytesMut::with_capacity(64);
/// ///
/// buf.put(b'h'); /// buf.put_u8(b'h');
/// buf.put(b'e'); /// buf.put_u8(b'e');
/// buf.put("llo"); /// buf.put("llo");
/// ///
/// assert_eq!(&buf[..], b"hello"); /// assert_eq!(&buf[..], b"hello");
@@ -842,7 +842,7 @@ impl Bytes {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use bytes::{Buf, IntoBuf, Bytes}; /// use bytes::{Buf, Bytes};
/// ///
/// let buf = Bytes::from(&b"abc"[..]); /// let buf = Bytes::from(&b"abc"[..]);
/// let mut iter = buf.iter(); /// let mut iter = buf.iter();
@@ -943,7 +943,7 @@ impl FromIterator<u8> for BytesMut {
for i in iter { for i in iter {
out.reserve(1); out.reserve(1);
out.put(i); out.put_u8(i);
} }
out out
@@ -1602,14 +1602,6 @@ impl BufMut for BytesMut {
} }
} }
impl<'a> IntoBuf for &'a BytesMut {
type Buf = &'a [u8];
fn into_buf(self) -> Self::Buf {
self.as_ref()
}
}
impl AsRef<[u8]> for BytesMut { impl AsRef<[u8]> for BytesMut {
#[inline] #[inline]
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
-1
View File
@@ -75,7 +75,6 @@ pub mod buf;
pub use crate::buf::{ pub use crate::buf::{
Buf, Buf,
BufMut, BufMut,
IntoBuf,
}; };
mod bytes; mod bytes;
+1 -1
View File
@@ -32,7 +32,7 @@ fn test_vec_as_mut_buf() {
#[test] #[test]
fn test_put_u8() { fn test_put_u8() {
let mut buf = Vec::with_capacity(8); let mut buf = Vec::with_capacity(8);
buf.put::<u8>(33); buf.put_u8(33);
assert_eq!(b"\x21", &buf[..]); assert_eq!(b"\x21", &buf[..]);
} }
+6 -6
View File
@@ -302,14 +302,14 @@ fn fns_defined_for_bytes_mut() {
fn reserve_convert() { fn reserve_convert() {
// Inline -> Vec // Inline -> Vec
let mut bytes = BytesMut::with_capacity(8); let mut bytes = BytesMut::with_capacity(8);
bytes.put("hello"); bytes.put("hello".as_bytes());
bytes.reserve(40); bytes.reserve(40);
assert_eq!(bytes.capacity(), 45); assert_eq!(bytes.capacity(), 45);
assert_eq!(bytes, "hello"); assert_eq!(bytes, "hello");
// Inline -> Inline // Inline -> Inline
let mut bytes = BytesMut::with_capacity(inline_cap()); let mut bytes = BytesMut::with_capacity(inline_cap());
bytes.put("abcdefghijkl"); bytes.put("abcdefghijkl".as_bytes());
let a = bytes.split_to(10); let a = bytes.split_to(10);
bytes.reserve(inline_cap() - 3); bytes.reserve(inline_cap() - 3);
@@ -336,7 +336,7 @@ fn reserve_convert() {
#[test] #[test]
fn reserve_growth() { fn reserve_growth() {
let mut bytes = BytesMut::with_capacity(64); let mut bytes = BytesMut::with_capacity(64);
bytes.put("hello world"); bytes.put("hello world".as_bytes());
let _ = bytes.split(); let _ = bytes.split();
bytes.reserve(65); bytes.reserve(65);
@@ -348,7 +348,7 @@ fn reserve_allocates_at_least_original_capacity() {
let mut bytes = BytesMut::with_capacity(1024); let mut bytes = BytesMut::with_capacity(1024);
for i in 0..1020 { for i in 0..1020 {
bytes.put(i as u8); bytes.put_u8(i as u8);
} }
let _other = bytes.split(); let _other = bytes.split();
@@ -364,7 +364,7 @@ fn reserve_max_original_capacity_value() {
let mut bytes = BytesMut::with_capacity(SIZE); let mut bytes = BytesMut::with_capacity(SIZE);
for _ in 0..SIZE { for _ in 0..SIZE {
bytes.put(0u8); bytes.put_u8(0u8);
} }
let _other = bytes.split(); let _other = bytes.split();
@@ -381,7 +381,7 @@ fn reserve_max_original_capacity_value() {
fn reserve_vec_recycling() { fn reserve_vec_recycling() {
let mut bytes = BytesMut::from(Vec::with_capacity(16)); let mut bytes = BytesMut::from(Vec::with_capacity(16));
assert_eq!(bytes.capacity(), 16); assert_eq!(bytes.capacity(), 16);
bytes.put("0123456789012345"); bytes.put("0123456789012345".as_bytes());
bytes.advance(10); bytes.advance(10);
assert_eq!(bytes.capacity(), 6); assert_eq!(bytes.capacity(), 6);
bytes.reserve(8); bytes.reserve(8);
+3 -3
View File
@@ -9,7 +9,7 @@ fn collect_two_bufs() {
let a = Bytes::from(&b"hello"[..]); let a = Bytes::from(&b"hello"[..]);
let b = Bytes::from(&b"world"[..]); let b = Bytes::from(&b"world"[..]);
let res: Vec<u8> = a.chain(b).collect(); let res = a.chain(b).to_bytes();
assert_eq!(res, &b"helloworld"[..]); assert_eq!(res, &b"helloworld"[..]);
} }
@@ -21,8 +21,8 @@ fn writing_chained() {
{ {
let mut buf = Chain::new(&mut a, &mut b); let mut buf = Chain::new(&mut a, &mut b);
for i in 0..128 { for i in 0u8..128 {
buf.put(i as u8); buf.put_u8(i);
} }
} }
-33
View File
@@ -1,33 +0,0 @@
#![deny(warnings, rust_2018_idioms)]
use bytes::{Buf, Bytes, BytesMut};
const LONG: &'static [u8] = b"mary had a little lamb, little lamb, little lamb";
const SHORT: &'static [u8] = b"hello world";
#[test]
fn collect_to_vec() {
let buf: Vec<u8> = SHORT.collect();
assert_eq!(buf, SHORT);
let buf: Vec<u8> = LONG.collect();
assert_eq!(buf, LONG);
}
#[test]
fn collect_to_bytes() {
let buf: Bytes = SHORT.collect();
assert_eq!(buf, SHORT);
let buf: Bytes = LONG.collect();
assert_eq!(buf, LONG);
}
#[test]
fn collect_to_bytes_mut() {
let buf: BytesMut = SHORT.collect();
assert_eq!(buf, SHORT);
let buf: BytesMut = LONG.collect();
assert_eq!(buf, LONG);
}