diff options
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | .github/workflows/ci.yml | 2 | ||||
-rw-r--r-- | Android.bp | 4 | ||||
-rw-r--r-- | CHANGELOG.md | 15 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | Cargo.toml.orig | 2 | ||||
-rw-r--r-- | METADATA | 10 | ||||
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | src/buf/buf_impl.rs | 2 | ||||
-rw-r--r-- | src/buf/buf_mut.rs | 1 | ||||
-rw-r--r-- | src/buf/iter.rs | 4 | ||||
-rw-r--r-- | src/bytes.rs | 36 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | tests/test_bytes.rs | 45 |
14 files changed, 117 insertions, 18 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 2836d3d..b4c4c4e 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,6 +1,6 @@ { "git": { - "sha1": "9b227220dfeb5d176b807c9bf27e1ee438935f78" + "sha1": "21ed3328364716fa30a4bf7502c913bbf0a90f45" }, "path_in_vcs": "" }
\ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f6c6851..a4f7b1d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: @@ -22,7 +22,7 @@ rust_defaults { name: "bytes_test_defaults", crate_name: "bytes", cargo_env_compat: true, - cargo_pkg_version: "1.3.0", + cargo_pkg_version: "1.4.0", test_suites: ["general-tests"], auto_gen_config: true, edition: "2018", @@ -153,7 +153,7 @@ rust_library { host_supported: true, crate_name: "bytes", cargo_env_compat: true, - cargo_pkg_version: "1.3.0", + cargo_pkg_version: "1.4.0", srcs: ["src/lib.rs"], edition: "2018", features: [ diff --git a/CHANGELOG.md b/CHANGELOG.md index 760c210..a1bad4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -12,7 +12,7 @@ [package] edition = "2018" name = "bytes" -version = "1.3.0" +version = "1.4.0" authors = [ "Carl Lerche <me@carllerche.com>", "Sean McArthur <sean@seanmonstar.com>", diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 9efd8a1..4a96ec1 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -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 <me@carllerche.com>", @@ -11,13 +11,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/bytes/bytes-1.3.0.crate" + value: "https://static.crates.io/crates/bytes/bytes-1.4.0.crate" } - version: "1.3.0" + version: "1.4.0" license_type: NOTICE last_upgrade_date { - year: 2022 - month: 12 - day: 8 + year: 2023 + month: 2 + day: 15 } } @@ -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). diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index d903d5c..366cfc9 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -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, diff --git a/src/buf/buf_mut.rs b/src/buf/buf_mut.rs index ada07a3..685fcc7 100644 --- a/src/buf/buf_mut.rs +++ b/src/buf/buf_mut.rs @@ -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, diff --git a/src/buf/iter.rs b/src/buf/iter.rs index 8914a40..c694e3d 100644 --- a/src/buf/iter.rs +++ b/src/buf/iter.rs @@ -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 } } diff --git a/src/bytes.rs b/src/bytes.rs index fa43d3a..0404a72 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -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. //! diff --git a/tests/test_bytes.rs b/tests/test_bytes.rs index 3481f8d..da1b074 100644 --- a/tests/test_bytes.rs +++ b/tests/test_bytes.rs @@ -1164,3 +1164,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); + } + } +} |