mirror of
https://github.com/torvalds/linux.git
synced 2024-11-11 06:31:49 +00:00
247b365dc8
The `kernel` crate currently includes all the abstractions that wrap kernel features written in C. These abstractions call the C side of the kernel via the generated bindings with the `bindgen` tool. Modules developed in Rust should never call the bindings themselves. In the future, as the abstractions grow in number, we may need to split this crate into several, possibly following a similar subdivision in subsystems as the kernel itself and/or moving the code to the actual subsystems. Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Co-developed-by: Alex Gaynor <alex.gaynor@gmail.com> Signed-off-by: Alex Gaynor <alex.gaynor@gmail.com> Co-developed-by: Geoffrey Thomas <geofft@ldpreload.com> Signed-off-by: Geoffrey Thomas <geofft@ldpreload.com> Co-developed-by: Finn Behrens <me@kloenk.de> Signed-off-by: Finn Behrens <me@kloenk.de> Co-developed-by: Adam Bratschi-Kaye <ark.email@gmail.com> Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.com> Co-developed-by: Sven Van Asbroeck <thesven73@gmail.com> Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com> Co-developed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Gary Guo <gary@garyguo.net> Co-developed-by: Boris-Chengbiao Zhou <bobo1239@web.de> Signed-off-by: Boris-Chengbiao Zhou <bobo1239@web.de> Co-developed-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Co-developed-by: Fox Chen <foxhlchen@gmail.com> Signed-off-by: Fox Chen <foxhlchen@gmail.com> Co-developed-by: Viktor Garske <viktor@v-gar.de> Signed-off-by: Viktor Garske <viktor@v-gar.de> Co-developed-by: Dariusz Sosnowski <dsosnowski@dsosnowski.pl> Signed-off-by: Dariusz Sosnowski <dsosnowski@dsosnowski.pl> Co-developed-by: Léo Lanteri Thauvin <leseulartichaut@gmail.com> Signed-off-by: Léo Lanteri Thauvin <leseulartichaut@gmail.com> Co-developed-by: Niklas Mohrin <dev@niklasmohrin.de> Signed-off-by: Niklas Mohrin <dev@niklasmohrin.de> Co-developed-by: Milan Landaverde <milan@mdaverde.com> Signed-off-by: Milan Landaverde <milan@mdaverde.com> Co-developed-by: Morgan Bartlett <mjmouse9999@gmail.com> Signed-off-by: Morgan Bartlett <mjmouse9999@gmail.com> Co-developed-by: Maciej Falkowski <m.falkowski@samsung.com> Signed-off-by: Maciej Falkowski <m.falkowski@samsung.com> Co-developed-by: Nándor István Krácser <bonifaido@gmail.com> Signed-off-by: Nándor István Krácser <bonifaido@gmail.com> Co-developed-by: David Gow <davidgow@google.com> Signed-off-by: David Gow <davidgow@google.com> Co-developed-by: John Baublitz <john.m.baublitz@gmail.com> Signed-off-by: John Baublitz <john.m.baublitz@gmail.com> Co-developed-by: Björn Roy Baron <bjorn3_gh@protonmail.com> Signed-off-by: Björn Roy Baron <bjorn3_gh@protonmail.com> Signed-off-by: Wedson Almeida Filho <wedsonaf@google.com> Co-developed-by: Miguel Ojeda <ojeda@kernel.org> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
60 lines
1.9 KiB
Rust
60 lines
1.9 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//! Kernel errors.
|
|
//!
|
|
//! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h)
|
|
|
|
use alloc::collections::TryReserveError;
|
|
|
|
/// Contains the C-compatible error codes.
|
|
pub mod code {
|
|
/// Out of memory.
|
|
pub const ENOMEM: super::Error = super::Error(-(crate::bindings::ENOMEM as i32));
|
|
}
|
|
|
|
/// Generic integer kernel error.
|
|
///
|
|
/// The kernel defines a set of integer generic error codes based on C and
|
|
/// POSIX ones. These codes may have a more specific meaning in some contexts.
|
|
///
|
|
/// # Invariants
|
|
///
|
|
/// The value is a valid `errno` (i.e. `>= -MAX_ERRNO && < 0`).
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
pub struct Error(core::ffi::c_int);
|
|
|
|
impl Error {
|
|
/// Returns the kernel error code.
|
|
pub fn to_kernel_errno(self) -> core::ffi::c_int {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl From<TryReserveError> for Error {
|
|
fn from(_: TryReserveError) -> Error {
|
|
code::ENOMEM
|
|
}
|
|
}
|
|
|
|
/// A [`Result`] with an [`Error`] error type.
|
|
///
|
|
/// To be used as the return type for functions that may fail.
|
|
///
|
|
/// # Error codes in C and Rust
|
|
///
|
|
/// In C, it is common that functions indicate success or failure through
|
|
/// their return value; modifying or returning extra data through non-`const`
|
|
/// pointer parameters. In particular, in the kernel, functions that may fail
|
|
/// typically return an `int` that represents a generic error code. We model
|
|
/// those as [`Error`].
|
|
///
|
|
/// In Rust, it is idiomatic to model functions that may fail as returning
|
|
/// a [`Result`]. Since in the kernel many functions return an error code,
|
|
/// [`Result`] is a type alias for a [`core::result::Result`] that uses
|
|
/// [`Error`] as its error type.
|
|
///
|
|
/// Note that even if a function does not return anything when it succeeds,
|
|
/// it should still be modeled as returning a `Result` rather than
|
|
/// just an [`Error`].
|
|
pub type Result<T = ()> = core::result::Result<T, Error>;
|