Skip to content

Commit

Permalink
Replace iovec with std::io::IoSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Jun 7, 2019
1 parent 47fdf06 commit 566679b
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 71 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Expand Up @@ -3,7 +3,8 @@ dist: trusty
language: rust
services: docker
sudo: required
rust: stable
#rust: stable
rust: beta # we need 1.36, which is still beta

env:
global:
Expand All @@ -19,7 +20,7 @@ matrix:
#
# This job will also build and deploy the docs to gh-pages.
- env: TARGET=x86_64-unknown-linux-gnu
rust: 1.27.0
#rust: 1.36.0 (not stable yet)
after_success:
- |
pip install 'travis-cargo<0.2' --user &&
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -30,7 +30,6 @@ features = ["i128"]

[dependencies]
byteorder = "1.1.0"
iovec = { git = "https://github.com/carllerche/iovec" }
serde = { version = "1.0", optional = true }
either = { version = "1.5", default-features = false, optional = true }

Expand Down
13 changes: 6 additions & 7 deletions src/buf/buf.rs
@@ -1,8 +1,7 @@
use super::{IntoBuf, Take, Reader, FromBuf, Chain};
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use iovec::IoVec;

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

macro_rules! buf_get_impl {
($this:ident, $size:expr, $conv:path) => ({
Expand Down Expand Up @@ -120,7 +119,7 @@ pub trait Buf {
/// current position.
///
/// If the `Buf` is backed by disjoint slices of bytes, `bytes_vec` enables
/// fetching more than one slice at once. `dst` is a slice of `IoVec`
/// fetching more than one slice at once. `dst` is a slice of `IoSlice`
/// references, enabling the slice to be directly used with [`writev`]
/// without any further conversion. The sum of the lengths of all the
/// buffers in `dst` will be less than or equal to `Buf::remaining()`.
Expand All @@ -143,13 +142,13 @@ pub trait Buf {
/// with `dst` being a zero length slice.
///
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
fn bytes_vec<'a>(&'a self, dst: &mut [IoVec<'a>]) -> usize {
fn bytes_vec<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
if dst.is_empty() {
return 0;
}

if self.has_remaining() {
dst[0] = self.bytes().into();
dst[0] = IoSlice::new(self.bytes());
1
} else {
0
Expand Down Expand Up @@ -926,7 +925,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
(**self).bytes()
}

fn bytes_vec<'b>(&'b self, dst: &mut [IoVec<'b>]) -> usize {
fn bytes_vec<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
(**self).bytes_vec(dst)
}

Expand All @@ -944,7 +943,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
(**self).bytes()
}

fn bytes_vec<'b>(&'b self, dst: &mut [IoVec<'b>]) -> usize {
fn bytes_vec<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
(**self).bytes_vec(dst)
}

Expand Down
13 changes: 6 additions & 7 deletions src/buf/buf_mut.rs
@@ -1,8 +1,7 @@
use super::{IntoBuf, Writer};
use byteorder::{LittleEndian, ByteOrder, BigEndian};
use iovec::IoVecMut;

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

/// A trait for values that provide sequential write access to bytes.
///
Expand Down Expand Up @@ -164,7 +163,7 @@ pub trait BufMut {
///
/// If the `BufMut` is backed by disjoint slices of bytes, `bytes_vec_mut`
/// enables fetching more than one slice at once. `dst` is a slice of
/// mutable `IoVec` references, enabling the slice to be directly used with
/// mutable `IoSliceMut` references, enabling the slice to be directly used with
/// [`readv`] without any further conversion. The sum of the lengths of all
/// the buffers in `dst` will be less than or equal to
/// `Buf::remaining_mut()`.
Expand All @@ -187,13 +186,13 @@ pub trait BufMut {
/// with `dst` being a zero length slice.
///
/// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [IoVecMut<'a>]) -> usize {
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [IoSliceMut<'a>]) -> usize {
if dst.is_empty() {
return 0;
}

if self.has_remaining_mut() {
dst[0] = self.bytes_mut().into();
dst[0] = IoSliceMut::new(self.bytes_mut());
1
} else {
0
Expand Down Expand Up @@ -989,7 +988,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
(**self).bytes_mut()
}

unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [IoVecMut<'b>]) -> usize {
unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
(**self).bytes_vec_mut(dst)
}

Expand All @@ -1007,7 +1006,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
(**self).bytes_mut()
}

unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [IoVecMut<'b>]) -> usize {
unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
(**self).bytes_vec_mut(dst)
}

Expand Down
6 changes: 3 additions & 3 deletions src/buf/chain.rs
@@ -1,6 +1,6 @@
use {Buf, BufMut};
use buf::IntoIter;
use iovec::{IoVec, IoVecMut};
use std::io::{IoSlice, IoSliceMut};

/// A `Chain` sequences two buffers.
///
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<T, U> Buf for Chain<T, U>
self.b.advance(cnt);
}

fn bytes_vec<'a>(&'a self, dst: &mut [IoVec<'a>]) -> usize {
fn bytes_vec<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
let mut n = self.a.bytes_vec(dst);
n += self.b.bytes_vec(&mut dst[n..]);
n
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<T, U> BufMut for Chain<T, U>
self.b.advance_mut(cnt);
}

unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [IoVecMut<'a>]) -> usize {
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [IoSliceMut<'a>]) -> usize {
let mut n = self.a.bytes_vec_mut(dst);
n += self.b.bytes_vec_mut(&mut dst[n..]);
n
Expand Down
6 changes: 3 additions & 3 deletions src/either.rs
Expand Up @@ -4,7 +4,7 @@ use {Buf, BufMut};

use self::either::Either;
use self::either::Either::*;
use iovec::{IoVec, IoVecMut};
use std::io::{IoSlice, IoSliceMut};

impl<L, R> Buf for Either<L, R>
where
Expand All @@ -25,7 +25,7 @@ where
}
}

fn bytes_vec<'a>(&'a self, dst: &mut [IoVec<'a>]) -> usize {
fn bytes_vec<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
match *self {
Left(ref b) => b.bytes_vec(dst),
Right(ref b) => b.bytes_vec(dst),
Expand Down Expand Up @@ -66,7 +66,7 @@ where
}
}

unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [IoVecMut<'a>]) -> usize {
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [IoSliceMut<'a>]) -> usize {
match *self {
Left(ref mut b) => b.bytes_vec_mut(dst),
Right(ref mut b) => b.bytes_vec_mut(dst),
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Expand Up @@ -72,7 +72,6 @@
#![doc(html_root_url = "https://docs.rs/bytes/0.5.0")]

extern crate byteorder;
extern crate iovec;

pub mod buf;
pub use buf::{
Expand Down
10 changes: 4 additions & 6 deletions tests/test_buf.rs
@@ -1,9 +1,8 @@
extern crate bytes;
extern crate byteorder;
extern crate iovec;

use bytes::Buf;
use iovec::IoVec;
use std::io::IoSlice;

#[test]
fn test_fresh_cursor_vec() {
Expand Down Expand Up @@ -48,11 +47,10 @@ fn test_get_u16_buffer_underflow() {
fn test_bufs_vec() {
let buf = &b"hello world"[..];

let b1: &[u8] = &mut [0];
let b2: &[u8] = &mut [0];
let b1: &[u8] = &mut [];
let b2: &[u8] = &mut [];

let mut dst: [IoVec; 2] =
[b1.into(), b2.into()];
let mut dst = [IoSlice::new(b1), IoSlice::new(b2)];

assert_eq!(1, buf.bytes_vec(&mut dst[..]));
}
9 changes: 4 additions & 5 deletions tests/test_buf_mut.rs
@@ -1,11 +1,10 @@
extern crate bytes;
extern crate byteorder;
extern crate iovec;

use bytes::{BufMut, BytesMut};
use iovec::IoVecMut;
use std::usize;
use std::fmt::Write;
use std::io::IoSliceMut;

#[test]
fn test_vec_as_mut_buf() {
Expand Down Expand Up @@ -72,12 +71,12 @@ fn test_clone() {

#[test]
fn test_bufs_vec_mut() {
use std::mem;

let mut buf = BytesMut::from(&b"hello world"[..]);
let b1: &mut [u8] = &mut [];
let b2: &mut [u8] = &mut [];
let mut dst = [IoSliceMut::new(b1), IoSliceMut::new(b2)];

unsafe {
let mut dst: [IoVecMut; 2] = mem::zeroed();
assert_eq!(1, buf.bytes_vec_mut(&mut dst[..]));
}
}
Expand Down
87 changes: 51 additions & 36 deletions tests/test_chain.rs
@@ -1,9 +1,8 @@
extern crate bytes;
extern crate iovec;

use bytes::{Buf, BufMut, Bytes, BytesMut};
use bytes::buf::Chain;
use iovec::IoVec;
use std::io::IoSlice;

#[test]
fn collect_two_bufs() {
Expand Down Expand Up @@ -54,68 +53,84 @@ fn vectored_read() {
let mut buf = a.chain(b);

{
let b1: &[u8] = &mut [0];
let b2: &[u8] = &mut [0];
let b3: &[u8] = &mut [0];
let b4: &[u8] = &mut [0];
let mut iovecs: [IoVec; 4] =
[b1.into(), b2.into(), b3.into(), b4.into()];
let b1: &[u8] = &mut [];
let b2: &[u8] = &mut [];
let b3: &[u8] = &mut [];
let b4: &[u8] = &mut [];
let mut iovecs = [
IoSlice::new(b1),
IoSlice::new(b2),
IoSlice::new(b3),
IoSlice::new(b4),
];

assert_eq!(2, buf.bytes_vec(&mut iovecs));
assert_eq!(iovecs[0][..], b"hello"[..]);
assert_eq!(iovecs[1][..], b"world"[..]);
assert_eq!(iovecs[2][..], b"\0"[..]);
assert_eq!(iovecs[3][..], b"\0"[..]);
assert_eq!(iovecs[2][..], b""[..]);
assert_eq!(iovecs[3][..], b""[..]);
}

buf.advance(2);

{
let b1: &[u8] = &mut [0];
let b2: &[u8] = &mut [0];
let b3: &[u8] = &mut [0];
let b4: &[u8] = &mut [0];
let mut iovecs: [IoVec; 4] =
[b1.into(), b2.into(), b3.into(), b4.into()];
let b1: &[u8] = &mut [];
let b2: &[u8] = &mut [];
let b3: &[u8] = &mut [];
let b4: &[u8] = &mut [];
let mut iovecs = [
IoSlice::new(b1),
IoSlice::new(b2),
IoSlice::new(b3),
IoSlice::new(b4),
];

assert_eq!(2, buf.bytes_vec(&mut iovecs));
assert_eq!(iovecs[0][..], b"llo"[..]);
assert_eq!(iovecs[1][..], b"world"[..]);
assert_eq!(iovecs[2][..], b"\0"[..]);
assert_eq!(iovecs[3][..], b"\0"[..]);
assert_eq!(iovecs[2][..], b""[..]);
assert_eq!(iovecs[3][..], b""[..]);
}

buf.advance(3);

{
let b1: &[u8] = &mut [0];
let b2: &[u8] = &mut [0];
let b3: &[u8] = &mut [0];
let b4: &[u8] = &mut [0];
let mut iovecs: [IoVec; 4] =
[b1.into(), b2.into(), b3.into(), b4.into()];
let b1: &[u8] = &mut [];
let b2: &[u8] = &mut [];
let b3: &[u8] = &mut [];
let b4: &[u8] = &mut [];
let mut iovecs = [
IoSlice::new(b1),
IoSlice::new(b2),
IoSlice::new(b3),
IoSlice::new(b4),
];

assert_eq!(1, buf.bytes_vec(&mut iovecs));
assert_eq!(iovecs[0][..], b"world"[..]);
assert_eq!(iovecs[1][..], b"\0"[..]);
assert_eq!(iovecs[2][..], b"\0"[..]);
assert_eq!(iovecs[3][..], b"\0"[..]);
assert_eq!(iovecs[1][..], b""[..]);
assert_eq!(iovecs[2][..], b""[..]);
assert_eq!(iovecs[3][..], b""[..]);
}

buf.advance(3);

{
let b1: &[u8] = &mut [0];
let b2: &[u8] = &mut [0];
let b3: &[u8] = &mut [0];
let b4: &[u8] = &mut [0];
let mut iovecs: [IoVec; 4] =
[b1.into(), b2.into(), b3.into(), b4.into()];
let b1: &[u8] = &mut [];
let b2: &[u8] = &mut [];
let b3: &[u8] = &mut [];
let b4: &[u8] = &mut [];
let mut iovecs = [
IoSlice::new(b1),
IoSlice::new(b2),
IoSlice::new(b3),
IoSlice::new(b4),
];

assert_eq!(1, buf.bytes_vec(&mut iovecs));
assert_eq!(iovecs[0][..], b"ld"[..]);
assert_eq!(iovecs[1][..], b"\0"[..]);
assert_eq!(iovecs[2][..], b"\0"[..]);
assert_eq!(iovecs[3][..], b"\0"[..]);
assert_eq!(iovecs[1][..], b""[..]);
assert_eq!(iovecs[2][..], b""[..]);
assert_eq!(iovecs[3][..], b""[..]);
}
}

0 comments on commit 566679b

Please sign in to comment.