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