Skip to content

Commit c17e401

Browse files
cbeck88carllerche
authored andcommitted
Add no_std support, by adding an std feature (#281)
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 73426df commit c17e401

14 files changed

+77
-25
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 }

ci/azure-test-stable.yml

+4
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ jobs:
4040
- ${{ if parameters.benches }}:
4141
- script: cargo check --benches
4242
displayName: Check benchmarks
43+
44+
# Run with no default defaults
45+
- script: cargo ${{ parameters.cmd }}
46+
displayName: cargo ${{ parameters.cmd }} --no-default-features

src/buf/buf_impl.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
use super::{Take, Reader, Chain};
1+
use super::{Take, 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;
10+
11+
use alloc::{boxed::Box};
412

513
macro_rules! buf_get_impl {
614
($this:ident, $typ:tt::$conv:tt) => ({
@@ -148,6 +156,7 @@ pub trait Buf {
148156
/// with `dst` being a zero length slice.
149157
///
150158
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
159+
#[cfg(feature = "std")]
151160
fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
152161
if dst.is_empty() {
153162
return 0;
@@ -884,6 +893,7 @@ pub trait Buf {
884893
/// assert_eq!(11, num);
885894
/// assert_eq!(&dst[..11], &b"hello world"[..]);
886895
/// ```
896+
#[cfg(feature = "std")]
887897
fn reader(self) -> Reader<Self> where Self: Sized {
888898
super::reader::new(self)
889899
}
@@ -915,6 +925,7 @@ impl<T: Buf + ?Sized> Buf for &mut T {
915925
(**self).bytes()
916926
}
917927

928+
#[cfg(feature = "std")]
918929
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
919930
(**self).bytes_vectored(dst)
920931
}
@@ -933,6 +944,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
933944
(**self).bytes()
934945
}
935946

947+
#[cfg(feature = "std")]
936948
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
937949
(**self).bytes_vectored(dst)
938950
}

src/buf/buf_mut.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
use super::{Writer};
1+
#[cfg(feature = "std")]
2+
use super::Writer;
23

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

511
/// A trait for values that provide sequential write access to bytes.
612
///
@@ -185,6 +191,7 @@ pub trait BufMut {
185191
/// with `dst` being a zero length slice.
186192
///
187193
/// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html
194+
#[cfg(feature = "std")]
188195
unsafe fn bytes_vectored_mut<'a>(&'a mut self, dst: &mut [IoSliceMut<'a>]) -> usize {
189196
if dst.is_empty() {
190197
return 0;
@@ -913,6 +920,7 @@ pub trait BufMut {
913920
///
914921
/// assert_eq!(*buf, b"hello world"[..]);
915922
/// ```
923+
#[cfg(feature = "std")]
916924
fn writer(self) -> Writer<Self> where Self: Sized {
917925
super::writer::new(self)
918926
}
@@ -927,6 +935,7 @@ impl<T: BufMut + ?Sized> BufMut for &mut T {
927935
(**self).bytes_mut()
928936
}
929937

938+
#[cfg(feature = "std")]
930939
unsafe fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
931940
(**self).bytes_vectored_mut(dst)
932941
}
@@ -945,6 +954,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
945954
(**self).bytes_mut()
946955
}
947956

957+
#[cfg(feature = "std")]
948958
unsafe fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
949959
(**self).bytes_vectored_mut(dst)
950960
}
@@ -968,7 +978,7 @@ impl BufMut for &mut [u8] {
968978
#[inline]
969979
unsafe fn advance_mut(&mut self, cnt: usize) {
970980
// Lifetime dance taken from `impl Write for &mut [u8]`.
971-
let (_, b) = std::mem::replace(self, &mut []).split_at_mut(cnt);
981+
let (_, b) = core::mem::replace(self, &mut []).split_at_mut(cnt);
972982
*self = b;
973983
}
974984
}
@@ -994,7 +1004,7 @@ impl BufMut for Vec<u8> {
9941004

9951005
#[inline]
9961006
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
997-
use std::slice;
1007+
use core::slice;
9981008

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

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..]);
@@ -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

+7-1
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

+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

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ 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 core::ops::{Deref, DerefMut, RangeBounds};
7+
use core::sync::atomic::{self, AtomicUsize, AtomicPtr};
8+
use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
9+
use core::iter::{FromIterator, Iterator};
10+
11+
use alloc::{vec::Vec, string::String, boxed::Box, borrow::{Borrow, BorrowMut}};
1112

1213
/// A reference counted contiguous slice of memory.
1314
///
@@ -316,10 +317,10 @@ struct Inner {
316317
}
317318

318319
// 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
320+
// the same as `core::sync::Arc` but without the weak counter. The ref counting
320321
// fns are based on the ones found in `std`.
321322
//
322-
// The main reason to use `Shared` instead of `std::sync::Arc` is that it ends
323+
// The main reason to use `Shared` instead of `core::sync::Arc` is that it ends
323324
// up making the overall code simpler and easier to reason about. This is due to
324325
// some of the logic around setting `Inner::arc` and other ways the `arc` field
325326
// is used. Using `Arc` ended up requiring a number of funky transmutes and
@@ -527,7 +528,7 @@ impl Bytes {
527528
/// Requires that `begin <= end` and `end <= self.len()`, otherwise slicing
528529
/// will panic.
529530
pub fn slice(&self, range: impl RangeBounds<usize>) -> Bytes {
530-
use std::ops::Bound;
531+
use core::ops::Bound;
531532

532533
let len = self.len();
533534

@@ -857,7 +858,7 @@ impl Bytes {
857858
/// assert_eq!(iter.next().map(|b| *b), Some(b'c'));
858859
/// assert_eq!(iter.next(), None);
859860
/// ```
860-
pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, u8> {
861+
pub fn iter<'a>(&'a self) -> core::slice::Iter<'a, u8> {
861862
self.bytes().iter()
862863
}
863864
}
@@ -1031,7 +1032,7 @@ impl IntoIterator for Bytes {
10311032

10321033
impl<'a> IntoIterator for &'a Bytes {
10331034
type Item = &'a u8;
1034-
type IntoIter = std::slice::Iter<'a, u8>;
1035+
type IntoIter = core::slice::Iter<'a, u8>;
10351036

10361037
fn into_iter(self) -> Self::IntoIter {
10371038
self.as_ref().into_iter()
@@ -1539,7 +1540,7 @@ impl BytesMut {
15391540
/// assert_eq!(iter.next().map(|b| *b), Some(b'c'));
15401541
/// assert_eq!(iter.next(), None);
15411542
/// ```
1542-
pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, u8> {
1543+
pub fn iter<'a>(&'a self) -> core::slice::Iter<'a, u8> {
15431544
self.bytes().iter()
15441545
}
15451546
}
@@ -1780,7 +1781,7 @@ impl IntoIterator for BytesMut {
17801781

17811782
impl<'a> IntoIterator for &'a BytesMut {
17821783
type Item = &'a u8;
1783-
type IntoIter = std::slice::Iter<'a, u8>;
1784+
type IntoIter = core::slice::Iter<'a, u8>;
17841785

17851786
fn into_iter(self) -> Self::IntoIter {
17861787
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/hex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{Bytes, BytesMut};
2-
use std::fmt::{Formatter, LowerHex, Result, UpperHex};
2+
use core::fmt::{Formatter, LowerHex, Result, UpperHex};
33

44
struct BytesRef<'a>(&'a [u8]);
55

src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@
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+
#![no_std]
75+
76+
extern crate alloc;
77+
78+
#[cfg(feature = "std")]
79+
extern crate std;
80+
7481
pub mod buf;
7582
pub use crate::buf::{
7683
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)