Skip to content

Commit

Permalink
PoC: impl Buf for Bytes
Browse files Browse the repository at this point in the history
This is not meant to be an efficient implementation, but instead as a
proof-of-concept that `Buf` can by implemented for `Bytes` using only
the existing public API.

All tests pass, but a few of the doc-tests are failing because of the
changes to IntoIterator associated types.
  • Loading branch information
danburkert committed Jul 2, 2017
1 parent b9ccd2a commit 6617157
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,11 +766,15 @@ impl Bytes {
}
}

impl IntoBuf for Bytes {
type Buf = Cursor<Self>;

fn into_buf(self) -> Self::Buf {
Cursor::new(self)
impl Buf for Bytes {
fn remaining(&self) -> usize {
self.len()
}
fn bytes(&self) -> &[u8] {
&self[..]
}
fn advance(&mut self, cnt: usize) {
self.split_to(cnt);
}
}

Expand Down Expand Up @@ -886,7 +890,7 @@ impl Borrow<[u8]> for Bytes {

impl IntoIterator for Bytes {
type Item = u8;
type IntoIter = Iter<Cursor<Bytes>>;
type IntoIter = Iter<Bytes>;

fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
Expand All @@ -895,10 +899,10 @@ impl IntoIterator for Bytes {

impl<'a> IntoIterator for &'a Bytes {
type Item = u8;
type IntoIter = Iter<Cursor<&'a Bytes>>;
type IntoIter = Iter<Bytes>;

fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
self.clone().into_iter()
}
}

Expand Down Expand Up @@ -1375,10 +1379,10 @@ impl BufMut for BytesMut {
}

impl IntoBuf for BytesMut {
type Buf = Cursor<Self>;
type Buf = Bytes;

fn into_buf(self) -> Self::Buf {
Cursor::new(self)
self.freeze()
}
}

Expand Down Expand Up @@ -1540,7 +1544,7 @@ impl Clone for BytesMut {

impl IntoIterator for BytesMut {
type Item = u8;
type IntoIter = Iter<Cursor<BytesMut>>;
type IntoIter = Iter<Bytes>;

fn into_iter(self) -> Self::IntoIter {
self.into_buf().iter()
Expand Down

0 comments on commit 6617157

Please sign in to comment.