diff --git a/Cargo.toml b/Cargo.toml index bf588a165..ea534e696 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,8 @@ edition = "2018" [features] default = ["std"] -std = [] +std = ["alloc"] +alloc = [] [dependencies] serde = { version = "1.0.60", optional = true, default-features = false, features = ["alloc"] } diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index a33c8a42d..b9cedb7cd 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -7,6 +7,7 @@ use core::{cmp, mem, ptr}; #[cfg(feature = "std")] use std::io::IoSlice; +#[cfg(feature = "alloc")] use alloc::boxed::Box; macro_rules! buf_get_impl { @@ -813,6 +814,7 @@ pub trait Buf { /// let bytes = (&b"hello world"[..]).copy_to_bytes(5); /// assert_eq!(&bytes[..], &b"hello"[..]); /// ``` + #[cfg(feature = "alloc")] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { use super::BufMut; @@ -1004,6 +1006,7 @@ macro_rules! deref_forward_buf { (**self).get_int_le(nbytes) } + #[cfg(feature = "alloc")] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { (**self).copy_to_bytes(len) } @@ -1014,6 +1017,7 @@ impl Buf for &mut T { deref_forward_buf!(); } +#[cfg(feature = "alloc")] impl Buf for Box { deref_forward_buf!(); } diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index eccd5096d..59faa174a 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -4,6 +4,7 @@ use crate::buf::{writer, Writer}; use core::{cmp, mem, ptr, usize}; +#[cfg(feature = "alloc")] use alloc::{boxed::Box, vec::Vec}; /// A trait for values that provide sequential write access to bytes. @@ -990,6 +991,7 @@ unsafe impl BufMut for &mut T { deref_forward_bufmut!(); } +#[cfg(feature = "alloc")] unsafe impl BufMut for Box { deref_forward_bufmut!(); } @@ -1014,6 +1016,7 @@ unsafe impl BufMut for &mut [u8] { } } +#[cfg(feature = "alloc")] unsafe impl BufMut for Vec { #[inline] fn remaining_mut(&self) -> usize { diff --git a/src/lib.rs b/src/lib.rs index dd8cc9661..93b8b89b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,11 +84,16 @@ extern crate std; pub mod buf; pub use crate::buf::{Buf, BufMut}; +#[cfg(feature = "alloc")] mod bytes; +#[cfg(feature = "alloc")] mod bytes_mut; +#[cfg(feature = "alloc")] mod fmt; mod loom; +#[cfg(feature = "alloc")] pub use crate::bytes::Bytes; +#[cfg(feature = "alloc")] pub use crate::bytes_mut::BytesMut; // Optional Serde support diff --git a/tests/test_buf.rs b/tests/test_buf.rs index fbad003a4..8a4c25a69 100644 --- a/tests/test_buf.rs +++ b/tests/test_buf.rs @@ -56,6 +56,7 @@ fn test_bufs_vec() { assert_eq!(1, buf.chunks_vectored(&mut dst[..])); } +#[cfg(feature = "alloc")] #[test] fn test_vec_deque() { use std::collections::VecDeque; @@ -98,10 +99,13 @@ fn test_deref_buf_forwards() { // these should all use the specialized method assert_eq!(Special.get_u8(), b'x'); assert_eq!((&mut Special as &mut dyn Buf).get_u8(), b'x'); + #[cfg(feature = "alloc")] assert_eq!((Box::new(Special) as Box).get_u8(), b'x'); + #[cfg(feature = "alloc")] assert_eq!(Box::new(Special).get_u8(), b'x'); } +#[cfg(feature = "alloc")] #[test] fn copy_to_bytes_less() { let mut buf = &b"hello world"[..]; @@ -111,6 +115,7 @@ fn copy_to_bytes_less() { assert_eq!(buf, &b" world"[..]) } +#[cfg(feature = "alloc")] #[test] #[should_panic] fn copy_to_bytes_overflow() { diff --git a/tests/test_buf_mut.rs b/tests/test_buf_mut.rs index 8d270e30a..558757550 100644 --- a/tests/test_buf_mut.rs +++ b/tests/test_buf_mut.rs @@ -1,4 +1,5 @@ #![warn(rust_2018_idioms)] +#![cfg(feature = "alloc")] use bytes::buf::UninitSlice; use bytes::{BufMut, BytesMut}; diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index b9e6ce4d3..84f4e7914 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -1,4 +1,5 @@ #![warn(rust_2018_idioms)] +#![cfg(feature = "alloc")] use bytes::{Buf, BufMut, Bytes, BytesMut}; diff --git a/tests/test_bytes_odd_alloc.rs b/tests/test_bytes_odd_alloc.rs index 04ba7c2f1..c3212ab1b 100644 --- a/tests/test_bytes_odd_alloc.rs +++ b/tests/test_bytes_odd_alloc.rs @@ -2,6 +2,7 @@ //! vectors (pointers where the LSB is set). #![cfg(not(miri))] // Miri does not support custom allocators (also, Miri is "odd" by default with 50% chance) +#![cfg(feature = "alloc")] use std::alloc::{GlobalAlloc, Layout, System}; use std::ptr; diff --git a/tests/test_bytes_vec_alloc.rs b/tests/test_bytes_vec_alloc.rs index 418a9cd64..fc7b4da88 100644 --- a/tests/test_bytes_vec_alloc.rs +++ b/tests/test_bytes_vec_alloc.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "alloc")] use std::alloc::{GlobalAlloc, Layout, System}; use std::{mem, ptr}; diff --git a/tests/test_chain.rs b/tests/test_chain.rs index 500ccd4a8..94bf16aca 100644 --- a/tests/test_chain.rs +++ b/tests/test_chain.rs @@ -1,9 +1,14 @@ #![warn(rust_2018_idioms)] -use bytes::{Buf, BufMut, Bytes}; +use bytes::{Buf, BufMut}; + +#[cfg(feature = "alloc")] +use bytes::BytesMut; + #[cfg(feature = "std")] use std::io::IoSlice; +#[cfg(feature = "alloc")] #[test] fn collect_two_bufs() { let a = Bytes::from(&b"hello"[..]); @@ -33,6 +38,7 @@ fn writing_chained() { } } +#[cfg(feature = "alloc")] #[test] fn iterating_two_bufs() { let a = Bytes::from(&b"hello"[..]); diff --git a/tests/test_debug.rs b/tests/test_debug.rs index 08d2f254e..7bc5ca87b 100644 --- a/tests/test_debug.rs +++ b/tests/test_debug.rs @@ -1,4 +1,5 @@ #![warn(rust_2018_idioms)] +#![cfg(feature = "alloc")] use bytes::Bytes; diff --git a/tests/test_iter.rs b/tests/test_iter.rs index a5bfddddf..a15077b9c 100644 --- a/tests/test_iter.rs +++ b/tests/test_iter.rs @@ -1,4 +1,5 @@ #![warn(rust_2018_idioms)] +#![cfg(feature = "alloc")] use bytes::Bytes;