xref: /linux/rust/kernel/alloc/kvec/errors.rs (revision ec7714e4947909190ffb3041a03311a975350fe0)
19def0d0aSAlice Ryhl // SPDX-License-Identifier: GPL-2.0
29def0d0aSAlice Ryhl 
39def0d0aSAlice Ryhl //! Errors for the [`Vec`] type.
49def0d0aSAlice Ryhl 
59def0d0aSAlice Ryhl use core::fmt::{self, Debug, Formatter};
69def0d0aSAlice Ryhl use kernel::prelude::*;
79def0d0aSAlice Ryhl 
89def0d0aSAlice Ryhl /// Error type for [`Vec::push_within_capacity`].
99def0d0aSAlice Ryhl pub struct PushError<T>(pub T);
109def0d0aSAlice Ryhl 
119def0d0aSAlice Ryhl impl<T> Debug for PushError<T> {
fmt(&self, f: &mut Formatter<'_>) -> fmt::Result129def0d0aSAlice Ryhl     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
139def0d0aSAlice Ryhl         write!(f, "Not enough capacity")
149def0d0aSAlice Ryhl     }
159def0d0aSAlice Ryhl }
169def0d0aSAlice Ryhl 
179def0d0aSAlice Ryhl impl<T> From<PushError<T>> for Error {
from(_: PushError<T>) -> Error189def0d0aSAlice Ryhl     fn from(_: PushError<T>) -> Error {
199def0d0aSAlice Ryhl         // Returning ENOMEM isn't appropriate because the system is not out of memory. The vector
209def0d0aSAlice Ryhl         // is just full and we are refusing to resize it.
219def0d0aSAlice Ryhl         EINVAL
229def0d0aSAlice Ryhl     }
239def0d0aSAlice Ryhl }
24294a7ecbSAlice Ryhl 
25294a7ecbSAlice Ryhl /// Error type for [`Vec::remove`].
26294a7ecbSAlice Ryhl pub struct RemoveError;
27294a7ecbSAlice Ryhl 
28294a7ecbSAlice Ryhl impl Debug for RemoveError {
fmt(&self, f: &mut Formatter<'_>) -> fmt::Result29294a7ecbSAlice Ryhl     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
30294a7ecbSAlice Ryhl         write!(f, "Index out of bounds")
31294a7ecbSAlice Ryhl     }
32294a7ecbSAlice Ryhl }
33294a7ecbSAlice Ryhl 
34294a7ecbSAlice Ryhl impl From<RemoveError> for Error {
from(_: RemoveError) -> Error35294a7ecbSAlice Ryhl     fn from(_: RemoveError) -> Error {
36294a7ecbSAlice Ryhl         EINVAL
37294a7ecbSAlice Ryhl     }
38294a7ecbSAlice Ryhl }
39*771c5a7dSAlice Ryhl 
40*771c5a7dSAlice Ryhl /// Error type for [`Vec::insert_within_capacity`].
41*771c5a7dSAlice Ryhl pub enum InsertError<T> {
42*771c5a7dSAlice Ryhl     /// The value could not be inserted because the index is out of bounds.
43*771c5a7dSAlice Ryhl     IndexOutOfBounds(T),
44*771c5a7dSAlice Ryhl     /// The value could not be inserted because the vector is out of capacity.
45*771c5a7dSAlice Ryhl     OutOfCapacity(T),
46*771c5a7dSAlice Ryhl }
47*771c5a7dSAlice Ryhl 
48*771c5a7dSAlice Ryhl impl<T> Debug for InsertError<T> {
fmt(&self, f: &mut Formatter<'_>) -> fmt::Result49*771c5a7dSAlice Ryhl     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
50*771c5a7dSAlice Ryhl         match self {
51*771c5a7dSAlice Ryhl             InsertError::IndexOutOfBounds(_) => write!(f, "Index out of bounds"),
52*771c5a7dSAlice Ryhl             InsertError::OutOfCapacity(_) => write!(f, "Not enough capacity"),
53*771c5a7dSAlice Ryhl         }
54*771c5a7dSAlice Ryhl     }
55*771c5a7dSAlice Ryhl }
56*771c5a7dSAlice Ryhl 
57*771c5a7dSAlice Ryhl impl<T> From<InsertError<T>> for Error {
from(_: InsertError<T>) -> Error58*771c5a7dSAlice Ryhl     fn from(_: InsertError<T>) -> Error {
59*771c5a7dSAlice Ryhl         EINVAL
60*771c5a7dSAlice Ryhl     }
61*771c5a7dSAlice Ryhl }
62