Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return a proper error for into_inner
Browse files Browse the repository at this point in the history
patrickariel committed Dec 20, 2024
1 parent 812c83a commit dcd84f5
Showing 4 changed files with 33 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
@@ -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<T, const CAP: usize> ArrayVec<T, CAP> {

/// 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()) }
}
21 changes: 21 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -47,3 +47,24 @@ impl<T> fmt::Debug for CapacityError<T> {
}
}

/// 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)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
4 changes: 2 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit dcd84f5

Please sign in to comment.