From 1da5cf2aaf932b6de9ae89dc0b6bfbb08f415319 Mon Sep 17 00:00:00 2001 From: Douman Date: Mon, 26 Aug 2019 20:31:04 +0200 Subject: [PATCH 1/5] Remove IntoBuf/FromBuf As consequence we remove Buf::collect and instead place it only into Chain. This makes user to collect underlying bytes harder as can be illustrated with VecDeque --- src/buf/buf.rs | 54 ++++++++--------- src/buf/buf_mut.rs | 10 +--- src/buf/chain.rs | 27 +++++---- src/buf/from_buf.rs | 117 ------------------------------------- src/buf/into_buf.rs | 129 ----------------------------------------- src/buf/iter.rs | 14 ++--- src/buf/mod.rs | 4 -- src/buf/vec_deque.rs | 4 +- src/bytes.rs | 18 ++---- src/lib.rs | 1 - tests/test_buf_mut.rs | 2 +- tests/test_bytes.rs | 12 ++-- tests/test_chain.rs | 4 +- tests/test_from_buf.rs | 33 ----------- 14 files changed, 67 insertions(+), 362 deletions(-) delete mode 100644 src/buf/from_buf.rs delete mode 100644 src/buf/into_buf.rs delete mode 100644 tests/test_from_buf.rs diff --git a/src/buf/buf.rs b/src/buf/buf.rs index 97b85fc39..405022578 100644 --- a/src/buf/buf.rs +++ b/src/buf/buf.rs @@ -1,4 +1,4 @@ -use super::{IntoBuf, Take, Reader, FromBuf, Chain}; +use super::{Take, Reader, Chain}; use std::{cmp, io::IoSlice, ptr, mem}; @@ -787,30 +787,6 @@ pub trait Buf { 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`. - /// - /// ``` - /// use bytes::Buf; - /// - /// let buf = &b"hello world"[..]; - /// let vec: Vec = buf.collect(); - /// - /// assert_eq!(vec, b"hello world"); - /// ``` - fn collect(self) -> B - where Self: Sized, - B: FromBuf, - { - B::from_buf(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 @@ -853,11 +829,10 @@ pub trait Buf { /// let full: Vec = chain.collect(); /// assert_eq!(full, b"hello world"); /// ``` - fn chain(self, next: U) -> Chain - where U: IntoBuf, - Self: Sized, + fn chain(self, next: U) -> Chain + where Self: Sized { - Chain::new(self, next.into_buf()) + Chain::new(self, next) } /// Creates a "by reference" adaptor for this instance of `Buf`. @@ -896,10 +871,10 @@ pub trait Buf { /// # Examples /// /// ``` - /// use bytes::{Buf, IntoBuf, Bytes}; + /// use bytes::{Buf, Bytes}; /// 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 dst = [0; 1024]; @@ -967,6 +942,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]> { fn remaining(&self) -> usize { if self.is_some() { diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index 05ae794a8..7cd6be803 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -1,4 +1,4 @@ -use super::{IntoBuf, Writer}; +use super::{Writer}; use std::{mem, cmp, io::IoSliceMut, ptr, usize}; @@ -208,7 +208,7 @@ pub trait BufMut { /// /// let mut buf = vec![]; /// - /// buf.put(b'h'); + /// buf.put_u8(b'h'); /// buf.put(&b"ello"[..]); /// buf.put(" world"); /// @@ -218,11 +218,7 @@ pub trait BufMut { /// # Panics /// /// Panics if `self` does not have enough capacity to contain `src`. - fn put(&mut self, src: T) where Self: Sized { - use super::Buf; - - let mut src = src.into_buf(); - + fn put(&mut self, mut src: T) where Self: Sized { assert!(self.remaining_mut() >= src.remaining()); while src.has_remaining() { diff --git a/src/buf/chain.rs b/src/buf/chain.rs index 936a086b7..6ce48fe2c 100644 --- a/src/buf/chain.rs +++ b/src/buf/chain.rs @@ -1,6 +1,7 @@ use crate::{Buf, BufMut}; use crate::buf::IntoIter; use std::io::{IoSlice, IoSliceMut}; +use core::iter::FromIterator; /// A `Chain` sequences two buffers. /// @@ -14,10 +15,10 @@ use std::io::{IoSlice, IoSliceMut}; /// # Examples /// /// ``` -/// use bytes::{Bytes, Buf, IntoBuf}; +/// use bytes::{Bytes, Buf}; /// use bytes::buf::Chain; /// -/// let buf = Bytes::from(&b"hello "[..]).into_buf() +/// let buf = Bytes::from(&b"hello "[..]) /// .chain(Bytes::from(&b"world"[..])); /// /// let full: Bytes = buf.collect(); @@ -60,9 +61,9 @@ impl Chain { /// # 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"[..])); /// /// assert_eq!(buf.first_ref()[..], b"hello"[..]); @@ -76,9 +77,9 @@ impl Chain { /// # 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"[..])); /// /// buf.first_mut().advance(1); @@ -95,9 +96,9 @@ impl Chain { /// # 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"[..])); /// /// assert_eq!(buf.last_ref()[..], b"world"[..]); @@ -111,9 +112,9 @@ impl Chain { /// # 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"[..])); /// /// buf.last_mut().advance(1); @@ -142,6 +143,12 @@ impl Chain { pub fn into_inner(self) -> (T, U) { (self.a, self.b) } + + #[inline] + /// Consumes self and returns joined value, containing underlying bufs. + pub fn collect>(self) -> B where T: Buf, U: Buf { + self.into_iter().collect() + } } impl Buf for Chain diff --git a/src/buf/from_buf.rs b/src/buf/from_buf.rs deleted file mode 100644 index db91a95d3..000000000 --- a/src/buf/from_buf.rs +++ /dev/null @@ -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 = 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 -/// struct MyBuffer(Vec); -/// -/// impl FromBuf for MyBuffer { -/// fn from_buf(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(buf: T) -> Self where T: IntoBuf; -} - -impl FromBuf for Vec { - fn from_buf(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(buf: T) -> Self - where T: IntoBuf - { - BytesMut::from_buf(buf).freeze() - } -} - -impl FromBuf for BytesMut { - fn from_buf(buf: T) -> Self - where T: IntoBuf - { - let buf = buf.into_buf(); - let mut ret = BytesMut::with_capacity(buf.remaining()); - ret.put(buf); - ret - } -} diff --git a/src/buf/into_buf.rs b/src/buf/into_buf.rs deleted file mode 100644 index 559021ad3..000000000 --- a/src/buf/into_buf.rs +++ /dev/null @@ -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 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 { - type Buf = BytesMut; - - fn into_buf(self) -> Self::Buf { - self.into() - } -} - -impl<'a> IntoBuf for &'a Vec { - 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]) - } -} diff --git a/src/buf/iter.rs b/src/buf/iter.rs index a1bf89b79..a93e3599b 100644 --- a/src/buf/iter.rs +++ b/src/buf/iter.rs @@ -9,9 +9,9 @@ use crate::Buf; /// 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(); /// /// assert_eq!(iter.next(), Some(b'a')); @@ -52,9 +52,9 @@ impl IntoIter { /// # Examples /// /// ```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(); /// /// assert_eq!(iter.next(), Some(b'a')); @@ -73,9 +73,9 @@ impl IntoIter { /// # Examples /// /// ```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(); /// /// assert_eq!(iter.next(), Some(b'a')); @@ -93,7 +93,7 @@ impl IntoIter { /// # Examples /// /// ```rust - /// use bytes::{Buf, IntoBuf, BytesMut}; + /// use bytes::{Buf, BytesMut}; /// /// let buf = BytesMut::from(&b"abc"[..]); /// let mut iter = buf.into_iter(); diff --git a/src/buf/mod.rs b/src/buf/mod.rs index a127549a6..81e22d3fb 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -18,9 +18,7 @@ mod buf; mod buf_mut; -mod from_buf; mod chain; -mod into_buf; mod iter; mod reader; mod take; @@ -29,9 +27,7 @@ mod writer; pub use self::buf::Buf; pub use self::buf_mut::BufMut; -pub use self::from_buf::FromBuf; pub use self::chain::Chain; -pub use self::into_buf::IntoBuf; pub use self::iter::IntoIter; pub use self::reader::Reader; pub use self::take::Take; diff --git a/src/buf/vec_deque.rs b/src/buf/vec_deque.rs index 1cd650f51..4464426b6 100644 --- a/src/buf/vec_deque.rs +++ b/src/buf/vec_deque.rs @@ -34,6 +34,8 @@ mod tests { buffer.advance(6); assert_eq!(b"world", buffer.bytes()); buffer.extend(b" piece"); - assert_eq!(b"world piece" as &[u8], &buffer.collect::>()[..]); + let mut out = [0; 11]; + buffer.copy_to_slice(&mut out); + assert_eq!(b"world piece", &out[..]); } } diff --git a/src/bytes.rs b/src/bytes.rs index 3f8b23a6f..99022f0b3 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -1,4 +1,4 @@ -use crate::{Buf, BufMut, IntoBuf}; +use crate::{Buf, BufMut}; use crate::buf::IntoIter; use crate::debug; @@ -133,8 +133,8 @@ pub struct Bytes { /// /// let mut buf = BytesMut::with_capacity(64); /// -/// buf.put(b'h'); -/// buf.put(b'e'); +/// buf.put_u8(b'h'); +/// buf.put_u8(b'e'); /// buf.put("llo"); /// /// assert_eq!(&buf[..], b"hello"); @@ -842,7 +842,7 @@ impl Bytes { /// # Examples /// /// ``` - /// use bytes::{Buf, IntoBuf, Bytes}; + /// use bytes::{Buf, Bytes}; /// /// let buf = Bytes::from(&b"abc"[..]); /// let mut iter = buf.iter(); @@ -943,7 +943,7 @@ impl FromIterator for BytesMut { for i in iter { out.reserve(1); - out.put(i); + out.put_u8(i); } 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 { #[inline] fn as_ref(&self) -> &[u8] { diff --git a/src/lib.rs b/src/lib.rs index 84bccb2b2..114de41a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,7 +75,6 @@ pub mod buf; pub use crate::buf::{ Buf, BufMut, - IntoBuf, }; mod bytes; diff --git a/tests/test_buf_mut.rs b/tests/test_buf_mut.rs index 2c9f1f2c4..d545d2b1d 100644 --- a/tests/test_buf_mut.rs +++ b/tests/test_buf_mut.rs @@ -32,7 +32,7 @@ fn test_vec_as_mut_buf() { #[test] fn test_put_u8() { let mut buf = Vec::with_capacity(8); - buf.put::(33); + buf.put_u8(33); assert_eq!(b"\x21", &buf[..]); } diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index 70a8205e0..3c51ac5e4 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -302,14 +302,14 @@ fn fns_defined_for_bytes_mut() { fn reserve_convert() { // Inline -> Vec let mut bytes = BytesMut::with_capacity(8); - bytes.put("hello"); + bytes.put("hello".as_bytes()); bytes.reserve(40); assert_eq!(bytes.capacity(), 45); assert_eq!(bytes, "hello"); // Inline -> Inline let mut bytes = BytesMut::with_capacity(inline_cap()); - bytes.put("abcdefghijkl"); + bytes.put("abcdefghijkl".as_bytes()); let a = bytes.split_to(10); bytes.reserve(inline_cap() - 3); @@ -336,7 +336,7 @@ fn reserve_convert() { #[test] fn reserve_growth() { let mut bytes = BytesMut::with_capacity(64); - bytes.put("hello world"); + bytes.put("hello world".as_bytes()); let _ = bytes.split(); bytes.reserve(65); @@ -348,7 +348,7 @@ fn reserve_allocates_at_least_original_capacity() { let mut bytes = BytesMut::with_capacity(1024); for i in 0..1020 { - bytes.put(i as u8); + bytes.put_u8(i as u8); } let _other = bytes.split(); @@ -364,7 +364,7 @@ fn reserve_max_original_capacity_value() { let mut bytes = BytesMut::with_capacity(SIZE); for _ in 0..SIZE { - bytes.put(0u8); + bytes.put_u8(0u8); } let _other = bytes.split(); @@ -381,7 +381,7 @@ fn reserve_max_original_capacity_value() { fn reserve_vec_recycling() { let mut bytes = BytesMut::from(Vec::with_capacity(16)); assert_eq!(bytes.capacity(), 16); - bytes.put("0123456789012345"); + bytes.put("0123456789012345".as_bytes()); bytes.advance(10); assert_eq!(bytes.capacity(), 6); bytes.reserve(8); diff --git a/tests/test_chain.rs b/tests/test_chain.rs index 6708028fb..6abd61bfc 100644 --- a/tests/test_chain.rs +++ b/tests/test_chain.rs @@ -21,8 +21,8 @@ fn writing_chained() { { let mut buf = Chain::new(&mut a, &mut b); - for i in 0..128 { - buf.put(i as u8); + for i in 0u8..128 { + buf.put_u8(i); } } diff --git a/tests/test_from_buf.rs b/tests/test_from_buf.rs deleted file mode 100644 index 5f644e132..000000000 --- a/tests/test_from_buf.rs +++ /dev/null @@ -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 = SHORT.collect(); - assert_eq!(buf, SHORT); - - let buf: Vec = 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); -} From 0af1c0f989efdbb806fde59eca4c0e0b0f305c8f Mon Sep 17 00:00:00 2001 From: Douman Date: Tue, 27 Aug 2019 07:06:53 +0200 Subject: [PATCH 2/5] Add Buf::into_bytes --- src/buf/buf.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/buf/buf.rs b/src/buf/buf.rs index 405022578..3e105fc0c 100644 --- a/src/buf/buf.rs +++ b/src/buf/buf.rs @@ -887,6 +887,20 @@ pub trait Buf { fn reader(self) -> Reader where Self: Sized { super::reader::new(self) } + + ///Consumes self and transforms into `Bytes` + /// + /// # Examples + /// + /// ``` + /// use bytes::{Buf}; + /// + /// let bytes = "hello world".into_bytes(); + /// assert_eq!(&bytes[..], &b"hello world"[..]); + /// ``` + fn into_bytes(&self) -> crate::Bytes { + self.bytes().into() + } } impl Buf for &mut T { From 329948b7aa3907aa3b56b5d4c1147e9a5bc07bb1 Mon Sep 17 00:00:00 2001 From: Douman Date: Tue, 27 Aug 2019 08:28:57 +0200 Subject: [PATCH 3/5] Fix cross CI --- ci/azure-cross-compile.yml | 1 + ci/cross-patch | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ci/azure-cross-compile.yml b/ci/azure-cross-compile.yml index cdcc197a2..62840f552 100644 --- a/ci/azure-cross-compile.yml +++ b/ci/azure-cross-compile.yml @@ -37,6 +37,7 @@ jobs: - script: | git clone https://github.com/rust-embedded/cross.git cd cross + git reset --hard fb1cb1d7288151f4349f1cb4c990e0e2281764da #Is broken after this commit (images are not uploaded to new docker hub) git apply ../ci/cross-patch cargo install --path . rm -rf cross diff --git a/ci/cross-patch b/ci/cross-patch index f59b55106..b1fb4ba27 100644 --- a/ci/cross-patch +++ b/ci/cross-patch @@ -1,5 +1,5 @@ -diff --git a/src/docker.rs b/src/docker.rs -index 1525b87..5c9cd54 100644 +diff --git a/src/docker.rs b/src/docker.rs +index 6ea745d..15fef81 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -62,7 +62,7 @@ pub fn register(target: &Target, verbose: bool) -> Result<()> { @@ -12,8 +12,8 @@ index 1525b87..5c9cd54 100644 .args(&["sh", "-c", cmd]) .run(verbose) @@ -160,7 +160,7 @@ pub fn run(target: &Target, - .args(&["-v", &format!("{}:/rust:ro", sysroot.display())]) - .args(&["-v", &format!("{}:/target", target_dir.display())]) + .args(&["-v", &format!("{}:/rust:Z,ro", sysroot.display())]) + .args(&["-v", &format!("{}:/target:Z", target_dir.display())]) .args(&["-w", "/project"]) - .args(&["-it", &image(toml, target)?]) + .args(&["-i", &image(toml, target)?]) From 9fda18dcb8f07debaa68d9748a20cb45d16e1e39 Mon Sep 17 00:00:00 2001 From: Douman Date: Tue, 27 Aug 2019 21:29:43 +0200 Subject: [PATCH 4/5] Remove collect --- src/buf/buf.rs | 4 ++-- src/buf/chain.rs | 31 +++++++++++++++++++++---------- tests/test_chain.rs | 2 +- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/buf/buf.rs b/src/buf/buf.rs index 3e105fc0c..6cf5ff217 100644 --- a/src/buf/buf.rs +++ b/src/buf/buf.rs @@ -826,8 +826,8 @@ pub trait Buf { /// /// let chain = b"hello "[..].chain(&b"world"[..]); /// - /// let full: Vec = chain.collect(); - /// assert_eq!(full, b"hello world"); + /// let full = chain.into_bytes(); + /// assert_eq!(full.bytes(), b"hello world"); /// ``` fn chain(self, next: U) -> Chain where Self: Sized diff --git a/src/buf/chain.rs b/src/buf/chain.rs index 6ce48fe2c..6231a7025 100644 --- a/src/buf/chain.rs +++ b/src/buf/chain.rs @@ -1,7 +1,6 @@ use crate::{Buf, BufMut}; use crate::buf::IntoIter; use std::io::{IoSlice, IoSliceMut}; -use core::iter::FromIterator; /// A `Chain` sequences two buffers. /// @@ -21,7 +20,7 @@ use core::iter::FromIterator; /// let buf = Bytes::from(&b"hello "[..]) /// .chain(Bytes::from(&b"world"[..])); /// -/// let full: Bytes = buf.collect(); +/// let full: Bytes = buf.into_bytes(); /// assert_eq!(full[..], b"hello world"[..]); /// ``` /// @@ -84,7 +83,7 @@ impl Chain { /// /// buf.first_mut().advance(1); /// - /// let full: Bytes = buf.collect(); + /// let full: Bytes = buf.into_bytes(); /// assert_eq!(full[..], b"ello world"[..]); /// ``` pub fn first_mut(&mut self) -> &mut T { @@ -119,7 +118,7 @@ impl Chain { /// /// buf.last_mut().advance(1); /// - /// let full: Bytes = buf.collect(); + /// let full: Bytes = buf.into_bytes(); /// assert_eq!(full[..], b"hello orld"[..]); /// ``` pub fn last_mut(&mut self) -> &mut U { @@ -143,12 +142,6 @@ impl Chain { pub fn into_inner(self) -> (T, U) { (self.a, self.b) } - - #[inline] - /// Consumes self and returns joined value, containing underlying bufs. - pub fn collect>(self) -> B where T: Buf, U: Buf { - self.into_iter().collect() - } } impl Buf for Chain @@ -190,6 +183,24 @@ impl Buf for Chain n += self.b.bytes_vectored(&mut dst[n..]); n } + + fn into_bytes(&self) -> crate::Bytes { + let left = self.a.bytes(); + let right = self.b.bytes(); + + let mut bytes = crate::BytesMut::with_capacity(left.len() + right.len()); + + unsafe { + let len = left.len(); + bytes.bytes_mut()[..len].copy_from_slice(left); + bytes.advance_mut(len); + let len = right.len(); + bytes.bytes_mut()[..len].copy_from_slice(right); + bytes.advance_mut(len); + } + + bytes.freeze() + } } impl BufMut for Chain diff --git a/tests/test_chain.rs b/tests/test_chain.rs index 6abd61bfc..466d94bbf 100644 --- a/tests/test_chain.rs +++ b/tests/test_chain.rs @@ -9,7 +9,7 @@ fn collect_two_bufs() { let a = Bytes::from(&b"hello"[..]); let b = Bytes::from(&b"world"[..]); - let res: Vec = a.chain(b).collect(); + let res = a.chain(b).into_bytes(); assert_eq!(res, &b"helloworld"[..]); } From 300ca5d0933f970ae94a64674a6ce0e7590d1a3b Mon Sep 17 00:00:00 2001 From: Douman Date: Tue, 27 Aug 2019 21:59:04 +0200 Subject: [PATCH 5/5] into_bytes -> to_bytes --- src/buf/buf.rs | 15 +++++++++------ src/buf/chain.rs | 26 ++++++++------------------ tests/test_chain.rs | 2 +- 3 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/buf/buf.rs b/src/buf/buf.rs index 6cf5ff217..e126bb460 100644 --- a/src/buf/buf.rs +++ b/src/buf/buf.rs @@ -824,9 +824,9 @@ pub trait Buf { /// ``` /// use bytes::Buf; /// - /// let chain = b"hello "[..].chain(&b"world"[..]); + /// let mut chain = b"hello "[..].chain(&b"world"[..]); /// - /// let full = chain.into_bytes(); + /// let full = chain.to_bytes(); /// assert_eq!(full.bytes(), b"hello world"); /// ``` fn chain(self, next: U) -> Chain @@ -888,18 +888,21 @@ pub trait Buf { super::reader::new(self) } - ///Consumes self and transforms into `Bytes` + /// Consumes remaining bytes inside self and returns new instance of `Bytes` /// /// # Examples /// /// ``` /// use bytes::{Buf}; /// - /// let bytes = "hello world".into_bytes(); + /// let bytes = "hello world".to_bytes(); /// assert_eq!(&bytes[..], &b"hello world"[..]); /// ``` - fn into_bytes(&self) -> crate::Bytes { - self.bytes().into() + fn to_bytes(&mut self) -> crate::Bytes { + use super::BufMut; + let mut ret = crate::BytesMut::with_capacity(self.remaining()); + ret.put(self); + ret.freeze() } } diff --git a/src/buf/chain.rs b/src/buf/chain.rs index 6231a7025..83ece6313 100644 --- a/src/buf/chain.rs +++ b/src/buf/chain.rs @@ -17,10 +17,10 @@ use std::io::{IoSlice, IoSliceMut}; /// use bytes::{Bytes, Buf}; /// use bytes::buf::Chain; /// -/// let buf = Bytes::from(&b"hello "[..]) +/// let mut buf = Bytes::from(&b"hello "[..]) /// .chain(Bytes::from(&b"world"[..])); /// -/// let full: Bytes = buf.into_bytes(); +/// let full: Bytes = buf.to_bytes(); /// assert_eq!(full[..], b"hello world"[..]); /// ``` /// @@ -83,7 +83,7 @@ impl Chain { /// /// buf.first_mut().advance(1); /// - /// let full: Bytes = buf.into_bytes(); + /// let full: Bytes = buf.to_bytes(); /// assert_eq!(full[..], b"ello world"[..]); /// ``` pub fn first_mut(&mut self) -> &mut T { @@ -118,7 +118,7 @@ impl Chain { /// /// buf.last_mut().advance(1); /// - /// let full: Bytes = buf.into_bytes(); + /// let full: Bytes = buf.to_bytes(); /// assert_eq!(full[..], b"hello orld"[..]); /// ``` pub fn last_mut(&mut self) -> &mut U { @@ -184,21 +184,11 @@ impl Buf for Chain n } - fn into_bytes(&self) -> crate::Bytes { - let left = self.a.bytes(); - let right = self.b.bytes(); - - let mut bytes = crate::BytesMut::with_capacity(left.len() + right.len()); - - unsafe { - let len = left.len(); - bytes.bytes_mut()[..len].copy_from_slice(left); - bytes.advance_mut(len); - let len = right.len(); - bytes.bytes_mut()[..len].copy_from_slice(right); - bytes.advance_mut(len); - } + 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() } } diff --git a/tests/test_chain.rs b/tests/test_chain.rs index 466d94bbf..2887575a5 100644 --- a/tests/test_chain.rs +++ b/tests/test_chain.rs @@ -9,7 +9,7 @@ fn collect_two_bufs() { let a = Bytes::from(&b"hello"[..]); let b = Bytes::from(&b"world"[..]); - let res = a.chain(b).into_bytes(); + let res = a.chain(b).to_bytes(); assert_eq!(res, &b"helloworld"[..]); }