From 13f1a5619190c4221b3e93f185fc0d89067cd93a Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Fri, 16 Jun 2017 16:00:54 -0700 Subject: [PATCH] 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 --- .travis.yml | 5 +++++ Cargo.toml | 9 ++++++++- src/buf/buf.rs | 19 ++++++++++++++++--- src/buf/buf_mut.rs | 14 ++++++++++++-- src/buf/chain.rs | 3 +++ src/buf/into_buf.rs | 9 +++++++++ src/buf/mod.rs | 2 ++ src/buf/reader.rs | 2 ++ src/buf/take.rs | 2 +- src/buf/writer.rs | 2 ++ src/bytes.rs | 40 +++++++++++++++++++++------------------- src/debug.rs | 2 +- src/lib.rs | 9 +++++++++ 13 files changed, 91 insertions(+), 27 deletions(-) diff --git a/.travis.yml b/.travis.yml index eb0d1eb58..3ed71ab6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,6 +36,11 @@ matrix: # Serde implementation - env: EXTRA_ARGS="--features serde" + # Ensure crate compiles without default features (i.e. for no_std) + - env: EXTRA_ARGS="--no-default-features" + script: cargo build $EXTRA_ARGS + rust: nightly + before_install: set -e install: diff --git a/Cargo.toml b/Cargo.toml index b4eb61f41..9e4eda86d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,8 +21,15 @@ categories = ["network-programming", "data-structures"] [dependencies] byteorder = "1.0.0" -iovec = "0.1" serde = { version = "1.0", optional = true } +[dependencies.iovec] +version = "0.1" +optional = true + [dev-dependencies] serde_test = "1.0" + +[features] +default = ["std"] +std = ["iovec"] diff --git a/src/buf/buf.rs b/src/buf/buf.rs index 42280e793..6b9ae457c 100644 --- a/src/buf/buf.rs +++ b/src/buf/buf.rs @@ -1,8 +1,15 @@ -use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain}; +use super::{IntoBuf, Take, Reader, Iter, Chain}; +#[cfg(feature = "std")] +use super::FromBuf; + use byteorder::ByteOrder; +#[cfg(feature = "std")] use iovec::IoVec; -use std::{cmp, io, ptr}; +use core::{cmp, ptr}; + +#[cfg(feature = "std")] +use std::io; /// Read bytes from a buffer. /// @@ -12,7 +19,7 @@ use std::{cmp, io, ptr}; /// position. It can be thought of as an efficient `Iterator` for collections of /// bytes. /// -/// The simplest `Buf` is a `Cursor` wrapping a `[u8]`. +/// The simplest `Buf` is a `io::Cursor` wrapping a `[u8]`. /// /// ``` /// use bytes::Buf; @@ -113,6 +120,7 @@ pub trait Buf { /// with `dst` being a zero length slice. /// /// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html + #[cfg(feature = "std")] fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize { if dst.is_empty() { return 0; @@ -520,6 +528,7 @@ pub trait Buf { /// /// assert_eq!(vec, &b"hello world"[..]); /// ``` + #[cfg(feature = "std")] fn collect(self) -> B where Self: Sized, B: FromBuf, @@ -662,6 +671,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T { (**self).bytes() } + #[cfg(feature = "std")] fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize { (**self).bytes_vec(dst) } @@ -671,6 +681,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T { } } +#[cfg(feature = "std")] impl Buf for Box { fn remaining(&self) -> usize { (**self).remaining() @@ -680,6 +691,7 @@ impl Buf for Box { (**self).bytes() } + #[cfg(feature = "std")] fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize { (**self).bytes_vec(dst) } @@ -689,6 +701,7 @@ impl Buf for Box { } } +#[cfg(feature = "std")] impl> Buf for io::Cursor { fn remaining(&self) -> usize { let len = self.get_ref().as_ref().len(); diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index b03103a61..c480be823 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -1,8 +1,12 @@ use super::{IntoBuf, Writer}; use byteorder::ByteOrder; +#[cfg(feature = "std")] use iovec::IoVec; -use std::{cmp, io, ptr, usize}; +use core::{cmp, ptr, usize}; + +#[cfg(feature = "std")] +use std::io; /// A trait for values that provide sequential write access to bytes. /// @@ -188,6 +192,7 @@ pub trait BufMut { /// with `dst` being a zero length slice. /// /// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html + #[cfg(feature = "std")] unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize { if dst.is_empty() { return 0; @@ -645,6 +650,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T { (**self).bytes_mut() } + #[cfg(feature = "std")] unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize { (**self).bytes_vec_mut(dst) } @@ -654,6 +660,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T { } } +#[cfg(feature = "std")] impl BufMut for Box { fn remaining_mut(&self) -> usize { (**self).remaining_mut() @@ -663,6 +670,7 @@ impl BufMut for Box { (**self).bytes_mut() } + #[cfg(feature = "std")] unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize { (**self).bytes_vec_mut(dst) } @@ -672,6 +680,7 @@ impl BufMut for Box { } } +#[cfg(feature = "std")] impl + AsRef<[u8]>> BufMut for io::Cursor { fn remaining_mut(&self) -> usize { use Buf; @@ -700,6 +709,7 @@ impl + AsRef<[u8]>> BufMut for io::Cursor { } } +#[cfg(feature = "std")] impl BufMut for Vec { #[inline] fn remaining_mut(&self) -> usize { @@ -721,7 +731,7 @@ impl BufMut for Vec { #[inline] unsafe fn bytes_mut(&mut self) -> &mut [u8] { - use std::slice; + use core::slice; if self.capacity() == self.len() { self.reserve(64); // Grow the vec diff --git a/src/buf/chain.rs b/src/buf/chain.rs index 7dd44ab02..22fe04232 100644 --- a/src/buf/chain.rs +++ b/src/buf/chain.rs @@ -1,4 +1,5 @@ use {Buf, BufMut}; +#[cfg(feature = "std")] use iovec::IoVec; /// A `Chain` sequences two buffers. @@ -177,6 +178,7 @@ impl Buf for Chain self.b.advance(cnt); } + #[cfg(feature = "std")] fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize { let mut n = self.a.bytes_vec(dst); n += self.b.bytes_vec(&mut dst[n..]); @@ -218,6 +220,7 @@ impl BufMut for Chain self.b.advance_mut(cnt); } + #[cfg(feature = "std")] unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize { let mut n = self.a.bytes_vec_mut(dst); n += self.b.bytes_vec_mut(&mut dst[n..]); diff --git a/src/buf/into_buf.rs b/src/buf/into_buf.rs index 1071908a2..5742978e1 100644 --- a/src/buf/into_buf.rs +++ b/src/buf/into_buf.rs @@ -1,5 +1,6 @@ use super::{Buf}; +#[cfg(feature = "std")] use std::io; /// Conversion into a `Buf` @@ -55,6 +56,7 @@ impl IntoBuf for T { } } +#[cfg(feature = "std")] impl<'a> IntoBuf for &'a [u8] { type Buf = io::Cursor<&'a [u8]>; @@ -63,6 +65,7 @@ impl<'a> IntoBuf for &'a [u8] { } } +#[cfg(feature = "std")] impl<'a> IntoBuf for &'a str { type Buf = io::Cursor<&'a [u8]>; @@ -71,6 +74,7 @@ impl<'a> IntoBuf for &'a str { } } +#[cfg(feature = "std")] impl IntoBuf for Vec { type Buf = io::Cursor>; @@ -79,6 +83,7 @@ impl IntoBuf for Vec { } } +#[cfg(feature = "std")] impl<'a> IntoBuf for &'a Vec { type Buf = io::Cursor<&'a [u8]>; @@ -89,6 +94,7 @@ impl<'a> IntoBuf for &'a Vec { // Kind of annoying... but this impl is required to allow passing `&'static // [u8]` where for<'a> &'a T: IntoBuf is required. +#[cfg(feature = "std")] impl<'a> IntoBuf for &'a &'static [u8] { type Buf = io::Cursor<&'static [u8]>; @@ -97,6 +103,7 @@ impl<'a> IntoBuf for &'a &'static [u8] { } } +#[cfg(feature = "std")] impl<'a> IntoBuf for &'a &'static str { type Buf = io::Cursor<&'static [u8]>; @@ -105,6 +112,7 @@ impl<'a> IntoBuf for &'a &'static str { } } +#[cfg(feature = "std")] impl IntoBuf for String { type Buf = io::Cursor>; @@ -113,6 +121,7 @@ impl IntoBuf for String { } } +#[cfg(feature = "std")] impl<'a> IntoBuf for &'a String { type Buf = io::Cursor<&'a [u8]>; diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 1f74e0ab4..335f51962 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -18,6 +18,7 @@ mod buf; mod buf_mut; +#[cfg(feature = "std")] mod from_buf; mod chain; mod into_buf; @@ -28,6 +29,7 @@ mod writer; pub use self::buf::Buf; pub use self::buf_mut::BufMut; +#[cfg(feature = "std")] pub use self::from_buf::FromBuf; pub use self::chain::Chain; pub use self::into_buf::IntoBuf; diff --git a/src/buf/reader.rs b/src/buf/reader.rs index 59f9c3304..771f967f0 100644 --- a/src/buf/reader.rs +++ b/src/buf/reader.rs @@ -1,5 +1,6 @@ use {Buf}; +#[cfg(feature = "std")] use std::{cmp, io}; /// A `Buf` adapter which implements `io::Read` for the inner value. @@ -78,6 +79,7 @@ impl Reader { } } +#[cfg(feature = "std")] impl io::Read for Reader { fn read(&mut self, dst: &mut [u8]) -> io::Result { let len = cmp::min(self.buf.remaining(), dst.len()); diff --git a/src/buf/take.rs b/src/buf/take.rs index a0c8ed479..a0babace5 100644 --- a/src/buf/take.rs +++ b/src/buf/take.rs @@ -1,6 +1,6 @@ use {Buf}; -use std::cmp; +use core::cmp; /// A `Buf` adapter which limits the bytes read from an underlying buffer. /// diff --git a/src/buf/writer.rs b/src/buf/writer.rs index 38a739aa6..18279b47c 100644 --- a/src/buf/writer.rs +++ b/src/buf/writer.rs @@ -1,5 +1,6 @@ use BufMut; +#[cfg(feature = "std")] use std::{cmp, io}; /// A `BufMut` adapter which implements `io::Write` for the inner value. @@ -74,6 +75,7 @@ impl Writer { } } +#[cfg(feature = "std")] impl io::Write for Writer { fn write(&mut self, src: &[u8]) -> io::Result { let n = cmp::min(self.buf.remaining_mut(), src.len()); diff --git a/src/bytes.rs b/src/bytes.rs index 6572434a2..3af3a74a8 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -2,11 +2,13 @@ use {IntoBuf, Buf, BufMut}; use buf::Iter; use debug; -use std::{cmp, fmt, mem, hash, ops, slice, ptr, usize}; -use std::borrow::Borrow; -use std::io::Cursor; -use std::sync::atomic::{self, AtomicUsize, AtomicPtr}; -use std::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel}; +use core::{cmp, fmt, mem, hash, ops, slice, ptr, usize}; +use core::borrow::Borrow; +use core::sync::atomic::{self, AtomicUsize, AtomicPtr}; +use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel}; + +#[cfg(feature = "std")] +use std::io; /// A reference counted contiguous slice of memory. /// @@ -320,10 +322,10 @@ struct Inner2 { } // Thread-safe reference-counted container for the shared storage. This mostly -// the same as `std::sync::Arc` but without the weak counter. The ref counting +// the same as `core::sync::Arc` but without the weak counter. The ref counting // fns are based on the ones found in `std`. // -// The main reason to use `Shared` instead of `std::sync::Arc` is that it ends +// The main reason to use `Shared` instead of `core::sync::Arc` is that it ends // up making the overall code simpler and easier to reason about. This is due to // some of the logic around setting `Inner::arc` and other ways the `arc` field // is used. Using `Arc` ended up requiring a number of funky transmutes and @@ -767,18 +769,18 @@ impl Bytes { } impl IntoBuf for Bytes { - type Buf = Cursor; + type Buf = io::Cursor; fn into_buf(self) -> Self::Buf { - Cursor::new(self) + io::Cursor::new(self) } } impl<'a> IntoBuf for &'a Bytes { - type Buf = Cursor; + type Buf = io::Cursor; fn into_buf(self) -> Self::Buf { - Cursor::new(self) + io::Cursor::new(self) } } @@ -886,7 +888,7 @@ impl Borrow<[u8]> for Bytes { impl IntoIterator for Bytes { type Item = u8; - type IntoIter = Iter>; + type IntoIter = Iter>; fn into_iter(self) -> Self::IntoIter { self.into_buf().iter() @@ -895,7 +897,7 @@ impl IntoIterator for Bytes { impl<'a> IntoIterator for &'a Bytes { type Item = u8; - type IntoIter = Iter>; + type IntoIter = Iter>; fn into_iter(self) -> Self::IntoIter { self.into_buf().iter() @@ -1375,18 +1377,18 @@ impl BufMut for BytesMut { } impl IntoBuf for BytesMut { - type Buf = Cursor; + type Buf = io::Cursor; fn into_buf(self) -> Self::Buf { - Cursor::new(self) + io::Cursor::new(self) } } impl<'a> IntoBuf for &'a BytesMut { - type Buf = Cursor<&'a BytesMut>; + type Buf = io::Cursor<&'a BytesMut>; fn into_buf(self) -> Self::Buf { - Cursor::new(self) + io::Cursor::new(self) } } @@ -1546,7 +1548,7 @@ impl Clone for BytesMut { impl IntoIterator for BytesMut { type Item = u8; - type IntoIter = Iter>; + type IntoIter = Iter>; fn into_iter(self) -> Self::IntoIter { self.into_buf().iter() @@ -1555,7 +1557,7 @@ impl IntoIterator for BytesMut { impl<'a> IntoIterator for &'a BytesMut { type Item = u8; - type IntoIter = Iter>; + type IntoIter = Iter>; fn into_iter(self) -> Self::IntoIter { self.into_buf().iter() diff --git a/src/debug.rs b/src/debug.rs index abead058a..9f4cbfb9a 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,4 +1,4 @@ -use std::fmt; +use core::fmt; /// Alternative implementation of `fmt::Debug` for byte slice. /// diff --git a/src/lib.rs b/src/lib.rs index fbe65721a..8227e8011 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,8 +70,13 @@ #![deny(warnings, missing_docs, missing_debug_implementations)] #![doc(html_root_url = "https://docs.rs/bytes/0.4")] +#![cfg_attr(not(feature = "std"), no_std)] extern crate byteorder; + +#[cfg(feature = "std")] +extern crate core; +#[cfg(feature = "std")] extern crate iovec; pub mod buf; @@ -88,8 +93,12 @@ pub use buf::{ Take, }; +#[cfg(feature = "std")] mod bytes; +#[cfg(feature = "std")] mod debug; + +#[cfg(feature = "std")] pub use bytes::{Bytes, BytesMut}; pub use byteorder::{ByteOrder, BigEndian, LittleEndian};