Skip to content

WIP: Make alloc optional #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
4 changes: 4 additions & 0 deletions src/buf/buf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
}
Expand All @@ -1014,6 +1017,7 @@ impl<T: Buf + ?Sized> Buf for &mut T {
deref_forward_buf!();
}

#[cfg(feature = "alloc")]
impl<T: Buf + ?Sized> Buf for Box<T> {
deref_forward_buf!();
}
Expand Down
3 changes: 3 additions & 0 deletions src/buf/buf_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -990,6 +991,7 @@ unsafe impl<T: BufMut + ?Sized> BufMut for &mut T {
deref_forward_bufmut!();
}

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

#[cfg(feature = "alloc")]
unsafe impl BufMut for Vec<u8> {
#[inline]
fn remaining_mut(&self) -> usize {
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<dyn Buf>).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"[..];
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions tests/test_buf_mut.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(rust_2018_idioms)]
#![cfg(feature = "alloc")]

use bytes::buf::UninitSlice;
use bytes::{BufMut, BytesMut};
Expand Down
1 change: 1 addition & 0 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(rust_2018_idioms)]
#![cfg(feature = "alloc")]

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

Expand Down
1 change: 1 addition & 0 deletions tests/test_bytes_odd_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/test_bytes_vec_alloc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "alloc")]
use std::alloc::{GlobalAlloc, Layout, System};
use std::{mem, ptr};

Expand Down
8 changes: 7 additions & 1 deletion tests/test_chain.rs
Original file line number Diff line number Diff line change
@@ -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"[..]);
Expand Down Expand Up @@ -33,6 +38,7 @@ fn writing_chained() {
}
}

#[cfg(feature = "alloc")]
#[test]
fn iterating_two_bufs() {
let a = Bytes::from(&b"hello"[..]);
Expand Down
1 change: 1 addition & 0 deletions tests/test_debug.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(rust_2018_idioms)]
#![cfg(feature = "alloc")]

use bytes::Bytes;

Expand Down
1 change: 1 addition & 0 deletions tests/test_iter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(rust_2018_idioms)]
#![cfg(feature = "alloc")]

use bytes::Bytes;

Expand Down