Skip to content

Commit e0982c9

Browse files
committed
Feature gate allocator-dependent features on "use_alloc" or "use_std"
Flags anything dependent on an allocator behind a feature gate, ensuring the library still compiles without default features (although it may not be very useful) Adds "use_alloc" to obtain things like Box, String, and Vec from liballoc.
1 parent 6871b20 commit e0982c9

File tree

10 files changed

+46
-32
lines changed

10 files changed

+46
-32
lines changed

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ matrix:
3737
- env: EXTRA_ARGS="--features serde"
3838

3939
# Ensure crate compiles without default features (i.e. for no_std)
40-
- script: cargo build --no-default-features
40+
- env: EXTRA_ARGS="--no-default-features"
41+
script: cargo build $EXTRA_ARGS
42+
rust: nightly
43+
44+
# Ensure crate compiles with "use_alloc" instead of "use_std" (i.e. for no_std)
45+
- env: EXTRA_ARGS="--no-default-features --features use_alloc"
46+
script: cargo build $EXTRA_ARGS
4147
rust: nightly
4248

4349
before_install: set -e

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ serde_test = "1.0"
3232

3333
[features]
3434
default = ["use_std"]
35+
use_alloc = []
3536
use_std = ["iovec"]

src/buf/buf.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
1+
use super::{IntoBuf, Take, Reader, Iter, Chain};
2+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
3+
use super::FromBuf;
4+
25
use byteorder::ByteOrder;
36
#[cfg(feature = "use_std")]
47
use iovec::IoVec;
58

6-
#[cfg(not(feature = "use_std"))]
9+
#[cfg(feature = "use_alloc")]
710
use alloc::boxed::Box;
811
use core::{cmp, ptr};
9-
#[cfg(not(feature = "use_std"))]
1012
use buf::Cursor;
11-
#[cfg(feature = "use_std")]
12-
use std::io::Cursor;
1313

1414
/// Read bytes from a buffer.
1515
///
@@ -528,6 +528,7 @@ pub trait Buf {
528528
///
529529
/// assert_eq!(vec, &b"hello world"[..]);
530530
/// ```
531+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
531532
fn collect<B>(self) -> B
532533
where Self: Sized,
533534
B: FromBuf,
@@ -680,6 +681,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
680681
}
681682
}
682683

684+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
683685
impl<T: Buf + ?Sized> Buf for Box<T> {
684686
fn remaining(&self) -> usize {
685687
(**self).remaining()

src/buf/buf_mut.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ use byteorder::ByteOrder;
33
#[cfg(feature = "use_std")]
44
use iovec::IoVec;
55

6-
#[cfg(not(feature = "use_std"))]
6+
#[cfg(feature = "use_alloc")]
77
use alloc::boxed::Box;
8-
#[cfg(not(feature = "use_std"))]
8+
#[cfg(feature = "use_alloc")]
99
use alloc::vec::Vec;
1010
use core::{cmp, ptr, usize};
11-
#[cfg(not(feature = "use_std"))]
1211
use buf::Cursor;
13-
#[cfg(feature = "use_std")]
14-
use std::io::Cursor;
1512

1613
/// A trait for values that provide sequential write access to bytes.
1714
///
@@ -665,6 +662,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
665662
}
666663
}
667664

665+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
668666
impl<T: BufMut + ?Sized> BufMut for Box<T> {
669667
fn remaining_mut(&self) -> usize {
670668
(**self).remaining_mut()
@@ -712,6 +710,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for Cursor<T> {
712710
}
713711
}
714712

713+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
715714
impl BufMut for Vec<u8> {
716715
#[inline]
717716
fn remaining_mut(&self) -> usize {

src/buf/cursor.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#[cfg(feature = "use_std")]
12+
pub use std::io::Cursor;
13+
1114
/// A `Cursor` wraps another type and provides it with a
1215
/// [`Seek`] implementation.
1316
///
@@ -18,12 +21,14 @@
1821
/// The standard library implements some I/O traits on various types which
1922
/// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and
2023
/// `Cursor<`[`&[u8]`][bytes]`>`.
24+
#[cfg(not(feature = "use_std"))]
2125
#[derive(Clone, Debug)]
2226
pub struct Cursor<T> {
2327
inner: T,
2428
pos: u64,
2529
}
2630

31+
#[cfg(not(feature = "use_std"))]
2732
impl<T> Cursor<T> {
2833
/// Creates a new cursor wrapping the provided underlying I/O object.
2934
///
@@ -34,9 +39,6 @@ impl<T> Cursor<T> {
3439
Cursor { pos: 0, inner: inner }
3540
}
3641

37-
/// Consumes this cursor, returning the underlying value.
38-
pub fn into_inner(self) -> T { self.inner }
39-
4042
/// Gets a reference to the underlying value in this cursor.
4143
pub fn get_ref(&self) -> &T { &self.inner }
4244

src/buf/from_buf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use {Buf, BufMut, IntoBuf, Bytes, BytesMut};
22

3-
#[cfg(not(feature = "use_std"))]
3+
#[cfg(feature = "use_alloc")]
44
use alloc::vec::Vec;
55

66
/// Conversion from a [`Buf`]

src/buf/into_buf.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use super::{Buf};
22

3-
#[cfg(not(feature = "use_std"))]
4-
use buf::Cursor;
5-
#[cfg(not(feature = "use_std"))]
3+
#[cfg(feature = "use_alloc")]
64
use alloc::string::String;
7-
#[cfg(not(feature = "use_std"))]
5+
#[cfg(feature = "use_alloc")]
86
use alloc::vec::Vec;
9-
#[cfg(feature = "use_std")]
10-
use std::io::Cursor;
7+
8+
use buf::Cursor;
119

1210
/// Conversion into a `Buf`
1311
///
@@ -78,6 +76,7 @@ impl<'a> IntoBuf for &'a str {
7876
}
7977
}
8078

79+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
8180
impl IntoBuf for Vec<u8> {
8281
type Buf = Cursor<Vec<u8>>;
8382

@@ -86,6 +85,7 @@ impl IntoBuf for Vec<u8> {
8685
}
8786
}
8887

88+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
8989
impl<'a> IntoBuf for &'a Vec<u8> {
9090
type Buf = Cursor<&'a [u8]>;
9191

@@ -112,6 +112,7 @@ impl<'a> IntoBuf for &'a &'static str {
112112
}
113113
}
114114

115+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
115116
impl IntoBuf for String {
116117
type Buf = Cursor<Vec<u8>>;
117118

@@ -120,6 +121,7 @@ impl IntoBuf for String {
120121
}
121122
}
122123

124+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
123125
impl<'a> IntoBuf for &'a String {
124126
type Buf = Cursor<&'a [u8]>;
125127

src/buf/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
1919
mod buf;
2020
mod buf_mut;
21+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
2122
mod from_buf;
2223
mod chain;
23-
#[cfg(not(feature = "use_std"))]
2424
mod cursor;
2525
mod into_buf;
2626
mod iter;
@@ -30,9 +30,9 @@ mod writer;
3030

3131
pub use self::buf::Buf;
3232
pub use self::buf_mut::BufMut;
33+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
3334
pub use self::from_buf::FromBuf;
3435
pub use self::chain::Chain;
35-
#[cfg(not(feature = "use_std"))]
3636
pub use self::cursor::Cursor;
3737
pub use self::into_buf::IntoBuf;
3838
pub use self::iter::Iter;

src/bytes.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ use core::borrow::Borrow;
77
use core::sync::atomic::{self, AtomicUsize, AtomicPtr};
88
use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
99

10-
#[cfg(not(feature = "use_std"))]
11-
use buf::Cursor;
12-
#[cfg(not(feature = "use_std"))]
10+
#[cfg(feature = "use_alloc")]
1311
use alloc::boxed::Box;
14-
#[cfg(not(feature = "use_std"))]
12+
#[cfg(feature = "use_alloc")]
1513
use alloc::string::String;
16-
#[cfg(not(feature = "use_std"))]
14+
#[cfg(feature = "use_alloc")]
1715
use alloc::vec::Vec;
18-
#[cfg(feature = "use_std")]
19-
use std::io::Cursor;
16+
17+
use buf::Cursor;
2018

2119
/// A reference counted contiguous slice of memory.
2220
///

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@
7171
#![deny(warnings, missing_docs, missing_debug_implementations)]
7272
#![doc(html_root_url = "https://docs.rs/bytes/0.4")]
7373
#![cfg_attr(not(feature = "use_std"), no_std)]
74-
#![cfg_attr(not(feature = "use_std"), feature(alloc))]
74+
#![cfg_attr(feature = "use_alloc", feature(alloc))]
7575

7676
extern crate byteorder;
7777

78-
#[cfg(not(feature = "use_std"))]
78+
#[cfg(feature = "use_alloc")]
7979
extern crate alloc;
8080
#[cfg(feature = "use_std")]
8181
extern crate core;
@@ -96,8 +96,12 @@ pub use buf::{
9696
Take,
9797
};
9898

99+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
99100
mod bytes;
101+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
100102
mod debug;
103+
104+
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
101105
pub use bytes::{Bytes, BytesMut};
102106

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

0 commit comments

Comments
 (0)