Skip to content

Commit b2fef63

Browse files
committed
Make alloc optional - the easy way
1 parent e418280 commit b2fef63

12 files changed

+32
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ edition = "2018"
2222

2323
[features]
2424
default = ["std"]
25-
std = []
25+
std = ["alloc"]
26+
alloc = []
2627

2728
[dependencies]
2829
serde = { version = "1.0.60", optional = true, default-features = false, features = ["alloc"] }

src/buf/buf_impl.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use core::{cmp, mem, ptr};
77
#[cfg(feature = "std")]
88
use std::io::IoSlice;
99

10+
#[cfg(feature = "alloc")]
1011
use alloc::boxed::Box;
1112

1213
macro_rules! buf_get_impl {
@@ -813,6 +814,7 @@ pub trait Buf {
813814
/// let bytes = (&b"hello world"[..]).copy_to_bytes(5);
814815
/// assert_eq!(&bytes[..], &b"hello"[..]);
815816
/// ```
817+
#[cfg(feature = "alloc")]
816818
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
817819
use super::BufMut;
818820

@@ -1004,6 +1006,7 @@ macro_rules! deref_forward_buf {
10041006
(**self).get_int_le(nbytes)
10051007
}
10061008

1009+
#[cfg(feature = "alloc")]
10071010
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
10081011
(**self).copy_to_bytes(len)
10091012
}
@@ -1014,6 +1017,7 @@ impl<T: Buf + ?Sized> Buf for &mut T {
10141017
deref_forward_buf!();
10151018
}
10161019

1020+
#[cfg(feature = "alloc")]
10171021
impl<T: Buf + ?Sized> Buf for Box<T> {
10181022
deref_forward_buf!();
10191023
}

src/buf/buf_mut.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::buf::{writer, Writer};
44

55
use core::{cmp, mem, ptr, usize};
66

7+
#[cfg(feature = "alloc")]
78
use alloc::{boxed::Box, vec::Vec};
89

910
/// A trait for values that provide sequential write access to bytes.
@@ -990,6 +991,7 @@ unsafe impl<T: BufMut + ?Sized> BufMut for &mut T {
990991
deref_forward_bufmut!();
991992
}
992993

994+
#[cfg(feature = "alloc")]
993995
unsafe impl<T: BufMut + ?Sized> BufMut for Box<T> {
994996
deref_forward_bufmut!();
995997
}
@@ -1014,6 +1016,7 @@ unsafe impl BufMut for &mut [u8] {
10141016
}
10151017
}
10161018

1019+
#[cfg(feature = "alloc")]
10171020
unsafe impl BufMut for Vec<u8> {
10181021
#[inline]
10191022
fn remaining_mut(&self) -> usize {

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,16 @@ extern crate std;
8484
pub mod buf;
8585
pub use crate::buf::{Buf, BufMut};
8686

87+
#[cfg(feature = "alloc")]
8788
mod bytes;
89+
#[cfg(feature = "alloc")]
8890
mod bytes_mut;
91+
#[cfg(feature = "alloc")]
8992
mod fmt;
9093
mod loom;
94+
#[cfg(feature = "alloc")]
9195
pub use crate::bytes::Bytes;
96+
#[cfg(feature = "alloc")]
9297
pub use crate::bytes_mut::BytesMut;
9398

9499
// Optional Serde support

tests/test_buf.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ fn test_bufs_vec() {
5656
assert_eq!(1, buf.chunks_vectored(&mut dst[..]));
5757
}
5858

59+
#[cfg(feature = "alloc")]
5960
#[test]
6061
fn test_vec_deque() {
6162
use std::collections::VecDeque;
@@ -98,10 +99,13 @@ fn test_deref_buf_forwards() {
9899
// these should all use the specialized method
99100
assert_eq!(Special.get_u8(), b'x');
100101
assert_eq!((&mut Special as &mut dyn Buf).get_u8(), b'x');
102+
#[cfg(feature = "alloc")]
101103
assert_eq!((Box::new(Special) as Box<dyn Buf>).get_u8(), b'x');
104+
#[cfg(feature = "alloc")]
102105
assert_eq!(Box::new(Special).get_u8(), b'x');
103106
}
104107

108+
#[cfg(feature = "alloc")]
105109
#[test]
106110
fn copy_to_bytes_less() {
107111
let mut buf = &b"hello world"[..];
@@ -111,6 +115,7 @@ fn copy_to_bytes_less() {
111115
assert_eq!(buf, &b" world"[..])
112116
}
113117

118+
#[cfg(feature = "alloc")]
114119
#[test]
115120
#[should_panic]
116121
fn copy_to_bytes_overflow() {

tests/test_buf_mut.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(rust_2018_idioms)]
2+
#![cfg(feature = "alloc")]
23

34
use bytes::buf::UninitSlice;
45
use bytes::{BufMut, BytesMut};

tests/test_bytes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(rust_2018_idioms)]
2+
#![cfg(feature = "alloc")]
23

34
use bytes::{Buf, BufMut, Bytes, BytesMut};
45

tests/test_bytes_odd_alloc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! vectors (pointers where the LSB is set).
33
44
#![cfg(not(miri))] // Miri does not support custom allocators (also, Miri is "odd" by default with 50% chance)
5+
#![cfg(feature = "alloc")]
56

67
use std::alloc::{GlobalAlloc, Layout, System};
78
use std::ptr;

tests/test_bytes_vec_alloc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg(feature = "alloc")]
12
use std::alloc::{GlobalAlloc, Layout, System};
23
use std::{mem, ptr};
34

tests/test_chain.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#![warn(rust_2018_idioms)]
22

3-
use bytes::{Buf, BufMut, Bytes};
3+
use bytes::{Buf, BufMut};
4+
5+
#[cfg(feature = "alloc")]
6+
use bytes::BytesMut;
7+
48
#[cfg(feature = "std")]
59
use std::io::IoSlice;
610

11+
#[cfg(feature = "alloc")]
712
#[test]
813
fn collect_two_bufs() {
914
let a = Bytes::from(&b"hello"[..]);
@@ -33,6 +38,7 @@ fn writing_chained() {
3338
}
3439
}
3540

41+
#[cfg(feature = "alloc")]
3642
#[test]
3743
fn iterating_two_bufs() {
3844
let a = Bytes::from(&b"hello"[..]);

0 commit comments

Comments
 (0)