From 229772f28a3035080bad58279766262f35f1265c Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Mon, 19 Jun 2017 10:46:29 -0700 Subject: [PATCH] Feature gate allocator-dependent features on "use_alloc" or "use_std" 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. --- .travis.yml | 4 ++++ Cargo.toml | 1 + src/buf/buf.rs | 9 +++++++-- src/buf/buf_mut.rs | 6 ++++-- src/buf/from_buf.rs | 2 +- src/buf/into_buf.rs | 13 +++++++++---- src/buf/mod.rs | 2 ++ src/bytes.rs | 11 ++++++----- src/lib.rs | 8 ++++++-- 9 files changed, 40 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index fa72afa03..f23475378 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/Cargo.toml b/Cargo.toml index ca1af38c6..1ba8ec2e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,4 +32,5 @@ serde_test = "1.0" [features] default = ["use_std"] +use_alloc = [] use_std = ["iovec"] diff --git a/src/buf/buf.rs b/src/buf/buf.rs index 7f1a66186..a24d73134 100644 --- a/src/buf/buf.rs +++ b/src/buf/buf.rs @@ -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"))] @@ -528,6 +531,7 @@ pub trait Buf { /// /// assert_eq!(vec, &b"hello world"[..]); /// ``` + #[cfg(any(feature = "use_alloc", feature = "use_std"))] fn collect(self) -> B where Self: Sized, B: FromBuf, @@ -680,6 +684,7 @@ impl<'a, T: Buf + ?Sized> Buf for &'a mut T { } } +#[cfg(any(feature = "use_alloc", feature = "use_std"))] impl Buf for Box { fn remaining(&self) -> usize { (**self).remaining() diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index 82b5f2389..d24c6622c 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -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"))] @@ -665,6 +665,7 @@ impl<'a, T: BufMut + ?Sized> BufMut for &'a mut T { } } +#[cfg(any(feature = "use_alloc", feature = "use_std"))] impl BufMut for Box { fn remaining_mut(&self) -> usize { (**self).remaining_mut() @@ -712,6 +713,7 @@ impl + AsRef<[u8]>> BufMut for Cursor { } } +#[cfg(any(feature = "use_alloc", feature = "use_std"))] impl BufMut for Vec { #[inline] fn remaining_mut(&self) -> usize { diff --git a/src/buf/from_buf.rs b/src/buf/from_buf.rs index 4e0a2c078..9bac9b86b 100644 --- a/src/buf/from_buf.rs +++ b/src/buf/from_buf.rs @@ -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`] diff --git a/src/buf/into_buf.rs b/src/buf/into_buf.rs index c7f685c45..f2db1260c 100644 --- a/src/buf/into_buf.rs +++ b/src/buf/into_buf.rs @@ -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; @@ -78,6 +79,7 @@ impl<'a> IntoBuf for &'a str { } } +#[cfg(any(feature = "use_alloc", feature = "use_std"))] impl IntoBuf for Vec { type Buf = Cursor>; @@ -86,6 +88,7 @@ impl IntoBuf for Vec { } } +#[cfg(any(feature = "use_alloc", feature = "use_std"))] impl<'a> IntoBuf for &'a Vec { type Buf = Cursor<&'a [u8]>; @@ -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>; @@ -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]>; diff --git a/src/buf/mod.rs b/src/buf/mod.rs index 63a9a3018..de9d4165b 100644 --- a/src/buf/mod.rs +++ b/src/buf/mod.rs @@ -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"))] @@ -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"))] diff --git a/src/bytes.rs b/src/bytes.rs index 24d062fd8..62afc1452 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -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; diff --git a/src/lib.rs b/src/lib.rs index 6b1f6fabc..294d88335 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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};