Skip to content

Commit dcd84f5

Browse files
committed
Return a proper error for into_inner
1 parent 812c83a commit dcd84f5

4 files changed

Lines changed: 33 additions & 8 deletions

File tree

src/arrayvec.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::mem::MaybeUninit;
2121
use serde::{Serialize, Deserialize, Serializer, Deserializer};
2222

2323
use crate::LenUint;
24-
use crate::errors::CapacityError;
24+
use crate::errors::{CapacityError, UnderfilledError};
2525
use crate::arrayvec_impl::ArrayVecImpl;
2626
use crate::utils::MakeMaybeUninit;
2727

@@ -687,11 +687,15 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
687687

688688
/// Return the inner fixed size array, if it is full to its capacity.
689689
///
690-
/// Return an `Ok` value with the array if length equals capacity,
691-
/// return an `Err` with self otherwise.
692-
pub fn into_inner(self) -> Result<[T; CAP], Self> {
690+
/// # Errors
691+
///
692+
/// This method will return an error if the array is not filled to its
693+
/// capacity (see [`capacity`]).
694+
///
695+
/// [`capacity`]: #method.capacity
696+
pub fn into_inner(self) -> Result<[T; CAP], UnderfilledError> {
693697
if self.len() < self.capacity() {
694-
Err(self)
698+
Err(UnderfilledError::new(self.capacity(), self.len()))
695699
} else {
696700
unsafe { Ok(self.into_inner_unchecked()) }
697701
}

src/errors.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,24 @@ impl<T> fmt::Debug for CapacityError<T> {
4747
}
4848
}
4949

50+
/// 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+
}
56+
57+
impl UnderfilledError {
58+
pub const fn new(capacity: usize, len: usize) -> Self {
59+
Self { capacity, len }
60+
}
61+
}
62+
63+
#[cfg(feature="std")]
64+
impl Error for UnderfilledError {}
65+
66+
impl fmt::Display for UnderfilledError {
67+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68+
write!(f, "capacity is not filled: expected {}, got {}", self.capacity, self.len)
69+
}
70+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ mod errors;
6565
mod utils;
6666

6767
pub use crate::array_string::ArrayString;
68-
pub use crate::errors::CapacityError;
68+
pub use crate::errors::{CapacityError, UnderfilledError};
6969

7070
pub use crate::arrayvec::{ArrayVec, IntoIter, Drain};

tests/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern crate arrayvec;
33

44
use arrayvec::ArrayVec;
55
use arrayvec::ArrayString;
6+
use arrayvec::UnderfilledError;
67
use std::mem;
78
use arrayvec::CapacityError;
89

@@ -455,8 +456,7 @@ fn test_insert() {
455456
fn test_into_inner_1() {
456457
let mut v = ArrayVec::from([1, 2]);
457458
v.pop();
458-
let u = v.clone();
459-
assert_eq!(v.into_inner(), Err(u));
459+
assert_eq!(v.into_inner(), Err(UnderfilledError::new(2, 1)));
460460
}
461461

462462
#[test]

0 commit comments

Comments
 (0)