diff --git a/src/array_string.rs b/src/array_string.rs index 51a4c16..ddf31a8 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -12,6 +12,7 @@ use std::slice; use array::{Array, ArrayExt}; use array::Index; use CapacityError; +use errors::PubCrateNew; use odds::char::encode_utf8; #[cfg(feature="serde-1")] diff --git a/src/errors.rs b/src/errors.rs index 134926a..0336216 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -10,13 +10,19 @@ pub struct CapacityError { element: T, } -impl CapacityError { - pub(crate) fn new(element: T) -> CapacityError { +pub trait PubCrateNew { + fn new(elt: T) -> Self; +} + +impl PubCrateNew for CapacityError { + fn new(element: T) -> CapacityError { CapacityError { element: element, } } +} +impl CapacityError { /// Extract the overflowing element pub fn element(self) -> T { self.element @@ -50,59 +56,18 @@ impl fmt::Debug for CapacityError { } } -pub enum InsertError { - Capacity(CapacityError), - OutOfBounds(OutOfBoundsError), -} - -impl InsertError { - fn description(&self) -> &'static str { - match *self { - InsertError::Capacity(_) => "ArrayVec is already at full capacity", - InsertError::OutOfBounds(_) => "index is out of bounds", - } - } -} - -#[cfg(feature="std")] -/// Requires `features="std"`. -impl Error for InsertError { - fn description(&self) -> &str { - self.description() - } - fn cause(&self) -> Option<&Error> { - match *self { - InsertError::Capacity(ref e) => Some(e as &Error), - InsertError::OutOfBounds(ref e) => Some(e as &Error), - } - } -} - -impl fmt::Display for InsertError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.description()) - } -} - -impl fmt::Debug for InsertError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - InsertError::Capacity(_) => write!(f, "InsertError::Capacity: ")?, - InsertError::OutOfBounds(_) => write!(f, "InsertError::OutOfBounds: ")?, - } - write!(f, "{}", self.description()) - } -} - pub struct OutOfBoundsError { _priv: () } -impl OutOfBoundsError { - pub(crate) fn new() -> Self { +impl PubCrateNew<()> for OutOfBoundsError { + fn new(_: ()) -> Self { OutOfBoundsError { _priv: () } } +} + +impl OutOfBoundsError { fn description(&self) -> &'static str { "remove index is out of bounds" } diff --git a/src/lib.rs b/src/lib.rs index 8dbbadd..c82e56a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -256,12 +256,14 @@ impl ArrayVec { /// Insert `element` in position `index`. /// - /// Shift up all elements after `index`. + /// Shift up all elements after `index`; the `index` must be less than + /// or equal to the length. /// - /// Returns an error if + /// Returns an error if: /// /// - The vector is at full capacity - /// - The index is out of bounds + /// + /// ***Panics*** `index` is out of bounds. /// /// ``` /// use arrayvec::ArrayVec; @@ -269,18 +271,17 @@ impl ArrayVec { /// let mut array = ArrayVec::<[_; 2]>::new(); /// /// assert!(array.try_insert(0, "x").is_ok()); - /// assert!(array.try_insert(3, "w").is_err()); /// assert!(array.try_insert(0, "y").is_ok()); /// assert!(array.try_insert(0, "z").is_err()); /// assert_eq!(&array[..], &["y", "x"]); /// /// ``` - pub fn try_insert(&mut self, index: usize, element: A::Item) -> Result<(), InsertError> { - if self.len() == self.capacity() { - return Err(InsertError::Capacity(CapacityError::new(element))); - } + pub fn try_insert(&mut self, index: usize, element: A::Item) -> Result<(), CapacityError> { if index > self.len() { - return Err(InsertError::OutOfBounds(OutOfBoundsError::new())); + panic!("ArrayVec::try_insert: {} is out of bounds (length is {})", index, self.len()); + } + if self.len() == self.capacity() { + return Err(CapacityError::new(element)); } let len = self.len(); @@ -368,10 +369,10 @@ impl ArrayVec { pub fn try_swap_remove(&mut self, index: usize) -> Result { let len = self.len(); if index >= len { - return Err(OutOfBoundsError::new()) + return Err(OutOfBoundsError::new(())) } self.swap(index, len - 1); - self.pop().ok_or_else(|| panic!()) + self.pop().ok_or_else(|| unreachable!()) } /// Remove the element at `index` and shift down the following elements. @@ -413,9 +414,9 @@ impl ArrayVec { /// ``` pub fn try_remove(&mut self, index: usize) -> Result { if index >= self.len() { - Err(OutOfBoundsError::new()) + Err(OutOfBoundsError::new(())) } else { - self.drain(index..index + 1).next().ok_or_else(|| panic!()) + self.drain(index..index + 1).next().ok_or_else(|| unreachable!()) } } diff --git a/tests/tests.rs b/tests/tests.rs index 0787f68..0b88576 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -4,7 +4,7 @@ extern crate arrayvec; use arrayvec::ArrayVec; use arrayvec::ArrayString; use std::mem; -use arrayvec::errors::InsertError; +use arrayvec::errors::CapacityError; use std::collections::HashMap; @@ -222,13 +222,12 @@ fn test_drop_panic_into_iter() { fn test_insert() { let mut v = ArrayVec::from([]); assert_matches!(v.try_push(1), Err(_)); - assert_matches!(v.try_insert(0, 1), Err(_)); let mut v = ArrayVec::<[_; 3]>::new(); v.insert(0, 0); v.insert(1, 1); - let ret1 = v.try_insert(3, 3); - assert_matches!(ret1, Err(InsertError::OutOfBounds(_))); + //let ret1 = v.try_insert(3, 3); + //assert_matches!(ret1, Err(InsertError::OutOfBounds(_))); assert_eq!(&v[..], &[0, 1]); v.insert(2, 2); assert_eq!(&v[..], &[0, 1, 2]); @@ -238,8 +237,9 @@ fn test_insert() { assert_matches!(ret2, Err(_)); let mut v = ArrayVec::from([2]); - assert_matches!(v.try_insert(1, 1), Err(InsertError::Capacity(_))); - assert_matches!(v.try_insert(2, 1), Err(InsertError::Capacity(_))); + assert_matches!(v.try_insert(0, 1), Err(CapacityError { .. })); + assert_matches!(v.try_insert(1, 1), Err(CapacityError { .. })); + //assert_matches!(v.try_insert(2, 1), Err(CapacityError { .. })); } #[test] @@ -386,13 +386,11 @@ fn test_insert_at_length() { assert_eq!(&v[..], &["a", "b"]); } +#[should_panic] #[test] fn test_insert_out_of_bounds() { let mut v = ArrayVec::<[_; 8]>::new(); - let result = v.try_insert(1, "test"); - assert_matches!(result, Err(_)); - assert_eq!(v.len(), 0); - + let _ = v.try_insert(1, "test"); } /*