Skip to content

Commit a90575c

Browse files
tonychainjbowens
authored andcommitted
"allocator" and "nightly" features (for no_std environments)
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.
1 parent 2c6b1d8 commit a90575c

File tree

8 files changed

+73
-14
lines changed

8 files changed

+73
-14
lines changed

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ matrix:
4040
- env: EXTRA_ARGS="--no-default-features"
4141
script: cargo build $EXTRA_ARGS
4242

43+
# Ensure crate compiles with "nightly" feature (i.e. for allocation w\ no_std)
44+
- env: EXTRA_ARGS="--no-default-features --features nightly"
45+
script: cargo build $EXTRA_ARGS
46+
rust: nightly
47+
48+
# Ensure crate compiles with both "std" and "nightly" features
49+
- env: EXTRA_ARGS="--no-default-features --features std,nightly"
50+
script: cargo build $EXTRA_ARGS
51+
rust: nightly
52+
53+
# Allow "nightly" feature to fail (so we aren't blocked on upstream nightly changes)
54+
allow_failures:
55+
- env: EXTRA_ARGS="--no-default-features --features nightly"
56+
script: cargo build $EXTRA_ARGS
57+
rust: nightly
58+
- env: EXTRA_ARGS="--no-default-features --features std,nightly"
59+
script: cargo build $EXTRA_ARGS
60+
rust: nightly
61+
4362
before_install: set -e
4463

4564
install:

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ documentation = "https://carllerche.github.io/bytes/bytes"
99
homepage = "https://github.com/carllerche/bytes"
1010
repository = "https://github.com/carllerche/bytes"
1111
readme = "README.md"
12-
categories = ["no-std"]
1312
keywords = ["buffers", "zero-copy", "io"]
1413
exclude = [
1514
".gitignore",
@@ -18,7 +17,7 @@ exclude = [
1817
"bench/**/*",
1918
"test/**/*"
2019
]
21-
categories = ["network-programming", "data-structures"]
20+
categories = ["network-programming", "data-structures", "no-std"]
2221

2322
[dependencies]
2423
byteorder = "1.0.0"
@@ -32,5 +31,7 @@ optional = true
3231
serde_test = "1.0"
3332

3433
[features]
34+
allocator = []
3535
default = ["std"]
36-
std = ["iovec"]
36+
nightly = ["allocator"]
37+
std = ["allocator", "iovec"]

src/buf/buf.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{IntoBuf, Take, Reader, Iter, Chain};
2-
#[cfg(feature = "std")]
2+
#[cfg(feature = "allocator")]
33
use super::FromBuf;
44

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

14+
#[allow(unused_imports)]
15+
use prelude::*;
16+
1417
/// Read bytes from a buffer.
1518
///
1619
/// A buffer stores bytes in memory such that read operations are infallible.
@@ -528,7 +531,7 @@ pub trait Buf {
528531
///
529532
/// assert_eq!(vec, &b"hello world"[..]);
530533
/// ```
531-
#[cfg(feature = "std")]
534+
#[cfg(feature = "allocator")]
532535
fn collect<B>(self) -> B
533536
where Self: Sized,
534537
B: FromBuf,
@@ -681,7 +684,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
681684
}
682685
}
683686

684-
#[cfg(feature = "std")]
687+
#[cfg(feature = "allocator")]
685688
impl<T: Buf + ?Sized> Buf for Box<T> {
686689
fn remaining(&self) -> usize {
687690
(**self).remaining()

src/buf/buf_mut.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{IntoBuf, Writer};
22
use byteorder::ByteOrder;
3+
34
#[cfg(feature = "std")]
45
use iovec::IoVec;
56

@@ -8,6 +9,9 @@ use core::{cmp, ptr, usize};
89
#[cfg(feature = "std")]
910
use std::io;
1011

12+
#[allow(unused_imports)]
13+
use prelude::*;
14+
1115
/// A trait for values that provide sequential write access to bytes.
1216
///
1317
/// Write bytes to a buffer
@@ -660,7 +664,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
660664
}
661665
}
662666

663-
#[cfg(feature = "std")]
667+
#[cfg(feature = "allocator")]
664668
impl<T: BufMut + ?Sized> BufMut for Box<T> {
665669
fn remaining_mut(&self) -> usize {
666670
(**self).remaining_mut()
@@ -709,7 +713,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
709713
}
710714
}
711715

712-
#[cfg(feature = "std")]
716+
#[cfg(feature = "allocator")]
713717
impl BufMut for Vec<u8> {
714718
#[inline]
715719
fn remaining_mut(&self) -> usize {

src/buf/from_buf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use {Buf, BufMut, IntoBuf, Bytes, BytesMut};
22

3+
#[allow(unused_imports)]
4+
use prelude::*;
5+
36
/// Conversion from a [`Buf`]
47
///
58
/// Implementing `FromBuf` for a type defines how it is created from a buffer.

src/buf/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
1919
mod buf;
2020
mod buf_mut;
21-
#[cfg(feature = "std")]
21+
#[cfg(feature = "allocator")]
2222
mod from_buf;
2323
mod chain;
2424
mod into_buf;
@@ -29,7 +29,7 @@ mod writer;
2929

3030
pub use self::buf::Buf;
3131
pub use self::buf_mut::BufMut;
32-
#[cfg(feature = "std")]
32+
#[cfg(feature = "allocator")]
3333
pub use self::from_buf::FromBuf;
3434
pub use self::chain::Chain;
3535
pub use self::into_buf::IntoBuf;

src/bytes.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use {IntoBuf, Buf, BufMut};
1+
#[cfg(feature = "std")]
2+
use {IntoBuf, Buf};
3+
use BufMut;
4+
#[cfg(feature = "std")]
25
use buf::Iter;
36
use debug;
47

@@ -11,6 +14,9 @@ use core::iter::{FromIterator, Iterator};
1114
#[cfg(feature = "std")]
1215
use std::io;
1316

17+
#[allow(unused_imports)]
18+
use prelude::*;
19+
1420
/// A reference counted contiguous slice of memory.
1521
///
1622
/// `Bytes` is an efficient container for storing and operating on contiguous
@@ -751,6 +757,7 @@ impl Bytes {
751757
}
752758
}
753759

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

@@ -759,6 +766,7 @@ impl IntoBuf for Bytes {
759766
}
760767
}
761768

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

@@ -889,6 +897,7 @@ impl Borrow<[u8]> for Bytes {
889897
}
890898
}
891899

900+
#[cfg(feature = "std")]
892901
impl IntoIterator for Bytes {
893902
type Item = u8;
894903
type IntoIter = Iter<io::Cursor<Bytes>>;
@@ -898,6 +907,7 @@ impl IntoIterator for Bytes {
898907
}
899908
}
900909

910+
#[cfg(feature = "std")]
901911
impl<'a> IntoIterator for &'a Bytes {
902912
type Item = u8;
903913
type IntoIter = Iter<io::Cursor<&'a Bytes>>;
@@ -1373,6 +1383,7 @@ impl BufMut for BytesMut {
13731383
}
13741384
}
13751385

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

@@ -1381,6 +1392,7 @@ impl IntoBuf for BytesMut {
13811392
}
13821393
}
13831394

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

@@ -1540,6 +1552,7 @@ impl Clone for BytesMut {
15401552
}
15411553
}
15421554

1555+
#[cfg(feature = "std")]
15431556
impl IntoIterator for BytesMut {
15441557
type Item = u8;
15451558
type IntoIter = Iter<io::Cursor<BytesMut>>;
@@ -1549,6 +1562,7 @@ impl IntoIterator for BytesMut {
15491562
}
15501563
}
15511564

1565+
#[cfg(feature = "std")]
15521566
impl<'a> IntoIterator for &'a BytesMut {
15531567
type Item = u8;
15541568
type IntoIter = Iter<io::Cursor<&'a BytesMut>>;

src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@
7171
#![deny(warnings, missing_docs, missing_debug_implementations)]
7272
#![doc(html_root_url = "https://docs.rs/bytes/0.4")]
7373
#![cfg_attr(not(feature = "std"), no_std)]
74+
#![cfg_attr(feature = "nightly", feature(alloc))]
7475

7576
extern crate byteorder;
7677

78+
#[cfg(feature = "nightly")]
79+
extern crate alloc;
7780
#[cfg(feature = "std")]
7881
extern crate core;
7982
#[cfg(feature = "std")]
@@ -93,12 +96,12 @@ pub use buf::{
9396
Take,
9497
};
9598

96-
#[cfg(feature = "std")]
99+
#[cfg(feature = "allocator")]
97100
mod bytes;
98-
#[cfg(feature = "std")]
101+
#[cfg(feature = "allocator")]
99102
mod debug;
100103

101-
#[cfg(feature = "std")]
104+
#[cfg(feature = "allocator")]
102105
pub use bytes::{Bytes, BytesMut};
103106

104107
pub use byteorder::{ByteOrder, BigEndian, LittleEndian};
@@ -107,3 +110,15 @@ pub use byteorder::{ByteOrder, BigEndian, LittleEndian};
107110
#[cfg(feature = "serde")]
108111
#[doc(hidden)]
109112
pub mod serde;
113+
114+
/// Custom (internal-only) prelude for this module
115+
mod prelude {
116+
#[cfg(feature = "nightly")]
117+
pub use alloc::boxed::Box;
118+
119+
#[cfg(feature = "nightly")]
120+
pub use alloc::string::String;
121+
122+
#[cfg(feature = "nightly")]
123+
pub use alloc::vec::Vec;
124+
}

0 commit comments

Comments
 (0)