Skip to content

Commit 28faf90

Browse files
committed
alloc support (for no_std environments)
Gates all allocator-dependent features on the 'alloc' cargo feature, allowing use in no_std environments which have a heap and defined allocator, but not full support for 'std'.
1 parent 18f2a7e commit 28faf90

19 files changed

+111
-28
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ matrix:
3737
- env: EXTRA_ARGS="--no-default-features"
3838
script: cargo build $EXTRA_ARGS
3939

40+
# `alloc` crate-based implementation
41+
- env: EXTRA_ARGS="--no-default-features --features=alloc --lib --tests"
42+
rust: nightly # TODO: test 'alloc' on stable rust when it is fully stabilized
43+
4044
# Serde implementation
4145
- env: EXTRA_ARGS="--features serde"
4246

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ serde_test = "1.0"
3838

3939
[features]
4040
default = ["std"]
41+
alloc = []
4142
i128 = ["byteorder/i128"]
42-
std = ["iovec"]
43+
std = ["alloc", "iovec"]

src/buf/buf.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
22
use byteorder::{BigEndian, ByteOrder, LittleEndian};
33
#[cfg(feature = "iovec")]
44
use iovec::IoVec;
5-
#[cfg(feature = "std")]
5+
#[cfg(feature = "alloc")]
66
use prelude::*;
77

88
use core::{cmp, ptr};
@@ -1077,7 +1077,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
10771077
}
10781078
}
10791079

1080-
#[cfg(feature = "std")]
1080+
#[cfg(feature = "alloc")]
10811081
impl<T: Buf + ?Sized> Buf for Box<T> {
10821082
fn remaining(&self) -> usize {
10831083
(**self).remaining()

src/buf/buf_mut.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{IntoBuf, Writer};
22
use byteorder::{LittleEndian, ByteOrder, BigEndian};
33
#[cfg(feature = "iovec")]
44
use iovec::IoVec;
5-
#[cfg(feature = "std")]
5+
#[cfg(feature = "alloc")]
66
use prelude::*;
77

88
use core::{cmp, ptr, usize};
@@ -1088,7 +1088,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
10881088
}
10891089
}
10901090

1091-
#[cfg(feature = "std")]
1091+
#[cfg(feature = "alloc")]
10921092
impl<T: BufMut + ?Sized> BufMut for Box<T> {
10931093
fn remaining_mut(&self) -> usize {
10941094
(**self).remaining_mut()
@@ -1098,6 +1098,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
10981098
(**self).bytes_mut()
10991099
}
11001100

1101+
#[cfg(feature = "iovec")]
11011102
unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize {
11021103
(**self).bytes_vec_mut(dst)
11031104
}
@@ -1136,7 +1137,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
11361137
}
11371138
}
11381139

1139-
#[cfg(feature = "std")]
1140+
#[cfg(feature = "alloc")]
11401141
impl BufMut for Vec<u8> {
11411142
#[inline]
11421143
fn remaining_mut(&self) -> usize {

src/buf/from_buf.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use {IntoBuf};
2-
#[cfg(feature = "std")]
2+
#[cfg(feature = "alloc")]
33
use prelude::*;
4-
#[cfg(feature = "std")]
4+
#[cfg(feature = "alloc")]
55
use {Buf, BufMut, Bytes, BytesMut};
66

77
/// Conversion from a [`Buf`]
@@ -90,7 +90,7 @@ pub trait FromBuf {
9090
fn from_buf<T>(buf: T) -> Self where T: IntoBuf;
9191
}
9292

93-
#[cfg(feature = "std")]
93+
#[cfg(feature = "alloc")]
9494
impl FromBuf for Vec<u8> {
9595
fn from_buf<T>(buf: T) -> Self
9696
where T: IntoBuf
@@ -102,7 +102,7 @@ impl FromBuf for Vec<u8> {
102102
}
103103
}
104104

105-
#[cfg(feature = "std")]
105+
#[cfg(feature = "alloc")]
106106
impl FromBuf for Bytes {
107107
fn from_buf<T>(buf: T) -> Self
108108
where T: IntoBuf
@@ -111,7 +111,7 @@ impl FromBuf for Bytes {
111111
}
112112
}
113113

114-
#[cfg(feature = "std")]
114+
#[cfg(feature = "alloc")]
115115
impl FromBuf for BytesMut {
116116
fn from_buf<T>(buf: T) -> Self
117117
where T: IntoBuf

src/buf/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod into_buf;
2424
mod iter;
2525
mod reader;
2626
mod take;
27-
#[cfg(feature = "std")]
27+
#[cfg(feature = "alloc")]
2828
mod vec_deque;
2929
mod writer;
3030

src/buf/vec_deque.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(all(feature = "alloc", not(feature = "std")))]
2+
use alloc::collections::vec_deque::VecDeque;
3+
#[cfg(feature = "std")]
14
use std::collections::VecDeque;
25

36
use super::Buf;

src/bytes.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
use {IntoBuf, Buf, BufMut};
1+
use {BufMut};
2+
#[cfg(feature = "std")]
3+
use {Buf, IntoBuf};
4+
#[cfg(feature = "std")]
25
use buf::Iter;
36
use debug;
47
use prelude::*;
58

6-
use std::{cmp, fmt, mem, hash, ops, slice, ptr, usize};
7-
use std::borrow::{Borrow, BorrowMut};
9+
use core::{cmp, fmt, mem, hash, ops, slice, ptr, usize};
10+
use core::borrow::{Borrow, BorrowMut};
11+
use core::sync::atomic::{self, AtomicUsize, AtomicPtr};
12+
use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
13+
use core::iter::{FromIterator, Iterator};
14+
15+
#[cfg(feature = "std")]
816
use std::io::Cursor;
9-
use std::sync::atomic::{self, AtomicUsize, AtomicPtr};
10-
use std::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
11-
use std::iter::{FromIterator, Iterator};
1217

1318
/// A reference counted contiguous slice of memory.
1419
///
@@ -835,6 +840,7 @@ impl Bytes {
835840
}
836841
}
837842

843+
#[cfg(feature = "std")]
838844
impl IntoBuf for Bytes {
839845
type Buf = Cursor<Self>;
840846

@@ -843,6 +849,7 @@ impl IntoBuf for Bytes {
843849
}
844850
}
845851

852+
#[cfg(feature = "std")]
846853
impl<'a> IntoBuf for &'a Bytes {
847854
type Buf = Cursor<Self>;
848855

@@ -986,6 +993,7 @@ impl Borrow<[u8]> for Bytes {
986993
}
987994
}
988995

996+
#[cfg(feature = "std")]
989997
impl IntoIterator for Bytes {
990998
type Item = u8;
991999
type IntoIter = Iter<Cursor<Bytes>>;
@@ -995,6 +1003,7 @@ impl IntoIterator for Bytes {
9951003
}
9961004
}
9971005

1006+
#[cfg(feature = "std")]
9981007
impl<'a> IntoIterator for &'a Bytes {
9991008
type Item = u8;
10001009
type IntoIter = Iter<Cursor<&'a Bytes>>;
@@ -1563,6 +1572,7 @@ impl BufMut for BytesMut {
15631572
}
15641573
}
15651574

1575+
#[cfg(feature = "std")]
15661576
impl IntoBuf for BytesMut {
15671577
type Buf = Cursor<Self>;
15681578

@@ -1571,6 +1581,7 @@ impl IntoBuf for BytesMut {
15711581
}
15721582
}
15731583

1584+
#[cfg(feature = "std")]
15741585
impl<'a> IntoBuf for &'a BytesMut {
15751586
type Buf = Cursor<&'a BytesMut>;
15761587

@@ -1736,6 +1747,7 @@ impl Clone for BytesMut {
17361747
}
17371748
}
17381749

1750+
#[cfg(feature = "std")]
17391751
impl IntoIterator for BytesMut {
17401752
type Item = u8;
17411753
type IntoIter = Iter<Cursor<BytesMut>>;
@@ -1745,6 +1757,7 @@ impl IntoIterator for BytesMut {
17451757
}
17461758
}
17471759

1760+
#[cfg(feature = "std")]
17481761
impl<'a> IntoIterator for &'a BytesMut {
17491762
type Item = u8;
17501763
type IntoIter = Iter<Cursor<&'a BytesMut>>;

src/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt;
1+
use core::fmt;
22

33
/// Alternative implementation of `fmt::Debug` for byte slice.
44
///

src/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
#![doc(html_root_url = "https://docs.rs/bytes/0.4.12")]
7373
#![no_std]
7474

75+
// TODO: remove this when alloc stabilization lands in the 'nightly' channel
76+
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))]
77+
78+
#[cfg(all(feature = "alloc", not(feature = "std")))]
79+
extern crate alloc;
7580
extern crate byteorder;
7681
#[cfg(feature = "iovec")]
7782
extern crate iovec;
@@ -92,13 +97,12 @@ pub use buf::{
9297
Take,
9398
};
9499

95-
#[cfg(feature = "std")]
100+
#[cfg(feature = "alloc")]
96101
mod bytes;
97-
#[cfg(feature = "std")]
98102
mod debug;
99-
#[cfg(feature = "std")]
100-
pub use bytes::{Bytes, BytesMut};
101103
mod prelude;
104+
#[cfg(feature = "alloc")]
105+
pub use bytes::{Bytes, BytesMut};
102106

103107
#[deprecated]
104108
pub use byteorder::{ByteOrder, BigEndian, LittleEndian};

src/prelude.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
//! Crate-local import prelude, primarily intended as a facade for accessing
22
//! heap-allocated data structures.
33
4+
#[cfg(all(feature = "alloc", not(feature = "std")))]
5+
pub use alloc::boxed::Box;
6+
#[cfg(all(feature = "alloc", not(feature = "std")))]
7+
pub use alloc::string::String;
8+
#[cfg(all(feature = "alloc", not(feature = "std")))]
9+
pub use alloc::vec::Vec;
10+
411
#[cfg(feature = "std")]
512
pub use std::prelude::v1::*;

tests/test_buf.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
extern crate bytes;
22
extern crate byteorder;
3+
#[cfg(feature = "iovec")]
34
extern crate iovec;
45

6+
#[cfg(feature = "std")]
57
use bytes::Buf;
8+
#[cfg(feature = "iovec")]
69
use iovec::IoVec;
10+
#[cfg(feature = "std")]
711
use std::io::Cursor;
812

913
#[test]
14+
#[cfg(feature = "std")]
1015
fn test_fresh_cursor_vec() {
1116
let mut buf = Cursor::new(b"hello".to_vec());
1217

@@ -25,12 +30,14 @@ fn test_fresh_cursor_vec() {
2530
}
2631

2732
#[test]
33+
#[cfg(feature = "std")]
2834
fn test_get_u8() {
2935
let mut buf = Cursor::new(b"\x21zomg");
3036
assert_eq!(0x21, buf.get_u8());
3137
}
3238

3339
#[test]
40+
#[cfg(feature = "std")]
3441
fn test_get_u16() {
3542
let buf = b"\x21\x54zomg";
3643
assert_eq!(0x2154, Cursor::new(buf).get_u16_be());
@@ -39,12 +46,14 @@ fn test_get_u16() {
3946

4047
#[test]
4148
#[should_panic]
49+
#[cfg(feature = "std")]
4250
fn test_get_u16_buffer_underflow() {
4351
let mut buf = Cursor::new(b"\x21");
4452
buf.get_u16_be();
4553
}
4654

4755
#[test]
56+
#[cfg(all(feature = "iovec", feature = "std"))]
4857
fn test_bufs_vec() {
4958
let buf = Cursor::new(b"hello world");
5059

tests/test_buf_mut.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
extern crate bytes;
22
extern crate byteorder;
3+
#[cfg(feature = "iovec")]
34
extern crate iovec;
45

5-
use bytes::{BufMut, BytesMut};
6+
use bytes::BufMut;
7+
#[cfg(feature = "std")]
8+
use bytes::BytesMut;
9+
#[cfg(feature = "iovec")]
610
use iovec::IoVec;
11+
#[cfg(feature = "std")]
712
use std::usize;
13+
#[cfg(feature = "std")]
814
use std::fmt::Write;
915

1016
#[test]
17+
#[cfg(feature = "std")]
1118
fn test_vec_as_mut_buf() {
1219
let mut buf = Vec::with_capacity(64);
1320

@@ -61,6 +68,7 @@ fn test_vec_advance_mut() {
6168
}
6269

6370
#[test]
71+
#[cfg(feature = "std")]
6472
fn test_clone() {
6573
let mut buf = BytesMut::with_capacity(100);
6674
buf.write_str("this is a test").unwrap();
@@ -71,6 +79,7 @@ fn test_clone() {
7179
}
7280

7381
#[test]
82+
#[cfg(feature = "iovec")]
7483
fn test_bufs_vec_mut() {
7584
use std::mem;
7685

0 commit comments

Comments
 (0)