Skip to content

Commit d0a74b1

Browse files
committed
"nightly" feature (for using allocation in no_std environments)
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.
1 parent 66998e5 commit d0a74b1

File tree

8 files changed

+62
-11
lines changed

8 files changed

+62
-11
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ serde_test = "1.0"
3333

3434
[features]
3535
default = ["std"]
36+
nightly = []
3637
std = ["iovec"]

src/buf/buf.rs

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

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

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

1113
#[cfg(feature = "std")]
@@ -528,7 +530,7 @@ pub trait Buf {
528530
///
529531
/// assert_eq!(vec, &b"hello world"[..]);
530532
/// ```
531-
#[cfg(feature = "std")]
533+
#[cfg(any(feature = "std", feature = "nightly"))]
532534
fn collect<B>(self) -> B
533535
where Self: Sized,
534536
B: FromBuf,
@@ -681,7 +683,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
681683
}
682684
}
683685

684-
#[cfg(feature = "std")]
686+
#[cfg(any(feature = "std", feature = "nightly"))]
685687
impl<T: Buf + ?Sized> Buf for Box<T> {
686688
fn remaining(&self) -> usize {
687689
(**self).remaining()

src/buf/buf_mut.rs

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

7+
#[cfg(feature = "nightly")]
8+
use alloc::boxed::Box;
9+
#[cfg(feature = "nightly")]
10+
use alloc::vec::Vec;
611
use core::{cmp, ptr, usize};
712

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

663-
#[cfg(feature = "std")]
668+
#[cfg(any(feature = "std", feature = "nightly"))]
664669
impl<T: BufMut + ?Sized> BufMut for Box<T> {
665670
fn remaining_mut(&self) -> usize {
666671
(**self).remaining_mut()
@@ -709,7 +714,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
709714
}
710715
}
711716

712-
#[cfg(feature = "std")]
717+
#[cfg(any(feature = "std", feature = "nightly"))]
713718
impl BufMut for Vec<u8> {
714719
#[inline]
715720
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+
#[cfg(feature = "nightly")]
4+
use alloc::vec::Vec;
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(any(feature = "std", feature = "nightly"))]
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(any(feature = "std", feature = "nightly"))]
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: 19 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

@@ -7,6 +10,13 @@ use core::borrow::Borrow;
710
use core::sync::atomic::{self, AtomicUsize, AtomicPtr};
811
use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
912

13+
#[cfg(feature = "nightly")]
14+
use alloc::boxed::Box;
15+
#[cfg(feature = "nightly")]
16+
use alloc::string::String;
17+
#[cfg(feature = "nightly")]
18+
use alloc::vec::Vec;
19+
1020
#[cfg(feature = "std")]
1121
use std::io;
1222

@@ -768,6 +778,7 @@ impl Bytes {
768778
}
769779
}
770780

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

@@ -776,6 +787,7 @@ impl IntoBuf for Bytes {
776787
}
777788
}
778789

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

@@ -886,6 +898,7 @@ impl Borrow<[u8]> for Bytes {
886898
}
887899
}
888900

901+
#[cfg(feature = "std")]
889902
impl IntoIterator for Bytes {
890903
type Item = u8;
891904
type IntoIter = Iter<io::Cursor<Bytes>>;
@@ -895,6 +908,7 @@ impl IntoIterator for Bytes {
895908
}
896909
}
897910

911+
#[cfg(feature = "std")]
898912
impl<'a> IntoIterator for &'a Bytes {
899913
type Item = u8;
900914
type IntoIter = Iter<io::Cursor<&'a Bytes>>;
@@ -1376,6 +1390,7 @@ impl BufMut for BytesMut {
13761390
}
13771391
}
13781392

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

@@ -1384,6 +1399,7 @@ impl IntoBuf for BytesMut {
13841399
}
13851400
}
13861401

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

@@ -1546,6 +1562,7 @@ impl Clone for BytesMut {
15461562
}
15471563
}
15481564

1565+
#[cfg(feature = "std")]
15491566
impl IntoIterator for BytesMut {
15501567
type Item = u8;
15511568
type IntoIter = Iter<io::Cursor<BytesMut>>;
@@ -1555,6 +1572,7 @@ impl IntoIterator for BytesMut {
15551572
}
15561573
}
15571574

1575+
#[cfg(feature = "std")]
15581576
impl<'a> IntoIterator for &'a BytesMut {
15591577
type Item = u8;
15601578
type IntoIter = Iter<io::Cursor<&'a BytesMut>>;

src/lib.rs

Lines changed: 6 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(any(feature = "std", feature = "nightly"))]
97100
mod bytes;
98-
#[cfg(feature = "std")]
101+
#[cfg(any(feature = "std", feature = "nightly"))]
99102
mod debug;
100103

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

104107
pub use byteorder::{ByteOrder, BigEndian, LittleEndian};

0 commit comments

Comments
 (0)