Skip to content

Commit

Permalink
"nightly" feature (for using allocation in no_std environments)
Browse files Browse the repository at this point in the history
To use Box, String, and Vec in no_std environments, we need to obtain them from
liballoc, which is presently unstable and therefore requires nightly.

So as to avoid any confusion as to the (in)stability of this approach, the
feature is named "nightly".

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 18, 2017
1 parent 66998e5 commit d0a74b1
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 11 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ serde_test = "1.0"

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

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

#[cfg(feature = "nightly")]
use alloc::boxed::Box;
use core::{cmp, ptr};

#[cfg(feature = "std")]
Expand Down Expand Up @@ -528,7 +530,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 +683,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
}
}

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

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

#[cfg(feature = "nightly")]
use alloc::boxed::Box;
#[cfg(feature = "nightly")]
use alloc::vec::Vec;
use core::{cmp, ptr, usize};

#[cfg(feature = "std")]
Expand Down Expand Up @@ -660,7 +665,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
}
}

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

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "nightly"))]
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};

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

/// 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(any(feature = "std", feature = "nightly"))]
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(any(feature = "std", feature = "nightly"))]
pub use self::from_buf::FromBuf;
pub use self::chain::Chain;
pub use self::into_buf::IntoBuf;
Expand Down
20 changes: 19 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 @@ -7,6 +10,13 @@ use core::borrow::Borrow;
use core::sync::atomic::{self, AtomicUsize, AtomicPtr};
use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};

#[cfg(feature = "nightly")]
use alloc::boxed::Box;
#[cfg(feature = "nightly")]
use alloc::string::String;
#[cfg(feature = "nightly")]
use alloc::vec::Vec;

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

Expand Down Expand Up @@ -768,6 +778,7 @@ impl Bytes {
}
}

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

Expand All @@ -776,6 +787,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 +898,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 +908,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 +1390,7 @@ impl BufMut for BytesMut {
}
}

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

Expand All @@ -1384,6 +1399,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 +1562,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 +1572,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
9 changes: 6 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(any(feature = "std", feature = "nightly"))]
mod bytes;
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "nightly"))]
mod debug;

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

pub use byteorder::{ByteOrder, BigEndian, LittleEndian};
Expand Down

0 comments on commit d0a74b1

Please sign in to comment.