mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-22 00:00:15 +02:00
Format with rustfmt (#389)
* Format with rustfmt * Add rustfmt check to CI
This commit is contained in:
+5
-4
@@ -3,9 +3,10 @@ pr: ["master"]
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# Check formatting
|
# Check formatting
|
||||||
# - template: ci/azure-rustfmt.yml
|
- template: ci/azure-rustfmt.yml
|
||||||
# parameters:
|
parameters:
|
||||||
# name: rustfmt
|
name: rustfmt
|
||||||
|
rust_version: stable
|
||||||
|
|
||||||
# Apply clippy lints
|
# Apply clippy lints
|
||||||
# - template: ci/azure-clippy.yml
|
# - template: ci/azure-clippy.yml
|
||||||
@@ -60,7 +61,7 @@ jobs:
|
|||||||
- template: ci/azure-deploy-docs.yml
|
- template: ci/azure-deploy-docs.yml
|
||||||
parameters:
|
parameters:
|
||||||
dependsOn:
|
dependsOn:
|
||||||
# - rustfmt
|
- rustfmt
|
||||||
# - clippy
|
# - clippy
|
||||||
- stable
|
- stable
|
||||||
- nightly
|
- nightly
|
||||||
|
|||||||
+1
-1
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
use test::Bencher;
|
|
||||||
use bytes::Buf;
|
use bytes::Buf;
|
||||||
|
use test::Bencher;
|
||||||
|
|
||||||
/// Dummy Buf implementation
|
/// Dummy Buf implementation
|
||||||
struct TestBuf {
|
struct TestBuf {
|
||||||
|
|||||||
+3
-2
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
use test::Bencher;
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
use test::Bencher;
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn deref_unique(b: &mut Bencher) {
|
fn deref_unique(b: &mut Bencher) {
|
||||||
@@ -42,7 +42,8 @@ fn deref_static(b: &mut Bencher) {
|
|||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn clone_static(b: &mut Bencher) {
|
fn clone_static(b: &mut Bencher) {
|
||||||
let bytes = Bytes::from_static("hello world 1234567890 and have a good byte 0987654321".as_bytes());
|
let bytes =
|
||||||
|
Bytes::from_static("hello world 1234567890 and have a good byte 0987654321".as_bytes());
|
||||||
|
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
for _ in 0..1024 {
|
for _ in 0..1024 {
|
||||||
|
|||||||
+28
-11
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
use test::Bencher;
|
|
||||||
use bytes::{BufMut, BytesMut};
|
use bytes::{BufMut, BytesMut};
|
||||||
|
use test::Bencher;
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn alloc_small(b: &mut Bencher) {
|
fn alloc_small(b: &mut Bencher) {
|
||||||
@@ -29,7 +29,6 @@ fn alloc_big(b: &mut Bencher) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn deref_unique(b: &mut Bencher) {
|
fn deref_unique(b: &mut Bencher) {
|
||||||
let mut buf = BytesMut::with_capacity(4096);
|
let mut buf = BytesMut::with_capacity(4096);
|
||||||
@@ -92,7 +91,9 @@ fn deref_two(b: &mut Bencher) {
|
|||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn clone_frozen(b: &mut Bencher) {
|
fn clone_frozen(b: &mut Bencher) {
|
||||||
let bytes = BytesMut::from(&b"hello world 1234567890 and have a good byte 0987654321"[..]).split().freeze();
|
let bytes = BytesMut::from(&b"hello world 1234567890 and have a good byte 0987654321"[..])
|
||||||
|
.split()
|
||||||
|
.freeze();
|
||||||
|
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
for _ in 0..1024 {
|
for _ in 0..1024 {
|
||||||
@@ -137,7 +138,9 @@ fn fmt_write(b: &mut Bencher) {
|
|||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let _ = write!(buf, "{}", s);
|
let _ = write!(buf, "{}", s);
|
||||||
test::black_box(&buf);
|
test::black_box(&buf);
|
||||||
unsafe { buf.set_len(0); }
|
unsafe {
|
||||||
|
buf.set_len(0);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +155,9 @@ fn bytes_mut_extend(b: &mut Bencher) {
|
|||||||
buf.extend(&data);
|
buf.extend(&data);
|
||||||
}
|
}
|
||||||
test::black_box(&buf);
|
test::black_box(&buf);
|
||||||
unsafe { buf.set_len(0); }
|
unsafe {
|
||||||
|
buf.set_len(0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +174,9 @@ fn put_slice_bytes_mut(b: &mut Bencher) {
|
|||||||
buf.put_slice(&data);
|
buf.put_slice(&data);
|
||||||
}
|
}
|
||||||
test::black_box(&buf);
|
test::black_box(&buf);
|
||||||
unsafe { buf.set_len(0); }
|
unsafe {
|
||||||
|
buf.set_len(0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +191,9 @@ fn put_u8_bytes_mut(b: &mut Bencher) {
|
|||||||
buf.put_u8(b'x');
|
buf.put_u8(b'x');
|
||||||
}
|
}
|
||||||
test::black_box(&buf);
|
test::black_box(&buf);
|
||||||
unsafe { buf.set_len(0); }
|
unsafe {
|
||||||
|
buf.set_len(0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,7 +208,9 @@ fn put_slice_vec(b: &mut Bencher) {
|
|||||||
buf.put_slice(&data);
|
buf.put_slice(&data);
|
||||||
}
|
}
|
||||||
test::black_box(&buf);
|
test::black_box(&buf);
|
||||||
unsafe { buf.set_len(0); }
|
unsafe {
|
||||||
|
buf.set_len(0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,7 +225,9 @@ fn put_u8_vec(b: &mut Bencher) {
|
|||||||
buf.put_u8(b'x');
|
buf.put_u8(b'x');
|
||||||
}
|
}
|
||||||
test::black_box(&buf);
|
test::black_box(&buf);
|
||||||
unsafe { buf.set_len(0); }
|
unsafe {
|
||||||
|
buf.set_len(0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,7 +242,9 @@ fn put_slice_vec_extend(b: &mut Bencher) {
|
|||||||
buf.extend_from_slice(&data);
|
buf.extend_from_slice(&data);
|
||||||
}
|
}
|
||||||
test::black_box(&buf);
|
test::black_box(&buf);
|
||||||
unsafe { buf.set_len(0); }
|
unsafe {
|
||||||
|
buf.set_len(0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,6 +259,8 @@ fn put_u8_vec_push(b: &mut Bencher) {
|
|||||||
buf.push(b'x');
|
buf.push(b'x');
|
||||||
}
|
}
|
||||||
test::black_box(&buf);
|
test::black_box(&buf);
|
||||||
unsafe { buf.set_len(0); }
|
unsafe {
|
||||||
|
buf.set_len(0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
jobs:
|
||||||
|
# Check formatting
|
||||||
|
- job: ${{ parameters.name }}
|
||||||
|
displayName: Check rustfmt
|
||||||
|
pool:
|
||||||
|
vmImage: ubuntu-16.04
|
||||||
|
steps:
|
||||||
|
- template: azure-install-rust.yml
|
||||||
|
parameters:
|
||||||
|
rust_version: ${{ parameters.rust_version }}
|
||||||
|
- script: |
|
||||||
|
cargo fmt --all -- --check
|
||||||
|
displayName: Check formatting
|
||||||
+98
-97
@@ -1,22 +1,23 @@
|
|||||||
use core::{cmp, ptr, mem};
|
use core::{cmp, mem, ptr};
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::io::IoSlice;
|
use std::io::IoSlice;
|
||||||
|
|
||||||
use alloc::{boxed::Box};
|
use alloc::boxed::Box;
|
||||||
|
|
||||||
macro_rules! buf_get_impl {
|
macro_rules! buf_get_impl {
|
||||||
($this:ident, $typ:tt::$conv:tt) => ({
|
($this:ident, $typ:tt::$conv:tt) => {{
|
||||||
const SIZE: usize = mem::size_of::<$typ>();
|
const SIZE: usize = mem::size_of::<$typ>();
|
||||||
// try to convert directly from the bytes
|
// try to convert directly from the bytes
|
||||||
// this Option<ret> trick is to avoid keeping a borrow on self
|
// this Option<ret> trick is to avoid keeping a borrow on self
|
||||||
// when advance() is called (mut borrow) and to call bytes() only once
|
// when advance() is called (mut borrow) and to call bytes() only once
|
||||||
let ret = $this.bytes().get(..SIZE).map(|src| unsafe {
|
let ret = $this
|
||||||
$typ::$conv(*(src as *const _ as *const [_; SIZE]))
|
.bytes()
|
||||||
});
|
.get(..SIZE)
|
||||||
|
.map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) });
|
||||||
|
|
||||||
if let Some(ret) = ret {
|
if let Some(ret) = ret {
|
||||||
// if the direct conversion was possible, advance and return
|
// if the direct conversion was possible, advance and return
|
||||||
$this.advance(SIZE);
|
$this.advance(SIZE);
|
||||||
return ret;
|
return ret;
|
||||||
} else {
|
} else {
|
||||||
@@ -25,8 +26,8 @@ macro_rules! buf_get_impl {
|
|||||||
$this.copy_to_slice(&mut buf); // (do the advance)
|
$this.copy_to_slice(&mut buf); // (do the advance)
|
||||||
return $typ::$conv(buf);
|
return $typ::$conv(buf);
|
||||||
}
|
}
|
||||||
});
|
}};
|
||||||
(le => $this:ident, $typ:tt, $len_to_read:expr) => ({
|
(le => $this:ident, $typ:tt, $len_to_read:expr) => {{
|
||||||
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
|
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
|
||||||
|
|
||||||
// The same trick as above does not improve the best case speed.
|
// The same trick as above does not improve the best case speed.
|
||||||
@@ -34,12 +35,12 @@ macro_rules! buf_get_impl {
|
|||||||
let mut buf = [0; (mem::size_of::<$typ>())];
|
let mut buf = [0; (mem::size_of::<$typ>())];
|
||||||
$this.copy_to_slice(&mut buf[..($len_to_read)]);
|
$this.copy_to_slice(&mut buf[..($len_to_read)]);
|
||||||
return $typ::from_le_bytes(buf);
|
return $typ::from_le_bytes(buf);
|
||||||
});
|
}};
|
||||||
(be => $this:ident, $typ:tt, $len_to_read:expr) => {{
|
(be => $this:ident, $typ:tt, $len_to_read:expr) => {{
|
||||||
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
|
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
|
||||||
|
|
||||||
let mut buf = [0; (mem::size_of::<$typ>())];
|
let mut buf = [0; (mem::size_of::<$typ>())];
|
||||||
$this.copy_to_slice(&mut buf[mem::size_of::<$typ>()-($len_to_read)..]);
|
$this.copy_to_slice(&mut buf[mem::size_of::<$typ>() - ($len_to_read)..]);
|
||||||
return $typ::from_be_bytes(buf);
|
return $typ::from_be_bytes(buf);
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
@@ -251,8 +252,7 @@ pub trait Buf {
|
|||||||
let src = self.bytes();
|
let src = self.bytes();
|
||||||
cnt = cmp::min(src.len(), dst.len() - off);
|
cnt = cmp::min(src.len(), dst.len() - off);
|
||||||
|
|
||||||
ptr::copy_nonoverlapping(
|
ptr::copy_nonoverlapping(src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
|
||||||
src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
|
|
||||||
|
|
||||||
off += cnt;
|
off += cnt;
|
||||||
}
|
}
|
||||||
@@ -810,109 +810,108 @@ pub trait Buf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! deref_forward_buf {
|
macro_rules! deref_forward_buf {
|
||||||
() => (
|
() => {
|
||||||
fn remaining(&self) -> usize {
|
fn remaining(&self) -> usize {
|
||||||
(**self).remaining()
|
(**self).remaining()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bytes(&self) -> &[u8] {
|
fn bytes(&self) -> &[u8] {
|
||||||
(**self).bytes()
|
(**self).bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
|
fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
|
||||||
(**self).bytes_vectored(dst)
|
(**self).bytes_vectored(dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn advance(&mut self, cnt: usize) {
|
fn advance(&mut self, cnt: usize) {
|
||||||
(**self).advance(cnt)
|
(**self).advance(cnt)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_remaining(&self) -> bool {
|
fn has_remaining(&self) -> bool {
|
||||||
(**self).has_remaining()
|
(**self).has_remaining()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_to_slice(&mut self, dst: &mut [u8]) {
|
fn copy_to_slice(&mut self, dst: &mut [u8]) {
|
||||||
(**self).copy_to_slice(dst)
|
(**self).copy_to_slice(dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_u8(&mut self) -> u8 {
|
fn get_u8(&mut self) -> u8 {
|
||||||
(**self).get_u8()
|
(**self).get_u8()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_i8(&mut self) -> i8 {
|
fn get_i8(&mut self) -> i8 {
|
||||||
(**self).get_i8()
|
(**self).get_i8()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_u16(&mut self) -> u16 {
|
fn get_u16(&mut self) -> u16 {
|
||||||
(**self).get_u16()
|
(**self).get_u16()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_u16_le(&mut self) -> u16 {
|
fn get_u16_le(&mut self) -> u16 {
|
||||||
(**self).get_u16_le()
|
(**self).get_u16_le()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_i16(&mut self) -> i16 {
|
fn get_i16(&mut self) -> i16 {
|
||||||
(**self).get_i16()
|
(**self).get_i16()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_i16_le(&mut self) -> i16 {
|
fn get_i16_le(&mut self) -> i16 {
|
||||||
(**self).get_i16_le()
|
(**self).get_i16_le()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_u32(&mut self) -> u32 {
|
fn get_u32(&mut self) -> u32 {
|
||||||
(**self).get_u32()
|
(**self).get_u32()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_u32_le(&mut self) -> u32 {
|
fn get_u32_le(&mut self) -> u32 {
|
||||||
(**self).get_u32_le()
|
(**self).get_u32_le()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_i32(&mut self) -> i32 {
|
fn get_i32(&mut self) -> i32 {
|
||||||
(**self).get_i32()
|
(**self).get_i32()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_i32_le(&mut self) -> i32 {
|
fn get_i32_le(&mut self) -> i32 {
|
||||||
(**self).get_i32_le()
|
(**self).get_i32_le()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_u64(&mut self) -> u64 {
|
fn get_u64(&mut self) -> u64 {
|
||||||
(**self).get_u64()
|
(**self).get_u64()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_u64_le(&mut self) -> u64 {
|
fn get_u64_le(&mut self) -> u64 {
|
||||||
(**self).get_u64_le()
|
(**self).get_u64_le()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_i64(&mut self) -> i64 {
|
fn get_i64(&mut self) -> i64 {
|
||||||
(**self).get_i64()
|
(**self).get_i64()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_i64_le(&mut self) -> i64 {
|
fn get_i64_le(&mut self) -> i64 {
|
||||||
(**self).get_i64_le()
|
(**self).get_i64_le()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_uint(&mut self, nbytes: usize) -> u64 {
|
fn get_uint(&mut self, nbytes: usize) -> u64 {
|
||||||
(**self).get_uint(nbytes)
|
(**self).get_uint(nbytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_uint_le(&mut self, nbytes: usize) -> u64 {
|
fn get_uint_le(&mut self, nbytes: usize) -> u64 {
|
||||||
(**self).get_uint_le(nbytes)
|
(**self).get_uint_le(nbytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_int(&mut self, nbytes: usize) -> i64 {
|
fn get_int(&mut self, nbytes: usize) -> i64 {
|
||||||
(**self).get_int(nbytes)
|
(**self).get_int(nbytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_int_le(&mut self, nbytes: usize) -> i64 {
|
fn get_int_le(&mut self, nbytes: usize) -> i64 {
|
||||||
(**self).get_int_le(nbytes)
|
(**self).get_int_le(nbytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&mut self) -> crate::Bytes {
|
fn to_bytes(&mut self) -> crate::Bytes {
|
||||||
(**self).to_bytes()
|
(**self).to_bytes()
|
||||||
}
|
}
|
||||||
|
};
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Buf + ?Sized> Buf for &mut T {
|
impl<T: Buf + ?Sized> Buf for &mut T {
|
||||||
@@ -950,7 +949,8 @@ impl Buf for Option<[u8; 1]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bytes(&self) -> &[u8] {
|
fn bytes(&self) -> &[u8] {
|
||||||
self.as_ref().map(AsRef::as_ref)
|
self.as_ref()
|
||||||
|
.map(AsRef::as_ref)
|
||||||
.unwrap_or(Default::default())
|
.unwrap_or(Default::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -994,7 +994,8 @@ impl<T: AsRef<[u8]>> Buf for std::io::Cursor<T> {
|
|||||||
|
|
||||||
fn advance(&mut self, cnt: usize) {
|
fn advance(&mut self, cnt: usize) {
|
||||||
let pos = (self.position() as usize)
|
let pos = (self.position() as usize)
|
||||||
.checked_add(cnt).expect("overflow");
|
.checked_add(cnt)
|
||||||
|
.expect("overflow");
|
||||||
|
|
||||||
assert!(pos <= self.get_ref().as_ref().len());
|
assert!(pos <= self.get_ref().as_ref().len());
|
||||||
self.set_position(pos as u64);
|
self.set_position(pos as u64);
|
||||||
|
|||||||
+89
-79
@@ -1,9 +1,13 @@
|
|||||||
use core::{cmp, mem::{self, MaybeUninit}, ptr, usize};
|
use core::{
|
||||||
|
cmp,
|
||||||
|
mem::{self, MaybeUninit},
|
||||||
|
ptr, usize,
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use alloc::{vec::Vec, boxed::Box};
|
use alloc::{boxed::Box, vec::Vec};
|
||||||
|
|
||||||
/// A trait for values that provide sequential write access to bytes.
|
/// A trait for values that provide sequential write access to bytes.
|
||||||
///
|
///
|
||||||
@@ -226,7 +230,10 @@ pub trait BufMut {
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if `self` does not have enough capacity to contain `src`.
|
/// Panics if `self` does not have enough capacity to contain `src`.
|
||||||
fn put<T: super::Buf>(&mut self, mut src: T) where Self: Sized {
|
fn put<T: super::Buf>(&mut self, mut src: T)
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
assert!(self.remaining_mut() >= src.remaining());
|
assert!(self.remaining_mut() >= src.remaining());
|
||||||
|
|
||||||
while src.has_remaining() {
|
while src.has_remaining() {
|
||||||
@@ -237,14 +244,13 @@ pub trait BufMut {
|
|||||||
let d = self.bytes_mut();
|
let d = self.bytes_mut();
|
||||||
l = cmp::min(s.len(), d.len());
|
l = cmp::min(s.len(), d.len());
|
||||||
|
|
||||||
ptr::copy_nonoverlapping(
|
ptr::copy_nonoverlapping(s.as_ptr(), d.as_mut_ptr() as *mut u8, l);
|
||||||
s.as_ptr(),
|
|
||||||
d.as_mut_ptr() as *mut u8,
|
|
||||||
l);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
src.advance(l);
|
src.advance(l);
|
||||||
unsafe { self.advance_mut(l); }
|
unsafe {
|
||||||
|
self.advance_mut(l);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,7 +276,12 @@ pub trait BufMut {
|
|||||||
fn put_slice(&mut self, src: &[u8]) {
|
fn put_slice(&mut self, src: &[u8]) {
|
||||||
let mut off = 0;
|
let mut off = 0;
|
||||||
|
|
||||||
assert!(self.remaining_mut() >= src.len(), "buffer overflow; remaining = {}; src = {}", self.remaining_mut(), src.len());
|
assert!(
|
||||||
|
self.remaining_mut() >= src.len(),
|
||||||
|
"buffer overflow; remaining = {}; src = {}",
|
||||||
|
self.remaining_mut(),
|
||||||
|
src.len()
|
||||||
|
);
|
||||||
|
|
||||||
while off < src.len() {
|
while off < src.len() {
|
||||||
let cnt;
|
let cnt;
|
||||||
@@ -279,16 +290,14 @@ pub trait BufMut {
|
|||||||
let dst = self.bytes_mut();
|
let dst = self.bytes_mut();
|
||||||
cnt = cmp::min(dst.len(), src.len() - off);
|
cnt = cmp::min(dst.len(), src.len() - off);
|
||||||
|
|
||||||
ptr::copy_nonoverlapping(
|
ptr::copy_nonoverlapping(src[off..].as_ptr(), dst.as_mut_ptr() as *mut u8, cnt);
|
||||||
src[off..].as_ptr(),
|
|
||||||
dst.as_mut_ptr() as *mut u8,
|
|
||||||
cnt);
|
|
||||||
|
|
||||||
off += cnt;
|
off += cnt;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { self.advance_mut(cnt); }
|
unsafe {
|
||||||
|
self.advance_mut(cnt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -872,84 +881,84 @@ pub trait BufMut {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! deref_forward_bufmut {
|
macro_rules! deref_forward_bufmut {
|
||||||
() => (
|
() => {
|
||||||
fn remaining_mut(&self) -> usize {
|
fn remaining_mut(&self) -> usize {
|
||||||
(**self).remaining_mut()
|
(**self).remaining_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
|
fn bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
|
||||||
(**self).bytes_mut()
|
(**self).bytes_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
|
fn bytes_vectored_mut<'b>(&'b mut self, dst: &mut [IoSliceMut<'b>]) -> usize {
|
||||||
(**self).bytes_vectored_mut(dst)
|
(**self).bytes_vectored_mut(dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn advance_mut(&mut self, cnt: usize) {
|
unsafe fn advance_mut(&mut self, cnt: usize) {
|
||||||
(**self).advance_mut(cnt)
|
(**self).advance_mut(cnt)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_slice(&mut self, src: &[u8]) {
|
fn put_slice(&mut self, src: &[u8]) {
|
||||||
(**self).put_slice(src)
|
(**self).put_slice(src)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_u8(&mut self, n: u8) {
|
fn put_u8(&mut self, n: u8) {
|
||||||
(**self).put_u8(n)
|
(**self).put_u8(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_i8(&mut self, n: i8) {
|
fn put_i8(&mut self, n: i8) {
|
||||||
(**self).put_i8(n)
|
(**self).put_i8(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_u16(&mut self, n: u16) {
|
fn put_u16(&mut self, n: u16) {
|
||||||
(**self).put_u16(n)
|
(**self).put_u16(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_u16_le(&mut self, n: u16) {
|
fn put_u16_le(&mut self, n: u16) {
|
||||||
(**self).put_u16_le(n)
|
(**self).put_u16_le(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_i16(&mut self, n: i16) {
|
fn put_i16(&mut self, n: i16) {
|
||||||
(**self).put_i16(n)
|
(**self).put_i16(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_i16_le(&mut self, n: i16) {
|
fn put_i16_le(&mut self, n: i16) {
|
||||||
(**self).put_i16_le(n)
|
(**self).put_i16_le(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_u32(&mut self, n: u32) {
|
fn put_u32(&mut self, n: u32) {
|
||||||
(**self).put_u32(n)
|
(**self).put_u32(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_u32_le(&mut self, n: u32) {
|
fn put_u32_le(&mut self, n: u32) {
|
||||||
(**self).put_u32_le(n)
|
(**self).put_u32_le(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_i32(&mut self, n: i32) {
|
fn put_i32(&mut self, n: i32) {
|
||||||
(**self).put_i32(n)
|
(**self).put_i32(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_i32_le(&mut self, n: i32) {
|
fn put_i32_le(&mut self, n: i32) {
|
||||||
(**self).put_i32_le(n)
|
(**self).put_i32_le(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_u64(&mut self, n: u64) {
|
fn put_u64(&mut self, n: u64) {
|
||||||
(**self).put_u64(n)
|
(**self).put_u64(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_u64_le(&mut self, n: u64) {
|
fn put_u64_le(&mut self, n: u64) {
|
||||||
(**self).put_u64_le(n)
|
(**self).put_u64_le(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_i64(&mut self, n: i64) {
|
fn put_i64(&mut self, n: i64) {
|
||||||
(**self).put_i64(n)
|
(**self).put_i64(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn put_i64_le(&mut self, n: i64) {
|
fn put_i64_le(&mut self, n: i64) {
|
||||||
(**self).put_i64_le(n)
|
(**self).put_i64_le(n)
|
||||||
}
|
}
|
||||||
)
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: BufMut + ?Sized> BufMut for &mut T {
|
impl<T: BufMut + ?Sized> BufMut for &mut T {
|
||||||
@@ -1013,15 +1022,16 @@ impl BufMut for Vec<u8> {
|
|||||||
let len = self.len();
|
let len = self.len();
|
||||||
|
|
||||||
let ptr = self.as_mut_ptr() as *mut MaybeUninit<u8>;
|
let ptr = self.as_mut_ptr() as *mut MaybeUninit<u8>;
|
||||||
unsafe {
|
unsafe { &mut slice::from_raw_parts_mut(ptr, cap)[len..] }
|
||||||
&mut slice::from_raw_parts_mut(ptr, cap)[len..]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specialize these methods so they can skip checking `remaining_mut`
|
// Specialize these methods so they can skip checking `remaining_mut`
|
||||||
// and `advance_mut`.
|
// and `advance_mut`.
|
||||||
|
|
||||||
fn put<T: super::Buf>(&mut self, mut src: T) where Self: Sized {
|
fn put<T: super::Buf>(&mut self, mut src: T)
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
// In case the src isn't contiguous, reserve upfront
|
// In case the src isn't contiguous, reserve upfront
|
||||||
self.reserve(src.remaining());
|
self.reserve(src.remaining());
|
||||||
|
|
||||||
|
|||||||
+10
-11
@@ -1,12 +1,12 @@
|
|||||||
use crate::{Buf, BufMut};
|
|
||||||
use crate::buf::IntoIter;
|
use crate::buf::IntoIter;
|
||||||
|
use crate::{Buf, BufMut};
|
||||||
|
|
||||||
use core::mem::MaybeUninit;
|
use core::mem::MaybeUninit;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
use std::io::{IoSlice};
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use crate::buf::IoSliceMut;
|
use crate::buf::IoSliceMut;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use std::io::IoSlice;
|
||||||
|
|
||||||
/// A `Chain` sequences two buffers.
|
/// A `Chain` sequences two buffers.
|
||||||
///
|
///
|
||||||
@@ -41,10 +41,7 @@ pub struct Chain<T, U> {
|
|||||||
impl<T, U> Chain<T, U> {
|
impl<T, U> Chain<T, U> {
|
||||||
/// Creates a new `Chain` sequencing the provided values.
|
/// Creates a new `Chain` sequencing the provided values.
|
||||||
pub fn new(a: T, b: U) -> Chain<T, U> {
|
pub fn new(a: T, b: U) -> Chain<T, U> {
|
||||||
Chain {
|
Chain { a, b }
|
||||||
a,
|
|
||||||
b,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a reference to the first underlying `Buf`.
|
/// Gets a reference to the first underlying `Buf`.
|
||||||
@@ -137,8 +134,9 @@ impl<T, U> Chain<T, U> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U> Buf for Chain<T, U>
|
impl<T, U> Buf for Chain<T, U>
|
||||||
where T: Buf,
|
where
|
||||||
U: Buf,
|
T: Buf,
|
||||||
|
U: Buf,
|
||||||
{
|
{
|
||||||
fn remaining(&self) -> usize {
|
fn remaining(&self) -> usize {
|
||||||
self.a.remaining() + self.b.remaining()
|
self.a.remaining() + self.b.remaining()
|
||||||
@@ -179,8 +177,9 @@ impl<T, U> Buf for Chain<T, U>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T, U> BufMut for Chain<T, U>
|
impl<T, U> BufMut for Chain<T, U>
|
||||||
where T: BufMut,
|
where
|
||||||
U: BufMut,
|
T: BufMut,
|
||||||
|
U: BufMut,
|
||||||
{
|
{
|
||||||
fn remaining_mut(&self) -> usize {
|
fn remaining_mut(&self) -> usize {
|
||||||
self.a.remaining_mut() + self.b.remaining_mut()
|
self.a.remaining_mut() + self.b.remaining_mut()
|
||||||
|
|||||||
@@ -11,10 +11,7 @@ pub struct Limit<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn new<T>(inner: T, limit: usize) -> Limit<T> {
|
pub(super) fn new<T>(inner: T, limit: usize) -> Limit<T> {
|
||||||
Limit {
|
Limit { inner, limit }
|
||||||
inner,
|
|
||||||
limit,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Limit<T> {
|
impl<T> Limit<T> {
|
||||||
|
|||||||
+17
-7
@@ -10,9 +10,9 @@ mod take;
|
|||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
mod writer;
|
mod writer;
|
||||||
|
|
||||||
|
pub use self::chain::Chain;
|
||||||
pub use self::limit::Limit;
|
pub use self::limit::Limit;
|
||||||
pub use self::take::Take;
|
pub use self::take::Take;
|
||||||
pub use self::chain::Chain;
|
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use self::{reader::Reader, writer::Writer};
|
pub use self::{reader::Reader, writer::Writer};
|
||||||
@@ -41,7 +41,8 @@ pub trait BufExt: Buf {
|
|||||||
/// assert_eq!(dst, b" world");
|
/// assert_eq!(dst, b" world");
|
||||||
/// ```
|
/// ```
|
||||||
fn take(self, limit: usize) -> Take<Self>
|
fn take(self, limit: usize) -> Take<Self>
|
||||||
where Self: Sized
|
where
|
||||||
|
Self: Sized,
|
||||||
{
|
{
|
||||||
take::new(self, limit)
|
take::new(self, limit)
|
||||||
}
|
}
|
||||||
@@ -62,7 +63,8 @@ pub trait BufExt: Buf {
|
|||||||
/// assert_eq!(full.bytes(), b"hello world");
|
/// assert_eq!(full.bytes(), b"hello world");
|
||||||
/// ```
|
/// ```
|
||||||
fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
|
fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
|
||||||
where Self: Sized
|
where
|
||||||
|
Self: Sized,
|
||||||
{
|
{
|
||||||
Chain::new(self, next)
|
Chain::new(self, next)
|
||||||
}
|
}
|
||||||
@@ -91,7 +93,10 @@ pub trait BufExt: Buf {
|
|||||||
/// assert_eq!(&dst[..11], &b"hello world"[..]);
|
/// assert_eq!(&dst[..11], &b"hello world"[..]);
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
fn reader(self) -> Reader<Self> where Self: Sized {
|
fn reader(self) -> Reader<Self>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
reader::new(self)
|
reader::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,7 +119,8 @@ pub trait BufMutExt: BufMut {
|
|||||||
/// assert_eq!(dst.remaining_mut(), 10);
|
/// assert_eq!(dst.remaining_mut(), 10);
|
||||||
/// ```
|
/// ```
|
||||||
fn limit(self, limit: usize) -> Limit<Self>
|
fn limit(self, limit: usize) -> Limit<Self>
|
||||||
where Self: Sized
|
where
|
||||||
|
Self: Sized,
|
||||||
{
|
{
|
||||||
limit::new(self, limit)
|
limit::new(self, limit)
|
||||||
}
|
}
|
||||||
@@ -142,7 +148,10 @@ pub trait BufMutExt: BufMut {
|
|||||||
/// assert_eq!(*buf, b"hello world"[..]);
|
/// assert_eq!(*buf, b"hello world"[..]);
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
fn writer(self) -> Writer<Self> where Self: Sized {
|
fn writer(self) -> Writer<Self>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
writer::new(self)
|
writer::new(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +176,8 @@ pub trait BufMutExt: BufMut {
|
|||||||
/// assert_eq!(&b[..], b" world");
|
/// assert_eq!(&b[..], b" world");
|
||||||
/// ```
|
/// ```
|
||||||
fn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U>
|
fn chain_mut<U: BufMut>(self, next: U) -> Chain<Self, U>
|
||||||
where Self: Sized
|
where
|
||||||
|
Self: Sized,
|
||||||
{
|
{
|
||||||
Chain::new(self, next)
|
Chain::new(self, next)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{Buf};
|
use crate::Buf;
|
||||||
|
|
||||||
use std::{cmp, io};
|
use std::{cmp, io};
|
||||||
|
|
||||||
|
|||||||
+1
-4
@@ -13,10 +13,7 @@ pub struct Take<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new<T>(inner: T, limit: usize) -> Take<T> {
|
pub fn new<T>(inner: T, limit: usize) -> Take<T> {
|
||||||
Take {
|
Take { inner, limit }
|
||||||
inner,
|
|
||||||
limit,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Take<T> {
|
impl<T> Take<T> {
|
||||||
|
|||||||
+1
-2
@@ -109,7 +109,6 @@ impl<T> IntoIter<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<T: Buf> Iterator for IntoIter<T> {
|
impl<T: Buf> Iterator for IntoIter<T> {
|
||||||
type Item = u8;
|
type Item = u8;
|
||||||
|
|
||||||
@@ -130,4 +129,4 @@ impl<T: Buf> Iterator for IntoIter<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Buf> ExactSizeIterator for IntoIter<T> { }
|
impl<T: Buf> ExactSizeIterator for IntoIter<T> {}
|
||||||
|
|||||||
+1
-2
@@ -24,8 +24,7 @@ mod vec_deque;
|
|||||||
|
|
||||||
pub use self::buf_impl::Buf;
|
pub use self::buf_impl::Buf;
|
||||||
pub use self::buf_mut::BufMut;
|
pub use self::buf_mut::BufMut;
|
||||||
pub use self::ext::{BufExt, BufMutExt};
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use self::buf_mut::IoSliceMut;
|
pub use self::buf_mut::IoSliceMut;
|
||||||
|
pub use self::ext::{BufExt, BufMutExt};
|
||||||
pub use self::iter::IntoIter;
|
pub use self::iter::IntoIter;
|
||||||
|
|
||||||
|
|||||||
+32
-25
@@ -1,15 +1,14 @@
|
|||||||
use core::{cmp, fmt, hash, mem, ptr, slice, usize};
|
use core::iter::FromIterator;
|
||||||
use core::iter::{FromIterator};
|
|
||||||
use core::ops::{Deref, RangeBounds};
|
use core::ops::{Deref, RangeBounds};
|
||||||
|
use core::{cmp, fmt, hash, mem, ptr, slice, usize};
|
||||||
|
|
||||||
use alloc::{vec::Vec, string::String, boxed::Box, borrow::Borrow};
|
use alloc::{borrow::Borrow, boxed::Box, string::String, vec::Vec};
|
||||||
|
|
||||||
use crate::Buf;
|
|
||||||
use crate::buf::IntoIter;
|
use crate::buf::IntoIter;
|
||||||
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
use crate::loom::sync::atomic::AtomicMut;
|
use crate::loom::sync::atomic::AtomicMut;
|
||||||
|
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
||||||
|
use crate::Buf;
|
||||||
|
|
||||||
/// A reference counted contiguous slice of memory.
|
/// A reference counted contiguous slice of memory.
|
||||||
///
|
///
|
||||||
@@ -176,7 +175,6 @@ impl Bytes {
|
|||||||
self.len == 0
|
self.len == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///Creates `Bytes` instance from slice, by copying it.
|
///Creates `Bytes` instance from slice, by copying it.
|
||||||
pub fn copy_from_slice(data: &[u8]) -> Self {
|
pub fn copy_from_slice(data: &[u8]) -> Self {
|
||||||
data.to_vec().into()
|
data.to_vec().into()
|
||||||
@@ -238,7 +236,6 @@ impl Bytes {
|
|||||||
return Bytes::new();
|
return Bytes::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let mut ret = self.clone();
|
let mut ret = self.clone();
|
||||||
|
|
||||||
ret.len = end - begin;
|
ret.len = end - begin;
|
||||||
@@ -394,7 +391,6 @@ impl Bytes {
|
|||||||
return Bytes::new();
|
return Bytes::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let mut ret = self.clone();
|
let mut ret = self.clone();
|
||||||
|
|
||||||
unsafe { self.inc_start(at) };
|
unsafe { self.inc_start(at) };
|
||||||
@@ -429,8 +425,9 @@ impl Bytes {
|
|||||||
// The Vec "promotable" vtables do not store the capacity,
|
// The Vec "promotable" vtables do not store the capacity,
|
||||||
// so we cannot truncate while using this repr. We *have* to
|
// so we cannot truncate while using this repr. We *have* to
|
||||||
// promote using `split_off` so the capacity can be stored.
|
// promote using `split_off` so the capacity can be stored.
|
||||||
if self.vtable as *const Vtable == &PROMOTABLE_EVEN_VTABLE ||
|
if self.vtable as *const Vtable == &PROMOTABLE_EVEN_VTABLE
|
||||||
self.vtable as *const Vtable == &PROMOTABLE_ODD_VTABLE {
|
|| self.vtable as *const Vtable == &PROMOTABLE_ODD_VTABLE
|
||||||
|
{
|
||||||
drop(self.split_off(len));
|
drop(self.split_off(len));
|
||||||
} else {
|
} else {
|
||||||
self.len = len;
|
self.len = len;
|
||||||
@@ -455,7 +452,12 @@ impl Bytes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) unsafe fn with_vtable(ptr: *const u8, len: usize, data: AtomicPtr<()>, vtable: &'static Vtable) -> Bytes {
|
pub(crate) unsafe fn with_vtable(
|
||||||
|
ptr: *const u8,
|
||||||
|
len: usize,
|
||||||
|
data: AtomicPtr<()>,
|
||||||
|
vtable: &'static Vtable,
|
||||||
|
) -> Bytes {
|
||||||
Bytes {
|
Bytes {
|
||||||
ptr,
|
ptr,
|
||||||
len,
|
len,
|
||||||
@@ -468,9 +470,7 @@ impl Bytes {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_slice(&self) -> &[u8] {
|
fn as_slice(&self) -> &[u8] {
|
||||||
unsafe {
|
unsafe { slice::from_raw_parts(self.ptr, self.len) }
|
||||||
slice::from_raw_parts(self.ptr, self.len)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -489,18 +489,14 @@ unsafe impl Sync for Bytes {}
|
|||||||
impl Drop for Bytes {
|
impl Drop for Bytes {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe { (self.vtable.drop)(&mut self.data, self.ptr, self.len) }
|
||||||
(self.vtable.drop)(&mut self.data, self.ptr, self.len)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Bytes {
|
impl Clone for Bytes {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn clone(&self) -> Bytes {
|
fn clone(&self) -> Bytes {
|
||||||
unsafe {
|
unsafe { (self.vtable.clone)(&self.data, self.ptr, self.len) }
|
||||||
(self.vtable.clone)(&self.data, self.ptr, self.len)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -551,7 +547,10 @@ impl AsRef<[u8]> for Bytes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl hash::Hash for Bytes {
|
impl hash::Hash for Bytes {
|
||||||
fn hash<H>(&self, state: &mut H) where H: hash::Hasher {
|
fn hash<H>(&self, state: &mut H)
|
||||||
|
where
|
||||||
|
H: hash::Hasher,
|
||||||
|
{
|
||||||
self.as_slice().hash(state);
|
self.as_slice().hash(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -729,7 +728,8 @@ impl PartialOrd<Bytes> for &str {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes
|
impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes
|
||||||
where Bytes: PartialEq<T>
|
where
|
||||||
|
Bytes: PartialEq<T>,
|
||||||
{
|
{
|
||||||
fn eq(&self, other: &&'a T) -> bool {
|
fn eq(&self, other: &&'a T) -> bool {
|
||||||
*self == **other
|
*self == **other
|
||||||
@@ -737,7 +737,8 @@ impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes
|
impl<'a, T: ?Sized> PartialOrd<&'a T> for Bytes
|
||||||
where Bytes: PartialOrd<T>
|
where
|
||||||
|
Bytes: PartialOrd<T>,
|
||||||
{
|
{
|
||||||
fn partial_cmp(&self, other: &&'a T) -> Option<cmp::Ordering> {
|
fn partial_cmp(&self, other: &&'a T) -> Option<cmp::Ordering> {
|
||||||
self.partial_cmp(&**other)
|
self.partial_cmp(&**other)
|
||||||
@@ -953,7 +954,13 @@ unsafe fn shallow_clone_arc(shared: *mut Shared, ptr: *const u8, len: usize) ->
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cold]
|
#[cold]
|
||||||
unsafe fn shallow_clone_vec(atom: &AtomicPtr<()>, ptr: *const (), buf: *mut u8, offset: *const u8, len: usize) -> Bytes {
|
unsafe fn shallow_clone_vec(
|
||||||
|
atom: &AtomicPtr<()>,
|
||||||
|
ptr: *const (),
|
||||||
|
buf: *mut u8,
|
||||||
|
offset: *const u8,
|
||||||
|
len: usize,
|
||||||
|
) -> Bytes {
|
||||||
// If the buffer is still tracked in a `Vec<u8>`. It is time to
|
// If the buffer is still tracked in a `Vec<u8>`. It is time to
|
||||||
// promote the vec to an `Arc`. This could potentially be called
|
// promote the vec to an `Arc`. This could potentially be called
|
||||||
// concurrently, so some care must be taken.
|
// concurrently, so some care must be taken.
|
||||||
|
|||||||
+65
-46
@@ -1,18 +1,22 @@
|
|||||||
use core::{cmp, fmt, hash, isize, slice, usize};
|
use core::iter::{FromIterator, Iterator};
|
||||||
use core::mem::{self, ManuallyDrop};
|
use core::mem::{self, ManuallyDrop};
|
||||||
use core::ops::{Deref, DerefMut};
|
use core::ops::{Deref, DerefMut};
|
||||||
use core::ptr::{self, NonNull};
|
use core::ptr::{self, NonNull};
|
||||||
use core::iter::{FromIterator, Iterator};
|
use core::{cmp, fmt, hash, isize, slice, usize};
|
||||||
|
|
||||||
use alloc::{vec::Vec, string::String, boxed::Box, borrow::{Borrow, BorrowMut}};
|
use alloc::{
|
||||||
|
borrow::{Borrow, BorrowMut},
|
||||||
|
boxed::Box,
|
||||||
|
string::String,
|
||||||
|
vec::Vec,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{Bytes, Buf, BufMut};
|
|
||||||
use crate::bytes::Vtable;
|
|
||||||
use crate::buf::IntoIter;
|
use crate::buf::IntoIter;
|
||||||
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
use crate::bytes::Vtable;
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
use crate::loom::sync::atomic::AtomicMut;
|
use crate::loom::sync::atomic::AtomicMut;
|
||||||
|
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
||||||
|
use crate::{Buf, BufMut, Bytes};
|
||||||
|
|
||||||
/// A unique reference to a contiguous slice of memory.
|
/// A unique reference to a contiguous slice of memory.
|
||||||
///
|
///
|
||||||
@@ -247,9 +251,7 @@ impl BytesMut {
|
|||||||
let len = self.len;
|
let len = self.len;
|
||||||
let data = AtomicPtr::new(self.data as _);
|
let data = AtomicPtr::new(self.data as _);
|
||||||
mem::forget(self);
|
mem::forget(self);
|
||||||
unsafe {
|
unsafe { Bytes::with_vtable(ptr, len, data, &SHARED_VTABLE) }
|
||||||
Bytes::with_vtable(ptr, len, data, &SHARED_VTABLE)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,7 +393,9 @@ impl BytesMut {
|
|||||||
/// [`split_off`]: #method.split_off
|
/// [`split_off`]: #method.split_off
|
||||||
pub fn truncate(&mut self, len: usize) {
|
pub fn truncate(&mut self, len: usize) {
|
||||||
if len <= self.len() {
|
if len <= self.len() {
|
||||||
unsafe { self.set_len(len); }
|
unsafe {
|
||||||
|
self.set_len(len);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -572,7 +576,8 @@ impl BytesMut {
|
|||||||
self.cap += off;
|
self.cap += off;
|
||||||
} else {
|
} else {
|
||||||
// No space - allocate more
|
// No space - allocate more
|
||||||
let mut v = ManuallyDrop::new(rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off));
|
let mut v =
|
||||||
|
ManuallyDrop::new(rebuild_vec(self.ptr.as_ptr(), self.len, self.cap, off));
|
||||||
v.reserve(additional);
|
v.reserve(additional);
|
||||||
|
|
||||||
// Update the info
|
// Update the info
|
||||||
@@ -588,7 +593,6 @@ impl BytesMut {
|
|||||||
debug_assert_eq!(kind, KIND_ARC);
|
debug_assert_eq!(kind, KIND_ARC);
|
||||||
let shared: *mut Shared = self.data as _;
|
let shared: *mut Shared = self.data as _;
|
||||||
|
|
||||||
|
|
||||||
// Reserving involves abandoning the currently shared buffer and
|
// Reserving involves abandoning the currently shared buffer and
|
||||||
// allocating a new vector with the requested capacity.
|
// allocating a new vector with the requested capacity.
|
||||||
//
|
//
|
||||||
@@ -632,9 +636,7 @@ impl BytesMut {
|
|||||||
// check.
|
// check.
|
||||||
let double = v.capacity().checked_shl(1).unwrap_or(new_cap);
|
let double = v.capacity().checked_shl(1).unwrap_or(new_cap);
|
||||||
|
|
||||||
new_cap = cmp::max(
|
new_cap = cmp::max(cmp::max(double, new_cap), original_capacity);
|
||||||
cmp::max(double, new_cap),
|
|
||||||
original_capacity);
|
|
||||||
} else {
|
} else {
|
||||||
new_cap = cmp::max(new_cap, original_capacity);
|
new_cap = cmp::max(new_cap, original_capacity);
|
||||||
}
|
}
|
||||||
@@ -683,14 +685,12 @@ impl BytesMut {
|
|||||||
// Reserved above
|
// Reserved above
|
||||||
debug_assert!(dst.len() >= cnt);
|
debug_assert!(dst.len() >= cnt);
|
||||||
|
|
||||||
ptr::copy_nonoverlapping(
|
ptr::copy_nonoverlapping(extend.as_ptr(), dst.as_mut_ptr() as *mut u8, cnt);
|
||||||
extend.as_ptr(),
|
|
||||||
dst.as_mut_ptr() as *mut u8,
|
|
||||||
cnt);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { self.advance_mut(cnt); }
|
unsafe {
|
||||||
|
self.advance_mut(cnt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Absorbs a `BytesMut` that was previously split off.
|
/// Absorbs a `BytesMut` that was previously split off.
|
||||||
@@ -755,16 +755,12 @@ impl BytesMut {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_slice(&self) -> &[u8] {
|
fn as_slice(&self) -> &[u8] {
|
||||||
unsafe {
|
unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
|
||||||
slice::from_raw_parts(self.ptr.as_ptr(), self.len)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_slice_mut(&mut self) -> &mut [u8] {
|
fn as_slice_mut(&mut self) -> &mut [u8] {
|
||||||
unsafe {
|
unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) }
|
||||||
slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn set_start(&mut self, start: usize) {
|
unsafe fn set_start(&mut self, start: usize) {
|
||||||
@@ -793,7 +789,7 @@ impl BytesMut {
|
|||||||
// on 64 bit systems and will only happen on 32 bit systems
|
// on 64 bit systems and will only happen on 32 bit systems
|
||||||
// when shifting past 134,217,727 bytes. As such, we don't
|
// when shifting past 134,217,727 bytes. As such, we don't
|
||||||
// worry too much about performance here.
|
// worry too much about performance here.
|
||||||
self.promote_to_shared(/*ref_count = */1);
|
self.promote_to_shared(/*ref_count = */ 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -825,10 +821,10 @@ impl BytesMut {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let ptr = unsafe { self.ptr.as_ptr().offset(self.len as isize) };
|
let ptr = unsafe { self.ptr.as_ptr().offset(self.len as isize) };
|
||||||
if ptr == other.ptr.as_ptr() &&
|
if ptr == other.ptr.as_ptr()
|
||||||
self.kind() == KIND_ARC &&
|
&& self.kind() == KIND_ARC
|
||||||
other.kind() == KIND_ARC &&
|
&& other.kind() == KIND_ARC
|
||||||
self.data == other.data
|
&& self.data == other.data
|
||||||
{
|
{
|
||||||
// Contiguous blocks, just combine directly
|
// Contiguous blocks, just combine directly
|
||||||
self.len += other.len;
|
self.len += other.len;
|
||||||
@@ -889,7 +885,7 @@ impl BytesMut {
|
|||||||
increment_shared(self.data);
|
increment_shared(self.data);
|
||||||
ptr::read(self)
|
ptr::read(self)
|
||||||
} else {
|
} else {
|
||||||
self.promote_to_shared(/*ref_count = */2);
|
self.promote_to_shared(/*ref_count = */ 2);
|
||||||
ptr::read(self)
|
ptr::read(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -957,7 +953,9 @@ impl Buf for BytesMut {
|
|||||||
cnt,
|
cnt,
|
||||||
self.remaining(),
|
self.remaining(),
|
||||||
);
|
);
|
||||||
unsafe { self.set_start(cnt); }
|
unsafe {
|
||||||
|
self.set_start(cnt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_bytes(&mut self) -> crate::Bytes {
|
fn to_bytes(&mut self) -> crate::Bytes {
|
||||||
@@ -974,7 +972,12 @@ impl BufMut for BytesMut {
|
|||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn advance_mut(&mut self, cnt: usize) {
|
unsafe fn advance_mut(&mut self, cnt: usize) {
|
||||||
let new_len = self.len() + cnt;
|
let new_len = self.len() + cnt;
|
||||||
assert!(new_len <= self.cap, "new_len = {}; capacity = {}", new_len, self.cap);
|
assert!(
|
||||||
|
new_len <= self.cap,
|
||||||
|
"new_len = {}; capacity = {}",
|
||||||
|
new_len,
|
||||||
|
self.cap
|
||||||
|
);
|
||||||
self.len = new_len;
|
self.len = new_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -989,7 +992,10 @@ impl BufMut for BytesMut {
|
|||||||
// Specialize these methods so they can skip checking `remaining_mut`
|
// Specialize these methods so they can skip checking `remaining_mut`
|
||||||
// and `advance_mut`.
|
// and `advance_mut`.
|
||||||
|
|
||||||
fn put<T: crate::Buf>(&mut self, mut src: T) where Self: Sized {
|
fn put<T: crate::Buf>(&mut self, mut src: T)
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
while src.has_remaining() {
|
while src.has_remaining() {
|
||||||
let s = src.bytes();
|
let s = src.bytes();
|
||||||
let l = s.len();
|
let l = s.len();
|
||||||
@@ -1068,8 +1074,7 @@ impl Ord for BytesMut {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Eq for BytesMut {
|
impl Eq for BytesMut {}
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for BytesMut {
|
impl Default for BytesMut {
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -1079,7 +1084,10 @@ impl Default for BytesMut {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl hash::Hash for BytesMut {
|
impl hash::Hash for BytesMut {
|
||||||
fn hash<H>(&self, state: &mut H) where H: hash::Hasher {
|
fn hash<H>(&self, state: &mut H)
|
||||||
|
where
|
||||||
|
H: hash::Hasher,
|
||||||
|
{
|
||||||
let s: &[u8] = self.as_ref();
|
let s: &[u8] = self.as_ref();
|
||||||
s.hash(state);
|
s.hash(state);
|
||||||
}
|
}
|
||||||
@@ -1139,7 +1147,10 @@ impl<'a> IntoIterator for &'a BytesMut {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Extend<u8> for BytesMut {
|
impl Extend<u8> for BytesMut {
|
||||||
fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item = u8> {
|
fn extend<T>(&mut self, iter: T)
|
||||||
|
where
|
||||||
|
T: IntoIterator<Item = u8>,
|
||||||
|
{
|
||||||
let iter = iter.into_iter();
|
let iter = iter.into_iter();
|
||||||
|
|
||||||
let (lower, _) = iter.size_hint();
|
let (lower, _) = iter.size_hint();
|
||||||
@@ -1156,7 +1167,10 @@ impl Extend<u8> for BytesMut {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Extend<&'a u8> for BytesMut {
|
impl<'a> Extend<&'a u8> for BytesMut {
|
||||||
fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item = &'a u8> {
|
fn extend<T>(&mut self, iter: T)
|
||||||
|
where
|
||||||
|
T: IntoIterator<Item = &'a u8>,
|
||||||
|
{
|
||||||
self.extend(iter.into_iter().map(|b| *b))
|
self.extend(iter.into_iter().map(|b| *b))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1234,7 +1248,10 @@ impl Shared {
|
|||||||
|
|
||||||
fn original_capacity_to_repr(cap: usize) -> usize {
|
fn original_capacity_to_repr(cap: usize) -> usize {
|
||||||
let width = PTR_WIDTH - ((cap >> MIN_ORIGINAL_CAPACITY_WIDTH).leading_zeros() as usize);
|
let width = PTR_WIDTH - ((cap >> MIN_ORIGINAL_CAPACITY_WIDTH).leading_zeros() as usize);
|
||||||
cmp::min(width, MAX_ORIGINAL_CAPACITY_WIDTH - MIN_ORIGINAL_CAPACITY_WIDTH)
|
cmp::min(
|
||||||
|
width,
|
||||||
|
MAX_ORIGINAL_CAPACITY_WIDTH - MIN_ORIGINAL_CAPACITY_WIDTH,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn original_capacity_from_repr(repr: usize) -> usize {
|
fn original_capacity_from_repr(repr: usize) -> usize {
|
||||||
@@ -1402,7 +1419,8 @@ impl PartialOrd<BytesMut> for String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut
|
impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut
|
||||||
where BytesMut: PartialEq<T>
|
where
|
||||||
|
BytesMut: PartialEq<T>,
|
||||||
{
|
{
|
||||||
fn eq(&self, other: &&'a T) -> bool {
|
fn eq(&self, other: &&'a T) -> bool {
|
||||||
*self == **other
|
*self == **other
|
||||||
@@ -1410,7 +1428,8 @@ impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: ?Sized> PartialOrd<&'a T> for BytesMut
|
impl<'a, T: ?Sized> PartialOrd<&'a T> for BytesMut
|
||||||
where BytesMut: PartialOrd<T>
|
where
|
||||||
|
BytesMut: PartialOrd<T>,
|
||||||
{
|
{
|
||||||
fn partial_cmp(&self, other: &&'a T) -> Option<cmp::Ordering> {
|
fn partial_cmp(&self, other: &&'a T) -> Option<cmp::Ordering> {
|
||||||
self.partial_cmp(*other)
|
self.partial_cmp(*other)
|
||||||
@@ -1528,8 +1547,8 @@ mod fuzz {
|
|||||||
use loom::sync::Arc;
|
use loom::sync::Arc;
|
||||||
use loom::thread;
|
use loom::thread;
|
||||||
|
|
||||||
use crate::Bytes;
|
|
||||||
use super::BytesMut;
|
use super::BytesMut;
|
||||||
|
use crate::Bytes;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bytes_mut_cloning_frozen() {
|
fn bytes_mut_cloning_frozen() {
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
use core::fmt::{Debug, Formatter, Result};
|
use core::fmt::{Debug, Formatter, Result};
|
||||||
|
|
||||||
use crate::{Bytes, BytesMut};
|
|
||||||
use super::BytesRef;
|
use super::BytesRef;
|
||||||
|
use crate::{Bytes, BytesMut};
|
||||||
|
|
||||||
/// Alternative implementation of `std::fmt::Debug` for byte slice.
|
/// Alternative implementation of `std::fmt::Debug` for byte slice.
|
||||||
///
|
///
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
use core::fmt::{Formatter, LowerHex, Result, UpperHex};
|
use core::fmt::{Formatter, LowerHex, Result, UpperHex};
|
||||||
|
|
||||||
use crate::{Bytes, BytesMut};
|
|
||||||
use super::BytesRef;
|
use super::BytesRef;
|
||||||
|
use crate::{Bytes, BytesMut};
|
||||||
|
|
||||||
impl LowerHex for BytesRef<'_> {
|
impl LowerHex for BytesRef<'_> {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||||
|
|||||||
+9
-8
@@ -1,4 +1,9 @@
|
|||||||
#![deny(warnings, missing_docs, missing_debug_implementations, rust_2018_idioms)]
|
#![deny(
|
||||||
|
warnings,
|
||||||
|
missing_docs,
|
||||||
|
missing_debug_implementations,
|
||||||
|
rust_2018_idioms
|
||||||
|
)]
|
||||||
#![doc(html_root_url = "https://docs.rs/bytes/0.5.4")]
|
#![doc(html_root_url = "https://docs.rs/bytes/0.5.4")]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
@@ -72,24 +77,20 @@
|
|||||||
//! perform a syscall, which has the potential of failing. Operations on `Buf`
|
//! perform a syscall, which has the potential of failing. Operations on `Buf`
|
||||||
//! and `BufMut` are infallible.
|
//! and `BufMut` are infallible.
|
||||||
|
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
extern crate std;
|
extern crate std;
|
||||||
|
|
||||||
pub mod buf;
|
pub mod buf;
|
||||||
pub use crate::buf::{
|
pub use crate::buf::{Buf, BufMut};
|
||||||
Buf,
|
|
||||||
BufMut,
|
|
||||||
};
|
|
||||||
|
|
||||||
mod bytes_mut;
|
|
||||||
mod bytes;
|
mod bytes;
|
||||||
|
mod bytes_mut;
|
||||||
mod fmt;
|
mod fmt;
|
||||||
mod loom;
|
mod loom;
|
||||||
pub use crate::bytes_mut::BytesMut;
|
|
||||||
pub use crate::bytes::Bytes;
|
pub use crate::bytes::Bytes;
|
||||||
|
pub use crate::bytes_mut::BytesMut;
|
||||||
|
|
||||||
// Optional Serde support
|
// Optional Serde support
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
|
|||||||
+18
-11
@@ -1,15 +1,16 @@
|
|||||||
|
use super::{Bytes, BytesMut};
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use core::{cmp, fmt};
|
use core::{cmp, fmt};
|
||||||
use serde::{Serialize, Serializer, Deserialize, Deserializer, de};
|
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||||
use super::{Bytes, BytesMut};
|
|
||||||
|
|
||||||
macro_rules! serde_impl {
|
macro_rules! serde_impl {
|
||||||
($ty:ident, $visitor_ty:ident, $from_slice:ident, $from_vec:ident) => (
|
($ty:ident, $visitor_ty:ident, $from_slice:ident, $from_vec:ident) => {
|
||||||
impl Serialize for $ty {
|
impl Serialize for $ty {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: Serializer
|
where
|
||||||
|
S: Serializer,
|
||||||
{
|
{
|
||||||
serializer.serialize_bytes(&self)
|
serializer.serialize_bytes(&self)
|
||||||
}
|
}
|
||||||
@@ -26,7 +27,8 @@ macro_rules! serde_impl {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
|
fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
|
||||||
where V: de::SeqAccess<'de>
|
where
|
||||||
|
V: de::SeqAccess<'de>,
|
||||||
{
|
{
|
||||||
let len = cmp::min(seq.size_hint().unwrap_or(0), 4096);
|
let len = cmp::min(seq.size_hint().unwrap_or(0), 4096);
|
||||||
let mut values: Vec<u8> = Vec::with_capacity(len);
|
let mut values: Vec<u8> = Vec::with_capacity(len);
|
||||||
@@ -40,28 +42,32 @@ macro_rules! serde_impl {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
|
||||||
where E: de::Error
|
where
|
||||||
|
E: de::Error,
|
||||||
{
|
{
|
||||||
Ok($ty::$from_slice(v))
|
Ok($ty::$from_slice(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
|
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
|
||||||
where E: de::Error
|
where
|
||||||
|
E: de::Error,
|
||||||
{
|
{
|
||||||
Ok($ty::$from_vec(v))
|
Ok($ty::$from_vec(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||||
where E: de::Error
|
where
|
||||||
|
E: de::Error,
|
||||||
{
|
{
|
||||||
Ok($ty::$from_slice(v.as_bytes()))
|
Ok($ty::$from_slice(v.as_bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
|
||||||
where E: de::Error
|
where
|
||||||
|
E: de::Error,
|
||||||
{
|
{
|
||||||
Ok($ty::$from_vec(v.into_bytes()))
|
Ok($ty::$from_vec(v.into_bytes()))
|
||||||
}
|
}
|
||||||
@@ -70,12 +76,13 @@ macro_rules! serde_impl {
|
|||||||
impl<'de> Deserialize<'de> for $ty {
|
impl<'de> Deserialize<'de> for $ty {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize<D>(deserializer: D) -> Result<$ty, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<$ty, D::Error>
|
||||||
where D: Deserializer<'de>
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
deserializer.deserialize_byte_buf($visitor_ty)
|
deserializer.deserialize_byte_buf($visitor_ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
serde_impl!(Bytes, BytesVisitor, copy_from_slice, from);
|
serde_impl!(Bytes, BytesVisitor, copy_from_slice, from);
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#![deny(warnings, rust_2018_idioms)]
|
#![deny(warnings, rust_2018_idioms)]
|
||||||
|
|
||||||
use bytes::{BufMut, BytesMut};
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use bytes::buf::IoSliceMut;
|
use bytes::buf::IoSliceMut;
|
||||||
use core::usize;
|
use bytes::{BufMut, BytesMut};
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
use core::usize;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vec_as_mut_buf() {
|
fn test_vec_as_mut_buf() {
|
||||||
|
|||||||
+5
-7
@@ -1,6 +1,6 @@
|
|||||||
#![deny(warnings, rust_2018_idioms)]
|
#![deny(warnings, rust_2018_idioms)]
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut, Buf, BufMut};
|
use bytes::{Buf, BufMut, Bytes, BytesMut};
|
||||||
|
|
||||||
use std::usize;
|
use std::usize;
|
||||||
|
|
||||||
@@ -44,7 +44,6 @@ fn test_layout() {
|
|||||||
mem::size_of::<Option<BytesMut>>(),
|
mem::size_of::<Option<BytesMut>>(),
|
||||||
"BytesMut should be same size as Option<BytesMut>",
|
"BytesMut should be same size as Option<BytesMut>",
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -87,13 +86,11 @@ fn fmt_write() {
|
|||||||
write!(a, "{}", &s[..64]).unwrap();
|
write!(a, "{}", &s[..64]).unwrap();
|
||||||
assert_eq!(a, s[..64].as_bytes());
|
assert_eq!(a, s[..64].as_bytes());
|
||||||
|
|
||||||
|
|
||||||
let mut b = BytesMut::with_capacity(64);
|
let mut b = BytesMut::with_capacity(64);
|
||||||
write!(b, "{}", &s[..32]).unwrap();
|
write!(b, "{}", &s[..32]).unwrap();
|
||||||
write!(b, "{}", &s[32..64]).unwrap();
|
write!(b, "{}", &s[32..64]).unwrap();
|
||||||
assert_eq!(b, s[..64].as_bytes());
|
assert_eq!(b, s[..64].as_bytes());
|
||||||
|
|
||||||
|
|
||||||
let mut c = BytesMut::with_capacity(64);
|
let mut c = BytesMut::with_capacity(64);
|
||||||
write!(c, "{}", s).unwrap();
|
write!(c, "{}", s).unwrap();
|
||||||
assert_eq!(c, s[..].as_bytes());
|
assert_eq!(c, s[..].as_bytes());
|
||||||
@@ -305,11 +302,13 @@ fn split_off_to_at_gt_len() {
|
|||||||
|
|
||||||
assert!(panic::catch_unwind(move || {
|
assert!(panic::catch_unwind(move || {
|
||||||
let _ = make_bytes().split_to(5);
|
let _ = make_bytes().split_to(5);
|
||||||
}).is_err());
|
})
|
||||||
|
.is_err());
|
||||||
|
|
||||||
assert!(panic::catch_unwind(move || {
|
assert!(panic::catch_unwind(move || {
|
||||||
let _ = make_bytes().split_off(5);
|
let _ = make_bytes().split_off(5);
|
||||||
}).is_err());
|
})
|
||||||
|
.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -864,7 +863,6 @@ fn slice_ref_works() {
|
|||||||
test_slice_ref(&bytes, 9, 9, b"");
|
test_slice_ref(&bytes, 9, 9, b"");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn slice_ref_empty() {
|
fn slice_ref_empty() {
|
||||||
let bytes = Bytes::from(&b""[..]);
|
let bytes = Bytes::from(&b""[..]);
|
||||||
|
|||||||
@@ -39,7 +39,11 @@ unsafe impl GlobalAlloc for Ledger {
|
|||||||
let off_ptr = (ptr as *mut usize).offset(-1);
|
let off_ptr = (ptr as *mut usize).offset(-1);
|
||||||
let orig_size = off_ptr.read();
|
let orig_size = off_ptr.read();
|
||||||
if orig_size != layout.size() {
|
if orig_size != layout.size() {
|
||||||
panic!("bad dealloc: alloc size was {}, dealloc size is {}", orig_size, layout.size());
|
panic!(
|
||||||
|
"bad dealloc: alloc size was {}, dealloc size is {}",
|
||||||
|
orig_size,
|
||||||
|
layout.size()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let new_layout = match Layout::from_size_align(layout.size() + USIZE_SIZE, 1) {
|
let new_layout = match Layout::from_size_align(layout.size() + USIZE_SIZE, 1) {
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
#![deny(warnings, rust_2018_idioms)]
|
#![deny(warnings, rust_2018_idioms)]
|
||||||
|
|
||||||
use bytes::{Buf, BufMut, Bytes};
|
|
||||||
use bytes::buf::{BufExt, BufMutExt};
|
use bytes::buf::{BufExt, BufMutExt};
|
||||||
|
use bytes::{Buf, BufMut, Bytes};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::io::IoSlice;
|
use std::io::IoSlice;
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ fn iter_len() {
|
|||||||
assert_eq!(iter.len(), 11);
|
assert_eq!(iter.len(), 11);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_iter_len() {
|
fn empty_iter_len() {
|
||||||
let buf = Bytes::from_static(b"");
|
let buf = Bytes::from_static(b"");
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
use std::io::{BufRead, Read};
|
use std::io::{BufRead, Read};
|
||||||
|
|
||||||
use bytes::buf::{BufExt};
|
use bytes::buf::BufExt;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn read() {
|
fn read() {
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
#![cfg(feature = "serde")]
|
#![cfg(feature = "serde")]
|
||||||
#![deny(warnings, rust_2018_idioms)]
|
#![deny(warnings, rust_2018_idioms)]
|
||||||
|
|
||||||
use serde_test::{Token, assert_tokens};
|
use serde_test::{assert_tokens, Token};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ser_de_empty() {
|
fn test_ser_de_empty() {
|
||||||
|
|||||||
Reference in New Issue
Block a user