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 4 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
72 changes: 39 additions & 33 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 @@ -850,14 +826,13 @@ pub trait Buf {
///
/// let chain = b"hello "[..].chain(&b"world"[..]);
///
/// let full: Vec<u8> = chain.collect();
/// assert_eq!(full, b"hello world");
/// let full = chain.into_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,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
44 changes: 31 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 buf = Bytes::from(&b"hello "[..])
/// .chain(Bytes::from(&b"world"[..]));
///
/// let full: Bytes = buf.collect();
/// let full: Bytes = buf.into_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.into_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.into_bytes();
/// assert_eq!(full[..], b"hello orld"[..]);
/// ```
pub fn last_mut(&mut self) -> &mut U {
Expand Down Expand Up @@ -183,6 +183,24 @@ impl<T, U> Buf for Chain<T, U>
n += self.b.bytes_vectored(&mut dst[n..]);
n
}

fn into_bytes(&self) -> crate::Bytes {
let left = self.a.bytes();
let right = self.b.bytes();

let mut bytes = crate::BytesMut::with_capacity(left.len() + right.len());

unsafe {
DoumanAsh marked this conversation as resolved.
Show resolved Hide resolved
let len = left.len();
bytes.bytes_mut()[..len].copy_from_slice(left);
bytes.advance_mut(len);
let len = right.len();
bytes.bytes_mut()[..len].copy_from_slice(right);
bytes.advance_mut(len);
}

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.