Skip to main content

kernel/alloc/kvec/
errors.rs

1// SPDX-License-Identifier: GPL-2.0
2
3//! Errors for the [`Vec`] type.
4
5use kernel::fmt;
6use kernel::prelude::*;
7
8/// Error type for [`Vec::push_within_capacity`].
9pub struct PushError<T>(pub T);
10
11impl<T> fmt::Debug for PushError<T> {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        write!(f, "Not enough capacity")
14    }
15}
16
17impl<T> From<PushError<T>> for Error {
18    #[inline]
19    fn from(_: PushError<T>) -> Error {
20        // Returning ENOMEM isn't appropriate because the system is not out of memory. The vector
21        // is just full and we are refusing to resize it.
22        EINVAL
23    }
24}
25
26/// Error type for [`Vec::remove`].
27pub struct RemoveError;
28
29impl fmt::Debug for RemoveError {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        write!(f, "Index out of bounds")
32    }
33}
34
35impl From<RemoveError> for Error {
36    #[inline]
37    fn from(_: RemoveError) -> Error {
38        EINVAL
39    }
40}
41
42/// Error type for [`Vec::insert_within_capacity`].
43pub enum InsertError<T> {
44    /// The value could not be inserted because the index is out of bounds.
45    IndexOutOfBounds(T),
46    /// The value could not be inserted because the vector is out of capacity.
47    OutOfCapacity(T),
48}
49
50impl<T> fmt::Debug for InsertError<T> {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            InsertError::IndexOutOfBounds(_) => write!(f, "Index out of bounds"),
54            InsertError::OutOfCapacity(_) => write!(f, "Not enough capacity"),
55        }
56    }
57}
58
59impl<T> From<InsertError<T>> for Error {
60    #[inline]
61    fn from(_: InsertError<T>) -> Error {
62        EINVAL
63    }
64}