use core::convert::TryFrom;\r
use tinyvec_macros::impl_mirrored;\r
\r
+#[cfg(feature = "rustc_1_57")]\r
+use alloc::collections::TryReserveError;\r
+\r
#[cfg(feature = "serde")]\r
use core::marker::PhantomData;\r
#[cfg(feature = "serde")]\r
*self = TinyVec::Heap(v);\r
}\r
\r
+ /// Tries to move the content of the TinyVec to the heap, if it's inline.\r
+ ///\r
+ /// # Errors\r
+ ///\r
+ /// If the allocator reports a failure, then an error is returned and the\r
+ /// content is kept on the stack.\r
+ ///\r
+ /// ```rust\r
+ /// use tinyvec::*;\r
+ /// let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);\r
+ /// assert!(tv.is_inline());\r
+ /// assert_eq!(Ok(()), tv.try_move_to_the_heap());\r
+ /// assert!(tv.is_heap());\r
+ /// ```\r
+ #[cfg(feature = "rustc_1_57")]\r
+ pub fn try_move_to_the_heap(&mut self) -> Result<(), TryReserveError> {\r
+ let arr = match self {\r
+ TinyVec::Heap(_) => return Ok(()),\r
+ TinyVec::Inline(a) => a,\r
+ };\r
+\r
+ let v = arr.try_drain_to_vec()?;\r
+ *self = TinyVec::Heap(v);\r
+ return Ok(());\r
+ }\r
+\r
/// If TinyVec is inline, moves the content of it to the heap.\r
/// Also reserves additional space.\r
/// ```rust\r
*self = TinyVec::Heap(v);\r
}\r
\r
+ /// If TinyVec is inline, try to move the content of it to the heap.\r
+ /// Also reserves additional space.\r
+ ///\r
+ /// # Errors\r
+ ///\r
+ /// If the allocator reports a failure, then an error is returned.\r
+ ///\r
+ /// ```rust\r
+ /// use tinyvec::*;\r
+ /// let mut tv = tiny_vec!([i32; 4] => 1, 2, 3);\r
+ /// assert!(tv.is_inline());\r
+ /// assert_eq!(Ok(()), tv.try_move_to_the_heap_and_reserve(32));\r
+ /// assert!(tv.is_heap());\r
+ /// assert!(tv.capacity() >= 35);\r
+ /// ```\r
+ #[cfg(feature = "rustc_1_57")]\r
+ pub fn try_move_to_the_heap_and_reserve(\r
+ &mut self, n: usize,\r
+ ) -> Result<(), TryReserveError> {\r
+ let arr = match self {\r
+ TinyVec::Heap(h) => return h.try_reserve(n),\r
+ TinyVec::Inline(a) => a,\r
+ };\r
+\r
+ let v = arr.try_drain_to_vec_and_reserve(n)?;\r
+ *self = TinyVec::Heap(v);\r
+ return Ok(());\r
+ }\r
+\r
/// Reserves additional space.\r
/// Moves to the heap if array can't hold `n` more items\r
/// ```rust\r
return;\r
}\r
\r
+ /// Tries to reserve additional space.\r
+ /// Moves to the heap if array can't hold `n` more items.\r
+ ///\r
+ /// # Errors\r
+ ///\r
+ /// If the allocator reports a failure, then an error is returned.\r
+ ///\r
+ /// ```rust\r
+ /// use tinyvec::*;\r
+ /// let mut tv = tiny_vec!([i32; 4] => 1, 2, 3, 4);\r
+ /// assert!(tv.is_inline());\r
+ /// assert_eq!(Ok(()), tv.try_reserve(1));\r
+ /// assert!(tv.is_heap());\r
+ /// assert!(tv.capacity() >= 5);\r
+ /// ```\r
+ #[cfg(feature = "rustc_1_57")]\r
+ pub fn try_reserve(&mut self, n: usize) -> Result<(), TryReserveError> {\r
+ let arr = match self {\r
+ TinyVec::Heap(h) => return h.try_reserve(n),\r
+ TinyVec::Inline(a) => a,\r
+ };\r
+\r
+ if n > arr.capacity() - arr.len() {\r
+ let v = arr.try_drain_to_vec_and_reserve(n)?;\r
+ *self = TinyVec::Heap(v);\r
+ }\r
+\r
+ /* In this place array has enough place, so no work is needed more */\r
+ return Ok(());\r
+ }\r
+\r
/// Reserves additional space.\r
/// Moves to the heap if array can't hold `n` more items\r
///\r
return;\r
}\r
\r
+ /// Tries to reserve additional space.\r
+ /// Moves to the heap if array can't hold `n` more items\r
+ ///\r
+ /// # Errors\r
+ ///\r
+ /// If the allocator reports a failure, then an error is returned.\r
+ ///\r
+ /// From [Vec::try_reserve_exact](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.try_reserve_exact)\r
+ /// ```text\r
+ /// Note that the allocator may give the collection more space than it requests.\r
+ /// Therefore, capacity can not be relied upon to be precisely minimal.\r
+ /// Prefer `reserve` if future insertions are expected.\r
+ /// ```\r
+ /// ```rust\r
+ /// use tinyvec::*;\r
+ /// let mut tv = tiny_vec!([i32; 4] => 1, 2, 3, 4);\r
+ /// assert!(tv.is_inline());\r
+ /// assert_eq!(Ok(()), tv.try_reserve_exact(1));\r
+ /// assert!(tv.is_heap());\r
+ /// assert!(tv.capacity() >= 5);\r
+ /// ```\r
+ #[cfg(feature = "rustc_1_57")]\r
+ pub fn try_reserve_exact(&mut self, n: usize) -> Result<(), TryReserveError> {\r
+ let arr = match self {\r
+ TinyVec::Heap(h) => return h.try_reserve_exact(n),\r
+ TinyVec::Inline(a) => a,\r
+ };\r
+\r
+ if n > arr.capacity() - arr.len() {\r
+ let v = arr.try_drain_to_vec_and_reserve(n)?;\r
+ *self = TinyVec::Heap(v);\r
+ }\r
+\r
+ /* In this place array has enough place, so no work is needed more */\r
+ return Ok(());\r
+ }\r
+\r
/// Makes a new TinyVec with _at least_ the given capacity.\r
///\r
/// If the requested capacity is less than or equal to the array capacity you\r