.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
#[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
#[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
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
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