Skip to content

Commit 4dca055

Browse files
committed
Add no_std support, by adding an std feature
I was really hopeful initially that this could be done without having an `std` feature, but it turns out that `bytes` has a bunch of special additional integration with `std::io` stuff, e.g. `std::io::Reader` and `std::io::IoSlice`. To make the library work as `no_std` we add an `std` feature which is on by default. When it is off, we compile as `no_std` and make parts of the API that require `std::io` conditional on the `std` feature.
1 parent 234d814 commit 4dca055

12 files changed

+61
-22
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ edition = "2018"
1919

2020
publish = false
2121

22+
[features]
23+
default = ["std"]
24+
std = []
25+
2226
[dependencies]
2327
serde = { version = "1.0", optional = true }
2428
either = { version = "1.5", default-features = false, optional = true }

src/buf/buf.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
use super::{IntoBuf, Take, Reader, FromBuf, Chain};
1+
use super::{IntoBuf, Take, FromBuf, Chain};
22

3-
use std::{cmp, io::IoSlice, ptr, mem};
3+
#[cfg(feature = "std")]
4+
use super::Reader;
5+
6+
use core::{cmp, ptr, mem};
7+
8+
#[cfg(feature = "std")]
9+
use std::io::IoSlice;
410

511
macro_rules! buf_get_impl {
612
($this:ident, $typ:tt::$conv:tt) => ({
@@ -148,6 +154,7 @@ pub trait Buf {
148154
/// with `dst` being a zero length slice.
149155
///
150156
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
157+
#[cfg(feature = "std")]
151158
fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
152159
if dst.is_empty() {
153160
return 0;
@@ -909,6 +916,7 @@ pub trait Buf {
909916
/// assert_eq!(11, num);
910917
/// assert_eq!(&dst[..11], &b"hello world"[..]);
911918
/// ```
919+
#[cfg(feature = "std")]
912920
fn reader(self) -> Reader<Self> where Self: Sized {
913921
super::reader::new(self)
914922
}
@@ -923,6 +931,7 @@ impl<T: Buf + ?Sized> Buf for &mut T {
923931
(**self).bytes()
924932
}
925933

934+
#[cfg(feature = "std")]
926935
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
927936
(**self).bytes_vectored(dst)
928937
}
@@ -941,6 +950,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
941950
(**self).bytes()
942951
}
943952

953+
#[cfg(feature = "std")]
944954
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
945955
(**self).bytes_vectored(dst)
946956
}

src/buf/buf_mut.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
use super::{IntoBuf, Writer};
1+
use super::{IntoBuf};
22

3-
use std::{mem, cmp, io::IoSliceMut, ptr, usize};
3+
#[cfg(feature = "std")]
4+
use super::Writer;
5+
6+
use core::{mem, cmp, ptr, usize};
7+
8+
#[cfg(feature = "std")]
9+
use std::io::IoSliceMut;
410

511
/// A trait for values that provide sequential write access to bytes.
612
///
@@ -917,6 +923,7 @@ pub trait BufMut {
917923
///
918924
/// assert_eq!(*buf, b"hello world"[..]);
919925
/// ```
926+
#[cfg(feature = "std")]
920927
fn writer(self) -> Writer<Self> where Self: Sized {
921928
super::writer::new(self)
922929
}

src/buf/chain.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::{Buf, BufMut};
22
use crate::buf::IntoIter;
3+
4+
#[cfg(feature = "std")]
35
use std::io::{IoSlice, IoSliceMut};
46

57
/// A `Chain` sequences two buffers.
@@ -178,6 +180,7 @@ impl<T, U> Buf for Chain<T, U>
178180
self.b.advance(cnt);
179181
}
180182

183+
#[cfg(feature = "std")]
181184
fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
182185
let mut n = self.a.bytes_vectored(dst);
183186
n += self.b.bytes_vectored(&mut dst[n..]);
@@ -219,6 +222,7 @@ impl<T, U> BufMut for Chain<T, U>
219222
self.b.advance_mut(cnt);
220223
}
221224

225+
#[cfg(feature = "std")]
222226
unsafe fn bytes_vectored_mut<'a>(&'a mut self, dst: &mut [IoSliceMut<'a>]) -> usize {
223227
let mut n = self.a.bytes_vectored_mut(dst);
224228
n += self.b.bytes_vectored_mut(&mut dst[n..]);

src/buf/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ mod from_buf;
2222
mod chain;
2323
mod into_buf;
2424
mod iter;
25-
mod reader;
2625
mod take;
2726
mod vec_deque;
27+
28+
// When std::io::Reader etc. traits are not available, skip these
29+
#[cfg(feature = "std")]
30+
mod reader;
31+
#[cfg(feature = "std")]
2832
mod writer;
2933

3034
pub use self::buf::Buf;
@@ -33,6 +37,8 @@ pub use self::from_buf::FromBuf;
3337
pub use self::chain::Chain;
3438
pub use self::into_buf::IntoBuf;
3539
pub use self::iter::IntoIter;
40+
#[cfg(feature = "std")]
3641
pub use self::reader::Reader;
3742
pub use self::take::Take;
43+
#[cfg(feature = "std")]
3844
pub use self::writer::Writer;

src/buf/take.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::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-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::VecDeque;
1+
use alloc::collections::VecDeque;
22

33
use super::Buf;
44

src/bytes.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use crate::{Buf, BufMut, IntoBuf};
22
use crate::buf::IntoIter;
33
use crate::debug;
44

5-
use std::{cmp, fmt, mem, hash, slice, ptr, usize};
6-
use std::borrow::{Borrow, BorrowMut};
7-
use std::ops::{Deref, DerefMut, RangeBounds};
8-
use std::sync::atomic::{self, AtomicUsize, AtomicPtr};
9-
use std::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
10-
use std::iter::{FromIterator, Iterator};
5+
use core::{cmp, fmt, mem, hash, slice, ptr, usize};
6+
use alloc::borrow::{Borrow, BorrowMut};
7+
use core::ops::{Deref, DerefMut, RangeBounds};
8+
use core::sync::atomic::{self, AtomicUsize, AtomicPtr};
9+
use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
10+
use core::iter::{FromIterator, Iterator};
1111

1212
/// A reference counted contiguous slice of memory.
1313
///
@@ -316,10 +316,10 @@ struct Inner {
316316
}
317317

318318
// Thread-safe reference-counted container for the shared storage. This mostly
319-
// the same as `std::sync::Arc` but without the weak counter. The ref counting
319+
// the same as `core::sync::Arc` but without the weak counter. The ref counting
320320
// fns are based on the ones found in `std`.
321321
//
322-
// The main reason to use `Shared` instead of `std::sync::Arc` is that it ends
322+
// The main reason to use `Shared` instead of `core::sync::Arc` is that it ends
323323
// up making the overall code simpler and easier to reason about. This is due to
324324
// some of the logic around setting `Inner::arc` and other ways the `arc` field
325325
// is used. Using `Arc` ended up requiring a number of funky transmutes and
@@ -522,7 +522,7 @@ impl Bytes {
522522
/// Requires that `begin <= end` and `end <= self.len()`, otherwise slicing
523523
/// will panic.
524524
pub fn slice(&self, range: impl RangeBounds<usize>) -> Bytes {
525-
use std::ops::Bound;
525+
use core::ops::Bound;
526526

527527
let len = self.len();
528528

@@ -852,7 +852,7 @@ impl Bytes {
852852
/// assert_eq!(iter.next().map(|b| *b), Some(b'c'));
853853
/// assert_eq!(iter.next(), None);
854854
/// ```
855-
pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, u8> {
855+
pub fn iter<'a>(&'a self) -> core::slice::Iter<'a, u8> {
856856
self.bytes().iter()
857857
}
858858
}
@@ -1026,7 +1026,7 @@ impl IntoIterator for Bytes {
10261026

10271027
impl<'a> IntoIterator for &'a Bytes {
10281028
type Item = &'a u8;
1029-
type IntoIter = std::slice::Iter<'a, u8>;
1029+
type IntoIter = core::slice::Iter<'a, u8>;
10301030

10311031
fn into_iter(self) -> Self::IntoIter {
10321032
self.as_ref().into_iter()
@@ -1534,7 +1534,7 @@ impl BytesMut {
15341534
/// assert_eq!(iter.next().map(|b| *b), Some(b'c'));
15351535
/// assert_eq!(iter.next(), None);
15361536
/// ```
1537-
pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, u8> {
1537+
pub fn iter<'a>(&'a self) -> core::slice::Iter<'a, u8> {
15381538
self.bytes().iter()
15391539
}
15401540
}
@@ -1783,7 +1783,7 @@ impl IntoIterator for BytesMut {
17831783

17841784
impl<'a> IntoIterator for &'a BytesMut {
17851785
type Item = &'a u8;
1786-
type IntoIter = std::slice::Iter<'a, u8>;
1786+
type IntoIter = core::slice::Iter<'a, u8>;
17871787

17881788
fn into_iter(self) -> Self::IntoIter {
17891789
self.as_ref().into_iter()

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/either.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::{Buf, BufMut};
22

33
use either::Either;
44
use either::Either::*;
5+
6+
#[cfg(feature = "std")]
57
use std::io::{IoSlice, IoSliceMut};
68

79
impl<L, R> Buf for Either<L, R>
@@ -23,6 +25,7 @@ where
2325
}
2426
}
2527

28+
#[cfg(feature = "std")]
2629
fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
2730
match *self {
2831
Left(ref b) => b.bytes_vectored(dst),
@@ -64,6 +67,7 @@ where
6467
}
6568
}
6669

70+
#[cfg(feature = "std")]
6771
unsafe fn bytes_vectored_mut<'a>(&'a mut self, dst: &mut [IoSliceMut<'a>]) -> usize {
6872
match *self {
6973
Left(ref mut b) => b.bytes_vectored_mut(dst),

src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
#![deny(warnings, missing_docs, missing_debug_implementations, rust_2018_idioms)]
7272
#![doc(html_root_url = "https://docs.rs/bytes/0.5.0")]
7373

74+
#![cfg_attr(not(feature = "std"), no_std)]
75+
76+
extern crate alloc;
77+
7478
pub mod buf;
7579
pub use crate::buf::{
7680
Buf,

src/serde.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cmp, fmt};
1+
use core::{cmp, fmt};
22
use serde::{Serialize, Serializer, Deserialize, Deserializer, de};
33
use super::{Bytes, BytesMut};
44

0 commit comments

Comments
 (0)