mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-08 00:00:26 +02:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21ed332836 | ||
|
|
05e9d5cab9 | ||
|
|
f15bba3375 | ||
|
|
c93a94b974 | ||
|
|
050d65b2ce |
@@ -157,7 +157,7 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install Rust
|
||||
run: rustup update stable && rustup default stable
|
||||
run: rustup update $nightly && rustup default $nightly
|
||||
- name: Build documentation
|
||||
run: cargo doc --no-deps --all-features
|
||||
env:
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
# 1.4.0 (January 31, 2023)
|
||||
|
||||
### Added
|
||||
|
||||
- Make `IntoIter` constructor public (#581)
|
||||
|
||||
### Fixed
|
||||
|
||||
- Avoid large reallocations when freezing `BytesMut` (#592)
|
||||
|
||||
### Documented
|
||||
|
||||
- Document which functions require `std` (#591)
|
||||
- Fix duplicate "the the" typos (#585)
|
||||
|
||||
# 1.3.0 (November 20, 2022)
|
||||
|
||||
### Added
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ name = "bytes"
|
||||
# When releasing to crates.io:
|
||||
# - Update CHANGELOG.md.
|
||||
# - Create "v1.x.y" git tag.
|
||||
version = "1.3.0"
|
||||
version = "1.4.0"
|
||||
license = "MIT"
|
||||
authors = [
|
||||
"Carl Lerche <[email protected]>",
|
||||
|
||||
@@ -36,6 +36,15 @@ Serde support is optional and disabled by default. To enable use the feature `se
|
||||
bytes = { version = "1", features = ["serde"] }
|
||||
```
|
||||
|
||||
## Building documentation
|
||||
|
||||
When building the `bytes` documentation the `docsrs` option should be used, otherwise
|
||||
feature gates will not be shown. This requires a nightly toolchain:
|
||||
|
||||
```
|
||||
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the [MIT license](LICENSE).
|
||||
|
||||
@@ -160,6 +160,7 @@ pub trait Buf {
|
||||
///
|
||||
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
|
||||
if dst.is_empty() {
|
||||
return 0;
|
||||
@@ -1183,6 +1184,7 @@ pub trait Buf {
|
||||
/// assert_eq!(&dst[..11], &b"hello world"[..]);
|
||||
/// ```
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
fn reader(self) -> Reader<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
|
||||
@@ -1239,6 +1239,7 @@ pub unsafe trait BufMut {
|
||||
/// assert_eq!(*buf, b"hello world"[..]);
|
||||
/// ```
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
fn writer(self) -> Writer<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
|
||||
+1
-3
@@ -2,8 +2,6 @@ use crate::Buf;
|
||||
|
||||
/// Iterator over the bytes contained by the buffer.
|
||||
///
|
||||
/// This struct is created by the [`iter`] method on [`Buf`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
@@ -43,7 +41,7 @@ impl<T> IntoIter<T> {
|
||||
/// assert_eq!(iter.next(), Some(b'c'));
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
pub(crate) fn new(inner: T) -> IntoIter<T> {
|
||||
pub fn new(inner: T) -> IntoIter<T> {
|
||||
IntoIter { inner }
|
||||
}
|
||||
|
||||
|
||||
+32
-4
@@ -32,7 +32,7 @@ use crate::Buf;
|
||||
/// All `Bytes` implementations must fulfill the following requirements:
|
||||
/// - They are cheaply cloneable and thereby shareable between an unlimited amount
|
||||
/// of components, for example by modifying a reference count.
|
||||
/// - Instances can be sliced to refer to a subset of the the original buffer.
|
||||
/// - Instances can be sliced to refer to a subset of the original buffer.
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::Bytes;
|
||||
@@ -71,7 +71,7 @@ use crate::Buf;
|
||||
///
|
||||
/// For `Bytes` implementations which point to a reference counted shared storage
|
||||
/// (e.g. an `Arc<[u8]>`), sharing will be implemented by increasing the
|
||||
/// the reference count.
|
||||
/// reference count.
|
||||
///
|
||||
/// Due to this mechanism, multiple `Bytes` instances may point to the same
|
||||
/// shared memory region.
|
||||
@@ -807,8 +807,36 @@ impl From<&'static str> for Bytes {
|
||||
|
||||
impl From<Vec<u8>> for Bytes {
|
||||
fn from(vec: Vec<u8>) -> Bytes {
|
||||
let slice = vec.into_boxed_slice();
|
||||
slice.into()
|
||||
let mut vec = vec;
|
||||
let ptr = vec.as_mut_ptr();
|
||||
let len = vec.len();
|
||||
let cap = vec.capacity();
|
||||
|
||||
// Avoid an extra allocation if possible.
|
||||
if len == cap {
|
||||
return Bytes::from(vec.into_boxed_slice());
|
||||
}
|
||||
|
||||
let shared = Box::new(Shared {
|
||||
buf: ptr,
|
||||
cap,
|
||||
ref_cnt: AtomicUsize::new(1),
|
||||
});
|
||||
mem::forget(vec);
|
||||
|
||||
let shared = Box::into_raw(shared);
|
||||
// The pointer should be aligned, so this assert should
|
||||
// always succeed.
|
||||
debug_assert!(
|
||||
0 == (shared as usize & KIND_MASK),
|
||||
"internal: Box<Shared> should have an aligned pointer",
|
||||
);
|
||||
Bytes {
|
||||
ptr,
|
||||
len,
|
||||
data: AtomicPtr::new(shared as _),
|
||||
vtable: &SHARED_VTABLE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
|
||||
))]
|
||||
#![no_std]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
|
||||
//! Provides abstractions for working with bytes.
|
||||
//!
|
||||
|
||||
@@ -1163,3 +1163,48 @@ fn test_bytes_into_vec_promotable_even() {
|
||||
assert_eq!(Vec::from(b2), vec[20..]);
|
||||
assert_eq!(Vec::from(b1), vec[..20]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytes_vec_conversion() {
|
||||
let mut vec = Vec::with_capacity(10);
|
||||
vec.extend(b"abcdefg");
|
||||
let b = Bytes::from(vec);
|
||||
let v = Vec::from(b);
|
||||
assert_eq!(v.len(), 7);
|
||||
assert_eq!(v.capacity(), 10);
|
||||
|
||||
let mut b = Bytes::from(v);
|
||||
b.advance(1);
|
||||
let v = Vec::from(b);
|
||||
assert_eq!(v.len(), 6);
|
||||
assert_eq!(v.capacity(), 10);
|
||||
assert_eq!(v.as_slice(), b"bcdefg");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytes_mut_conversion() {
|
||||
let mut b1 = BytesMut::with_capacity(10);
|
||||
b1.extend(b"abcdefg");
|
||||
let b2 = Bytes::from(b1);
|
||||
let v = Vec::from(b2);
|
||||
assert_eq!(v.len(), 7);
|
||||
assert_eq!(v.capacity(), 10);
|
||||
|
||||
let mut b = Bytes::from(v);
|
||||
b.advance(1);
|
||||
let v = Vec::from(b);
|
||||
assert_eq!(v.len(), 6);
|
||||
assert_eq!(v.capacity(), 10);
|
||||
assert_eq!(v.as_slice(), b"bcdefg");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bytes_capacity_len() {
|
||||
for cap in 0..100 {
|
||||
for len in 0..=cap {
|
||||
let mut v = Vec::with_capacity(cap);
|
||||
v.resize(len, 0);
|
||||
let _ = Bytes::from(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user