Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a proper error for into_inner #284

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<T, CAP>> {
if self.len() < self.capacity() {
Err(self)
Err(UnderfilledError::new(self))
} else {
unsafe { Ok(self.into_inner_unchecked()) }
}
Expand Down
40 changes: 40 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = ()> {
Expand Down Expand Up @@ -47,3 +49,41 @@ impl<T> fmt::Debug for CapacityError<T> {
}
}

/// Error value indicating that capacity is not completely filled
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)]
pub struct UnderfilledError<T, const CAP: usize>(ArrayVec<T, CAP>);

impl<T, const CAP: usize> UnderfilledError<T, CAP> {
pub const fn new(inner: ArrayVec<T, CAP>) -> Self {
Self(inner)
}

pub fn take_vec(self) -> ArrayVec<T, CAP> {
self.0
}
}

impl<T, const CAP: usize> fmt::Debug for UnderfilledError<T, CAP> {
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<T, const CAP: usize> Error for UnderfilledError<T, CAP> {}

impl<T, const CAP: usize> fmt::Display for UnderfilledError<T, CAP> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"capacity is not filled: expected {}, got {}",
CAP,
self.0.len()
)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
3 changes: 2 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate arrayvec;

use arrayvec::ArrayVec;
use arrayvec::ArrayString;
use arrayvec::UnderfilledError;
use std::mem;
use arrayvec::CapacityError;

Expand Down Expand Up @@ -456,7 +457,7 @@ 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(u)));
}

#[test]
Expand Down
Loading