diff --git a/src/arrayvec.rs b/src/arrayvec.rs index fdf9663..949b1ba 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -80,6 +80,19 @@ impl> IndexMut for ArrayVec { impl ArrayVec { /// Move all values from `other` into this vec. + /// + /// ## Panics + /// * If the vec overflows its capacity + /// + /// ## Example + /// ```rust + /// use tinyvec::*; + /// let mut av = array_vec!([i32; 10], 1, 2, 3); + /// let mut av2 = array_vec!([i32; 10], 4, 5, 6); + /// av.append(&mut av2); + /// assert_eq!(av, &[1, 2, 3, 4, 5, 6][..]); + /// assert_eq!(av2, &[][..]); + /// ``` #[inline] pub fn append(&mut self, other: &mut Self) { for item in other.drain(..) { @@ -339,12 +352,14 @@ impl ArrayVec { /// Place an element onto the end of the vec. /// - /// See also, [`try_push`](ArrayVec::try_push) /// ## Panics /// * If the length of the vec would overflow the capacity. #[inline(always)] pub fn push(&mut self, val: A::Item) { - if self.try_push(val).is_err() { + if self.len < A::CAPACITY { + replace(&mut self.data.as_slice_mut()[self.len], val); + self.len += 1; + } else { panic!("ArrayVec: overflow!") } } @@ -580,27 +595,6 @@ impl ArrayVec { Err(data) } } - - /// Pushes an item if there's room. - /// - /// ## Failure - /// - /// If there's no more capacity the vec is unchanged, and you get the item - /// back in the `Err`. - #[inline] - pub fn try_push(&mut self, val: A::Item) -> Result<(), A::Item> { - if self.len < A::CAPACITY { - replace(&mut self.data.as_slice_mut()[self.len], val); - self.len += 1; - Ok(()) - } else { - Err(val) - } - } - - // LATER: try_insert ? - - // LATER: try_remove ? } /// Draining iterator for `ArrayVecDrain` diff --git a/src/lib.rs b/src/lib.rs index 184dc33..2100b1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,18 +23,15 @@ //! types](https://doc.rust-lang.org/std/default/trait.Default.html#implementors), //! so I think that you'll find these vecs useful in many cases. //! -//! * [`ArrayVec`](ArrayVec::) is an array-backed vec-like structure with a -//! fixed capacity. If you try to grow the length past the array's capacity it -//! will error or panic (depending on the method used). -//! * [`TinyVec`](TinyVec::) is an enum that's either an "inline" `ArrayVec` -//! or a "heap" `Vec`. If it's in array mode and you try to grow the vec -//! beyond its capacity it'll quietly transition into heap mode for you and -//! then continue operation. This type is naturally behind the `alloc` feature -//! gate. +//! * [`ArrayVec`](ArrayVec) is an array-backed vec-like structure with a fixed +//! capacity. If you try to grow the length past the array's capacity it will +//! error or panic (depending on the method used). +//! * (`alloc` feature) [`TinyVec`](TinyVec) is an enum that's either an +//! "Inline" `ArrayVec` or a "Heap" `Vec`. If it's Inline and you try to grow +//! the `ArrayVec` beyond its array capacity it will quietly transition into +//! Heap mode and then continue the operation. //! -//! ## Stability Goal -//! -//! The crate is still in development, but we have some very clear goals: +//! ## Crate Goals //! //! 1) The crate is 100% safe code. Not just a safe API, there are also no //! `unsafe` internals. `#![forbid(unsafe_code)]`. @@ -44,10 +41,10 @@ //! 3) The intended API is that, _as much as possible_, these types are //! essentially a "drop-in" replacement for the standard [`Vec`](Vec::) //! type. -//! * Stable `Vec` methods that the vecs here also have should have the exact -//! same signature. +//! * Stable `Vec` methods that the vecs here also have should be the same +//! general signature. //! * Unstable `Vec` methods are sometimes provided via a crate feature, but -//! if so they also require Nightly. +//! if so they also require a Nightly compiler. //! * Some methods are provided that _are not_ part of the `Vec` type, such //! as additional constructor methods. In this case, the names are rather //! long and whimsical in the hopes that they don't class with any possible diff --git a/src/tinyvec.rs b/src/tinyvec.rs index 9fc6650..9edf4f7 100644 --- a/src/tinyvec.rs +++ b/src/tinyvec.rs @@ -371,8 +371,6 @@ impl TinyVec { } /// Place an element onto the end of the vec. - /// - /// See also, [`try_push`](TinyVec::::try_push) /// ## Panics /// * If the length of the vec would overflow the capacity. #[inline(always)] @@ -580,12 +578,6 @@ impl TinyVec { let arr = ArrayVec::try_from_array_len(data, len)?; Ok(TinyVec::Inline(arr)) } - - // LATER: try_push ? - - // LATER: try_insert ? - - // LATER: try_remove ? } /// Draining iterator for `TinyVecDrain`