From 6617157bda5131e0acc7bf331a0f76cccfb9593e Mon Sep 17 00:00:00 2001 From: Dan Burkert Date: Sat, 1 Jul 2017 17:29:39 -0700 Subject: [PATCH] PoC: impl Buf for Bytes 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. --- src/bytes.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index 88a8c8f29..ef086ea61 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -766,11 +766,15 @@ impl Bytes { } } -impl IntoBuf for Bytes { - type Buf = Cursor; - - 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); } } @@ -886,7 +890,7 @@ impl Borrow<[u8]> for Bytes { impl IntoIterator for Bytes { type Item = u8; - type IntoIter = Iter>; + type IntoIter = Iter; fn into_iter(self) -> Self::IntoIter { self.into_buf().iter() @@ -895,10 +899,10 @@ impl IntoIterator for Bytes { impl<'a> IntoIterator for &'a Bytes { type Item = u8; - type IntoIter = Iter>; + type IntoIter = Iter; fn into_iter(self) -> Self::IntoIter { - self.into_buf().iter() + self.clone().into_iter() } } @@ -1375,10 +1379,10 @@ impl BufMut for BytesMut { } impl IntoBuf for BytesMut { - type Buf = Cursor; + type Buf = Bytes; fn into_buf(self) -> Self::Buf { - Cursor::new(self) + self.freeze() } } @@ -1540,7 +1544,7 @@ impl Clone for BytesMut { impl IntoIterator for BytesMut { type Item = u8; - type IntoIter = Iter>; + type IntoIter = Iter; fn into_iter(self) -> Self::IntoIter { self.into_buf().iter()