xref: /linux/rust/kernel/alloc/kvec/errors.rs (revision 26ff969926a08eee069767ddbbbc301adbcd9676)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Errors for the [`Vec`] type.
4 
5 use kernel::fmt;
6 use kernel::prelude::*;
7 
8 /// Error type for [`Vec::push_within_capacity`].
9 pub struct PushError<T>(pub T);
10 
11 impl<T> fmt::Debug for PushError<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result12     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13         write!(f, "Not enough capacity")
14     }
15 }
16 
17 impl<T> From<PushError<T>> for Error {
18     #[inline]
from(_: PushError<T>) -> Error19     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`].
27 pub struct RemoveError;
28 
29 impl fmt::Debug for RemoveError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result30     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31         write!(f, "Index out of bounds")
32     }
33 }
34 
35 impl From<RemoveError> for Error {
36     #[inline]
from(_: RemoveError) -> Error37     fn from(_: RemoveError) -> Error {
38         EINVAL
39     }
40 }
41 
42 /// Error type for [`Vec::insert_within_capacity`].
43 pub 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 
50 impl<T> fmt::Debug for InsertError<T> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result51     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 
59 impl<T> From<InsertError<T>> for Error {
60     #[inline]
from(_: InsertError<T>) -> Error61     fn from(_: InsertError<T>) -> Error {
62         EINVAL
63     }
64 }
65