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 1/3] 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] From 09b6854ccb8ab0a452c3dba03c68bcdc8395fa04 Mon Sep 17 00:00:00 2001 From: patrickariel <161032380+patrickariel@users.noreply.github.com> Date: Sat, 21 Dec 2024 00:35:41 +0700 Subject: [PATCH 2/3] Store original ArrayVec inside UnderfilledError --- src/arrayvec.rs | 4 ++-- src/errors.rs | 25 ++++++++++++++----------- tests/tests.rs | 3 ++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/arrayvec.rs b/src/arrayvec.rs index 5a69d0b..68ea8ed 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -693,9 +693,9 @@ impl ArrayVec { /// capacity (see [`capacity`]). /// /// [`capacity`]: #method.capacity - pub fn into_inner(self) -> Result<[T; CAP], UnderfilledError> { + pub fn into_inner(self) -> Result<[T; CAP], UnderfilledError> { if self.len() < self.capacity() { - Err(UnderfilledError::new(self.capacity(), self.len())) + Err(UnderfilledError::new(self)) } else { unsafe { Ok(self.into_inner_unchecked()) } } diff --git a/src/errors.rs b/src/errors.rs index 8389279..ad8d3a2 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 { @@ -48,23 +50,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, -} +#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] +pub struct UnderfilledError(ArrayVec); + +impl UnderfilledError { + pub const fn new(inner: ArrayVec) -> Self { + Self(inner) + } -impl UnderfilledError { - pub const fn new(capacity: usize, len: usize) -> Self { - Self { capacity, len } + pub fn take_vec(self) -> ArrayVec { + self.0 } } #[cfg(feature="std")] -impl Error for UnderfilledError {} +impl Error for UnderfilledError {} -impl fmt::Display 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) + write!(f, "capacity is not filled: expected {}, got {}", CAP, self.0.len()) } } \ No newline at end of file diff --git a/tests/tests.rs b/tests/tests.rs index 7c43d5d..4cd1326 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -456,7 +456,8 @@ fn test_insert() { fn test_into_inner_1() { let mut v = ArrayVec::from([1, 2]); v.pop(); - assert_eq!(v.into_inner(), Err(UnderfilledError::new(2, 1))); + let u = v.clone(); + assert_eq!(v.into_inner(), Err(UnderfilledError::new(u))); } #[test] From 921bebcba0232cb1100535337a63bc42c280a9aa Mon Sep 17 00:00:00 2001 From: patrickariel <161032380+patrickariel@users.noreply.github.com> Date: Sat, 21 Dec 2024 00:44:58 +0700 Subject: [PATCH 3/3] Implement Debug manually for UnderfilledError --- src/errors.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index ad8d3a2..6cffaaa 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -50,7 +50,7 @@ impl fmt::Debug for CapacityError { } /// Error value indicating that capacity is not completely filled -#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)] pub struct UnderfilledError(ArrayVec); impl UnderfilledError { @@ -63,11 +63,27 @@ impl UnderfilledError { } } +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 Error for UnderfilledError {} -impl fmt::Display 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()) + write!( + f, + "capacity is not filled: expected {}, got {}", + CAP, + self.0.len() + ) } } \ No newline at end of file