From dcd84f51afd8b7fed987304c1c13db36806e4a59 Mon Sep 17 00:00:00 2001 From: patrickariel <161032380+patrickariel@users.noreply.github.com> Date: Fri, 20 Dec 2024 23:16:51 +0700 Subject: [PATCH] Return a proper error for into_inner --- src/arrayvec.rs | 14 +++++++++----- src/errors.rs | 21 +++++++++++++++++++++ src/lib.rs | 2 +- tests/tests.rs | 4 ++-- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/arrayvec.rs b/src/arrayvec.rs index e5ea52d..5a69d0b 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.capacity(), self.len())) } else { unsafe { Ok(self.into_inner_unchecked()) } } diff --git a/src/errors.rs b/src/errors.rs index 7ca3ebc..8389279 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -47,3 +47,24 @@ impl fmt::Debug for CapacityError { } } +/// Error value indicating that capacity is not completely filled +#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] +pub struct UnderfilledError { + capacity: usize, + len: usize, +} + +impl UnderfilledError { + pub const fn new(capacity: usize, len: usize) -> Self { + Self { capacity, 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 {}", self.capacity, self.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..7c43d5d 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; @@ -455,8 +456,7 @@ fn test_insert() { 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(2, 1))); } #[test]