From dadeedb787a8a2870bb88930143e787ee95c8bd8 Mon Sep 17 00:00:00 2001 From: bluss Date: Sun, 30 Jul 2017 13:13:52 +0200 Subject: [PATCH] FEAT: Proper Error impls for errors Also use pub(crate), requiring Rust 1.18 --- src/errors.rs | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 86 +---------------------------------- 2 files changed, 125 insertions(+), 84 deletions(-) create mode 100644 src/errors.rs diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..168f37b --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,123 @@ +use std::fmt; +#[cfg(feature="std")] +use std::any::Any; +#[cfg(feature="std")] +use std::error::Error; + +/// Error value indicating insufficient capacity +#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] +pub struct CapacityError { + element: T, +} + +impl CapacityError { + pub(crate) fn new(element: T) -> CapacityError { + CapacityError { + element: element, + } + } + + /// Extract the overflowing element + pub fn element(self) -> T { + self.element + } + + /// Convert into a `CapacityError` that does not carry an element. + pub fn simplify(self) -> CapacityError { + CapacityError { element: () } + } +} + +const CAPERROR: &'static str = "insufficient capacity"; + +#[cfg(feature="std")] +/// Requires `features="std"`. +impl Error for CapacityError { + fn description(&self) -> &str { + CAPERROR + } +} + +impl fmt::Display for CapacityError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", CAPERROR) + } +} + +impl fmt::Debug for CapacityError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}: {}", "CapacityError", CAPERROR) + } +} + +pub enum InsertError { + Full(T), + OutOfBounds, +} + +impl InsertError { + fn description(&self) -> &'static str { + match *self { + InsertError::Full(_) => "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() + } +} + +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::Full(_) => write!(f, "InsertError::Full: ")?, + InsertError::OutOfBounds => write!(f, "InsertError::OutOfBounds: ")?, + } + write!(f, "{}", self.description()) + } +} + +pub struct RemoveError { + _priv: () +} + +impl RemoveError { + pub(crate) fn new() -> Self { + RemoveError { _priv: () } + } + + fn description(&self) -> &'static str { + "remove index is out of bounds" + } +} + +#[cfg(feature="std")] +/// Requires `features="std"`. +impl Error for RemoveError { + fn description(&self) -> &str { + self.description() + } +} + +impl fmt::Display for RemoveError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.description()) + } +} + +impl fmt::Debug for RemoveError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "RemoveError: {}", self.description()) + } +} diff --git a/src/lib.rs b/src/lib.rs index e7fe863..61b8975 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,10 +41,6 @@ use std::fmt; #[cfg(feature="std")] use std::io; -#[cfg(feature="std")] -use std::error::Error; -#[cfg(feature="std")] -use std::any::Any; // core but unused use nodrop::NoDrop; @@ -54,11 +50,13 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer}; mod array; mod array_string; mod range; +pub mod errors; pub use array::Array; pub use range::RangeArgument; use array::Index; pub use array_string::ArrayString; +use errors::*; unsafe fn new_array() -> A { @@ -944,83 +942,3 @@ impl<'de, T: Deserialize<'de>, A: Array> Deserialize<'de> for ArrayVec(PhantomData)) } } - -/// Error value indicating insufficient capacity -#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] -pub struct CapacityError { - element: T, -} - -impl CapacityError { - fn new(element: T) -> CapacityError { - CapacityError { - element: element, - } - } - - /// Extract the overflowing element - pub fn element(self) -> T { - self.element - } - - /// Convert into a `CapacityError` that does not carry an element. - pub fn simplify(self) -> CapacityError { - CapacityError { element: () } - } -} - -const CAPERROR: &'static str = "insufficient capacity"; - -#[cfg(feature="std")] -/// Requires `features="std"`. -impl Error for CapacityError { - fn description(&self) -> &str { - CAPERROR - } -} - -impl fmt::Display for CapacityError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", CAPERROR) - } -} - -impl fmt::Debug for CapacityError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}: {}", "CapacityError", CAPERROR) - } -} - -pub enum InsertError { - Full(T), - OutOfBounds, -} - -impl InsertError { - fn description(&self) -> &'static str { - match *self { - InsertError::Full(_) => "ArrayVec is already at full capacity", - InsertError::OutOfBounds => "index is out of bounds", - } - } -} - -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: ")?, - } - write!(f, "{}", self.description()) - } -} - -#[derive(Debug)] -pub struct RemoveError { -} - -impl RemoveError { - pub fn new() -> Self { - RemoveError { } - } -}