diff --git a/src/arrayvec.rs b/src/arrayvec.rs index e5ea52d..68ea8ed 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -21,7 +21,7 @@ use std::mem::MaybeUninit; use serde::{Serialize, Deserialize, Serializer, Deserializer}; use crate::LenUint; -use crate::errors::CapacityError; +use crate::errors::{CapacityError, UnderfilledError}; use crate::arrayvec_impl::ArrayVecImpl; use crate::utils::MakeMaybeUninit; @@ -687,11 +687,15 @@ impl ArrayVec { /// Return the inner fixed size array, if it is full to its capacity. /// - /// Return an `Ok` value with the array if length equals capacity, - /// return an `Err` with self otherwise. - pub fn into_inner(self) -> Result<[T; CAP], Self> { + /// # Errors + /// + /// This method will return an error if the array is not filled to its + /// capacity (see [`capacity`]). + /// + /// [`capacity`]: #method.capacity + pub fn into_inner(self) -> Result<[T; CAP], UnderfilledError> { if self.len() < self.capacity() { - Err(self) + Err(UnderfilledError::new(self)) } else { unsafe { Ok(self.into_inner_unchecked()) } } diff --git a/src/errors.rs b/src/errors.rs index 7ca3ebc..6cffaaa 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -4,6 +4,8 @@ use std::any::Any; #[cfg(feature="std")] use std::error::Error; +use crate::ArrayVec; + /// Error value indicating insufficient capacity #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)] pub struct CapacityError { @@ -47,3 +49,41 @@ impl fmt::Debug for CapacityError { } } +/// Error value indicating that capacity is not completely filled +#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)] +pub struct UnderfilledError(ArrayVec); + +impl UnderfilledError { + pub const fn new(inner: ArrayVec) -> Self { + Self(inner) + } + + pub fn take_vec(self) -> ArrayVec { + self.0 + } +} + +impl fmt::Debug for UnderfilledError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!( + f, + "UnderfilledError: capacity is not filled: expected {}, got {}", + CAP, + self.0.len() + ) + } +} + +#[cfg(feature="std")] +impl Error for UnderfilledError {} + +impl fmt::Display for UnderfilledError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "capacity is not filled: expected {}, got {}", + CAP, + self.0.len() + ) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 5c4bcee..8aba9c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,6 +65,6 @@ mod errors; mod utils; pub use crate::array_string::ArrayString; -pub use crate::errors::CapacityError; +pub use crate::errors::{CapacityError, UnderfilledError}; pub use crate::arrayvec::{ArrayVec, IntoIter, Drain}; diff --git a/tests/tests.rs b/tests/tests.rs index ff779ba..4cd1326 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -3,6 +3,7 @@ extern crate arrayvec; use arrayvec::ArrayVec; use arrayvec::ArrayString; +use arrayvec::UnderfilledError; use std::mem; use arrayvec::CapacityError; @@ -456,7 +457,7 @@ fn test_into_inner_1() { let mut v = ArrayVec::from([1, 2]); v.pop(); let u = v.clone(); - assert_eq!(v.into_inner(), Err(u)); + assert_eq!(v.into_inner(), Err(UnderfilledError::new(u))); } #[test]