Skip to content

Commit aaed035

Browse files
committed
no_std support (using enabled-by-default "std" feature)
- Changes all references to libcore features from ::std to ::core - Feature gates anything dependent on std on the "std" feature
1 parent 6750a26 commit aaed035

13 files changed

+91
-28
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ matrix:
3636
# Serde implementation
3737
- env: EXTRA_ARGS="--features serde"
3838

39+
# Ensure crate compiles without default features (i.e. for no_std)
40+
- env: EXTRA_ARGS="--no-default-features"
41+
script: cargo build $EXTRA_ARGS
42+
3943
before_install: set -e
4044

4145
install:

Cargo.toml

+9-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ exclude = [
1717
"bench/**/*",
1818
"test/**/*"
1919
]
20-
categories = ["network-programming", "data-structures"]
20+
categories = ["network-programming", "data-structures", "no-std"]
2121

2222
[dependencies]
2323
byteorder = "1.0.0"
24-
iovec = "0.1"
2524
serde = { version = "1.0", optional = true }
2625

26+
[dependencies.iovec]
27+
version = "0.1"
28+
optional = true
29+
2730
[dev-dependencies]
2831
serde_test = "1.0"
32+
33+
[features]
34+
default = ["std"]
35+
std = ["iovec"]

src/buf/buf.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
1+
use super::{IntoBuf, Take, Reader, Iter, Chain};
2+
#[cfg(feature = "std")]
3+
use super::FromBuf;
4+
25
use byteorder::ByteOrder;
6+
#[cfg(feature = "std")]
37
use iovec::IoVec;
48

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

714
/// Read bytes from a buffer.
815
///
@@ -12,7 +19,7 @@ use std::{cmp, io, ptr};
1219
/// position. It can be thought of as an efficient `Iterator` for collections of
1320
/// bytes.
1421
///
15-
/// The simplest `Buf` is a `Cursor` wrapping a `[u8]`.
22+
/// The simplest `Buf` is a `io::Cursor` wrapping a `[u8]`.
1623
///
1724
/// ```
1825
/// use bytes::Buf;
@@ -113,6 +120,7 @@ pub trait Buf {
113120
/// with `dst` being a zero length slice.
114121
///
115122
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
123+
#[cfg(feature = "std")]
116124
fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize {
117125
if dst.is_empty() {
118126
return 0;
@@ -520,6 +528,7 @@ pub trait Buf {
520528
///
521529
/// assert_eq!(vec, &b"hello world"[..]);
522530
/// ```
531+
#[cfg(feature = "std")]
523532
fn collect<B>(self) -> B
524533
where Self: Sized,
525534
B: FromBuf,
@@ -662,6 +671,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
662671
(**self).bytes()
663672
}
664673

674+
#[cfg(feature = "std")]
665675
fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
666676
(**self).bytes_vec(dst)
667677
}
@@ -671,6 +681,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
671681
}
672682
}
673683

684+
#[cfg(feature = "std")]
674685
impl<T: Buf + ?Sized> Buf for Box<T> {
675686
fn remaining(&self) -> usize {
676687
(**self).remaining()
@@ -680,6 +691,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
680691
(**self).bytes()
681692
}
682693

694+
#[cfg(feature = "std")]
683695
fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
684696
(**self).bytes_vec(dst)
685697
}
@@ -689,6 +701,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
689701
}
690702
}
691703

704+
#[cfg(feature = "std")]
692705
impl<T: AsRef<[u8]>> Buf for io::Cursor<T> {
693706
fn remaining(&self) -> usize {
694707
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,12 @@
11
use super::{IntoBuf, Writer};
22
use byteorder::ByteOrder;
3+
#[cfg(feature = "std")]
34
use iovec::IoVec;
45

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

711
/// A trait for values that provide sequential write access to bytes.
812
///
@@ -188,6 +192,7 @@ pub trait BufMut {
188192
/// with `dst` being a zero length slice.
189193
///
190194
/// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html
195+
#[cfg(feature = "std")]
191196
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize {
192197
if dst.is_empty() {
193198
return 0;
@@ -645,6 +650,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
645650
(**self).bytes_mut()
646651
}
647652

653+
#[cfg(feature = "std")]
648654
unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize {
649655
(**self).bytes_vec_mut(dst)
650656
}
@@ -654,6 +660,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
654660
}
655661
}
656662

663+
#[cfg(feature = "std")]
657664
impl<T: BufMut + ?Sized> BufMut for Box<T> {
658665
fn remaining_mut(&self) -> usize {
659666
(**self).remaining_mut()
@@ -663,6 +670,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
663670
(**self).bytes_mut()
664671
}
665672

673+
#[cfg(feature = "std")]
666674
unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize {
667675
(**self).bytes_vec_mut(dst)
668676
}
@@ -672,6 +680,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
672680
}
673681
}
674682

683+
#[cfg(feature = "std")]
675684
impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
676685
fn remaining_mut(&self) -> usize {
677686
use Buf;
@@ -700,6 +709,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
700709
}
701710
}
702711

712+
#[cfg(feature = "std")]
703713
impl BufMut for Vec<u8> {
704714
#[inline]
705715
fn remaining_mut(&self) -> usize {
@@ -721,7 +731,7 @@ impl BufMut for Vec<u8> {
721731

722732
#[inline]
723733
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
724-
use std::slice;
734+
use core::slice;
725735

726736
if self.capacity() == self.len() {
727737
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 = "std")]
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 = "std")]
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 = "std")]
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/into_buf.rs

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

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

56
/// Conversion into a `Buf`
@@ -55,6 +56,7 @@ impl<T: Buf> IntoBuf for T {
5556
}
5657
}
5758

59+
#[cfg(feature = "std")]
5860
impl<'a> IntoBuf for &'a [u8] {
5961
type Buf = io::Cursor<&'a [u8]>;
6062

@@ -63,6 +65,7 @@ impl<'a> IntoBuf for &'a [u8] {
6365
}
6466
}
6567

68+
#[cfg(feature = "std")]
6669
impl<'a> IntoBuf for &'a str {
6770
type Buf = io::Cursor<&'a [u8]>;
6871

@@ -71,6 +74,7 @@ impl<'a> IntoBuf for &'a str {
7174
}
7275
}
7376

77+
#[cfg(feature = "std")]
7478
impl IntoBuf for Vec<u8> {
7579
type Buf = io::Cursor<Vec<u8>>;
7680

@@ -79,6 +83,7 @@ impl IntoBuf for Vec<u8> {
7983
}
8084
}
8185

86+
#[cfg(feature = "std")]
8287
impl<'a> IntoBuf for &'a Vec<u8> {
8388
type Buf = io::Cursor<&'a [u8]>;
8489

@@ -89,6 +94,7 @@ impl<'a> IntoBuf for &'a Vec<u8> {
8994

9095
// Kind of annoying... but this impl is required to allow passing `&'static
9196
// [u8]` where for<'a> &'a T: IntoBuf is required.
97+
#[cfg(feature = "std")]
9298
impl<'a> IntoBuf for &'a &'static [u8] {
9399
type Buf = io::Cursor<&'static [u8]>;
94100

@@ -97,6 +103,7 @@ impl<'a> IntoBuf for &'a &'static [u8] {
97103
}
98104
}
99105

106+
#[cfg(feature = "std")]
100107
impl<'a> IntoBuf for &'a &'static str {
101108
type Buf = io::Cursor<&'static [u8]>;
102109

@@ -105,6 +112,7 @@ impl<'a> IntoBuf for &'a &'static str {
105112
}
106113
}
107114

115+
#[cfg(feature = "std")]
108116
impl IntoBuf for String {
109117
type Buf = io::Cursor<Vec<u8>>;
110118

@@ -113,6 +121,7 @@ impl IntoBuf for String {
113121
}
114122
}
115123

124+
#[cfg(feature = "std")]
116125
impl<'a> IntoBuf for &'a String {
117126
type Buf = io::Cursor<&'a [u8]>;
118127

src/buf/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
1919
mod buf;
2020
mod buf_mut;
21+
#[cfg(feature = "std")]
2122
mod from_buf;
2223
mod chain;
2324
mod into_buf;
@@ -28,6 +29,7 @@ mod writer;
2829

2930
pub use self::buf::Buf;
3031
pub use self::buf_mut::BufMut;
32+
#[cfg(feature = "std")]
3133
pub use self::from_buf::FromBuf;
3234
pub use self::chain::Chain;
3335
pub use self::into_buf::IntoBuf;

src/buf/reader.rs

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

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/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());

0 commit comments

Comments
 (0)