+1
-1
| [package] | ||
| name = "arrayvec" | ||
| version = "0.3.15" | ||
| version = "0.3.16" | ||
| authors = ["bluss"] | ||
@@ -5,0 +5,0 @@ license = "MIT/Apache-2.0" |
+7
-1
@@ -5,3 +5,3 @@ | ||
| A vector with fixed capacity. | ||
| A vector with fixed capacity. Requires Rust 1.2+. | ||
@@ -26,2 +26,8 @@ Please read the `API documentation here`__ | ||
| - 0.3.16 | ||
| - Added method ``.retain()`` to ``ArrayVec``. | ||
| - Added methods ``.as_slice(), .as_mut_slice()`` to ``ArrayVec`` and ``.as_str()`` | ||
| to ``ArrayString``. | ||
| - 0.3.15 | ||
@@ -28,0 +34,0 @@ |
+13
-4
@@ -53,2 +53,4 @@ use std::borrow::Borrow; | ||
| /// | ||
| /// **Errors** if the backing array is not large enough to fit the string. | ||
| /// | ||
| /// ``` | ||
@@ -81,5 +83,6 @@ /// use arrayvec::ArrayString; | ||
| /// | ||
| /// Returns `Ok` if the push succeeds, and returns `Err` if the backing | ||
| /// array is not large enough to fit the additional char. | ||
| /// Returns `Ok` if the push succeeds. | ||
| /// | ||
| /// **Errors** if the backing array is not large enough to fit the additional char. | ||
| /// | ||
| /// ``` | ||
@@ -104,5 +107,6 @@ /// use arrayvec::ArrayString; | ||
| /// | ||
| /// Returns `Ok` if the push succeeds, and returns `Err` if the | ||
| /// backing array is not large enough to fit the string. | ||
| /// Returns `Ok` if the push succeeds. | ||
| /// | ||
| /// **Errors** if the backing array is not large enough to fit the string. | ||
| /// | ||
| /// ``` | ||
@@ -154,2 +158,7 @@ /// use arrayvec::ArrayString; | ||
| } | ||
| /// Return a string slice of the whole `ArrayString`. | ||
| pub fn as_str(&self) -> &str { | ||
| self | ||
| } | ||
| } | ||
@@ -156,0 +165,0 @@ |
+69
-27
@@ -158,27 +158,2 @@ //! **arrayvec** provides the types `ArrayVec` and `ArrayString`: | ||
| /// Remove the last element in the vector. | ||
| /// | ||
| /// Return `Some(` *element* `)` if the vector is non-empty, else `None`. | ||
| /// | ||
| /// ``` | ||
| /// use arrayvec::ArrayVec; | ||
| /// | ||
| /// let mut array = ArrayVec::<[_; 2]>::new(); | ||
| /// | ||
| /// array.push(1); | ||
| /// | ||
| /// assert_eq!(array.pop(), Some(1)); | ||
| /// assert_eq!(array.pop(), None); | ||
| /// ``` | ||
| pub fn pop(&mut self) -> Option<A::Item> { | ||
| if self.len() == 0 { | ||
| return None | ||
| } | ||
| unsafe { | ||
| let new_len = self.len() - 1; | ||
| self.set_len(new_len); | ||
| Some(ptr::read(self.get_unchecked_mut(new_len))) | ||
| } | ||
| } | ||
| /// Insert `element` in position `index`. | ||
@@ -229,2 +204,27 @@ /// | ||
| /// Remove the last element in the vector. | ||
| /// | ||
| /// Return `Some(` *element* `)` if the vector is non-empty, else `None`. | ||
| /// | ||
| /// ``` | ||
| /// use arrayvec::ArrayVec; | ||
| /// | ||
| /// let mut array = ArrayVec::<[_; 2]>::new(); | ||
| /// | ||
| /// array.push(1); | ||
| /// | ||
| /// assert_eq!(array.pop(), Some(1)); | ||
| /// assert_eq!(array.pop(), None); | ||
| /// ``` | ||
| pub fn pop(&mut self) -> Option<A::Item> { | ||
| if self.len() == 0 { | ||
| return None | ||
| } | ||
| unsafe { | ||
| let new_len = self.len() - 1; | ||
| self.set_len(new_len); | ||
| Some(ptr::read(self.get_unchecked_mut(new_len))) | ||
| } | ||
| } | ||
| /// Remove the element at `index` and swap the last element into its place. | ||
@@ -282,2 +282,36 @@ /// | ||
| /// Retains only the elements specified by the predicate. | ||
| /// | ||
| /// In other words, remove all elements `e` such that `f(&mut e)` returns false. | ||
| /// This method operates in place and preserves the order of the retained | ||
| /// elements. | ||
| /// | ||
| /// ``` | ||
| /// use arrayvec::ArrayVec; | ||
| /// | ||
| /// let mut array = ArrayVec::from([1, 2, 3, 4]); | ||
| /// array.retain(|x| *x & 1 != 0 ); | ||
| /// assert_eq!(&array[..], &[1, 3]); | ||
| /// ``` | ||
| pub fn retain<F>(&mut self, mut f: F) | ||
| where F: FnMut(&mut A::Item) -> bool | ||
| { | ||
| let len = self.len(); | ||
| let mut del = 0; | ||
| { | ||
| let v = &mut **self; | ||
| for i in 0..len { | ||
| if !f(&mut v[i]) { | ||
| del += 1; | ||
| } else if del > 0 { | ||
| v.swap(i - del, i); | ||
| } | ||
| } | ||
| } | ||
| if del > 0 { | ||
| self.drain(len - del..); | ||
| } | ||
| } | ||
| /// Set the vector's length without dropping or moving out elements | ||
@@ -367,2 +401,12 @@ /// | ||
| } | ||
| /// Return a slice containing all elements of the vector. | ||
| pub fn as_slice(&self) -> &[A::Item] { | ||
| self | ||
| } | ||
| /// Return a mutable slice containing all elements of the vector. | ||
| pub fn as_mut_slice(&mut self) -> &mut [A::Item] { | ||
| self | ||
| } | ||
| } | ||
@@ -794,4 +838,2 @@ | ||
| #[cfg(feature="std")] | ||
| /// Requires `features="std"`. | ||
| impl<T> fmt::Display for CapacityError<T> { | ||
@@ -798,0 +840,0 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
+17
-0
@@ -162,2 +162,19 @@ extern crate arrayvec; | ||
| #[test] | ||
| fn test_retain() { | ||
| let mut v = ArrayVec::from([0; 8]); | ||
| for (i, elt) in v.iter_mut().enumerate() { | ||
| *elt = i; | ||
| } | ||
| v.retain(|_| true); | ||
| assert_eq!(&v[..], &[0, 1, 2, 3, 4, 5, 6, 7]); | ||
| v.retain(|elt| { | ||
| *elt /= 2; | ||
| *elt % 2 == 0 | ||
| }); | ||
| assert_eq!(&v[..], &[0, 0, 2, 2]); | ||
| v.retain(|_| false); | ||
| assert_eq!(&v[..], &[]); | ||
| } | ||
| #[test] | ||
| #[should_panic] | ||
@@ -164,0 +181,0 @@ fn test_drain_oob() { |