Skip to content

Commit

Permalink
"allocator" and "nightly" features (for no_std environments)
Browse files Browse the repository at this point in the history
This commit gates all allocator-dependent features on an "allocator" feature.

It also adds a "nightly" feature which enables using the "allocator" feature in
no_std environments. This requires using #[feature(alloc)] which requires
nightly.

The "allocator" feature is automatically enabled when either the "std" or
"nightly" features are enabled.

Travis CI is configured to check that builds succeed with both the "nightly"
feature along with "std" and "nightly" in combination. To avoid the problem
of nightly changes breaking the build, these combinations are specifically
flagged as allowed failures in the Travis CI configuration.
  • Loading branch information
tonychain committed Jul 20, 2017
1 parent 66998e5 commit e456afb
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 14 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ matrix:
- 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
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ documentation = "https://carllerche.github.io/bytes/bytes"
homepage = "https://github.com/carllerche/bytes"
repository = "https://github.com/carllerche/bytes"
readme = "README.md"
categories = ["no-std"]
keywords = ["buffers", "zero-copy", "io"]
exclude = [
".gitignore",
Expand All @@ -18,7 +17,7 @@ exclude = [
"bench/**/*",
"test/**/*"
]
categories = ["network-programming", "data-structures"]
categories = ["network-programming", "data-structures", "no-std"]

[dependencies]
byteorder = "1.0.0"
Expand All @@ -32,5 +31,7 @@ optional = true
serde_test = "1.0"

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

use byteorder::ByteOrder;
Expand All @@ -11,6 +11,9 @@ use core::{cmp, ptr};
#[cfg(feature = "std")]
use std::io;

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

/// Read bytes from a buffer.
///
/// A buffer stores bytes in memory such that read operations are infallible.
Expand Down Expand Up @@ -528,7 +531,7 @@ pub trait Buf {
///
/// assert_eq!(vec, &b"hello world"[..]);
/// ```
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "nightly"))]
fn collect<B>(self) -> B
where Self: Sized,
B: FromBuf,
Expand Down Expand Up @@ -681,7 +684,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
}
}

#[cfg(feature = "std")]
#[cfg(feature = "allocator")]
impl<T: Buf + ?Sized> Buf for Box<T> {
fn remaining(&self) -> usize {
(**self).remaining()
Expand Down
8 changes: 6 additions & 2 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{IntoBuf, Writer};
use byteorder::ByteOrder;

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

Expand All @@ -8,6 +9,9 @@ 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.
///
/// Write bytes to a buffer
Expand Down Expand Up @@ -660,7 +664,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
}
}

#[cfg(feature = "std")]
#[cfg(feature = "allocator")]
impl<T: BufMut + ?Sized> BufMut for Box<T> {
fn remaining_mut(&self) -> usize {
(**self).remaining_mut()
Expand Down Expand Up @@ -709,7 +713,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
}
}

#[cfg(feature = "std")]
#[cfg(feature = "allocator")]
impl BufMut for Vec<u8> {
#[inline]
fn remaining_mut(&self) -> usize {
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
4 changes: 2 additions & 2 deletions src/buf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

pub use self::buf::Buf;
pub use self::buf_mut::BufMut;
#[cfg(feature = "std")]
#[cfg(feature = "allocator")]
pub use self::from_buf::FromBuf;
pub use self::chain::Chain;
pub use self::into_buf::IntoBuf;
Expand Down
16 changes: 15 additions & 1 deletion src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use {IntoBuf, Buf, BufMut};
#[cfg(feature = "std")]
use {IntoBuf, Buf};
use BufMut;
#[cfg(feature = "std")]
use buf::Iter;
use debug;

Expand All @@ -10,6 +13,9 @@ use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
#[cfg(feature = "std")]
use std::io;

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

/// A reference counted contiguous slice of memory.
///
/// `Bytes` is an efficient container for storing and operating on contiguous
Expand Down Expand Up @@ -768,6 +774,7 @@ impl Bytes {
}
}

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

Expand All @@ -776,6 +783,7 @@ impl IntoBuf for Bytes {
}
}

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

Expand Down Expand Up @@ -886,6 +894,7 @@ impl Borrow<[u8]> for Bytes {
}
}

#[cfg(feature = "std")]
impl IntoIterator for Bytes {
type Item = u8;
type IntoIter = Iter<io::Cursor<Bytes>>;
Expand All @@ -895,6 +904,7 @@ impl IntoIterator for Bytes {
}
}

#[cfg(feature = "std")]
impl<'a> IntoIterator for &'a Bytes {
type Item = u8;
type IntoIter = Iter<io::Cursor<&'a Bytes>>;
Expand Down Expand Up @@ -1376,6 +1386,7 @@ impl BufMut for BytesMut {
}
}

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

Expand All @@ -1384,6 +1395,7 @@ impl IntoBuf for BytesMut {
}
}

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

Expand Down Expand Up @@ -1546,6 +1558,7 @@ impl Clone for BytesMut {
}
}

#[cfg(feature = "std")]
impl IntoIterator for BytesMut {
type Item = u8;
type IntoIter = Iter<io::Cursor<BytesMut>>;
Expand All @@ -1555,6 +1568,7 @@ impl IntoIterator for BytesMut {
}
}

#[cfg(feature = "std")]
impl<'a> IntoIterator for &'a BytesMut {
type Item = u8;
type IntoIter = Iter<io::Cursor<&'a BytesMut>>;
Expand Down
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@
#![deny(warnings, missing_docs, missing_debug_implementations)]
#![doc(html_root_url = "https://docs.rs/bytes/0.4")]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "nightly", feature(alloc))]

extern crate byteorder;

#[cfg(feature = "nightly")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate core;
#[cfg(feature = "std")]
Expand All @@ -93,12 +96,12 @@ pub use buf::{
Take,
};

#[cfg(feature = "std")]
#[cfg(feature = "allocator")]
mod bytes;
#[cfg(feature = "std")]
#[cfg(feature = "allocator")]
mod debug;

#[cfg(feature = "std")]
#[cfg(feature = "allocator")]
pub use bytes::{Bytes, BytesMut};

pub use byteorder::{ByteOrder, BigEndian, LittleEndian};
Expand All @@ -107,3 +110,15 @@ pub use byteorder::{ByteOrder, BigEndian, LittleEndian};
#[cfg(feature = "serde")]
#[doc(hidden)]
pub mod serde;

/// Custom (internal-only) prelude for this module
mod prelude {
#[cfg(feature = "nightly")]
pub use alloc::boxed::Box;

#[cfg(feature = "nightly")]
pub use alloc::string::String;

#[cfg(feature = "nightly")]
pub use alloc::vec::Vec;
}

0 comments on commit e456afb

Please sign in to comment.