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

"allocator" and "nightly" features (for no_std environments) #153

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ matrix:
# Serde implementation
- env: EXTRA_ARGS="--features serde"

# Ensure crate compiles without default features (i.e. for no_std)
- env: EXTRA_ARGS="--no-default-features"
script: cargo build $EXTRA_ARGS

# Ensure crate compiles with "nightly" feature (i.e. for allocation w\ no_std)
- env: EXTRA_ARGS="--no-default-features --features nightly"
script: cargo build $EXTRA_ARGS
rust: nightly

# Ensure crate compiles with both "std" and "nightly" features
- env: EXTRA_ARGS="--no-default-features --features std,nightly"
script: cargo build $EXTRA_ARGS
rust: nightly

# Allow "nightly" feature to fail (so we aren't blocked on upstream nightly changes)
allow_failures:
- env: EXTRA_ARGS="--no-default-features --features nightly"
script: cargo build $EXTRA_ARGS
rust: nightly
- env: EXTRA_ARGS="--no-default-features --features std,nightly"
script: cargo build $EXTRA_ARGS
rust: nightly

before_install: set -e

install:
Expand Down
13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ exclude = [
"bench/**/*",
"test/**/*"
]
categories = ["network-programming", "data-structures"]
categories = ["network-programming", "data-structures", "no-std"]

[dependencies]
byteorder = "1.0.0"
iovec = "0.1"
serde = { version = "1.0", optional = true }

[dependencies.iovec]
version = "0.1"
optional = true

[dev-dependencies]
serde_test = "1.0"

[features]
allocator = []
default = ["std"]
nightly = ["allocator"]
std = ["allocator", "iovec"]
22 changes: 19 additions & 3 deletions src/buf/buf.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
use super::{IntoBuf, Take, Reader, Iter, Chain};
#[cfg(feature = "allocator")]
use super::FromBuf;

use byteorder::ByteOrder;
#[cfg(feature = "std")]
use iovec::IoVec;

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

#[cfg(feature = "std")]
use std::io;

#[allow(unused_imports)]
use prelude::*;

/// Read bytes from a buffer.
///
Expand All @@ -12,7 +22,7 @@ use std::{cmp, io, ptr};
/// position. It can be thought of as an efficient `Iterator` for collections of
/// bytes.
///
/// The simplest `Buf` is a `Cursor` wrapping a `[u8]`.
/// The simplest `Buf` is a `io::Cursor` wrapping a `[u8]`.
///
/// ```
/// use bytes::Buf;
Expand Down Expand Up @@ -113,6 +123,7 @@ pub trait Buf {
/// with `dst` being a zero length slice.
///
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
#[cfg(feature = "std")]
fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize {
if dst.is_empty() {
return 0;
Expand Down Expand Up @@ -520,6 +531,7 @@ pub trait Buf {
///
/// assert_eq!(vec, &b"hello world"[..]);
/// ```
#[cfg(feature = "allocator")]
fn collect<B>(self) -> B
where Self: Sized,
B: FromBuf,
Expand Down Expand Up @@ -662,6 +674,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
(**self).bytes()
}

#[cfg(feature = "std")]
fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
(**self).bytes_vec(dst)
}
Expand All @@ -671,6 +684,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
}
}

#[cfg(feature = "allocator")]
impl<T: Buf + ?Sized> Buf for Box<T> {
fn remaining(&self) -> usize {
(**self).remaining()
Expand All @@ -680,6 +694,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
(**self).bytes()
}

#[cfg(feature = "std")]
fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
(**self).bytes_vec(dst)
}
Expand All @@ -689,6 +704,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
}
}

#[cfg(feature = "std")]
impl<T: AsRef<[u8]>> Buf for io::Cursor<T> {
fn remaining(&self) -> usize {
let len = self.get_ref().as_ref().len();
Expand Down
18 changes: 16 additions & 2 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use super::{IntoBuf, Writer};
use byteorder::ByteOrder;

#[cfg(feature = "std")]
use iovec::IoVec;

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

#[cfg(feature = "std")]
use std::io;

#[allow(unused_imports)]
use prelude::*;

/// A trait for values that provide sequential write access to bytes.
///
Expand Down Expand Up @@ -188,6 +196,7 @@ pub trait BufMut {
/// with `dst` being a zero length slice.
///
/// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html
#[cfg(feature = "std")]
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize {
if dst.is_empty() {
return 0;
Expand Down Expand Up @@ -645,6 +654,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
(**self).bytes_mut()
}

#[cfg(feature = "std")]
unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize {
(**self).bytes_vec_mut(dst)
}
Expand All @@ -654,6 +664,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
}
}

#[cfg(feature = "allocator")]
impl<T: BufMut + ?Sized> BufMut for Box<T> {
fn remaining_mut(&self) -> usize {
(**self).remaining_mut()
Expand All @@ -663,6 +674,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
(**self).bytes_mut()
}

#[cfg(feature = "std")]
unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize {
(**self).bytes_vec_mut(dst)
}
Expand All @@ -672,6 +684,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
}
}

#[cfg(feature = "std")]
impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
fn remaining_mut(&self) -> usize {
use Buf;
Expand Down Expand Up @@ -700,6 +713,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
}
}

#[cfg(feature = "allocator")]
impl BufMut for Vec<u8> {
#[inline]
fn remaining_mut(&self) -> usize {
Expand All @@ -721,7 +735,7 @@ impl BufMut for Vec<u8> {

#[inline]
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
use std::slice;
use core::slice;

if self.capacity() == self.len() {
self.reserve(64); // Grow the vec
Expand Down
3 changes: 3 additions & 0 deletions src/buf/chain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use {Buf, BufMut};
#[cfg(feature = "std")]
use iovec::IoVec;

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

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

#[cfg(feature = "std")]
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize {
let mut n = self.a.bytes_vec_mut(dst);
n += self.b.bytes_vec_mut(&mut dst[n..]);
Expand Down
3 changes: 3 additions & 0 deletions src/buf/from_buf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use {Buf, BufMut, IntoBuf, Bytes, BytesMut};

#[allow(unused_imports)]
use prelude::*;

/// Conversion from a [`Buf`]
///
/// Implementing `FromBuf` for a type defines how it is created from a buffer.
Expand Down
9 changes: 9 additions & 0 deletions src/buf/into_buf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{Buf};

#[cfg(feature = "std")]
use std::io;

/// Conversion into a `Buf`
Expand Down Expand Up @@ -55,6 +56,7 @@ impl<T: Buf> IntoBuf for T {
}
}

#[cfg(feature = "std")]
impl<'a> IntoBuf for &'a [u8] {
type Buf = io::Cursor<&'a [u8]>;

Expand All @@ -63,6 +65,7 @@ impl<'a> IntoBuf for &'a [u8] {
}
}

#[cfg(feature = "std")]
impl<'a> IntoBuf for &'a str {
type Buf = io::Cursor<&'a [u8]>;

Expand All @@ -71,6 +74,7 @@ impl<'a> IntoBuf for &'a str {
}
}

#[cfg(feature = "std")]
impl IntoBuf for Vec<u8> {
type Buf = io::Cursor<Vec<u8>>;

Expand All @@ -79,6 +83,7 @@ impl IntoBuf for Vec<u8> {
}
}

#[cfg(feature = "std")]
impl<'a> IntoBuf for &'a Vec<u8> {
type Buf = io::Cursor<&'a [u8]>;

Expand All @@ -89,6 +94,7 @@ impl<'a> IntoBuf for &'a Vec<u8> {

// Kind of annoying... but this impl is required to allow passing `&'static
// [u8]` where for<'a> &'a T: IntoBuf is required.
#[cfg(feature = "std")]
impl<'a> IntoBuf for &'a &'static [u8] {
type Buf = io::Cursor<&'static [u8]>;

Expand All @@ -97,6 +103,7 @@ impl<'a> IntoBuf for &'a &'static [u8] {
}
}

#[cfg(feature = "std")]
impl<'a> IntoBuf for &'a &'static str {
type Buf = io::Cursor<&'static [u8]>;

Expand All @@ -105,6 +112,7 @@ impl<'a> IntoBuf for &'a &'static str {
}
}

#[cfg(feature = "std")]
impl IntoBuf for String {
type Buf = io::Cursor<Vec<u8>>;

Expand All @@ -113,6 +121,7 @@ impl IntoBuf for String {
}
}

#[cfg(feature = "std")]
impl<'a> IntoBuf for &'a String {
type Buf = io::Cursor<&'a [u8]>;

Expand Down
2 changes: 2 additions & 0 deletions src/buf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

mod buf;
mod buf_mut;
#[cfg(feature = "allocator")]
mod from_buf;
mod chain;
mod into_buf;
Expand All @@ -28,6 +29,7 @@ mod writer;

pub use self::buf::Buf;
pub use self::buf_mut::BufMut;
#[cfg(feature = "allocator")]
pub use self::from_buf::FromBuf;
pub use self::chain::Chain;
pub use self::into_buf::IntoBuf;
Expand Down
2 changes: 2 additions & 0 deletions src/buf/reader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {Buf};

#[cfg(feature = "std")]
use std::{cmp, io};

/// A `Buf` adapter which implements `io::Read` for the inner value.
Expand Down Expand Up @@ -78,6 +79,7 @@ impl<B: Buf> Reader<B> {
}
}

#[cfg(feature = "std")]
impl<B: Buf + Sized> io::Read for Reader<B> {
fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
let len = cmp::min(self.buf.remaining(), dst.len());
Expand Down
2 changes: 1 addition & 1 deletion src/buf/take.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {Buf};

use std::cmp;
use core::cmp;

/// A `Buf` adapter which limits the bytes read from an underlying buffer.
///
Expand Down
2 changes: 2 additions & 0 deletions src/buf/writer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use BufMut;

#[cfg(feature = "std")]
use std::{cmp, io};

/// A `BufMut` adapter which implements `io::Write` for the inner value.
Expand Down Expand Up @@ -74,6 +75,7 @@ impl<B: BufMut> Writer<B> {
}
}

#[cfg(feature = "std")]
impl<B: BufMut + Sized> io::Write for Writer<B> {
fn write(&mut self, src: &[u8]) -> io::Result<usize> {
let n = cmp::min(self.buf.remaining_mut(), src.len());
Expand Down
Loading