FEAT: Remove InsertError again
try_insert has a capacity error, but panics if the index is out of bounds.
This commit is contained in:
@@ -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")]
|
||||
|
||||
+13
-48
@@ -10,13 +10,19 @@ pub struct CapacityError<T = ()> {
|
||||
element: T,
|
||||
}
|
||||
|
||||
impl<T> CapacityError<T> {
|
||||
pub(crate) fn new(element: T) -> CapacityError<T> {
|
||||
pub trait PubCrateNew<T> {
|
||||
fn new(elt: T) -> Self;
|
||||
}
|
||||
|
||||
impl<T> PubCrateNew<T> for CapacityError<T> {
|
||||
fn new(element: T) -> CapacityError<T> {
|
||||
CapacityError {
|
||||
element: element,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> CapacityError<T> {
|
||||
/// Extract the overflowing element
|
||||
pub fn element(self) -> T {
|
||||
self.element
|
||||
@@ -50,59 +56,18 @@ impl<T> fmt::Debug for CapacityError<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum InsertError<T> {
|
||||
Capacity(CapacityError<T>),
|
||||
OutOfBounds(OutOfBoundsError),
|
||||
}
|
||||
|
||||
impl<T> InsertError<T> {
|
||||
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<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> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.description())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for InsertError<T> {
|
||||
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"
|
||||
}
|
||||
|
||||
+14
-13
@@ -256,12 +256,14 @@ impl<A: Array> ArrayVec<A> {
|
||||
|
||||
/// 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<A: Array> ArrayVec<A> {
|
||||
/// 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<A::Item>> {
|
||||
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<A::Item>> {
|
||||
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<A: Array> ArrayVec<A> {
|
||||
pub fn try_swap_remove(&mut self, index: usize) -> Result<A::Item, OutOfBoundsError> {
|
||||
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<A: Array> ArrayVec<A> {
|
||||
/// ```
|
||||
pub fn try_remove(&mut self, index: usize) -> Result<A::Item, OutOfBoundsError> {
|
||||
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!())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user