+8
-4
@@ -50,6 +50,8 @@ impl<A: Array<Item=u8>> ArrayString<A> {
|
|||||||
///
|
///
|
||||||
/// Capacity is inferred from the type parameter.
|
/// Capacity is inferred from the type parameter.
|
||||||
///
|
///
|
||||||
|
/// **Errors** if the backing array is not large enough to fit the string.
|
||||||
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use arrayvec::ArrayString;
|
/// use arrayvec::ArrayString;
|
||||||
///
|
///
|
||||||
@@ -77,8 +79,9 @@ impl<A: Array<Item=u8>> ArrayString<A> {
|
|||||||
|
|
||||||
/// Adds the given char to the end of the string.
|
/// Adds the given char to the end of the string.
|
||||||
///
|
///
|
||||||
/// Returns `Ok` if the push succeeds, and returns `Err` if the backing
|
/// Returns `Ok` if the push succeeds.
|
||||||
/// array is not large enough to fit the additional char.
|
///
|
||||||
|
/// **Errors** if the backing array is not large enough to fit the additional char.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use arrayvec::ArrayString;
|
/// use arrayvec::ArrayString;
|
||||||
@@ -99,8 +102,9 @@ impl<A: Array<Item=u8>> ArrayString<A> {
|
|||||||
|
|
||||||
/// Adds the given string slice to the end of the string.
|
/// Adds the given string slice to the end of the string.
|
||||||
///
|
///
|
||||||
/// Returns `Ok` if the push succeeds, and returns `Err` if the
|
/// Returns `Ok` if the push succeeds.
|
||||||
/// backing array is not large enough to fit the string.
|
///
|
||||||
|
/// **Errors** if the backing array is not large enough to fit the string.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use arrayvec::ArrayString;
|
/// use arrayvec::ArrayString;
|
||||||
|
|||||||
+59
-25
@@ -155,31 +155,6 @@ impl<A: Array> ArrayVec<A> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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`.
|
/// Insert `element` in position `index`.
|
||||||
///
|
///
|
||||||
/// Shift up all elements after `index`. If any is pushed out, it is returned.
|
/// Shift up all elements after `index`. If any is pushed out, it is returned.
|
||||||
@@ -225,6 +200,31 @@ impl<A: Array> ArrayVec<A> {
|
|||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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.
|
/// Remove the element at `index` and swap the last element into its place.
|
||||||
///
|
///
|
||||||
/// This operation is O(1).
|
/// This operation is O(1).
|
||||||
@@ -277,6 +277,40 @@ impl<A: Array> ArrayVec<A> {
|
|||||||
while let Some(_) = self.pop() { }
|
while let Some(_) = self.pop() { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
/// Set the vector's length without dropping or moving out elements
|
||||||
///
|
///
|
||||||
/// May panic if `length` is greater than the capacity.
|
/// May panic if `length` is greater than the capacity.
|
||||||
|
|||||||
@@ -158,6 +158,23 @@ fn test_drain() {
|
|||||||
assert_eq!(&v[..], &[]);
|
assert_eq!(&v[..], &[]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn test_drain_oob() {
|
fn test_drain_oob() {
|
||||||
|
|||||||
Reference in New Issue
Block a user