]> git.proxmox.com Git - rustc.git/blobdiff - vendor/tinyvec/src/arrayvec.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / tinyvec / src / arrayvec.rs
index 7cf1bfcc1567ec2e95c577a1d1d952d0835a2dca..9cfe58d8dbc0e6085d2ac40c4c80a888c1a9424c 100644 (file)
@@ -1271,7 +1271,7 @@ impl<A: Array> From<A> for ArrayVec<A> {
       .as_slice()\r
       .len()\r
       .try_into()\r
-      .expect("ArrayVec::from> lenght must be in range 0..=u16::MAX");\r
+      .expect("ArrayVec::from> length must be in range 0..=u16::MAX");\r
     Self { len, data }\r
   }\r
 }\r
@@ -1281,6 +1281,15 @@ impl<A: Array> From<A> for ArrayVec<A> {
 #[derive(Debug, Copy, Clone)]\r
 pub struct TryFromSliceError(());\r
 \r
+impl core::fmt::Display for TryFromSliceError {\r
+  fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {\r
+    f.write_str("could not convert slice to ArrayVec")\r
+  }\r
+}\r
+\r
+#[cfg(feature = "std")]\r
+impl std::error::Error for TryFromSliceError {}\r
+\r
 impl<T, A> TryFrom<&'_ [T]> for ArrayVec<A>\r
 where\r
   T: Clone + Default,\r
@@ -1744,6 +1753,9 @@ where
 #[cfg(feature = "alloc")]\r
 use alloc::vec::Vec;\r
 \r
+#[cfg(all(feature = "alloc", feature = "rustc_1_57"))]\r
+use alloc::collections::TryReserveError;\r
+\r
 #[cfg(feature = "alloc")]\r
 impl<A: Array> ArrayVec<A> {\r
   /// Drains all elements to a Vec, but reserves additional space\r
@@ -1763,6 +1775,34 @@ impl<A: Array> ArrayVec<A> {
     return v;\r
   }\r
 \r
+  /// Tries to drain all elements to a Vec, but reserves additional space.\r
+  ///\r
+  /// # Errors\r
+  ///\r
+  /// If the allocator reports a failure, then an error is returned.\r
+  ///\r
+  /// ```\r
+  /// # use tinyvec::*;\r
+  /// let mut av = array_vec!([i32; 7] => 1, 2, 3);\r
+  /// let v = av.try_drain_to_vec_and_reserve(10);\r
+  /// assert!(matches!(v, Ok(_)));\r
+  /// let v = v.unwrap();\r
+  /// assert_eq!(v, &[1, 2, 3]);\r
+  /// assert_eq!(v.capacity(), 13);\r
+  /// ```\r
+  #[cfg(feature = "rustc_1_57")]\r
+  pub fn try_drain_to_vec_and_reserve(\r
+    &mut self, n: usize,\r
+  ) -> Result<Vec<A::Item>, TryReserveError> {\r
+    let cap = n + self.len();\r
+    let mut v = Vec::new();\r
+    v.try_reserve(cap)?;\r
+    let iter = self.iter_mut().map(take);\r
+    v.extend(iter);\r
+    self.set_len(0);\r
+    return Ok(v);\r
+  }\r
+\r
   /// Drains all elements to a Vec\r
   /// ```\r
   /// # use tinyvec::*;\r
@@ -1774,6 +1814,27 @@ impl<A: Array> ArrayVec<A> {
   pub fn drain_to_vec(&mut self) -> Vec<A::Item> {\r
     self.drain_to_vec_and_reserve(0)\r
   }\r
+\r
+  /// Tries to drain all elements to a Vec.\r
+  ///\r
+  /// # Errors\r
+  ///\r
+  /// If the allocator reports a failure, then an error is returned.\r
+  ///\r
+  /// ```\r
+  /// # use tinyvec::*;\r
+  /// let mut av = array_vec!([i32; 7] => 1, 2, 3);\r
+  /// let v = av.try_drain_to_vec();\r
+  /// assert!(matches!(v, Ok(_)));\r
+  /// let v = v.unwrap();\r
+  /// assert_eq!(v, &[1, 2, 3]);\r
+  /// // Vec may reserve more than necessary in order to prevent more future allocations.\r
+  /// assert!(v.capacity() >= 3);\r
+  /// ```\r
+  #[cfg(feature = "rustc_1_57")]\r
+  pub fn try_drain_to_vec(&mut self) -> Result<Vec<A::Item>, TryReserveError> {\r
+    self.try_drain_to_vec_and_reserve(0)\r
+  }\r
 }\r
 \r
 #[cfg(feature = "serde")]\r