Skip to content

Commit aaa1083

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 425432b commit aaa1083

12 files changed

+59
-20
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
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_impl.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use super::{Take, Reader, 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;
@@ -884,6 +891,7 @@ pub trait Buf {
884891
/// assert_eq!(11, num);
885892
/// assert_eq!(&dst[..11], &b"hello world"[..]);
886893
/// ```
894+
#[cfg(feature = "std")]
887895
fn reader(self) -> Reader<Self> where Self: Sized {
888896
super::reader::new(self)
889897
}
@@ -915,6 +923,7 @@ impl<T: Buf + ?Sized> Buf for &mut T {
915923
(**self).bytes()
916924
}
917925

926+
#[cfg(feature = "std")]
918927
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
919928
(**self).bytes_vectored(dst)
920929
}
@@ -933,6 +942,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
933942
(**self).bytes()
934943
}
935944

945+
#[cfg(feature = "std")]
936946
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
937947
(**self).bytes_vectored(dst)
938948
}

src/buf/buf_mut.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use super::{Writer};
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
///
@@ -913,6 +919,7 @@ pub trait BufMut {
913919
///
914920
/// assert_eq!(*buf, b"hello world"[..]);
915921
/// ```
922+
#[cfg(feature = "std")]
916923
fn writer(self) -> Writer<Self> where Self: Sized {
917924
super::writer::new(self)
918925
}

src/buf/chain.rs

Lines changed: 4 additions & 0 deletions
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..]);
@@ -227,6 +230,7 @@ impl<T, U> BufMut for Chain<T, U>
227230
self.b.advance_mut(cnt);
228231
}
229232

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

src/buf/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@ mod buf_impl;
2020
mod buf_mut;
2121
mod chain;
2222
mod iter;
23-
mod reader;
2423
mod take;
2524
mod vec_deque;
25+
26+
// When std::io::Reader etc. traits are not available, skip these
27+
#[cfg(feature = "std")]
28+
mod reader;
29+
#[cfg(feature = "std")]
2630
mod writer;
2731

2832
pub use self::buf_impl::Buf;
2933
pub use self::buf_mut::BufMut;
3034
pub use self::chain::Chain;
3135
pub use self::iter::IntoIter;
36+
#[cfg(feature = "std")]
3237
pub use self::reader::Reader;
3338
pub use self::take::Take;
39+
#[cfg(feature = "std")]
3440
pub use self::writer::Writer;

src/buf/take.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use crate::{Buf, BufMut};
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
@@ -527,7 +527,7 @@ impl Bytes {
527527
/// Requires that `begin <= end` and `end <= self.len()`, otherwise slicing
528528
/// will panic.
529529
pub fn slice(&self, range: impl RangeBounds<usize>) -> Bytes {
530-
use std::ops::Bound;
530+
use core::ops::Bound;
531531

532532
let len = self.len();
533533

@@ -857,7 +857,7 @@ impl Bytes {
857857
/// assert_eq!(iter.next().map(|b| *b), Some(b'c'));
858858
/// assert_eq!(iter.next(), None);
859859
/// ```
860-
pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, u8> {
860+
pub fn iter<'a>(&'a self) -> core::slice::Iter<'a, u8> {
861861
self.bytes().iter()
862862
}
863863
}
@@ -1031,7 +1031,7 @@ impl IntoIterator for Bytes {
10311031

10321032
impl<'a> IntoIterator for &'a Bytes {
10331033
type Item = &'a u8;
1034-
type IntoIter = std::slice::Iter<'a, u8>;
1034+
type IntoIter = core::slice::Iter<'a, u8>;
10351035

10361036
fn into_iter(self) -> Self::IntoIter {
10371037
self.as_ref().into_iter()
@@ -1539,7 +1539,7 @@ impl BytesMut {
15391539
/// assert_eq!(iter.next().map(|b| *b), Some(b'c'));
15401540
/// assert_eq!(iter.next(), None);
15411541
/// ```
1542-
pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, u8> {
1542+
pub fn iter<'a>(&'a self) -> core::slice::Iter<'a, u8> {
15431543
self.bytes().iter()
15441544
}
15451545
}
@@ -1780,7 +1780,7 @@ impl IntoIterator for BytesMut {
17801780

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

17851785
fn into_iter(self) -> Self::IntoIter {
17861786
self.as_ref().into_iter()

src/debug.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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)