Upgrade bytes to 1.4.0 am: 07286a835f am: 14c5df49c8

Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/bytes/+/2438286

Change-Id: Id88b300de88ace741a8dea9bd748a46d36e5f348
Signed-off-by: Automerger Merge Worker <[email protected]>
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 @@
     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:
diff --git a/Android.bp b/Android.bp
index 08a64e9..73c056c 100644
--- a/Android.bp
+++ b/Android.bp
@@ -22,7 +22,7 @@
     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 @@
     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
diff --git a/Cargo.toml b/Cargo.toml
index 5c91c4b..d0acb34 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,7 +12,7 @@
 [package]
 edition = "2018"
 name = "bytes"
-version = "1.3.0"
+version = "1.4.0"
 authors = [
     "Carl Lerche <[email protected]>",
     "Sean McArthur <[email protected]>",
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 @@
 # 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]>",
diff --git a/METADATA b/METADATA
index a151d0b..8f9837f 100644
--- a/METADATA
+++ b/METADATA
@@ -11,13 +11,13 @@
   }
   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
   }
 }
diff --git a/README.md b/README.md
index 468485d..be46642 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,15 @@
 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 @@
     ///
     /// [`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 @@
     /// 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 @@
     /// 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 @@
 
 /// 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 @@
     /// 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 @@
 /// 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 @@
 ///
 /// 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<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,
+        }
     }
 }
 
diff --git a/src/lib.rs b/src/lib.rs
index 706735e..af436b3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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 @@
     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);
+        }
+    }
+}