Skip to content

Commit 18f2a7e

Browse files
committed
no_std support (with default-enabled "std" cargo feature)
Feature gates anything dependent on `std` on the eponymous cargo feature, and changes references to libcore features from `std` to `core`
1 parent d43e283 commit 18f2a7e

16 files changed

+79
-9
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ matrix:
3333
- env: RUST_TEST_THREADS=1 TARGET=powerpc-unknown-linux-gnu
3434
- env: RUST_TEST_THREADS=1 TARGET=powerpc64-unknown-linux-gnu
3535

36+
# Ensure crate compiles without default features (i.e. for no_std)
37+
- env: EXTRA_ARGS="--no-default-features"
38+
script: cargo build $EXTRA_ARGS
39+
3640
# Serde implementation
3741
- env: EXTRA_ARGS="--features serde"
3842

Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ exclude = [
2222
"bench/**/*",
2323
"test/**/*"
2424
]
25-
categories = ["network-programming", "data-structures"]
25+
categories = ["network-programming", "data-structures", "no-std"]
2626

2727
[package.metadata.docs.rs]
2828
features = ["i128"]
2929

3030
[dependencies]
3131
byteorder = "1.1.0"
32-
iovec = "0.1"
32+
iovec = { version = "0.1", optional = true }
3333
serde = { version = "1.0", optional = true }
3434
either = { version = "1.5", default-features = false, optional = true }
3535

3636
[dev-dependencies]
3737
serde_test = "1.0"
3838

3939
[features]
40+
default = ["std"]
4041
i128 = ["byteorder/i128"]
42+
std = ["iovec"]

src/buf/buf.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
22
use byteorder::{BigEndian, ByteOrder, LittleEndian};
3+
#[cfg(feature = "iovec")]
34
use iovec::IoVec;
5+
#[cfg(feature = "std")]
6+
use prelude::*;
47

5-
use std::{cmp, io, ptr};
8+
use core::{cmp, ptr};
9+
#[cfg(feature = "std")]
10+
use std::io;
611

712
macro_rules! buf_get_impl {
813
($this:ident, $size:expr, $conv:path) => ({
@@ -146,6 +151,7 @@ pub trait Buf {
146151
/// with `dst` being a zero length slice.
147152
///
148153
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
154+
#[cfg(feature = "iovec")]
149155
fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize {
150156
if dst.is_empty() {
151157
return 0;
@@ -1061,6 +1067,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
10611067
(**self).bytes()
10621068
}
10631069

1070+
#[cfg(feature = "iovec")]
10641071
fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
10651072
(**self).bytes_vec(dst)
10661073
}
@@ -1070,6 +1077,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
10701077
}
10711078
}
10721079

1080+
#[cfg(feature = "std")]
10731081
impl<T: Buf + ?Sized> Buf for Box<T> {
10741082
fn remaining(&self) -> usize {
10751083
(**self).remaining()
@@ -1079,6 +1087,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
10791087
(**self).bytes()
10801088
}
10811089

1090+
#[cfg(feature = "iovec")]
10821091
fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
10831092
(**self).bytes_vec(dst)
10841093
}
@@ -1088,6 +1097,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
10881097
}
10891098
}
10901099

1100+
#[cfg(feature = "std")]
10911101
impl<T: AsRef<[u8]>> Buf for io::Cursor<T> {
10921102
fn remaining(&self) -> usize {
10931103
let len = self.get_ref().as_ref().len();

src/buf/buf_mut.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
use super::{IntoBuf, Writer};
22
use byteorder::{LittleEndian, ByteOrder, BigEndian};
3+
#[cfg(feature = "iovec")]
34
use iovec::IoVec;
5+
#[cfg(feature = "std")]
6+
use prelude::*;
47

5-
use std::{cmp, io, ptr, usize};
8+
use core::{cmp, ptr, usize};
9+
#[cfg(feature = "std")]
10+
use std::io;
611

712
/// A trait for values that provide sequential write access to bytes.
813
///
@@ -189,6 +194,7 @@ pub trait BufMut {
189194
/// with `dst` being a zero length slice.
190195
///
191196
/// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html
197+
#[cfg(feature = "iovec")]
192198
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize {
193199
if dst.is_empty() {
194200
return 0;
@@ -1072,6 +1078,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
10721078
(**self).bytes_mut()
10731079
}
10741080

1081+
#[cfg(feature = "iovec")]
10751082
unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize {
10761083
(**self).bytes_vec_mut(dst)
10771084
}
@@ -1081,6 +1088,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
10811088
}
10821089
}
10831090

1091+
#[cfg(feature = "std")]
10841092
impl<T: BufMut + ?Sized> BufMut for Box<T> {
10851093
fn remaining_mut(&self) -> usize {
10861094
(**self).remaining_mut()
@@ -1099,6 +1107,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
10991107
}
11001108
}
11011109

1110+
#[cfg(feature = "std")]
11021111
impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
11031112
fn remaining_mut(&self) -> usize {
11041113
use Buf;
@@ -1127,6 +1136,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
11271136
}
11281137
}
11291138

1139+
#[cfg(feature = "std")]
11301140
impl BufMut for Vec<u8> {
11311141
#[inline]
11321142
fn remaining_mut(&self) -> usize {
@@ -1148,7 +1158,7 @@ impl BufMut for Vec<u8> {
11481158

11491159
#[inline]
11501160
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
1151-
use std::slice;
1161+
use core::slice;
11521162

11531163
if self.capacity() == self.len() {
11541164
self.reserve(64); // Grow the vec

src/buf/chain.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use {Buf, BufMut};
2+
#[cfg(feature = "iovec")]
23
use iovec::IoVec;
34

45
/// A `Chain` sequences two buffers.
@@ -177,6 +178,7 @@ impl<T, U> Buf for Chain<T, U>
177178
self.b.advance(cnt);
178179
}
179180

181+
#[cfg(feature = "iovec")]
180182
fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize {
181183
let mut n = self.a.bytes_vec(dst);
182184
n += self.b.bytes_vec(&mut dst[n..]);
@@ -218,6 +220,7 @@ impl<T, U> BufMut for Chain<T, U>
218220
self.b.advance_mut(cnt);
219221
}
220222

223+
#[cfg(feature = "iovec")]
221224
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize {
222225
let mut n = self.a.bytes_vec_mut(dst);
223226
n += self.b.bytes_vec_mut(&mut dst[n..]);

src/buf/from_buf.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use {Buf, BufMut, IntoBuf, Bytes, BytesMut};
1+
use {IntoBuf};
2+
#[cfg(feature = "std")]
3+
use prelude::*;
4+
#[cfg(feature = "std")]
5+
use {Buf, BufMut, Bytes, BytesMut};
26

37
/// Conversion from a [`Buf`]
48
///
@@ -86,6 +90,7 @@ pub trait FromBuf {
8690
fn from_buf<T>(buf: T) -> Self where T: IntoBuf;
8791
}
8892

93+
#[cfg(feature = "std")]
8994
impl FromBuf for Vec<u8> {
9095
fn from_buf<T>(buf: T) -> Self
9196
where T: IntoBuf
@@ -97,6 +102,7 @@ impl FromBuf for Vec<u8> {
97102
}
98103
}
99104

105+
#[cfg(feature = "std")]
100106
impl FromBuf for Bytes {
101107
fn from_buf<T>(buf: T) -> Self
102108
where T: IntoBuf
@@ -105,6 +111,7 @@ impl FromBuf for Bytes {
105111
}
106112
}
107113

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

src/buf/into_buf.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use super::{Buf};
2+
#[cfg(feature = "std")]
3+
use prelude::*;
24

5+
#[cfg(feature = "std")]
36
use std::io;
47

58
/// Conversion into a `Buf`
@@ -55,6 +58,7 @@ impl<T: Buf> IntoBuf for T {
5558
}
5659
}
5760

61+
#[cfg(feature = "std")]
5862
impl<'a> IntoBuf for &'a [u8] {
5963
type Buf = io::Cursor<&'a [u8]>;
6064

@@ -63,6 +67,7 @@ impl<'a> IntoBuf for &'a [u8] {
6367
}
6468
}
6569

70+
#[cfg(feature = "std")]
6671
impl<'a> IntoBuf for &'a mut [u8] {
6772
type Buf = io::Cursor<&'a mut [u8]>;
6873

@@ -71,6 +76,7 @@ impl<'a> IntoBuf for &'a mut [u8] {
7176
}
7277
}
7378

79+
#[cfg(feature = "std")]
7480
impl<'a> IntoBuf for &'a str {
7581
type Buf = io::Cursor<&'a [u8]>;
7682

@@ -79,6 +85,7 @@ impl<'a> IntoBuf for &'a str {
7985
}
8086
}
8187

88+
#[cfg(feature = "std")]
8289
impl IntoBuf for Vec<u8> {
8390
type Buf = io::Cursor<Vec<u8>>;
8491

@@ -87,6 +94,7 @@ impl IntoBuf for Vec<u8> {
8794
}
8895
}
8996

97+
#[cfg(feature = "std")]
9098
impl<'a> IntoBuf for &'a Vec<u8> {
9199
type Buf = io::Cursor<&'a [u8]>;
92100

@@ -97,6 +105,7 @@ impl<'a> IntoBuf for &'a Vec<u8> {
97105

98106
// Kind of annoying... but this impl is required to allow passing `&'static
99107
// [u8]` where for<'a> &'a T: IntoBuf is required.
108+
#[cfg(feature = "std")]
100109
impl<'a> IntoBuf for &'a &'static [u8] {
101110
type Buf = io::Cursor<&'static [u8]>;
102111

@@ -105,6 +114,7 @@ impl<'a> IntoBuf for &'a &'static [u8] {
105114
}
106115
}
107116

117+
#[cfg(feature = "std")]
108118
impl<'a> IntoBuf for &'a &'static str {
109119
type Buf = io::Cursor<&'static [u8]>;
110120

@@ -113,6 +123,7 @@ impl<'a> IntoBuf for &'a &'static str {
113123
}
114124
}
115125

126+
#[cfg(feature = "std")]
116127
impl IntoBuf for String {
117128
type Buf = io::Cursor<Vec<u8>>;
118129

@@ -121,6 +132,7 @@ impl IntoBuf for String {
121132
}
122133
}
123134

135+
#[cfg(feature = "std")]
124136
impl<'a> IntoBuf for &'a String {
125137
type Buf = io::Cursor<&'a [u8]>;
126138

src/buf/mod.rs

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

src/buf/reader.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use {Buf};
22

3+
#[cfg(feature = "std")]
34
use std::{cmp, io};
45

56
/// A `Buf` adapter which implements `io::Read` for the inner value.
@@ -78,6 +79,7 @@ impl<B: Buf> Reader<B> {
7879
}
7980
}
8081

82+
#[cfg(feature = "std")]
8183
impl<B: Buf + Sized> io::Read for Reader<B> {
8284
fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
8385
let len = cmp::min(self.buf.remaining(), dst.len());
@@ -87,6 +89,7 @@ impl<B: Buf + Sized> io::Read for Reader<B> {
8789
}
8890
}
8991

92+
#[cfg(feature = "std")]
9093
impl<B: Buf + Sized> io::BufRead for Reader<B> {
9194
fn fill_buf(&mut self) -> io::Result<&[u8]> {
9295
Ok(self.buf.bytes())

src/buf/take.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use {Buf};
22

3-
use std::cmp;
3+
use core::cmp;
44

55
/// A `Buf` adapter which limits the bytes read from an underlying buffer.
66
///

src/buf/vec_deque.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl Buf for VecDeque<u8> {
2424
#[cfg(test)]
2525
mod tests {
2626
use super::*;
27+
use prelude::*;
2728

2829
#[test]
2930
fn hello_world() {

src/buf/writer.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use BufMut;
22

3+
#[cfg(feature = "std")]
34
use std::{cmp, io};
45

56
/// A `BufMut` adapter which implements `io::Write` for the inner value.
@@ -74,6 +75,7 @@ impl<B: BufMut> Writer<B> {
7475
}
7576
}
7677

78+
#[cfg(feature = "std")]
7779
impl<B: BufMut + Sized> io::Write for Writer<B> {
7880
fn write(&mut self, src: &[u8]) -> io::Result<usize> {
7981
let n = cmp::min(self.buf.remaining_mut(), src.len());

src/bytes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use {IntoBuf, Buf, BufMut};
22
use buf::Iter;
33
use debug;
4+
use prelude::*;
45

56
use std::{cmp, fmt, mem, hash, ops, slice, ptr, usize};
67
use std::borrow::{Borrow, BorrowMut};
@@ -1499,7 +1500,7 @@ impl BytesMut {
14991500
}
15001501

15011502
unsafe {
1502-
ptr = self.inner.ptr.offset(self.inner.len as isize);
1503+
ptr = self.inner.ptr.offset(self.inner.len as isize);
15031504
}
15041505
if ptr == other.inner.ptr &&
15051506
self.inner.kind() == KIND_ARC &&

src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@
7070
7171
#![deny(warnings, missing_docs, missing_debug_implementations)]
7272
#![doc(html_root_url = "https://docs.rs/bytes/0.4.12")]
73+
#![no_std]
7374

7475
extern crate byteorder;
76+
#[cfg(feature = "iovec")]
7577
extern crate iovec;
78+
#[cfg(feature = "std")]
79+
extern crate std;
7680

7781
pub mod buf;
7882
pub use buf::{
@@ -88,9 +92,13 @@ pub use buf::{
8892
Take,
8993
};
9094

95+
#[cfg(feature = "std")]
9196
mod bytes;
97+
#[cfg(feature = "std")]
9298
mod debug;
99+
#[cfg(feature = "std")]
93100
pub use bytes::{Bytes, BytesMut};
101+
mod prelude;
94102

95103
#[deprecated]
96104
pub use byteorder::{ByteOrder, BigEndian, LittleEndian};

0 commit comments

Comments
 (0)