Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no_std support (with default-enabled "std" cargo feature) #255

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ matrix:
- env: RUST_TEST_THREADS=1 TARGET=powerpc-unknown-linux-gnu
- env: RUST_TEST_THREADS=1 TARGET=powerpc64-unknown-linux-gnu

# Ensure crate compiles without default features (i.e. for no_std)
- env: EXTRA_ARGS="--no-default-features"
script: cargo build $EXTRA_ARGS

# Serde implementation
- env: EXTRA_ARGS="--features serde"

Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ exclude = [
"bench/**/*",
"test/**/*"
]
categories = ["network-programming", "data-structures"]
categories = ["network-programming", "data-structures", "no-std"]

[package.metadata.docs.rs]
features = ["i128"]

[dependencies]
byteorder = "1.1.0"
iovec = "0.1"
iovec = { version = "0.1", optional = true }
serde = { version = "1.0", optional = true }
either = { version = "1.5", default-features = false, optional = true }

[dev-dependencies]
serde_test = "1.0"

[features]
default = ["std"]
i128 = ["byteorder/i128"]
std = ["iovec"]
12 changes: 11 additions & 1 deletion src/buf/buf.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
use byteorder::{BigEndian, ByteOrder, LittleEndian};
#[cfg(feature = "iovec")]
use iovec::IoVec;
#[cfg(feature = "std")]
use prelude::*;

use std::{cmp, io, ptr};
use core::{cmp, ptr};
#[cfg(feature = "std")]
use std::io;

macro_rules! buf_get_impl {
($this:ident, $size:expr, $conv:path) => ({
Expand Down Expand Up @@ -146,6 +151,7 @@ pub trait Buf {
/// with `dst` being a zero length slice.
///
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
#[cfg(feature = "iovec")]
fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize {
if dst.is_empty() {
return 0;
Expand Down Expand Up @@ -1061,6 +1067,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
(**self).bytes()
}

#[cfg(feature = "iovec")]
fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
(**self).bytes_vec(dst)
}
Expand All @@ -1070,6 +1077,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
}
}

#[cfg(feature = "std")]
impl<T: Buf + ?Sized> Buf for Box<T> {
fn remaining(&self) -> usize {
(**self).remaining()
Expand All @@ -1079,6 +1087,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
(**self).bytes()
}

#[cfg(feature = "iovec")]
fn bytes_vec<'b>(&'b self, dst: &mut [&'b IoVec]) -> usize {
(**self).bytes_vec(dst)
}
Expand All @@ -1088,6 +1097,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
}
}

#[cfg(feature = "std")]
impl<T: AsRef<[u8]>> Buf for io::Cursor<T> {
fn remaining(&self) -> usize {
let len = self.get_ref().as_ref().len();
Expand Down
14 changes: 12 additions & 2 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use super::{IntoBuf, Writer};
use byteorder::{LittleEndian, ByteOrder, BigEndian};
#[cfg(feature = "iovec")]
use iovec::IoVec;
#[cfg(feature = "std")]
use prelude::*;

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.
///
Expand Down Expand Up @@ -189,6 +194,7 @@ pub trait BufMut {
/// with `dst` being a zero length slice.
///
/// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html
#[cfg(feature = "iovec")]
unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize {
if dst.is_empty() {
return 0;
Expand Down Expand Up @@ -1072,6 +1078,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
(**self).bytes_mut()
}

#[cfg(feature = "iovec")]
unsafe fn bytes_vec_mut<'b>(&'b mut self, dst: &mut [&'b mut IoVec]) -> usize {
(**self).bytes_vec_mut(dst)
}
Expand All @@ -1081,6 +1088,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
}
}

#[cfg(feature = "std")]
impl<T: BufMut + ?Sized> BufMut for Box<T> {
fn remaining_mut(&self) -> usize {
(**self).remaining_mut()
Expand All @@ -1099,6 +1107,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
}
}

#[cfg(feature = "std")]
impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
fn remaining_mut(&self) -> usize {
use Buf;
Expand Down Expand Up @@ -1127,6 +1136,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for io::Cursor<T> {
}
}

#[cfg(feature = "std")]
impl BufMut for Vec<u8> {
#[inline]
fn remaining_mut(&self) -> usize {
Expand All @@ -1148,7 +1158,7 @@ impl BufMut for Vec<u8> {

#[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
Expand Down
3 changes: 3 additions & 0 deletions src/buf/chain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use {Buf, BufMut};
#[cfg(feature = "iovec")]
use iovec::IoVec;

/// A `Chain` sequences two buffers.
Expand Down Expand Up @@ -177,6 +178,7 @@ impl<T, U> Buf for Chain<T, U>
self.b.advance(cnt);
}

#[cfg(feature = "iovec")]
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..]);
Expand Down Expand Up @@ -218,6 +220,7 @@ impl<T, U> BufMut for Chain<T, U>
self.b.advance_mut(cnt);
}

#[cfg(feature = "iovec")]
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..]);
Expand Down
9 changes: 8 additions & 1 deletion src/buf/from_buf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use {Buf, BufMut, IntoBuf, Bytes, BytesMut};
use {IntoBuf};
#[cfg(feature = "std")]
use prelude::*;
#[cfg(feature = "std")]
use {Buf, BufMut, Bytes, BytesMut};

/// Conversion from a [`Buf`]
///
Expand Down Expand Up @@ -86,6 +90,7 @@ pub trait FromBuf {
fn from_buf<T>(buf: T) -> Self where T: IntoBuf;
}

#[cfg(feature = "std")]
impl FromBuf for Vec<u8> {
fn from_buf<T>(buf: T) -> Self
where T: IntoBuf
Expand All @@ -97,6 +102,7 @@ impl FromBuf for Vec<u8> {
}
}

#[cfg(feature = "std")]
impl FromBuf for Bytes {
fn from_buf<T>(buf: T) -> Self
where T: IntoBuf
Expand All @@ -105,6 +111,7 @@ impl FromBuf for Bytes {
}
}

#[cfg(feature = "std")]
impl FromBuf for BytesMut {
fn from_buf<T>(buf: T) -> Self
where T: IntoBuf
Expand Down
12 changes: 12 additions & 0 deletions src/buf/into_buf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{Buf};
#[cfg(feature = "std")]
use prelude::*;

#[cfg(feature = "std")]
use std::io;

/// Conversion into a `Buf`
Expand Down Expand Up @@ -55,6 +58,7 @@ impl<T: Buf> IntoBuf for T {
}
}

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

Expand All @@ -63,6 +67,7 @@ impl<'a> IntoBuf for &'a [u8] {
}
}

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

Expand All @@ -71,6 +76,7 @@ impl<'a> IntoBuf for &'a mut [u8] {
}
}

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

Expand All @@ -79,6 +85,7 @@ impl<'a> IntoBuf for &'a str {
}
}

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

Expand All @@ -87,6 +94,7 @@ impl IntoBuf for Vec<u8> {
}
}

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

Expand All @@ -97,6 +105,7 @@ impl<'a> IntoBuf for &'a Vec<u8> {

// 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]>;

Expand All @@ -105,6 +114,7 @@ impl<'a> IntoBuf for &'a &'static [u8] {
}
}

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

Expand All @@ -113,6 +123,7 @@ impl<'a> IntoBuf for &'a &'static str {
}
}

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

Expand All @@ -121,6 +132,7 @@ impl IntoBuf for String {
}
}

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

Expand Down
1 change: 1 addition & 0 deletions src/buf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod into_buf;
mod iter;
mod reader;
mod take;
#[cfg(feature = "std")]
mod vec_deque;
mod writer;

Expand Down
3 changes: 3 additions & 0 deletions src/buf/reader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {Buf};

#[cfg(feature = "std")]
use std::{cmp, io};

/// A `Buf` adapter which implements `io::Read` for the inner value.
Expand Down Expand Up @@ -78,6 +79,7 @@ impl<B: Buf> Reader<B> {
}
}

#[cfg(feature = "std")]
impl<B: Buf + Sized> io::Read for Reader<B> {
fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
let len = cmp::min(self.buf.remaining(), dst.len());
Expand All @@ -87,6 +89,7 @@ impl<B: Buf + Sized> io::Read for Reader<B> {
}
}

#[cfg(feature = "std")]
impl<B: Buf + Sized> io::BufRead for Reader<B> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
Ok(self.buf.bytes())
Expand Down
2 changes: 1 addition & 1 deletion src/buf/take.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {Buf};

use std::cmp;
use core::cmp;

/// A `Buf` adapter which limits the bytes read from an underlying buffer.
///
Expand Down
1 change: 1 addition & 0 deletions src/buf/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Buf for VecDeque<u8> {
#[cfg(test)]
mod tests {
use super::*;
use prelude::*;

#[test]
fn hello_world() {
Expand Down
2 changes: 2 additions & 0 deletions src/buf/writer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use BufMut;

#[cfg(feature = "std")]
use std::{cmp, io};

/// A `BufMut` adapter which implements `io::Write` for the inner value.
Expand Down Expand Up @@ -74,6 +75,7 @@ impl<B: BufMut> Writer<B> {
}
}

#[cfg(feature = "std")]
impl<B: BufMut + Sized> io::Write for Writer<B> {
fn write(&mut self, src: &[u8]) -> io::Result<usize> {
let n = cmp::min(self.buf.remaining_mut(), src.len());
Expand Down
3 changes: 2 additions & 1 deletion src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {IntoBuf, Buf, BufMut};
use buf::Iter;
use debug;
use prelude::*;

use std::{cmp, fmt, mem, hash, ops, slice, ptr, usize};
use std::borrow::{Borrow, BorrowMut};
Expand Down Expand Up @@ -1499,7 +1500,7 @@ impl BytesMut {
}

unsafe {
ptr = self.inner.ptr.offset(self.inner.len as isize);
ptr = self.inner.ptr.offset(self.inner.len as isize);
}
if ptr == other.inner.ptr &&
self.inner.kind() == KIND_ARC &&
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@

#![deny(warnings, missing_docs, missing_debug_implementations)]
#![doc(html_root_url = "https://docs.rs/bytes/0.4.12")]
#![no_std]

extern crate byteorder;
#[cfg(feature = "iovec")]
extern crate iovec;
#[cfg(feature = "std")]
extern crate std;

pub mod buf;
pub use buf::{
Expand All @@ -88,9 +92,13 @@ pub use buf::{
Take,
};

#[cfg(feature = "std")]
mod bytes;
#[cfg(feature = "std")]
mod debug;
#[cfg(feature = "std")]
pub use bytes::{Bytes, BytesMut};
mod prelude;

#[deprecated]
pub use byteorder::{ByteOrder, BigEndian, LittleEndian};
Expand Down
Loading