diff options
author | 2020-10-16 15:16:23 -0700 | |
---|---|---|
committer | 2020-10-16 15:16:23 -0700 | |
commit | 94c543f74b111e894d16faa43e4ad361b97ee87d (patch) | |
tree | 8cad5f70cc986e8b1952dc52436933e9a5a34f05 | |
parent | 447530b8a6f97fc6864b39d29b24efb4ac9202d3 (diff) | |
download | bytes-94c543f74b111e894d16faa43e4ad361b97ee87d.tar.gz bytes-94c543f74b111e894d16faa43e4ad361b97ee87d.tar.zst bytes-94c543f74b111e894d16faa43e4ad361b97ee87d.zip |
remove ext traits (#431)
-rw-r--r-- | src/buf/buf_impl.rs | 85 | ||||
-rw-r--r-- | src/buf/buf_mut.rs | 81 | ||||
-rw-r--r-- | src/buf/chain.rs (renamed from src/buf/ext/chain.rs) | 12 | ||||
-rw-r--r-- | src/buf/ext/mod.rs | 186 | ||||
-rw-r--r-- | src/buf/limit.rs (renamed from src/buf/ext/limit.rs) | 0 | ||||
-rw-r--r-- | src/buf/mod.rs | 15 | ||||
-rw-r--r-- | src/buf/reader.rs (renamed from src/buf/ext/reader.rs) | 4 | ||||
-rw-r--r-- | src/buf/take.rs (renamed from src/buf/ext/take.rs) | 10 | ||||
-rw-r--r-- | src/buf/writer.rs (renamed from src/buf/ext/writer.rs) | 6 | ||||
-rw-r--r-- | tests/test_chain.rs | 1 | ||||
-rw-r--r-- | tests/test_reader.rs | 6 | ||||
-rw-r--r-- | tests/test_take.rs | 2 |
12 files changed, 199 insertions, 209 deletions
diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 5cd7c68..c26681f 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -1,3 +1,7 @@ +#[cfg(feature = "std")] +use crate::buf::{reader, Reader}; +use crate::buf::{take, Chain, Take}; + use core::{cmp, mem, ptr}; #[cfg(feature = "std")] @@ -807,6 +811,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<Self> + 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<U: Buf>(self, next: U) -> Chain<Self, U> + 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<Self> + 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 ff25cb6..4f6e47d 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -1,3 +1,7 @@ +use crate::buf::{limit, Chain, Limit}; +#[cfg(feature = "std")] +use crate::buf::{writer, Writer}; + use core::{ cmp, mem::{self, MaybeUninit}, @@ -833,6 +837,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<Self> + 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<Self> + 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<U: BufMut>(self, next: U) -> Chain<Self, U> + 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 index 598dde7..020bf08 100644 --- a/src/buf/ext/chain.rs +++ b/src/buf/chain.rs @@ -18,7 +18,7 @@ use std::io::IoSlice; /// # Examples /// /// ``` -/// use bytes::{Bytes, Buf, buf::BufExt}; +/// use bytes::{Bytes, Buf}; /// /// let mut buf = (&b"hello "[..]) /// .chain(&b"world"[..]); @@ -47,7 +47,7 @@ impl<T, U> Chain<T, U> { /// # Examples /// /// ``` - /// use bytes::buf::BufExt; + /// use bytes::Buf; /// /// let buf = (&b"hello"[..]) /// .chain(&b"world"[..]); @@ -63,7 +63,7 @@ impl<T, U> Chain<T, U> { /// # Examples /// /// ``` - /// use bytes::{Buf, buf::BufExt}; + /// use bytes::Buf; /// /// let mut buf = (&b"hello"[..]) /// .chain(&b"world"[..]); @@ -82,7 +82,7 @@ impl<T, U> Chain<T, U> { /// # Examples /// /// ``` - /// use bytes::buf::BufExt; + /// use bytes::Buf; /// /// let buf = (&b"hello"[..]) /// .chain(&b"world"[..]); @@ -98,7 +98,7 @@ impl<T, U> Chain<T, U> { /// # Examples /// /// ``` - /// use bytes::{Buf, buf::BufExt}; + /// use bytes::Buf; /// /// let mut buf = (&b"hello "[..]) /// .chain(&b"world"[..]); @@ -117,7 +117,7 @@ impl<T, U> Chain<T, U> { /// # 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 4a29267..0000000 --- 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<Self> - 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<U: Buf>(self, next: U) -> Chain<Self, U> - 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<Self> - where - Self: Sized, - { - reader::new(self) - } -} - -impl<B: Buf + ?Sized> 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<Self> - 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<Self> - 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<U: BufMut>(self, next: U) -> Chain<Self, U> - where - Self: Sized, - { - Chain::new(self, next) - } -} - -impl<B: BufMut + ?Sized> BufMutExt for B {} diff --git a/src/buf/ext/limit.rs b/src/buf/limit.rs index a36ecee..a36ecee 100644 --- a/src/buf/ext/limit.rs +++ b/src/buf/limit.rs diff --git a/src/buf/mod.rs b/src/buf/mod.rs index ac28681..5c6d5f9 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -18,11 +18,22 @@ 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::ext::{BufExt, BufMutExt}; +pub use self::chain::Chain; 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}; diff --git a/src/buf/ext/reader.rs b/src/buf/reader.rs index dde3548..135db41 100644 --- a/src/buf/ext/reader.rs +++ b/src/buf/reader.rs @@ -24,7 +24,7 @@ impl<B: Buf> Reader<B> { /// # Examples /// /// ```rust - /// use bytes::buf::BufExt; + /// use bytes::Buf; /// /// let buf = b"hello world".reader(); /// @@ -46,7 +46,7 @@ impl<B: Buf> Reader<B> { /// # 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 index 1d84868..57b9f45 100644 --- a/src/buf/ext/take.rs +++ b/src/buf/take.rs @@ -22,7 +22,7 @@ impl<T> Take<T> { /// # 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<T> Take<T> { /// # Examples /// /// ```rust - /// use bytes::{Buf, buf::BufExt}; + /// use bytes::Buf; /// /// let buf = b"hello world".take(2); /// @@ -64,7 +64,7 @@ impl<T> Take<T> { /// # 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<T> Take<T> { /// # Examples /// /// ```rust - /// use bytes::{Buf, buf::BufExt}; + /// use bytes::Buf; /// /// let mut buf = b"hello world".take(2); /// @@ -110,7 +110,7 @@ impl<T> Take<T> { /// # 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 index a14197c..261d7cd 100644 --- a/src/buf/ext/writer.rs +++ b/src/buf/writer.rs @@ -24,7 +24,7 @@ impl<B: BufMut> Writer<B> { /// # Examples /// /// ```rust - /// use bytes::buf::BufMutExt; + /// use bytes::BufMut; /// /// let buf = Vec::with_capacity(1024).writer(); /// @@ -41,7 +41,7 @@ impl<B: BufMut> Writer<B> { /// # Examples /// /// ```rust - /// use bytes::buf::BufMutExt; + /// use bytes::BufMut; /// /// let mut buf = vec![].writer(); /// @@ -58,7 +58,7 @@ impl<B: BufMut> Writer<B> { /// # 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 6dbc45d..9be434f 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 10b480f..897aff6 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 0afb28b..40a1fa5 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() { |