Skip to content

Commit

Permalink
Feature gate allocator-dependent features on "use_alloc" or "use_std"
Browse files Browse the repository at this point in the history
Flags anything dependent on an allocator behind a feature gate, ensuring the
library still compiles without default features (although it may not be very
useful)

Adds "use_alloc" to obtain things like Box, String, and Vec from liballoc.
  • Loading branch information
tonychain committed Jun 26, 2017
1 parent 6871b20 commit e0982c9
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 32 deletions.
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ matrix:
- env: EXTRA_ARGS="--features serde"

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

# Ensure crate compiles with "use_alloc" instead of "use_std" (i.e. for no_std)
- env: EXTRA_ARGS="--no-default-features --features use_alloc"
script: cargo build $EXTRA_ARGS
rust: nightly

before_install: set -e
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ serde_test = "1.0"

[features]
default = ["use_std"]
use_alloc = []
use_std = ["iovec"]
12 changes: 7 additions & 5 deletions src/buf/buf.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
use super::{IntoBuf, Take, Reader, Iter, Chain};
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
use super::FromBuf;

use byteorder::ByteOrder;
#[cfg(feature = "use_std")]
use iovec::IoVec;

#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
use alloc::boxed::Box;
use core::{cmp, ptr};
#[cfg(not(feature = "use_std"))]
use buf::Cursor;
#[cfg(feature = "use_std")]
use std::io::Cursor;

/// Read bytes from a buffer.
///
Expand Down Expand Up @@ -528,6 +528,7 @@ pub trait Buf {
///
/// assert_eq!(vec, &b"hello world"[..]);
/// ```
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
fn collect<B>(self) -> B
where Self: Sized,
B: FromBuf,
Expand Down Expand Up @@ -680,6 +681,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T {
}
}

#[cfg(any(feature = "use_alloc", feature = "use_std"))]
impl<T: Buf + ?Sized> Buf for Box<T> {
fn remaining(&self) -> usize {
(**self).remaining()
Expand Down
9 changes: 4 additions & 5 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ use byteorder::ByteOrder;
#[cfg(feature = "use_std")]
use iovec::IoVec;

#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
use alloc::boxed::Box;
#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
use alloc::vec::Vec;
use core::{cmp, ptr, usize};
#[cfg(not(feature = "use_std"))]
use buf::Cursor;
#[cfg(feature = "use_std")]
use std::io::Cursor;

/// A trait for values that provide sequential write access to bytes.
///
Expand Down Expand Up @@ -665,6 +662,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T {
}
}

#[cfg(any(feature = "use_alloc", feature = "use_std"))]
impl<T: BufMut + ?Sized> BufMut for Box<T> {
fn remaining_mut(&self) -> usize {
(**self).remaining_mut()
Expand Down Expand Up @@ -712,6 +710,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>> BufMut for Cursor<T> {
}
}

#[cfg(any(feature = "use_alloc", feature = "use_std"))]
impl BufMut for Vec<u8> {
#[inline]
fn remaining_mut(&self) -> usize {
Expand Down
8 changes: 5 additions & 3 deletions src/buf/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

/// A `Cursor` wraps another type and provides it with a
/// [`Seek`] implementation.
///
Expand All @@ -18,12 +21,14 @@
/// The standard library implements some I/O traits on various types which
/// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and
/// `Cursor<`[`&[u8]`][bytes]`>`.
#[cfg(not(feature = "use_std"))]
#[derive(Clone, Debug)]
pub struct Cursor<T> {
inner: T,
pos: u64,
}

#[cfg(not(feature = "use_std"))]
impl<T> Cursor<T> {
/// Creates a new cursor wrapping the provided underlying I/O object.
///
Expand All @@ -34,9 +39,6 @@ impl<T> Cursor<T> {
Cursor { pos: 0, inner: inner }
}

/// Consumes this cursor, returning the underlying value.
pub fn into_inner(self) -> T { self.inner }

/// Gets a reference to the underlying value in this cursor.
pub fn get_ref(&self) -> &T { &self.inner }

Expand Down
2 changes: 1 addition & 1 deletion src/buf/from_buf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {Buf, BufMut, IntoBuf, Bytes, BytesMut};

#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
use alloc::vec::Vec;

/// Conversion from a [`Buf`]
Expand Down
14 changes: 8 additions & 6 deletions src/buf/into_buf.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use super::{Buf};

#[cfg(not(feature = "use_std"))]
use buf::Cursor;
#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
use alloc::string::String;
#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
use alloc::vec::Vec;
#[cfg(feature = "use_std")]
use std::io::Cursor;

use buf::Cursor;

/// Conversion into a `Buf`
///
Expand Down Expand Up @@ -78,6 +76,7 @@ impl<'a> IntoBuf for &'a str {
}
}

#[cfg(any(feature = "use_alloc", feature = "use_std"))]
impl IntoBuf for Vec<u8> {
type Buf = Cursor<Vec<u8>>;

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

#[cfg(any(feature = "use_alloc", feature = "use_std"))]
impl<'a> IntoBuf for &'a Vec<u8> {
type Buf = Cursor<&'a [u8]>;

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

#[cfg(any(feature = "use_alloc", feature = "use_std"))]
impl IntoBuf for String {
type Buf = Cursor<Vec<u8>>;

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

#[cfg(any(feature = "use_alloc", feature = "use_std"))]
impl<'a> IntoBuf for &'a String {
type Buf = Cursor<&'a [u8]>;

Expand Down
4 changes: 2 additions & 2 deletions src/buf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

mod buf;
mod buf_mut;
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
mod from_buf;
mod chain;
#[cfg(not(feature = "use_std"))]
mod cursor;
mod into_buf;
mod iter;
Expand All @@ -30,9 +30,9 @@ mod writer;

pub use self::buf::Buf;
pub use self::buf_mut::BufMut;
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
pub use self::from_buf::FromBuf;
pub use self::chain::Chain;
#[cfg(not(feature = "use_std"))]
pub use self::cursor::Cursor;
pub use self::into_buf::IntoBuf;
pub use self::iter::Iter;
Expand Down
12 changes: 5 additions & 7 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ use core::borrow::Borrow;
use core::sync::atomic::{self, AtomicUsize, AtomicPtr};
use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};

#[cfg(not(feature = "use_std"))]
use buf::Cursor;
#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
use alloc::boxed::Box;
#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
use alloc::string::String;
#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
use alloc::vec::Vec;
#[cfg(feature = "use_std")]
use std::io::Cursor;

use buf::Cursor;

/// A reference counted contiguous slice of memory.
///
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@
#![deny(warnings, missing_docs, missing_debug_implementations)]
#![doc(html_root_url = "https://docs.rs/bytes/0.4")]
#![cfg_attr(not(feature = "use_std"), no_std)]
#![cfg_attr(not(feature = "use_std"), feature(alloc))]
#![cfg_attr(feature = "use_alloc", feature(alloc))]

extern crate byteorder;

#[cfg(not(feature = "use_std"))]
#[cfg(feature = "use_alloc")]
extern crate alloc;
#[cfg(feature = "use_std")]
extern crate core;
Expand All @@ -96,8 +96,12 @@ pub use buf::{
Take,
};

#[cfg(any(feature = "use_alloc", feature = "use_std"))]
mod bytes;
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
mod debug;

#[cfg(any(feature = "use_alloc", feature = "use_std"))]
pub use bytes::{Bytes, BytesMut};

pub use byteorder::{ByteOrder, BigEndian, LittleEndian};
Expand Down

0 comments on commit e0982c9

Please sign in to comment.