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 19, 2017
1 parent 6871b20 commit 229772f
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ matrix:
- script: cargo build --no-default-features
rust: nightly

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

before_install: set -e

install:
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"]
9 changes: 7 additions & 2 deletions src/buf/buf.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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"))]
Expand Down Expand Up @@ -528,6 +531,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 +684,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
6 changes: 4 additions & 2 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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"))]
Expand Down Expand Up @@ -665,6 +665,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 +713,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
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
13 changes: 9 additions & 4 deletions src/buf/into_buf.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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(not(feature = "use_std"))]
use buf::Cursor;
#[cfg(feature = "use_std")]
use std::io::Cursor;

Expand Down Expand Up @@ -78,6 +79,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 +88,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 +115,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 +124,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
2 changes: 2 additions & 0 deletions src/buf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

mod buf;
mod buf_mut;
#[cfg(any(feature = "use_alloc", feature = "use_std"))]
mod from_buf;
mod chain;
#[cfg(not(feature = "use_std"))]
Expand All @@ -30,6 +31,7 @@ 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"))]
Expand Down
11 changes: 6 additions & 5 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ 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(not(feature = "use_std"))]
use buf::Cursor;
#[cfg(feature = "use_std")]
use std::io::Cursor;

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 229772f

Please sign in to comment.