From fc3a9465b84e0ffd3ad4a7f88bde6b93f3902086 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 16 Oct 2020 11:09:06 -0700 Subject: [PATCH 1/3] remove ext traits --- src/buf/buf_impl.rs | 83 ++++++++++++++++ src/buf/buf_mut.rs | 79 +++++++++++++++ src/buf/{ext => }/chain.rs | 12 +-- src/buf/ext/mod.rs | 186 ------------------------------------ src/buf/{ext => }/limit.rs | 0 src/buf/mod.rs | 15 ++- src/buf/{ext => }/reader.rs | 4 +- src/buf/{ext => }/take.rs | 10 +- src/buf/{ext => }/writer.rs | 6 +- tests/test_chain.rs | 1 - tests/test_reader.rs | 6 +- tests/test_take.rs | 2 +- 12 files changed, 195 insertions(+), 209 deletions(-) rename src/buf/{ext => }/chain.rs (95%) delete mode 100644 src/buf/ext/mod.rs rename src/buf/{ext => }/limit.rs (100%) rename src/buf/{ext => }/reader.rs (96%) rename src/buf/{ext => }/take.rs (94%) rename src/buf/{ext => }/writer.rs (94%) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 5cd7c686e..4ed239429 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -1,3 +1,5 @@ +use crate::buf::{Chain, reader, Reader, take, Take}; + use core::{cmp, mem, ptr}; #[cfg(feature = "std")] @@ -807,6 +809,87 @@ pub trait Buf { ret.put(self); ret.freeze() } + + /// 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 + /// `limit` bytes. + /// + /// # Examples + /// + /// ``` + /// use bytes::{Buf, BufMut}; + /// + /// let mut buf = b"hello world"[..].take(5); + /// let mut dst = vec![]; + /// + /// dst.put(&mut buf); + /// assert_eq!(dst, b"hello"); + /// + /// let mut buf = buf.into_inner(); + /// dst.clear(); + /// dst.put(&mut buf); + /// assert_eq!(dst, b" world"); + /// ``` + fn take(self, limit: usize) -> Take + where + Self: Sized, + { + take::new(self, limit) + } + + /// Creates an adaptor which will chain this buffer with another. + /// + /// The returned `Buf` instance will first consume all bytes from `self`. + /// Afterwards the output is equivalent to the output of next. + /// + /// # Examples + /// + /// ``` + /// use bytes::Buf; + /// + /// let mut chain = b"hello "[..].chain(&b"world"[..]); + /// + /// let full = chain.to_bytes(); + /// assert_eq!(full.bytes(), b"hello world"); + /// ``` + fn chain(self, next: U) -> Chain + where + Self: Sized, + { + Chain::new(self, next) + } + + /// Creates an adaptor which implements the `Read` trait for `self`. + /// + /// This function returns a new value which implements `Read` by adapting + /// the `Read` trait functions to the `Buf` trait functions. Given that + /// `Buf` operations are infallible, none of the `Read` functions will + /// return with `Err`. + /// + /// # Examples + /// + /// ``` + /// use bytes::{Bytes, Buf}; + /// use std::io::Read; + /// + /// let buf = Bytes::from("hello world"); + /// + /// let mut reader = buf.reader(); + /// let mut dst = [0; 1024]; + /// + /// let num = reader.read(&mut dst).unwrap(); + /// + /// assert_eq!(11, num); + /// assert_eq!(&dst[..11], &b"hello world"[..]); + /// ``` + #[cfg(feature = "std")] + fn reader(self) -> Reader + where + Self: Sized, + { + reader::new(self) + } } macro_rules! deref_forward_buf { diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index 628b240a3..a258e6f8a 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -1,3 +1,5 @@ +use crate::buf::{Chain, limit, Limit, writer, Writer}; + use core::{ cmp, mem::{self, MaybeUninit}, @@ -878,6 +880,83 @@ pub trait BufMut { fn put_f64_le(&mut self, n: f64) { self.put_u64_le(n.to_bits()); } + + /// Creates an adaptor which can write at most `limit` bytes to `self`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let arr = &mut [0u8; 128][..]; + /// assert_eq!(arr.remaining_mut(), 128); + /// + /// let dst = arr.limit(10); + /// assert_eq!(dst.remaining_mut(), 10); + /// ``` + fn limit(self, limit: usize) -> Limit + where + Self: Sized, + { + limit::new(self, limit) + } + + /// Creates an adaptor which implements the `Write` trait for `self`. + /// + /// This function returns a new value which implements `Write` by adapting + /// the `Write` trait functions to the `BufMut` trait functions. Given that + /// `BufMut` operations are infallible, none of the `Write` functions will + /// return with `Err`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// use std::io::Write; + /// + /// let mut buf = vec![].writer(); + /// + /// let num = buf.write(&b"hello world"[..]).unwrap(); + /// assert_eq!(11, num); + /// + /// let buf = buf.into_inner(); + /// + /// assert_eq!(*buf, b"hello world"[..]); + /// ``` + #[cfg(feature = "std")] + fn writer(self) -> Writer + where + Self: Sized, + { + writer::new(self) + } + + /// Creates an adapter which will chain this buffer with another. + /// + /// The returned `BufMut` instance will first write to all bytes from + /// `self`. Afterwards, it will write to `next`. + /// + /// # Examples + /// + /// ``` + /// use bytes::BufMut; + /// + /// let mut a = [0u8; 5]; + /// let mut b = [0u8; 6]; + /// + /// let mut chain = (&mut a[..]).chain_mut(&mut b[..]); + /// + /// chain.put_slice(b"hello world"); + /// + /// assert_eq!(&a[..], b"hello"); + /// assert_eq!(&b[..], b" world"); + /// ``` + fn chain_mut(self, next: U) -> Chain + where + Self: Sized, + { + Chain::new(self, next) + } } macro_rules! deref_forward_bufmut { diff --git a/src/buf/ext/chain.rs b/src/buf/chain.rs similarity index 95% rename from src/buf/ext/chain.rs rename to src/buf/chain.rs index e62e2f1b9..46b6f5753 100644 --- a/src/buf/ext/chain.rs +++ b/src/buf/chain.rs @@ -20,7 +20,7 @@ use std::io::IoSlice; /// # Examples /// /// ``` -/// use bytes::{Bytes, Buf, buf::BufExt}; +/// use bytes::{Bytes, Buf}; /// /// let mut buf = (&b"hello "[..]) /// .chain(&b"world"[..]); @@ -49,7 +49,7 @@ impl Chain { /// # Examples /// /// ``` - /// use bytes::buf::BufExt; + /// use bytes::Buf; /// /// let buf = (&b"hello"[..]) /// .chain(&b"world"[..]); @@ -65,7 +65,7 @@ impl Chain { /// # Examples /// /// ``` - /// use bytes::{Buf, buf::BufExt}; + /// use bytes::Buf; /// /// let mut buf = (&b"hello"[..]) /// .chain(&b"world"[..]); @@ -84,7 +84,7 @@ impl Chain { /// # Examples /// /// ``` - /// use bytes::buf::BufExt; + /// use bytes::Buf; /// /// let buf = (&b"hello"[..]) /// .chain(&b"world"[..]); @@ -100,7 +100,7 @@ impl Chain { /// # Examples /// /// ``` - /// use bytes::{Buf, buf::BufExt}; + /// use bytes::Buf; /// /// let mut buf = (&b"hello "[..]) /// .chain(&b"world"[..]); @@ -119,7 +119,7 @@ impl Chain { /// # Examples /// /// ``` - /// use bytes::buf::BufExt; + /// use bytes::Buf; /// /// let chain = (&b"hello"[..]) /// .chain(&b"world"[..]); diff --git a/src/buf/ext/mod.rs b/src/buf/ext/mod.rs deleted file mode 100644 index 4a292676a..000000000 --- a/src/buf/ext/mod.rs +++ /dev/null @@ -1,186 +0,0 @@ -//! Extra utilities for `Buf` and `BufMut` types. - -use super::{Buf, BufMut}; - -mod chain; -mod limit; -#[cfg(feature = "std")] -mod reader; -mod take; -#[cfg(feature = "std")] -mod writer; - -pub use self::chain::Chain; -pub use self::limit::Limit; -pub use self::take::Take; - -#[cfg(feature = "std")] -pub use self::{reader::Reader, writer::Writer}; - -/// Extra methods for implementations of `Buf`. -pub trait BufExt: Buf { - /// 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 - /// `limit` bytes. - /// - /// # Examples - /// - /// ``` - /// use bytes::{BufMut, buf::BufExt}; - /// - /// let mut buf = b"hello world"[..].take(5); - /// let mut dst = vec![]; - /// - /// dst.put(&mut buf); - /// assert_eq!(dst, b"hello"); - /// - /// let mut buf = buf.into_inner(); - /// dst.clear(); - /// dst.put(&mut buf); - /// assert_eq!(dst, b" world"); - /// ``` - fn take(self, limit: usize) -> Take - where - Self: Sized, - { - take::new(self, limit) - } - - /// Creates an adaptor which will chain this buffer with another. - /// - /// The returned `Buf` instance will first consume all bytes from `self`. - /// Afterwards the output is equivalent to the output of next. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Buf, buf::BufExt}; - /// - /// let mut chain = b"hello "[..].chain(&b"world"[..]); - /// - /// let full = chain.to_bytes(); - /// assert_eq!(full.bytes(), b"hello world"); - /// ``` - fn chain(self, next: U) -> Chain - where - Self: Sized, - { - Chain::new(self, next) - } - - /// Creates an adaptor which implements the `Read` trait for `self`. - /// - /// This function returns a new value which implements `Read` by adapting - /// the `Read` trait functions to the `Buf` trait functions. Given that - /// `Buf` operations are infallible, none of the `Read` functions will - /// return with `Err`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{Bytes, buf::BufExt}; - /// use std::io::Read; - /// - /// let buf = Bytes::from("hello world"); - /// - /// let mut reader = buf.reader(); - /// let mut dst = [0; 1024]; - /// - /// let num = reader.read(&mut dst).unwrap(); - /// - /// assert_eq!(11, num); - /// assert_eq!(&dst[..11], &b"hello world"[..]); - /// ``` - #[cfg(feature = "std")] - fn reader(self) -> Reader - where - Self: Sized, - { - reader::new(self) - } -} - -impl BufExt for B {} - -/// Extra methods for implementations of `BufMut`. -pub trait BufMutExt: BufMut { - /// Creates an adaptor which can write at most `limit` bytes to `self`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{BufMut, buf::BufMutExt}; - /// - /// let arr = &mut [0u8; 128][..]; - /// assert_eq!(arr.remaining_mut(), 128); - /// - /// let dst = arr.limit(10); - /// assert_eq!(dst.remaining_mut(), 10); - /// ``` - fn limit(self, limit: usize) -> Limit - where - Self: Sized, - { - limit::new(self, limit) - } - - /// Creates an adaptor which implements the `Write` trait for `self`. - /// - /// This function returns a new value which implements `Write` by adapting - /// the `Write` trait functions to the `BufMut` trait functions. Given that - /// `BufMut` operations are infallible, none of the `Write` functions will - /// return with `Err`. - /// - /// # Examples - /// - /// ``` - /// use bytes::buf::BufMutExt; - /// use std::io::Write; - /// - /// let mut buf = vec![].writer(); - /// - /// let num = buf.write(&b"hello world"[..]).unwrap(); - /// assert_eq!(11, num); - /// - /// let buf = buf.into_inner(); - /// - /// assert_eq!(*buf, b"hello world"[..]); - /// ``` - #[cfg(feature = "std")] - fn writer(self) -> Writer - where - Self: Sized, - { - writer::new(self) - } - - /// Creates an adapter which will chain this buffer with another. - /// - /// The returned `BufMut` instance will first write to all bytes from - /// `self`. Afterwards, it will write to `next`. - /// - /// # Examples - /// - /// ``` - /// use bytes::{BufMut, buf::BufMutExt}; - /// - /// let mut a = [0u8; 5]; - /// let mut b = [0u8; 6]; - /// - /// let mut chain = (&mut a[..]).chain_mut(&mut b[..]); - /// - /// chain.put_slice(b"hello world"); - /// - /// assert_eq!(&a[..], b"hello"); - /// assert_eq!(&b[..], b" world"); - /// ``` - fn chain_mut(self, next: U) -> Chain - where - Self: Sized, - { - Chain::new(self, next) - } -} - -impl BufMutExt for B {} diff --git a/src/buf/ext/limit.rs b/src/buf/limit.rs similarity index 100% rename from src/buf/ext/limit.rs rename to src/buf/limit.rs diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 1d7292c9e..e9f63b5db 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -18,13 +18,24 @@ mod buf_impl; mod buf_mut; -pub mod ext; +mod chain; mod iter; +mod limit; +#[cfg(feature = "std")] +mod reader; +mod take; mod vec_deque; +#[cfg(feature = "std")] +mod writer; pub use self::buf_impl::Buf; pub use self::buf_mut::BufMut; +pub use self::chain::Chain; #[cfg(feature = "std")] pub use self::buf_mut::IoSliceMut; -pub use self::ext::{BufExt, BufMutExt}; pub use self::iter::IntoIter; +pub use self::limit::Limit; +pub use self::take::Take; + +#[cfg(feature = "std")] +pub use self::{reader::Reader, writer::Writer}; \ No newline at end of file diff --git a/src/buf/ext/reader.rs b/src/buf/reader.rs similarity index 96% rename from src/buf/ext/reader.rs rename to src/buf/reader.rs index dde3548bf..135db4172 100644 --- a/src/buf/ext/reader.rs +++ b/src/buf/reader.rs @@ -24,7 +24,7 @@ impl Reader { /// # Examples /// /// ```rust - /// use bytes::buf::BufExt; + /// use bytes::Buf; /// /// let buf = b"hello world".reader(); /// @@ -46,7 +46,7 @@ impl Reader { /// # Examples /// /// ```rust - /// use bytes::{Buf, buf::BufExt}; + /// use bytes::Buf; /// use std::io; /// /// let mut buf = b"hello world".reader(); diff --git a/src/buf/ext/take.rs b/src/buf/take.rs similarity index 94% rename from src/buf/ext/take.rs rename to src/buf/take.rs index 1d84868bf..57b9f4547 100644 --- a/src/buf/ext/take.rs +++ b/src/buf/take.rs @@ -22,7 +22,7 @@ impl Take { /// # Examples /// /// ```rust - /// use bytes::buf::{BufMut, BufExt}; + /// use bytes::{Buf, BufMut}; /// /// let mut buf = b"hello world".take(2); /// let mut dst = vec![]; @@ -47,7 +47,7 @@ impl Take { /// # Examples /// /// ```rust - /// use bytes::{Buf, buf::BufExt}; + /// use bytes::Buf; /// /// let buf = b"hello world".take(2); /// @@ -64,7 +64,7 @@ impl Take { /// # Examples /// /// ```rust - /// use bytes::{Buf, BufMut, buf::BufExt}; + /// use bytes::{Buf, BufMut}; /// /// let mut buf = b"hello world".take(2); /// let mut dst = vec![]; @@ -88,7 +88,7 @@ impl Take { /// # Examples /// /// ```rust - /// use bytes::{Buf, buf::BufExt}; + /// use bytes::Buf; /// /// let mut buf = b"hello world".take(2); /// @@ -110,7 +110,7 @@ impl Take { /// # Examples /// /// ```rust - /// use bytes::{BufMut, buf::BufExt}; + /// use bytes::{Buf, BufMut}; /// /// let mut buf = b"hello world".take(2); /// let mut dst = vec![]; diff --git a/src/buf/ext/writer.rs b/src/buf/writer.rs similarity index 94% rename from src/buf/ext/writer.rs rename to src/buf/writer.rs index a14197c81..261d7cd09 100644 --- a/src/buf/ext/writer.rs +++ b/src/buf/writer.rs @@ -24,7 +24,7 @@ impl Writer { /// # Examples /// /// ```rust - /// use bytes::buf::BufMutExt; + /// use bytes::BufMut; /// /// let buf = Vec::with_capacity(1024).writer(); /// @@ -41,7 +41,7 @@ impl Writer { /// # Examples /// /// ```rust - /// use bytes::buf::BufMutExt; + /// use bytes::BufMut; /// /// let mut buf = vec![].writer(); /// @@ -58,7 +58,7 @@ impl Writer { /// # Examples /// /// ```rust - /// use bytes::buf::BufMutExt; + /// use bytes::BufMut; /// use std::io; /// /// let mut buf = vec![].writer(); diff --git a/tests/test_chain.rs b/tests/test_chain.rs index 6dbc45d04..9be434f37 100644 --- a/tests/test_chain.rs +++ b/tests/test_chain.rs @@ -1,6 +1,5 @@ #![warn(rust_2018_idioms)] -use bytes::buf::{BufExt, BufMutExt}; use bytes::{Buf, BufMut, Bytes}; #[cfg(feature = "std")] use std::io::IoSlice; diff --git a/tests/test_reader.rs b/tests/test_reader.rs index 10b480fcc..897aff645 100644 --- a/tests/test_reader.rs +++ b/tests/test_reader.rs @@ -3,13 +3,13 @@ use std::io::{BufRead, Read}; -use bytes::buf::BufExt; +use bytes::Buf; #[test] fn read() { let buf1 = &b"hello "[..]; let buf2 = &b"world"[..]; - let buf = BufExt::chain(buf1, buf2); // Disambiguate with Read::chain + let buf = Buf::chain(buf1, buf2); // Disambiguate with Read::chain let mut buffer = Vec::new(); buf.reader().read_to_end(&mut buffer).unwrap(); assert_eq!(b"hello world", &buffer[..]); @@ -19,7 +19,7 @@ fn read() { fn buf_read() { let buf1 = &b"hell"[..]; let buf2 = &b"o\nworld"[..]; - let mut reader = BufExt::chain(buf1, buf2).reader(); + let mut reader = Buf::chain(buf1, buf2).reader(); let mut line = String::new(); reader.read_line(&mut line).unwrap(); assert_eq!("hello\n", &line); diff --git a/tests/test_take.rs b/tests/test_take.rs index 0afb28bb4..40a1fa53d 100644 --- a/tests/test_take.rs +++ b/tests/test_take.rs @@ -1,6 +1,6 @@ #![warn(rust_2018_idioms)] -use bytes::buf::{Buf, BufExt}; +use bytes::buf::Buf; #[test] fn long_take() { From 19474592a2509fea4509537b4575f1d8810d6323 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 16 Oct 2020 15:00:05 -0700 Subject: [PATCH 2/3] fix build --- src/buf/buf_impl.rs | 4 +++- src/buf/buf_mut.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 4ed239429..bd925027e 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -1,4 +1,6 @@ -use crate::buf::{Chain, reader, Reader, take, Take}; +use crate::buf::{Chain, take, Take}; +#[cfg(feature = "std")] +use crate::buf::{reader, Reader}; use core::{cmp, mem, ptr}; diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index abc7a2b0f..0730d1612 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -1,4 +1,6 @@ -use crate::buf::{Chain, limit, Limit, writer, Writer}; +use crate::buf::{Chain, limit, Limit}; +#[cfg(feature = "std")] +use crate::buf::{writer, Writer}; use core::{ cmp, From 448d94f430e776e888a0546a6650ce81fbf51cd4 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 16 Oct 2020 15:00:30 -0700 Subject: [PATCH 3/3] fmt --- src/buf/buf_impl.rs | 2 +- src/buf/buf_mut.rs | 2 +- src/buf/mod.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index bd925027e..c26681fb0 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -1,6 +1,6 @@ -use crate::buf::{Chain, take, Take}; #[cfg(feature = "std")] use crate::buf::{reader, Reader}; +use crate::buf::{take, Chain, Take}; use core::{cmp, mem, ptr}; diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index 0730d1612..4f6e47d32 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -1,4 +1,4 @@ -use crate::buf::{Chain, limit, Limit}; +use crate::buf::{limit, Chain, Limit}; #[cfg(feature = "std")] use crate::buf::{writer, Writer}; diff --git a/src/buf/mod.rs b/src/buf/mod.rs index a4a53a9f3..5c6d5f9d5 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -36,4 +36,4 @@ pub use self::limit::Limit; pub use self::take::Take; #[cfg(feature = "std")] -pub use self::{reader::Reader, writer::Writer}; \ No newline at end of file +pub use self::{reader::Reader, writer::Writer};