Skip to content

Commit 47827e4

Browse files
committed
FIX: Remake extend_from_slice to try_extend_from_slice
Do nothing and return an error, if the slice doesn't fit in the remaining capacity.
1 parent 8e5ff2d commit 47827e4

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

benches/extend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn extend_with_slice_fn(b: &mut Bencher) {
4545
let data = [1; 512];
4646
b.iter(|| {
4747
v.clear();
48-
black_box(v.extend_from_slice(&data));
48+
black_box(v.try_extend_from_slice(&data));
4949
v[0]
5050
});
5151
b.bytes = v.capacity() as u64;

src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -543,21 +543,22 @@ impl<A: Array> ArrayVec<A> {
543543
///
544544
/// let mut vec: ArrayVec<[usize; 10]> = ArrayVec::new();
545545
/// vec.push(1);
546-
/// vec.extend_from_slice(&[2, 3]);
546+
/// vec.try_extend_from_slice(&[2, 3]).unwrap();
547547
/// assert_eq!(&vec[..], &[1, 2, 3]);
548548
/// ```
549549
///
550-
/// # Panics
550+
/// # Errors
551551
///
552-
/// This method will panic if the capacity left (see [`remaining_capacity`]) is
553-
/// smaller then the length of the provided slice.
552+
/// This method will return an error if the capacity left (see
553+
/// [`remaining_capacity`]) is smaller then the length of the provided
554+
/// slice.
554555
///
555556
/// [`remaining_capacity`]: #method.remaining_capacity
556-
pub fn extend_from_slice(&mut self, other: &[A::Item])
557+
pub fn try_extend_from_slice(&mut self, other: &[A::Item]) -> Result<(), CapacityError>
557558
where A::Item: Copy,
558559
{
559560
if self.remaining_capacity() < other.len() {
560-
panic!("ArrayVec::extend_from_slice: slice is larger then capacity left");
561+
return Err(CapacityError::new(()));
561562
}
562563

563564
let self_len = self.len();
@@ -568,6 +569,7 @@ impl<A: Array> ArrayVec<A> {
568569
ptr::copy_nonoverlapping(other.as_ptr(), dst, other_len);
569570
self.set_len(self_len + other_len);
570571
}
572+
Ok(())
571573
}
572574

573575
/// Create a draining iterator that removes the specified range in the vector
@@ -1080,7 +1082,8 @@ impl<A: Array> Ord for ArrayVec<A> where A::Item: Ord {
10801082
impl<A: Array<Item=u8>> io::Write for ArrayVec<A> {
10811083
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
10821084
let len = cmp::min(self.remaining_capacity(), data.len());
1083-
self.extend_from_slice(&data[..len]);
1085+
let _result = self.try_extend_from_slice(&data[..len]);
1086+
debug_assert!(_result.is_ok());
10841087
Ok(len)
10851088
}
10861089
fn flush(&mut self) -> io::Result<()> { Ok(()) }

tests/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn test_capacity_left() {
4545
fn test_extend_from_slice() {
4646
let mut vec: ArrayVec<[usize; 10]> = ArrayVec::new();
4747

48-
vec.extend_from_slice(&[1, 2, 3]);
48+
vec.try_extend_from_slice(&[1, 2, 3]);
4949
assert_eq!(vec.len(), 3);
5050
assert_eq!(&vec[..], &[1, 2, 3]);
5151
assert_eq!(vec.pop(), Some(3));

0 commit comments

Comments
 (0)