FEAT: Refactor errors. Insert is either out of bounds or capacity error
This commit is contained in:
+19
-13
@@ -51,15 +51,15 @@ impl<T> fmt::Debug for CapacityError<T> {
|
||||
}
|
||||
|
||||
pub enum InsertError<T> {
|
||||
Full(T),
|
||||
OutOfBounds,
|
||||
Capacity(CapacityError<T>),
|
||||
OutOfBounds(OutOfBoundsError),
|
||||
}
|
||||
|
||||
impl<T> InsertError<T> {
|
||||
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<T: Any> Error for InsertError<T> {
|
||||
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<T> fmt::Display for InsertError<T> {
|
||||
@@ -81,20 +87,20 @@ impl<T> fmt::Display for InsertError<T> {
|
||||
impl<T> fmt::Debug for InsertError<T> {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -277,10 +277,10 @@ impl<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
pub fn try_insert(&mut self, index: usize, element: A::Item) -> Result<(), InsertError<A::Item>> {
|
||||
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<A: Array> ArrayVec<A> {
|
||||
///
|
||||
/// assert!(array.try_swap_remove(10).is_err());
|
||||
/// ```
|
||||
pub fn try_swap_remove(&mut self, index: usize) -> Result<A::Item, RemoveError> {
|
||||
pub fn try_swap_remove(&mut self, index: usize) -> Result<A::Item, OutOfBoundsError> {
|
||||
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<A: Array> ArrayVec<A> {
|
||||
/// assert!(array.try_remove(2).is_err());
|
||||
/// assert!(array.try_remove(10).is_err());
|
||||
/// ```
|
||||
pub fn try_remove(&mut self, index: usize) -> Result<A::Item, RemoveError> {
|
||||
pub fn try_remove(&mut self, index: usize) -> Result<A::Item, OutOfBoundsError> {
|
||||
if index >= self.len() {
|
||||
Err(RemoveError::new())
|
||||
Err(OutOfBoundsError::new())
|
||||
} else {
|
||||
self.drain(index..index + 1).next().ok_or_else(|| panic!())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user