Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(body): make Chunk a type alias for bytes::Bytes #2046

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::borrow::Cow;
use std::error::Error as StdError;
use std::fmt;

use bytes::Bytes;
use futures_core::Stream; // for mpsc::Receiver
use futures_channel::{mpsc, oneshot};
#[cfg(feature = "stream")]
Expand Down Expand Up @@ -424,13 +423,6 @@ impl From<Chunk> for Body {
}
}

impl From<Bytes> for Body {
#[inline]
fn from(bytes: Bytes) -> Body {
Body::from(Chunk::from(bytes))
}
}

impl From<Vec<u8>> for Body {
#[inline]
fn from(vec: Vec<u8>) -> Body {
Expand Down
153 changes: 6 additions & 147 deletions src/body/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,157 +1,16 @@
use std::fmt;

use bytes::{Buf, Bytes};
use bytes::{Bytes};

/// A piece of a message body.
///
/// These are returned by [`Body`](::Body). It is an efficient buffer type.
///
/// A `Chunk` can be easily created by many of Rust's standard types that
/// represent a collection of bytes, using `Chunk::from`.
pub struct Chunk {
/// The buffer of bytes making up this body.
bytes: Bytes,
}

// An unexported type to prevent locking `Chunk::into_iter()` to `Bytes::into_iter()`.
#[derive(Debug)]
pub struct IntoIter {
inner: <Bytes as IntoIterator>::IntoIter,
}


impl Chunk {
/// Converts this `Chunk` directly into the `Bytes` type without copies.
///
/// This is simply an inherent alias for `Bytes::from(chunk)`, which exists,
/// but doesn't appear in rustdocs.
#[inline]
pub fn into_bytes(self) -> Bytes {
self.into()
}
}

impl Buf for Chunk {
#[inline]
fn remaining(&self) -> usize {
//perf: Bytes::len() isn't inline yet,
//so it's slightly slower than checking
//the length of the slice.
self.bytes().len()
}

#[inline]
fn bytes(&self) -> &[u8] {
&self.bytes
}

#[inline]
fn advance(&mut self, cnt: usize) {
self.bytes.advance(cnt);
}
}

impl From<Vec<u8>> for Chunk {
#[inline]
fn from(v: Vec<u8>) -> Chunk {
Chunk::from(Bytes::from(v))
}
}

impl From<&'static [u8]> for Chunk {
#[inline]
fn from(slice: &'static [u8]) -> Chunk {
Chunk::from(Bytes::from_static(slice))
}
}

impl From<String> for Chunk {
#[inline]
fn from(s: String) -> Chunk {
s.into_bytes().into()
}
}

impl From<&'static str> for Chunk {
#[inline]
fn from(slice: &'static str) -> Chunk {
slice.as_bytes().into()
}
}

impl From<Bytes> for Chunk {
#[inline]
fn from(bytes: Bytes) -> Chunk {
Chunk {
bytes: bytes,
}
}
}

impl From<Chunk> for Bytes {
#[inline]
fn from(chunk: Chunk) -> Bytes {
chunk.bytes
}
}

impl ::std::ops::Deref for Chunk {
type Target = [u8];

#[inline]
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}

impl AsRef<[u8]> for Chunk {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.bytes
}
}

impl fmt::Debug for Chunk {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.bytes, f)
}
}

impl Default for Chunk {
#[inline]
fn default() -> Chunk {
Chunk::from(Bytes::new())
}
}

impl IntoIterator for Chunk {
type Item = u8;
type IntoIter = IntoIter;

#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter {
inner: self.bytes.into_iter(),
}
}
}

impl Iterator for IntoIter {
type Item = u8;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}

impl ExactSizeIterator for IntoIter {}
///
/// Compatibility note: in Hyper v0.13 this type was changed from a
/// newtype wrapper around `bytes::Bytes` to being merely a type alias.
/// In future releases this type may be removed entirely in favor of `Bytes`.
pub type Chunk = Bytes;

#[cfg(test)]
mod tests {
Expand Down