@@ -543,21 +543,22 @@ impl<A: Array> ArrayVec<A> {
543
543
///
544
544
/// let mut vec: ArrayVec<[usize; 10]> = ArrayVec::new();
545
545
/// vec.push(1);
546
- /// vec.extend_from_slice (&[2, 3]);
546
+ /// vec.try_extend_from_slice (&[2, 3]).unwrap( );
547
547
/// assert_eq!(&vec[..], &[1, 2, 3]);
548
548
/// ```
549
549
///
550
- /// # Panics
550
+ /// # Errors
551
551
///
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.
554
555
///
555
556
/// [`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 >
557
558
where A :: Item : Copy ,
558
559
{
559
560
if self . remaining_capacity ( ) < other. len ( ) {
560
- panic ! ( "ArrayVec::extend_from_slice: slice is larger then capacity left" ) ;
561
+ return Err ( CapacityError :: new ( ( ) ) ) ;
561
562
}
562
563
563
564
let self_len = self . len ( ) ;
@@ -568,6 +569,7 @@ impl<A: Array> ArrayVec<A> {
568
569
ptr:: copy_nonoverlapping ( other. as_ptr ( ) , dst, other_len) ;
569
570
self . set_len ( self_len + other_len) ;
570
571
}
572
+ Ok ( ( ) )
571
573
}
572
574
573
575
/// 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 {
1080
1082
impl < A : Array < Item =u8 > > io:: Write for ArrayVec < A > {
1081
1083
fn write ( & mut self , data : & [ u8 ] ) -> io:: Result < usize > {
1082
1084
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( ) ) ;
1084
1087
Ok ( len)
1085
1088
}
1086
1089
fn flush ( & mut self ) -> io:: Result < ( ) > { Ok ( ( ) ) }
0 commit comments