From 345dd339426a3280a429846ccb9c2122f9e38c32 Mon Sep 17 00:00:00 2001 From: bluss Date: Sun, 30 Jul 2017 15:09:02 +0200 Subject: [PATCH] FEAT: Refactor errors. Insert is either out of bounds or capacity error --- src/errors.rs | 32 +++++++++++++++++++------------- src/lib.rs | 12 ++++++------ tests/tests.rs | 10 ++++++---- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 168f37b..134926a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -51,15 +51,15 @@ impl fmt::Debug for CapacityError { } pub enum InsertError { - Full(T), - OutOfBounds, + Capacity(CapacityError), + OutOfBounds(OutOfBoundsError), } impl InsertError { fn description(&self) -> &'static str { match *self { - InsertError::Full(_) => "ArrayVec is already at full capacity", - InsertError::OutOfBounds => "index is out of bounds", + InsertError::Capacity(_) => "ArrayVec is already at full capacity", + InsertError::OutOfBounds(_) => "index is out of bounds", } } } @@ -70,6 +70,12 @@ 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 { @@ -81,20 +87,20 @@ impl fmt::Display for InsertError { impl fmt::Debug for InsertError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - InsertError::Full(_) => write!(f, "InsertError::Full: ")?, - InsertError::OutOfBounds => write!(f, "InsertError::OutOfBounds: ")?, + InsertError::Capacity(_) => write!(f, "InsertError::Capacity: ")?, + InsertError::OutOfBounds(_) => write!(f, "InsertError::OutOfBounds: ")?, } write!(f, "{}", self.description()) } } -pub struct RemoveError { +pub struct OutOfBoundsError { _priv: () } -impl RemoveError { +impl OutOfBoundsError { pub(crate) fn new() -> Self { - RemoveError { _priv: () } + OutOfBoundsError { _priv: () } } fn description(&self) -> &'static str { @@ -104,20 +110,20 @@ impl RemoveError { #[cfg(feature="std")] /// Requires `features="std"`. -impl Error for RemoveError { +impl Error for OutOfBoundsError { fn description(&self) -> &str { self.description() } } -impl fmt::Display for RemoveError { +impl fmt::Display for OutOfBoundsError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.description()) } } -impl fmt::Debug for RemoveError { +impl fmt::Debug for OutOfBoundsError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "RemoveError: {}", self.description()) + write!(f, "OutOfBoundsError: {}", self.description()) } } diff --git a/src/lib.rs b/src/lib.rs index 30d8c9d..8dbbadd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -277,10 +277,10 @@ impl ArrayVec { /// ``` pub fn try_insert(&mut self, index: usize, element: A::Item) -> Result<(), InsertError> { if self.len() == self.capacity() { - return Err(InsertError::Full(element)); + return Err(InsertError::Capacity(CapacityError::new(element))); } if index > self.len() { - return Err(InsertError::OutOfBounds); + return Err(InsertError::OutOfBounds(OutOfBoundsError::new())); } let len = self.len(); @@ -365,10 +365,10 @@ impl ArrayVec { /// /// assert!(array.try_swap_remove(10).is_err()); /// ``` - pub fn try_swap_remove(&mut self, index: usize) -> Result { + pub fn try_swap_remove(&mut self, index: usize) -> Result { let len = self.len(); if index >= len { - return Err(RemoveError::new()) + return Err(OutOfBoundsError::new()) } self.swap(index, len - 1); self.pop().ok_or_else(|| panic!()) @@ -411,9 +411,9 @@ impl ArrayVec { /// assert!(array.try_remove(2).is_err()); /// assert!(array.try_remove(10).is_err()); /// ``` - pub fn try_remove(&mut self, index: usize) -> Result { + pub fn try_remove(&mut self, index: usize) -> Result { if index >= self.len() { - Err(RemoveError::new()) + Err(OutOfBoundsError::new()) } else { self.drain(index..index + 1).next().ok_or_else(|| panic!()) } diff --git a/tests/tests.rs b/tests/tests.rs index 8e73a90..19b95c2 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -4,6 +4,7 @@ extern crate arrayvec; use arrayvec::ArrayVec; use arrayvec::ArrayString; use std::mem; +use arrayvec::errors::InsertError; use std::collections::HashMap; @@ -226,18 +227,19 @@ fn test_insert() { let mut v = ArrayVec::<[_; 3]>::new(); v.insert(0, 0); v.insert(1, 1); - v.insert(2, 2); 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]); - assert_matches!(ret1, Err(_)); let ret2 = v.try_insert(1, 9); assert_eq!(&v[..], &[0, 1, 2]); assert_matches!(ret2, Err(_)); let mut v = ArrayVec::from([2]); - assert_matches!(v.try_insert(1, 1), Err(_)); - assert_matches!(v.try_insert(2, 1), Err(_)); + assert_matches!(v.try_insert(1, 1), Err(InsertError::Capacity(_))); + assert_matches!(v.try_insert(2, 1), Err(InsertError::Capacity(_))); } #[test]