Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,48 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
ArrayVecImpl::clear(self)
}

/// Resizes the `ArrayVec` in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `ArrayVec` is extended by
/// the difference, with each additional slot filled with the result of
/// calling the closure `f`. The return values from `f` will end up in the
/// `ArrayVec` in the order they have been generated.
///
/// If `new_len` is less than or equal to `len`, the `ArrayVec` is simply
/// truncated.
///
/// This method uses a closure to create new values on every push. If
/// you'd rather [`Clone`] a given value, use [`ArrayVec::resize`]. If
/// you want to use the [`Default`] to generate values, you can pass
/// [`Default::default`] as the second argument.
///
/// ***Panics*** if extending the vector exceeds its capacity.
///
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut vec: ArrayVec<_, 5> = (1..=3).collect();
/// vec.resize_with(5, Default::default);
/// assert_eq!(&vec[..], &[1, 2, 3, 0, 0]);
///
/// let mut vec = ArrayVec::<_, 4>::new();
/// let mut p = 1;
/// vec.resize_with(4, || { p *= 2; p });
/// assert_eq!(&vec[..], &[2, 4, 8, 16]);
/// ```
#[track_caller]
pub fn resize_with<F>(&mut self, new_len: usize, f: F)
where
F: FnMut() -> T,
{
let old_len = self.len();

if new_len > old_len {
self.extend(iter::repeat_with(f).take(new_len - old_len));
} else if old_len > new_len {
self.truncate(new_len);
}
}

/// Get pointer to where element at `index` would be
unsafe fn get_unchecked_ptr(&mut self, index: usize) -> *mut T {
Expand Down Expand Up @@ -804,6 +846,52 @@ impl<T, const CAP: usize> From<[T; CAP]> for ArrayVec<T, CAP> {
}
}

impl<T, const CAP: usize> ArrayVec<T, CAP>
where
T: Clone,
{
/// Resizes the `ArrayVec` in-place so that `len` is equal to `new_len`.
///
/// If `new_len` is greater than `len`, the `ArrayVec` is extended by the
/// difference, with each additional slot filled with `value`. If `new_len`
/// is less than or equal to `len`, the `ArrayVec` is simply truncated.
///
/// This method requires T to implement Clone, in order to be able to clone the
/// passed value. If you need more flexibility (or want to rely on Default instead
/// of Clone), use Vec::resize_with. If you only need to resize to a smaller size,
/// use Vec::truncate.
///
/// This method uses a closure to create new values on every push. If
/// you'd rather [`Clone`] a given value, use [`ArrayVec::resize`]. If
/// you want to use the [`Default`] to generate values, you can pass
/// [`Default::default`] as the second argument.
///
/// ***Panics*** if extending the vector exceeds its capacity.
///
///
/// ```
/// use arrayvec::ArrayVec;
///
/// let mut vec: ArrayVec<_, 3> = ArrayVec::new();
/// vec.push('a');
/// vec.resize(3, 'b');
/// assert_eq!(&vec[..], &['a', 'b', 'b']);
///
/// let mut vec = ArrayVec::from(['a', 'b', 'c', 'd']);
/// vec.resize(2, 'X');
/// assert_eq!(&vec[..], &['a', 'b']);
/// ```
#[track_caller]
pub fn resize(&mut self, new_len: usize, value: T) {
let old_len = self.len();

if new_len > old_len {
self.extend(iter::repeat(value).take(new_len - old_len));
} else if old_len > new_len {
self.truncate(new_len);
}
}
}

/// Try to create an `ArrayVec` from a slice. This will return an error if the slice was too big to
/// fit.
Expand Down
32 changes: 32 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,40 @@ fn test_drop_panics() {
// Check that all the tail elements drop, even if the first drop panics.
assert_eq!(flag.get(), tail_len as i32);
}
}

#[test]
fn test_resize() {
let mut v: ArrayVec<i32, 8> = ArrayVec::new();
v.push(1);
v.resize(5, 0);
assert_eq!(&v[..], &[1, 0, 0, 0, 0]);

v.resize(2, -1);
assert_eq!(&v[..], &[1, 0]);
}

#[test]
fn test_resize_with() {
let mut v: ArrayVec<i32, 8> = (1..=3).collect();
v.resize_with(5, Default::default);
assert_eq!(&v[..], &[1, 2, 3, 0, 0]);

let mut v: ArrayVec<i32, 8> = ArrayVec::new();
let mut p = 1;
v.resize_with(4, || {
p *= 2;
p
});
assert_eq!(&v[..], &[2, 4, 8, 16]);
}

#[should_panic]
#[test]
fn test_resize_capacity_panic() {
let mut v: ArrayVec<i32, 2> = ArrayVec::new();
v.push(1);
v.resize(3, 0);
}

#[test]
Expand Down
Loading