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

Remove IntoBuf/FromBuf #288

Merged
merged 5 commits into from Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
68 changes: 37 additions & 31 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 @@ -853,11 +829,10 @@ pub trait Buf {
/// let full: Vec<u8> = chain.collect();
/// assert_eq!(full, 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,20 @@ pub trait Buf {
fn reader(self) -> Reader<Self> where Self: Sized {
super::reader::new(self)
}

///Consumes self and transforms into `Bytes`
DoumanAsh marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
///
/// ```
/// use bytes::{Buf};
///
/// let bytes = "hello world".into_bytes();
/// assert_eq!(&bytes[..], &b"hello world"[..]);
/// ```
fn into_bytes(&self) -> crate::Bytes {
DoumanAsh marked this conversation as resolved.
Show resolved Hide resolved
self.bytes().into()
DoumanAsh marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl<T: Buf + ?Sized> Buf for &mut T {
Expand Down Expand Up @@ -967,6 +956,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
27 changes: 17 additions & 10 deletions src/buf/chain.rs
@@ -1,6 +1,7 @@
use crate::{Buf, BufMut};
use crate::buf::IntoIter;
use std::io::{IoSlice, IoSliceMut};
use core::iter::FromIterator;

/// A `Chain` sequences two buffers.
///
Expand All @@ -14,10 +15,10 @@ 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 buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..]));
///
/// let full: Bytes = buf.collect();
Expand Down Expand Up @@ -60,9 +61,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,9 +77,9 @@ 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);
Expand All @@ -95,9 +96,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,9 +112,9 @@ 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);
Expand Down Expand Up @@ -142,6 +143,12 @@ impl<T, U> Chain<T, U> {
pub fn into_inner(self) -> (T, U) {
(self.a, self.b)
}

#[inline]
/// Consumes self and returns joined value, containing underlying bufs.
pub fn collect<B: FromIterator<u8>>(self) -> B where T: Buf, U: Buf {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting you put this here. What is the reason preventing this from being on the trait? How could all T: Buf types get a collect fn?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on convo in Gitter, I think the way forward for this PR is to just remove collect everywhere. We can always bring it back later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

self.into_iter().collect()
}
}

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

This file was deleted.