Skip to content

Commit 09b6854

Browse files
committed
Store original ArrayVec inside UnderfilledError
1 parent dcd84f5 commit 09b6854

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/arrayvec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,9 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
693693
/// capacity (see [`capacity`]).
694694
///
695695
/// [`capacity`]: #method.capacity
696-
pub fn into_inner(self) -> Result<[T; CAP], UnderfilledError> {
696+
pub fn into_inner(self) -> Result<[T; CAP], UnderfilledError<T, CAP>> {
697697
if self.len() < self.capacity() {
698-
Err(UnderfilledError::new(self.capacity(), self.len()))
698+
Err(UnderfilledError::new(self))
699699
} else {
700700
unsafe { Ok(self.into_inner_unchecked()) }
701701
}

src/errors.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::any::Any;
44
#[cfg(feature="std")]
55
use std::error::Error;
66

7+
use crate::ArrayVec;
8+
79
/// Error value indicating insufficient capacity
810
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
911
pub struct CapacityError<T = ()> {
@@ -48,23 +50,24 @@ impl<T> fmt::Debug for CapacityError<T> {
4850
}
4951

5052
/// Error value indicating that capacity is not completely filled
51-
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
52-
pub struct UnderfilledError {
53-
capacity: usize,
54-
len: usize,
55-
}
53+
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
54+
pub struct UnderfilledError<T, const CAP: usize>(ArrayVec<T, CAP>);
55+
56+
impl<T, const CAP: usize> UnderfilledError<T, CAP> {
57+
pub const fn new(inner: ArrayVec<T, CAP>) -> Self {
58+
Self(inner)
59+
}
5660

57-
impl UnderfilledError {
58-
pub const fn new(capacity: usize, len: usize) -> Self {
59-
Self { capacity, len }
61+
pub fn take_vec(self) -> ArrayVec<T, CAP> {
62+
self.0
6063
}
6164
}
6265

6366
#[cfg(feature="std")]
64-
impl Error for UnderfilledError {}
67+
impl<T: fmt::Debug, const CAP: usize> Error for UnderfilledError<T, CAP> {}
6568

66-
impl fmt::Display for UnderfilledError {
69+
impl<T, const CAP: usize> fmt::Display for UnderfilledError<T, CAP> {
6770
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68-
write!(f, "capacity is not filled: expected {}, got {}", self.capacity, self.len)
71+
write!(f, "capacity is not filled: expected {}, got {}", CAP, self.0.len())
6972
}
7073
}

tests/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ fn test_insert() {
456456
fn test_into_inner_1() {
457457
let mut v = ArrayVec::from([1, 2]);
458458
v.pop();
459-
assert_eq!(v.into_inner(), Err(UnderfilledError::new(2, 1)));
459+
let u = v.clone();
460+
assert_eq!(v.into_inner(), Err(UnderfilledError::new(u)));
460461
}
461462

462463
#[test]

0 commit comments

Comments
 (0)