mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-10 00:00:09 +02:00
Add no_std support, by adding an std feature (#281)
To make the library work as `no_std` we add an `std` feature which is on by default. When it is off, we compile as `no_std` and make parts of the API that require `std::io` conditional on the `std` feature.
This commit is contained in:
+14
-2
@@ -1,6 +1,14 @@
|
||||
use super::{Take, Reader, Chain};
|
||||
use super::{Take, Chain};
|
||||
|
||||
use std::{cmp, io::IoSlice, ptr, mem};
|
||||
#[cfg(feature = "std")]
|
||||
use super::Reader;
|
||||
|
||||
use core::{cmp, ptr, mem};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::io::IoSlice;
|
||||
|
||||
use alloc::{boxed::Box};
|
||||
|
||||
macro_rules! buf_get_impl {
|
||||
($this:ident, $typ:tt::$conv:tt) => ({
|
||||
@@ -148,6 +156,7 @@ pub trait Buf {
|
||||
/// with `dst` being a zero length slice.
|
||||
///
|
||||
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
|
||||
#[cfg(feature = "std")]
|
||||
fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
|
||||
if dst.is_empty() {
|
||||
return 0;
|
||||
@@ -884,6 +893,7 @@ pub trait Buf {
|
||||
/// assert_eq!(11, num);
|
||||
/// assert_eq!(&dst[..11], &b"hello world"[..]);
|
||||
/// ```
|
||||
#[cfg(feature = "std")]
|
||||
fn reader(self) -> Reader<Self> where Self: Sized {
|
||||
super::reader::new(self)
|
||||
}
|
||||
@@ -915,6 +925,7 @@ impl<T: Buf + ?Sized> Buf for &mut T {
|
||||
(**self).bytes()
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
|
||||
(**self).bytes_vectored(dst)
|
||||
}
|
||||
@@ -933,6 +944,7 @@ impl<T: Buf + ?Sized> Buf for Box<T> {
|
||||
(**self).bytes()
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
|
||||
(**self).bytes_vectored(dst)
|
||||
}
|
||||
|
||||
+14
-4
@@ -1,6 +1,12 @@
|
||||
use super::{Writer};
|
||||
#[cfg(feature = "std")]
|
||||
use super::Writer;
|
||||
|
||||
use std::{mem, cmp, io::IoSliceMut, ptr, usize};
|
||||
use core::{mem, cmp, ptr, usize};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::io::IoSliceMut;
|
||||
|
||||
use alloc::{vec::Vec, boxed::Box};
|
||||
|
||||
/// A trait for values that provide sequential write access to bytes.
|
||||
///
|
||||
@@ -185,6 +191,7 @@ pub trait BufMut {
|
||||
/// with `dst` being a zero length slice.
|
||||
///
|
||||
/// [`readv`]: http://man7.org/linux/man-pages/man2/readv.2.html
|
||||
#[cfg(feature = "std")]
|
||||
unsafe fn bytes_vectored_mut<'a>(&'a mut self, dst: &mut [IoSliceMut<'a>]) -> usize {
|
||||
if dst.is_empty() {
|
||||
return 0;
|
||||
@@ -913,6 +920,7 @@ pub trait BufMut {
|
||||
///
|
||||
/// assert_eq!(*buf, b"hello world"[..]);
|
||||
/// ```
|
||||
#[cfg(feature = "std")]
|
||||
fn writer(self) -> Writer<Self> where Self: Sized {
|
||||
super::writer::new(self)
|
||||
}
|
||||
@@ -927,6 +935,7 @@ impl<T: BufMut + ?Sized> BufMut for &mut T {
|
||||
(**self).bytes_mut()
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
unsafe fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
|
||||
(**self).bytes_vectored_mut(dst)
|
||||
}
|
||||
@@ -945,6 +954,7 @@ impl<T: BufMut + ?Sized> BufMut for Box<T> {
|
||||
(**self).bytes_mut()
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
unsafe fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
|
||||
(**self).bytes_vectored_mut(dst)
|
||||
}
|
||||
@@ -968,7 +978,7 @@ impl BufMut for &mut [u8] {
|
||||
#[inline]
|
||||
unsafe fn advance_mut(&mut self, cnt: usize) {
|
||||
// Lifetime dance taken from `impl Write for &mut [u8]`.
|
||||
let (_, b) = std::mem::replace(self, &mut []).split_at_mut(cnt);
|
||||
let (_, b) = core::mem::replace(self, &mut []).split_at_mut(cnt);
|
||||
*self = b;
|
||||
}
|
||||
}
|
||||
@@ -994,7 +1004,7 @@ impl BufMut for Vec<u8> {
|
||||
|
||||
#[inline]
|
||||
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
|
||||
use std::slice;
|
||||
use core::slice;
|
||||
|
||||
if self.capacity() == self.len() {
|
||||
self.reserve(64); // Grow the vec
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use crate::{Buf, BufMut};
|
||||
use crate::buf::IntoIter;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::io::{IoSlice, IoSliceMut};
|
||||
|
||||
/// A `Chain` sequences two buffers.
|
||||
@@ -178,6 +180,7 @@ impl<T, U> Buf for Chain<T, U>
|
||||
self.b.advance(cnt);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
|
||||
let mut n = self.a.bytes_vectored(dst);
|
||||
n += self.b.bytes_vectored(&mut dst[n..]);
|
||||
@@ -227,6 +230,7 @@ impl<T, U> BufMut for Chain<T, U>
|
||||
self.b.advance_mut(cnt);
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
unsafe fn bytes_vectored_mut<'a>(&'a mut self, dst: &mut [IoSliceMut<'a>]) -> usize {
|
||||
let mut n = self.a.bytes_vectored_mut(dst);
|
||||
n += self.b.bytes_vectored_mut(&mut dst[n..]);
|
||||
|
||||
+7
-1
@@ -20,15 +20,21 @@ mod buf_impl;
|
||||
mod buf_mut;
|
||||
mod chain;
|
||||
mod iter;
|
||||
mod reader;
|
||||
mod take;
|
||||
mod vec_deque;
|
||||
|
||||
// When std::io::Reader etc. traits are not available, skip these
|
||||
#[cfg(feature = "std")]
|
||||
mod reader;
|
||||
#[cfg(feature = "std")]
|
||||
mod writer;
|
||||
|
||||
pub use self::buf_impl::Buf;
|
||||
pub use self::buf_mut::BufMut;
|
||||
pub use self::chain::Chain;
|
||||
pub use self::iter::IntoIter;
|
||||
#[cfg(feature = "std")]
|
||||
pub use self::reader::Reader;
|
||||
pub use self::take::Take;
|
||||
#[cfg(feature = "std")]
|
||||
pub use self::writer::Writer;
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
use crate::Buf;
|
||||
|
||||
use std::cmp;
|
||||
use core::cmp;
|
||||
|
||||
/// A `Buf` adapter which limits the bytes read from an underlying buffer.
|
||||
///
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::collections::VecDeque;
|
||||
use alloc::collections::VecDeque;
|
||||
|
||||
use super::Buf;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user