Skip to content
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

feat: make alloc optional #606

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -19,7 +19,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 @@ -1100,6 +1101,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 @@ -1324,6 +1326,7 @@ macro_rules! deref_forward_buf {
(**self).get_int_ne(nbytes)
}

#[cfg(feature = "alloc")]
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
(**self).copy_to_bytes(len)
}
Expand All @@ -1334,6 +1337,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 @@ -1379,6 +1380,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 Down Expand Up @@ -1454,6 +1456,7 @@ unsafe impl BufMut for &mut [core::mem::MaybeUninit<u8>] {
}
}

#[cfg(feature = "alloc")]
unsafe impl BufMut for Vec<u8> {
#[inline]
fn remaining_mut(&self) -> usize {
Expand Down
5 changes: 4 additions & 1 deletion src/buf/chain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::buf::{IntoIter, UninitSlice};
use crate::{Buf, BufMut, Bytes};
use crate::{Buf, BufMut};
#[cfg(feature = "alloc")]
use crate::Bytes;

#[cfg(feature = "std")]
use std::io::IoSlice;
Expand Down Expand Up @@ -171,6 +173,7 @@ where
n
}

#[cfg(feature = "alloc")]
fn copy_to_bytes(&mut self, len: usize) -> Bytes {
let a_rem = self.a.remaining();
if a_rem >= len {
Expand Down
1 change: 1 addition & 0 deletions src/buf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod limit;
mod reader;
mod take;
mod uninit_slice;
#[cfg(feature = "alloc")]
mod vec_deque;
#[cfg(feature = "std")]
mod writer;
Expand Down
5 changes: 4 additions & 1 deletion src/buf/take.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{Buf, Bytes};
use crate::Buf;
#[cfg(feature = "alloc")]
use crate::Bytes;

use core::cmp;

Expand Down Expand Up @@ -145,6 +147,7 @@ impl<T: Buf> Buf for Take<T> {
self.limit -= cnt;
}

#[cfg(feature = "alloc")]
fn copy_to_bytes(&mut self, len: usize) -> Bytes {
assert!(len <= self.remaining(), "`len` greater than remaining");

Expand Down
3 changes: 3 additions & 0 deletions src/fmt/debug.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::fmt::{Debug, Formatter, Result};

use super::BytesRef;
#[cfg(feature = "alloc")]
use crate::{Bytes, BytesMut};

/// Alternative implementation of `std::fmt::Debug` for byte slice.
Expand Down Expand Up @@ -36,12 +37,14 @@ impl Debug for BytesRef<'_> {
}
}

#[cfg(feature = "alloc")]
impl Debug for Bytes {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Debug::fmt(&BytesRef(self.as_ref()), f)
}
}

#[cfg(feature = "alloc")]
impl Debug for BytesMut {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Debug::fmt(&BytesRef(self.as_ref()), f)
Expand Down
6 changes: 6 additions & 0 deletions src/fmt/hex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::fmt::{Formatter, LowerHex, Result, UpperHex};

use super::BytesRef;
#[cfg(feature = "alloc")]
use crate::{Bytes, BytesMut};

impl LowerHex for BytesRef<'_> {
Expand All @@ -21,6 +22,7 @@ impl UpperHex for BytesRef<'_> {
}
}

#[cfg(feature = "alloc")]
macro_rules! hex_impl {
($tr:ident, $ty:ty) => {
impl $tr for $ty {
Expand All @@ -31,7 +33,11 @@ macro_rules! hex_impl {
};
}

#[cfg(feature = "alloc")]
hex_impl!(LowerHex, Bytes);
#[cfg(feature = "alloc")]
hex_impl!(LowerHex, BytesMut);
#[cfg(feature = "alloc")]
hex_impl!(UpperHex, Bytes);
#[cfg(feature = "alloc")]
hex_impl!(UpperHex, BytesMut);
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
//! perform a syscall, which has the potential of failing. Operations on `Buf`
//! and `BufMut` are infallible.

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "std")]
Expand All @@ -84,11 +85,15 @@ extern crate std;
pub mod buf;
pub use crate::buf::{Buf, BufMut};

#[cfg(feature = "alloc")]
mod bytes;
#[cfg(feature = "alloc")]
mod bytes_mut;
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
1 change: 1 addition & 0 deletions src/loom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(not(all(test, loom)))]
pub(crate) mod sync {
#[cfg(feature = "alloc")]
pub(crate) mod atomic {
pub(crate) use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};

Expand Down