1 // SPDX-License-Identifier: GPL-2.0
5 //! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h)
7 use alloc::collections::TryReserveError;
9 /// Contains the C-compatible error codes.
12 pub const ENOMEM: super::Error = super::Error(-(crate::bindings::ENOMEM as i32));
15 /// Generic integer kernel error.
17 /// The kernel defines a set of integer generic error codes based on C and
18 /// POSIX ones. These codes may have a more specific meaning in some contexts.
22 /// The value is a valid `errno` (i.e. `>= -MAX_ERRNO && < 0`).
23 #[derive(Clone, Copy, PartialEq, Eq)]
24 pub struct Error(core::ffi::c_int);
27 /// Returns the kernel error code.
28 pub fn to_kernel_errno(self) -> core::ffi::c_int {
33 impl From<TryReserveError> for Error {
34 fn from(_: TryReserveError) -> Error {
39 /// A [`Result`] with an [`Error`] error type.
41 /// To be used as the return type for functions that may fail.
43 /// # Error codes in C and Rust
45 /// In C, it is common that functions indicate success or failure through
46 /// their return value; modifying or returning extra data through non-`const`
47 /// pointer parameters. In particular, in the kernel, functions that may fail
48 /// typically return an `int` that represents a generic error code. We model
49 /// those as [`Error`].
51 /// In Rust, it is idiomatic to model functions that may fail as returning
52 /// a [`Result`]. Since in the kernel many functions return an error code,
53 /// [`Result`] is a type alias for a [`core::result::Result`] that uses
54 /// [`Error`] as its error type.
56 /// Note that even if a function does not return anything when it succeeds,
57 /// it should still be modeled as returning a `Result` rather than
58 /// just an [`Error`].
59 pub type Result<T = ()> = core::result::Result<T, Error>;