Skip to content

Commit

Permalink
Remove IntoBuf/FromBuf (#288)
Browse files Browse the repository at this point in the history
As consequence Buf::collect is removed as well, which is replaced with `Buf::into_bytes`. The advantage of `Buf::into_bytes` is that it can be optimized in cases where converting a `T: Buf` into a `Bytes` instance is efficient.
  • Loading branch information
Douman authored and carllerche committed Aug 27, 2019
1 parent 79e4b28 commit b6cb346
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 373 deletions.
1 change: 1 addition & 0 deletions ci/azure-cross-compile.yml
Expand Up @@ -37,6 +37,7 @@ jobs:
- script: |
git clone https://github.com/rust-embedded/cross.git
cd cross
git reset --hard fb1cb1d7288151f4349f1cb4c990e0e2281764da #Is broken after this commit (images are not uploaded to new docker hub)
git apply ../ci/cross-patch
cargo install --path .
rm -rf cross
Expand Down
8 changes: 4 additions & 4 deletions ci/cross-patch
@@ -1,5 +1,5 @@
diff --git a/src/docker.rs b/src/docker.rs
index 1525b87..5c9cd54 100644
diff --git a/src/docker.rs b/src/docker.rs
index 6ea745d..15fef81 100644
--- a/src/docker.rs
+++ b/src/docker.rs
@@ -62,7 +62,7 @@ pub fn register(target: &Target, verbose: bool) -> Result<()> {
Expand All @@ -12,8 +12,8 @@ index 1525b87..5c9cd54 100644
.args(&["sh", "-c", cmd])
.run(verbose)
@@ -160,7 +160,7 @@ pub fn run(target: &Target,
.args(&["-v", &format!("{}:/rust:ro", sysroot.display())])
.args(&["-v", &format!("{}:/target", target_dir.display())])
.args(&["-v", &format!("{}:/rust:Z,ro", sysroot.display())])
.args(&["-v", &format!("{}:/target:Z", target_dir.display())])
.args(&["-w", "/project"])
- .args(&["-it", &image(toml, target)?])
+ .args(&["-i", &image(toml, target)?])
Expand Down
77 changes: 43 additions & 34 deletions src/buf/buf.rs
@@ -1,4 +1,4 @@
use super::{IntoBuf, Take, Reader, FromBuf, Chain};
use super::{Take, Reader, Chain};

use std::{cmp, io::IoSlice, ptr, mem};

Expand Down Expand Up @@ -787,30 +787,6 @@ pub trait Buf {
f64::from_bits(Self::get_u64_le(self))
}

/// Transforms a `Buf` into a concrete buffer.
///
/// `collect()` can operate on any value that implements `Buf`, and turn it
/// into the relevant concrete buffer type.
///
/// # Examples
///
/// Collecting a buffer and loading the contents into a `Vec<u8>`.
///
/// ```
/// use bytes::Buf;
///
/// let buf = &b"hello world"[..];
/// let vec: Vec<u8> = buf.collect();
///
/// assert_eq!(vec, b"hello world");
/// ```
fn collect<B>(self) -> B
where Self: Sized,
B: FromBuf,
{
B::from_buf(self)
}

/// 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
Expand Down Expand Up @@ -848,16 +824,15 @@ pub trait Buf {
/// ```
/// use bytes::Buf;
///
/// let chain = b"hello "[..].chain(&b"world"[..]);
/// let mut chain = b"hello "[..].chain(&b"world"[..]);
///
/// let full: Vec<u8> = chain.collect();
/// assert_eq!(full, b"hello world");
/// let full = chain.to_bytes();
/// assert_eq!(full.bytes(), b"hello world");
/// ```
fn chain<U>(self, next: U) -> Chain<Self, U::Buf>
where U: IntoBuf,
Self: Sized,
fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
where Self: Sized
{
Chain::new(self, next.into_buf())
Chain::new(self, next)
}

/// Creates a "by reference" adaptor for this instance of `Buf`.
Expand Down Expand Up @@ -896,10 +871,10 @@ pub trait Buf {
/// # Examples
///
/// ```
/// use bytes::{Buf, IntoBuf, Bytes};
/// use bytes::{Buf, Bytes};
/// use std::io::Read;
///
/// let buf = Bytes::from("hello world").into_buf();
/// let buf = Bytes::from("hello world");
///
/// let mut reader = buf.reader();
/// let mut dst = [0; 1024];
Expand All @@ -912,6 +887,23 @@ pub trait Buf {
fn reader(self) -> Reader<Self> where Self: Sized {
super::reader::new(self)
}

/// Consumes remaining bytes inside self and returns new instance of `Bytes`
///
/// # Examples
///
/// ```
/// use bytes::{Buf};
///
/// let bytes = "hello world".to_bytes();
/// assert_eq!(&bytes[..], &b"hello world"[..]);
/// ```
fn to_bytes(&mut self) -> crate::Bytes {
use super::BufMut;
let mut ret = crate::BytesMut::with_capacity(self.remaining());
ret.put(self);
ret.freeze()
}
}

impl<T: Buf + ?Sized> Buf for &mut T {
Expand Down Expand Up @@ -967,6 +959,23 @@ impl Buf for &[u8] {
}
}

impl Buf for &str {
#[inline]
fn remaining(&self) -> usize {
self.len()
}

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

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

impl Buf for Option<[u8; 1]> {
fn remaining(&self) -> usize {
if self.is_some() {
Expand Down
10 changes: 3 additions & 7 deletions src/buf/buf_mut.rs
@@ -1,4 +1,4 @@
use super::{IntoBuf, Writer};
use super::{Writer};

use std::{mem, cmp, io::IoSliceMut, ptr, usize};

Expand Down Expand Up @@ -208,7 +208,7 @@ pub trait BufMut {
///
/// let mut buf = vec![];
///
/// buf.put(b'h');
/// buf.put_u8(b'h');
/// buf.put(&b"ello"[..]);
/// buf.put(" world");
///
Expand All @@ -218,11 +218,7 @@ pub trait BufMut {
/// # Panics
///
/// Panics if `self` does not have enough capacity to contain `src`.
fn put<T: IntoBuf>(&mut self, src: T) where Self: Sized {
use super::Buf;

let mut src = src.into_buf();

fn put<T: super::Buf>(&mut self, mut src: T) where Self: Sized {
assert!(self.remaining_mut() >= src.remaining());

while src.has_remaining() {
Expand Down
34 changes: 21 additions & 13 deletions src/buf/chain.rs
Expand Up @@ -14,13 +14,13 @@ use std::io::{IoSlice, IoSliceMut};
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf, IntoBuf};
/// use bytes::{Bytes, Buf};
/// use bytes::buf::Chain;
///
/// let buf = Bytes::from(&b"hello "[..]).into_buf()
/// let mut buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..]));
///
/// let full: Bytes = buf.collect();
/// let full: Bytes = buf.to_bytes();
/// assert_eq!(full[..], b"hello world"[..]);
/// ```
///
Expand Down Expand Up @@ -60,9 +60,9 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf, IntoBuf};
/// use bytes::{Bytes, Buf};
///
/// let buf = Bytes::from(&b"hello"[..]).into_buf()
/// let buf = Bytes::from(&b"hello"[..])
/// .chain(Bytes::from(&b"world"[..]));
///
/// assert_eq!(buf.first_ref()[..], b"hello"[..]);
Expand All @@ -76,14 +76,14 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf, IntoBuf};
/// use bytes::{Bytes, Buf};
///
/// let mut buf = Bytes::from(&b"hello "[..]).into_buf()
/// let mut buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..]));
///
/// buf.first_mut().advance(1);
///
/// let full: Bytes = buf.collect();
/// let full: Bytes = buf.to_bytes();
/// assert_eq!(full[..], b"ello world"[..]);
/// ```
pub fn first_mut(&mut self) -> &mut T {
Expand All @@ -95,9 +95,9 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf, IntoBuf};
/// use bytes::{Bytes, Buf};
///
/// let buf = Bytes::from(&b"hello"[..]).into_buf()
/// let buf = Bytes::from(&b"hello"[..])
/// .chain(Bytes::from(&b"world"[..]));
///
/// assert_eq!(buf.last_ref()[..], b"world"[..]);
Expand All @@ -111,14 +111,14 @@ impl<T, U> Chain<T, U> {
/// # Examples
///
/// ```
/// use bytes::{Bytes, Buf, IntoBuf};
/// use bytes::{Bytes, Buf};
///
/// let mut buf = Bytes::from(&b"hello "[..]).into_buf()
/// let mut buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..]));
///
/// buf.last_mut().advance(1);
///
/// let full: Bytes = buf.collect();
/// let full: Bytes = buf.to_bytes();
/// assert_eq!(full[..], b"hello orld"[..]);
/// ```
pub fn last_mut(&mut self) -> &mut U {
Expand Down Expand Up @@ -183,6 +183,14 @@ impl<T, U> Buf for Chain<T, U>
n += self.b.bytes_vectored(&mut dst[n..]);
n
}

fn to_bytes(&mut self) -> crate::Bytes {
let mut bytes: crate::BytesMut = self.a.to_bytes().try_mut()
.unwrap_or_else(|bytes| bytes.into());

bytes.put(&mut self.b);
bytes.freeze()
}
}

impl<T, U> BufMut for Chain<T, U>
Expand Down
117 changes: 0 additions & 117 deletions src/buf/from_buf.rs

This file was deleted.

0 comments on commit b6cb346

Please sign in to comment.