diff --git a/ci/azure-cross-compile.yml b/ci/azure-cross-compile.yml index cdcc197a21a366903aea99ca0d172ee38a534ae0..62840f552f7923c912dbedaee1d9ddbbc52776b6 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 f59b551068a93a77de5eb334829a7a036becf284..b1fb4ba270ff9faa77d3c12f758cc0991c776496 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)?]) diff --git a/src/buf/buf.rs b/src/buf/buf.rs index 97b85fc392f6da8a142b56ee1cff4a9fe3da9cda..e126bb4609768d08457eaf4d1f5f05180f3d0ab4 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<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`. /// /// This function returns a new instance of `Buf` which will read at most @@ -848,16 +824,15 @@ pub trait 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(); - /// assert_eq!(full, b"hello world"); + /// let full = chain.to_bytes(); + /// assert_eq!(full.bytes(), b"hello world"); /// ``` - fn chain<U>(self, next: U) -> Chain<Self, U::Buf> - where U: IntoBuf, - Self: Sized, + fn chain<U: Buf>(self, next: U) -> Chain<Self, U> + 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]; @@ -912,6 +887,23 @@ pub trait Buf { fn reader(self) -> Reader<Self> where Self: Sized { 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 { @@ -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]> { fn remaining(&self) -> usize { if self.is_some() { diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index 05ae794a86dd273bb060fb2348afb72eae8ec2e9..7cd6be803668e943241d32788a2a718c45d0587e 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<T: IntoBuf>(&mut self, src: T) where Self: Sized { - use super::Buf; - - let mut src = src.into_buf(); - + fn put<T: super::Buf>(&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 936a086b7555a9b3a924e967502c787702285e36..83ece6313d0431d2be4f8554f8a70b334ff7ee82 100644 --- a/src/buf/chain.rs +++ b/src/buf/chain.rs @@ -14,13 +14,13 @@ 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 mut buf = Bytes::from(&b"hello "[..]) /// .chain(Bytes::from(&b"world"[..])); /// -/// let full: Bytes = buf.collect(); +/// let full: Bytes = buf.to_bytes(); /// assert_eq!(full[..], b"hello world"[..]); /// ``` /// @@ -60,9 +60,9 @@ impl<T, U> Chain<T, U> { /// # 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,14 +76,14 @@ impl<T, U> Chain<T, U> { /// # 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); /// - /// let full: Bytes = buf.collect(); + /// let full: Bytes = buf.to_bytes(); /// assert_eq!(full[..], b"ello world"[..]); /// ``` pub fn first_mut(&mut self) -> &mut T { @@ -95,9 +95,9 @@ impl<T, U> Chain<T, U> { /// # 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,14 +111,14 @@ impl<T, U> Chain<T, U> { /// # 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); /// - /// let full: Bytes = buf.collect(); + /// let full: Bytes = buf.to_bytes(); /// assert_eq!(full[..], b"hello orld"[..]); /// ``` 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 } + + 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> diff --git a/src/buf/from_buf.rs b/src/buf/from_buf.rs deleted file mode 100644 index db91a95d3111006cb7f584597a5f3b909f3d27d6..0000000000000000000000000000000000000000 --- 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<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 - } -} diff --git a/src/buf/into_buf.rs b/src/buf/into_buf.rs deleted file mode 100644 index 559021ad3abd3bfba887789010eec8981270f787..0000000000000000000000000000000000000000 --- 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<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]) - } -} diff --git a/src/buf/iter.rs b/src/buf/iter.rs index a1bf89b7938562053dac752a94abf5ed95ade1d5..a93e3599b3a9c0990eef9019cace2fa8086f3336 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<T> IntoIter<T> { /// # 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<T> IntoIter<T> { /// # 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<T> IntoIter<T> { /// # 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 a127549a65681e4cc5237011b4ccbc20a10362ea..81e22d3fb0b6bdc7d8f44d6390a1dad5beadb134 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 1cd650f51ddcdae2417ce429424f77e28e0a2f54..4464426b6745bfa0cf838344c37e680ef8441199 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::<Vec<u8>>()[..]); + 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 3f8b23a6ffb8d324b317bac0c6b86e2b0fd4cd94..99022f0b382567127c17a7c49375beb654584f34 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<u8> 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 84bccb2b20a266f902265e4093c4ec901a62e2b2..114de41a57bb714439383f46f841e90393f780e3 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 2c9f1f2c497106f7f833ef550fcafa39502709ea..d545d2b1dd2525ec83e36c79234de4b305ee3de6 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::<u8>(33); + buf.put_u8(33); assert_eq!(b"\x21", &buf[..]); } diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index 70a8205e0090bc7ae5506cdfb7b456f6766f0aa1..3c51ac5e487081ce21c153b81dd1f6814c5c17c3 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 6708028fb3e456d5f9f4c48b352be0d9e02a9848..2887575a50058092434cc043c96a5dfa4b42ddee 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<u8> = a.chain(b).collect(); + let res = a.chain(b).to_bytes(); assert_eq!(res, &b"helloworld"[..]); } @@ -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 5f644e1329ddcefb98f78717587e7d7af057ed84..0000000000000000000000000000000000000000 --- 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<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); -}