Skip to content

Commit e456afb

Browse files
committed
"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 66998e5 commit e456afb

File tree

8 files changed

+73
-14
lines changed

8 files changed

+73
-14
lines changed

Diff for: .travis.yml

+19
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:

Diff for: Cargo.toml

+4-3
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"]

Diff for: src/buf/buf.rs

+6-3
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(any(feature = "std", feature = "nightly"))]
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()

Diff for: src/buf/buf_mut.rs

+6-2
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 {

Diff for: src/buf/from_buf.rs

+3
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.

Diff for: src/buf/mod.rs

+2-2
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;

Diff for: src/bytes.rs

+15-1
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

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

16+
#[allow(unused_imports)]
17+
use prelude::*;
18+
1319
/// A reference counted contiguous slice of memory.
1420
///
1521
/// `Bytes` is an efficient container for storing and operating on contiguous
@@ -768,6 +774,7 @@ impl Bytes {
768774
}
769775
}
770776

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

@@ -776,6 +783,7 @@ impl IntoBuf for Bytes {
776783
}
777784
}
778785

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

@@ -886,6 +894,7 @@ impl Borrow<[u8]> for Bytes {
886894
}
887895
}
888896

897+
#[cfg(feature = "std")]
889898
impl IntoIterator for Bytes {
890899
type Item = u8;
891900
type IntoIter = Iter<io::Cursor<Bytes>>;
@@ -895,6 +904,7 @@ impl IntoIterator for Bytes {
895904
}
896905
}
897906

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

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

@@ -1384,6 +1395,7 @@ impl IntoBuf for BytesMut {
13841395
}
13851396
}
13861397

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

@@ -1546,6 +1558,7 @@ impl Clone for BytesMut {
15461558
}
15471559
}
15481560

1561+
#[cfg(feature = "std")]
15491562
impl IntoIterator for BytesMut {
15501563
type Item = u8;
15511564
type IntoIter = Iter<io::Cursor<BytesMut>>;
@@ -1555,6 +1568,7 @@ impl IntoIterator for BytesMut {
15551568
}
15561569
}
15571570

1571+
#[cfg(feature = "std")]
15581572
impl<'a> IntoIterator for &'a BytesMut {
15591573
type Item = u8;
15601574
type IntoIter = Iter<io::Cursor<&'a BytesMut>>;

Diff for: src/lib.rs

+18-3
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)