diff --git a/.travis.yml b/.travis.yml index 2e42d7bc0..71c988994 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/Cargo.toml b/Cargo.toml index 3ba39de57..2760b6716 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", @@ -18,7 +17,7 @@ exclude = [ "bench/**/*", "test/**/*" ] -categories = ["network-programming", "data-structures"] +categories = ["network-programming", "data-structures", "no-std"] [dependencies] byteorder = "1.0.0" @@ -32,5 +31,7 @@ optional = true serde_test = "1.0" [features] +allocator = [] default = ["std"] -std = ["iovec"] +nightly = ["allocator"] +std = ["allocator", "iovec"] diff --git a/src/buf/buf.rs b/src/buf/buf.rs index 6b9ae457c..86c2dc574 100644 --- a/src/buf/buf.rs +++ b/src/buf/buf.rs @@ -1,5 +1,5 @@ use super::{IntoBuf, Take, Reader, Iter, Chain}; -#[cfg(feature = "std")] +#[cfg(feature = "allocator")] use super::FromBuf; use byteorder::ByteOrder; @@ -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. @@ -528,7 +531,7 @@ pub trait Buf { /// /// assert_eq!(vec, &b"hello world"[..]); /// ``` - #[cfg(feature = "std")] + #[cfg(any(feature = "std", feature = "nightly"))] fn collect(self) -> B where Self: Sized, B: FromBuf, @@ -681,7 +684,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T { } } -#[cfg(feature = "std")] +#[cfg(feature = "allocator")] impl Buf for Box { fn remaining(&self) -> usize { (**self).remaining() diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index c480be823..95c510ae3 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -1,5 +1,6 @@ use super::{IntoBuf, Writer}; use byteorder::ByteOrder; + #[cfg(feature = "std")] use iovec::IoVec; @@ -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 @@ -660,7 +664,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T { } } -#[cfg(feature = "std")] +#[cfg(feature = "allocator")] impl BufMut for Box { fn remaining_mut(&self) -> usize { (**self).remaining_mut() @@ -709,7 +713,7 @@ impl + AsRef<[u8]>> BufMut for io::Cursor { } } -#[cfg(feature = "std")] +#[cfg(feature = "allocator")] impl BufMut for Vec { #[inline] fn remaining_mut(&self) -> usize { diff --git a/src/buf/from_buf.rs b/src/buf/from_buf.rs index 55f5cef31..ea06eb34e 100644 --- a/src/buf/from_buf.rs +++ b/src/buf/from_buf.rs @@ -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. diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 335f51962..751b063ed 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -18,7 +18,7 @@ mod buf; mod buf_mut; -#[cfg(feature = "std")] +#[cfg(feature = "allocator")] mod from_buf; mod chain; mod into_buf; @@ -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; diff --git a/src/bytes.rs b/src/bytes.rs index 3af3a74a8..846d58bac 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -1,4 +1,7 @@ -use {IntoBuf, Buf, BufMut}; +#[cfg(feature = "std")] +use {IntoBuf, Buf}; +use BufMut; +#[cfg(feature = "std")] use buf::Iter; use debug; @@ -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 @@ -768,6 +774,7 @@ impl Bytes { } } +#[cfg(feature = "std")] impl IntoBuf for Bytes { type Buf = io::Cursor; @@ -776,6 +783,7 @@ impl IntoBuf for Bytes { } } +#[cfg(feature = "std")] impl<'a> IntoBuf for &'a Bytes { type Buf = io::Cursor; @@ -886,6 +894,7 @@ impl Borrow<[u8]> for Bytes { } } +#[cfg(feature = "std")] impl IntoIterator for Bytes { type Item = u8; type IntoIter = Iter>; @@ -895,6 +904,7 @@ impl IntoIterator for Bytes { } } +#[cfg(feature = "std")] impl<'a> IntoIterator for &'a Bytes { type Item = u8; type IntoIter = Iter>; @@ -1376,6 +1386,7 @@ impl BufMut for BytesMut { } } +#[cfg(feature = "std")] impl IntoBuf for BytesMut { type Buf = io::Cursor; @@ -1384,6 +1395,7 @@ impl IntoBuf for BytesMut { } } +#[cfg(feature = "std")] impl<'a> IntoBuf for &'a BytesMut { type Buf = io::Cursor<&'a BytesMut>; @@ -1546,6 +1558,7 @@ impl Clone for BytesMut { } } +#[cfg(feature = "std")] impl IntoIterator for BytesMut { type Item = u8; type IntoIter = Iter>; @@ -1555,6 +1568,7 @@ impl IntoIterator for BytesMut { } } +#[cfg(feature = "std")] impl<'a> IntoIterator for &'a BytesMut { type Item = u8; type IntoIter = Iter>; diff --git a/src/lib.rs b/src/lib.rs index 8227e8011..29b997d92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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")] @@ -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}; @@ -107,3 +110,15 @@ pub use byteorder::{ByteOrder, BigEndian, LittleEndian}; #[cfg(feature = "serde")] #[doc(hidden)] pub mod serde; + +/// Custom prelude for this module +pub 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; +}