projects/linux·blob 6f7e6393 · refs

61 lines · 1.6 KiB

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 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`].
26pub struct RemoveError;
27
28impl fmt::Debug for RemoveError {
29 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30 write!(f, "Index out of bounds")
31 }
32}
33
34impl From<RemoveError> for Error {
35 fn from(_: RemoveError) -> Error {
36 EINVAL
37 }
38}
39
40/// Error type for [`Vec::insert_within_capacity`].
41pub 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
48impl<T> fmt::Debug for InsertError<T> {
49 fn fmt(&self, f: &mut fmt::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
57impl<T> From<InsertError<T>> for Error {
58 fn from(_: InsertError<T>) -> Error {
59 EINVAL
60 }
61}