diff --git a/tokio-buf/Cargo.toml b/tokio-buf/Cargo.toml index 2eb938a50..83a904854 100644 --- a/tokio-buf/Cargo.toml +++ b/tokio-buf/Cargo.toml @@ -22,5 +22,5 @@ either = { version = "1.5", optional = true} futures = "0.1.23" [features] -default = ["ext"] -ext = ["bytes/either", "either"] +default = ["util"] +util = ["bytes/either", "either"] diff --git a/tokio-buf/src/errors.rs b/tokio-buf/src/errors.rs deleted file mode 100644 index d87f1f8c2..000000000 --- a/tokio-buf/src/errors.rs +++ /dev/null @@ -1,35 +0,0 @@ -//! Error types - -#[cfg(feature = "ext")] -pub use ext::CollectError; -#[cfg(feature = "ext")] -pub use ext::CollectVecError; -#[cfg(feature = "ext")] -pub use ext::LimitError; - -// Being crate-private, we should be able to swap the type out in a -// backwards compatible way. -pub(crate) mod internal { - use std::{error, fmt}; - - /// An error that can never occur - pub enum Never {} - - impl fmt::Debug for Never { - fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { - match *self {} - } - } - - impl fmt::Display for Never { - fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { - match *self {} - } - } - - impl error::Error for Never { - fn description(&self) -> &str { - match *self {} - } - } -} diff --git a/tokio-buf/src/lib.rs b/tokio-buf/src/lib.rs index b5da17b1f..0c4d7bd83 100644 --- a/tokio-buf/src/lib.rs +++ b/tokio-buf/src/lib.rs @@ -1,5 +1,5 @@ #![doc(html_root_url = "https://docs.rs/tokio-buf/0.1.0")] -#![deny(missing_docs, missing_debug_implementations)] +#![deny(missing_docs, missing_debug_implementations, unreachable_pub)] #![cfg_attr(test, deny(warnings))] //! Asynchronous stream of bytes. @@ -10,27 +10,26 @@ //! `Buf` (i.e, byte collections). extern crate bytes; -#[cfg(feature = "ext")] +#[cfg(feature = "util")] extern crate either; #[allow(unused)] #[macro_use] extern crate futures; -pub mod errors; -#[cfg(feature = "ext")] -pub mod ext; +mod never; +#[cfg(feature = "util")] +pub mod util; mod size_hint; mod str; +mod u8; pub use self::size_hint::SizeHint; #[doc(inline)] -#[cfg(feature = "ext")] -pub use ext::BufStreamExt; +#[cfg(feature = "util")] +pub use util::BufStreamExt; -use bytes::{Buf, Bytes, BytesMut}; -use errors::internal::Never; +use bytes::Buf; use futures::Poll; -use std::io; /// An asynchronous stream of bytes. /// @@ -98,64 +97,3 @@ pub trait BufStream { SizeHint::default() } } - -impl BufStream for Vec { - type Item = io::Cursor>; - type Error = Never; - - fn poll_buf(&mut self) -> Poll, Self::Error> { - if self.is_empty() { - return Ok(None.into()); - } - - poll_bytes(self) - } -} - -impl BufStream for &'static [u8] { - type Item = io::Cursor<&'static [u8]>; - type Error = Never; - - fn poll_buf(&mut self) -> Poll, Self::Error> { - if self.is_empty() { - return Ok(None.into()); - } - - poll_bytes(self) - } -} - -impl BufStream for Bytes { - type Item = io::Cursor; - type Error = Never; - - fn poll_buf(&mut self) -> Poll, Self::Error> { - if self.is_empty() { - return Ok(None.into()); - } - - poll_bytes(self) - } -} - -impl BufStream for BytesMut { - type Item = io::Cursor; - type Error = Never; - - fn poll_buf(&mut self) -> Poll, Self::Error> { - if self.is_empty() { - return Ok(None.into()); - } - - poll_bytes(self) - } -} - -fn poll_bytes(buf: &mut T) -> Poll>, Never> { - use std::mem; - - let bytes = mem::replace(buf, Default::default()); - let buf = io::Cursor::new(bytes); - - Ok(Some(buf).into()) -} diff --git a/tokio-buf/src/never.rs b/tokio-buf/src/never.rs new file mode 100644 index 000000000..dcbde39c5 --- /dev/null +++ b/tokio-buf/src/never.rs @@ -0,0 +1,22 @@ +use std::{error, fmt}; + +/// An error that can never occur +pub enum Never {} + +impl fmt::Debug for Never { + fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { + match *self {} + } +} + +impl fmt::Display for Never { + fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { + match *self {} + } +} + +impl error::Error for Never { + fn description(&self) -> &str { + match *self {} + } +} diff --git a/tokio-buf/src/str.rs b/tokio-buf/src/str.rs index 4b73ad005..b8a6eb643 100644 --- a/tokio-buf/src/str.rs +++ b/tokio-buf/src/str.rs @@ -1,4 +1,4 @@ -use errors::internal::Never; +use never::Never; use BufStream; use futures::Poll; diff --git a/tokio-buf/src/u8.rs b/tokio-buf/src/u8.rs new file mode 100644 index 000000000..dece1d7c9 --- /dev/null +++ b/tokio-buf/src/u8.rs @@ -0,0 +1,66 @@ +use BufStream; +use never::Never; +use bytes::{Bytes, BytesMut}; +use futures::Poll; +use std::io; + +impl BufStream for Vec { + type Item = io::Cursor>; + type Error = Never; + + fn poll_buf(&mut self) -> Poll, Self::Error> { + if self.is_empty() { + return Ok(None.into()); + } + + poll_bytes(self) + } +} + +impl BufStream for &'static [u8] { + type Item = io::Cursor<&'static [u8]>; + type Error = Never; + + fn poll_buf(&mut self) -> Poll, Self::Error> { + if self.is_empty() { + return Ok(None.into()); + } + + poll_bytes(self) + } +} + +impl BufStream for Bytes { + type Item = io::Cursor; + type Error = Never; + + fn poll_buf(&mut self) -> Poll, Self::Error> { + if self.is_empty() { + return Ok(None.into()); + } + + poll_bytes(self) + } +} + +impl BufStream for BytesMut { + type Item = io::Cursor; + type Error = Never; + + fn poll_buf(&mut self) -> Poll, Self::Error> { + if self.is_empty() { + return Ok(None.into()); + } + + poll_bytes(self) + } +} + +fn poll_bytes(buf: &mut T) -> Poll>, Never> { + use std::mem; + + let bytes = mem::replace(buf, Default::default()); + let buf = io::Cursor::new(bytes); + + Ok(Some(buf).into()) +} diff --git a/tokio-buf/src/ext/chain.rs b/tokio-buf/src/util/chain.rs similarity index 100% rename from tokio-buf/src/ext/chain.rs rename to tokio-buf/src/util/chain.rs diff --git a/tokio-buf/src/ext/collect.rs b/tokio-buf/src/util/collect.rs similarity index 100% rename from tokio-buf/src/ext/collect.rs rename to tokio-buf/src/util/collect.rs diff --git a/tokio-buf/src/ext/from.rs b/tokio-buf/src/util/from.rs similarity index 100% rename from tokio-buf/src/ext/from.rs rename to tokio-buf/src/util/from.rs diff --git a/tokio-buf/src/ext/limit.rs b/tokio-buf/src/util/limit.rs similarity index 100% rename from tokio-buf/src/ext/limit.rs rename to tokio-buf/src/util/limit.rs diff --git a/tokio-buf/src/ext/mod.rs b/tokio-buf/src/util/mod.rs similarity index 92% rename from tokio-buf/src/ext/mod.rs rename to tokio-buf/src/util/mod.rs index f0299129c..2a3dfbf7f 100644 --- a/tokio-buf/src/ext/mod.rs +++ b/tokio-buf/src/util/mod.rs @@ -10,9 +10,13 @@ pub use self::collect::Collect; pub use self::from::FromBufStream; pub use self::limit::Limit; -pub use self::collect::CollectError; -pub use self::from::CollectVecError; -pub use self::limit::LimitError; +pub mod error { + //! Error types + + pub use super::collect::CollectError; + pub use super::from::CollectVecError; + pub use super::limit::LimitError; +} use BufStream;