Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: error: add `From` implementations for `Error`

Add a set of `From` implementations for the `Error` kernel type.

These implementations allow to easily convert from standard Rust
error types to the usual kernel errors based on one of the `E*`
integer codes.

On top of that, the question mark Rust operator (`?`) implicitly
performs a conversion on the error value using the `From` trait
when propagating. Thus it is extra convenient to use.

For instance, a kernel function that needs to convert a `i64` into
a `i32` and to bubble up the error as a kernel error may write:

fn f(x: i64) -> Result<...> {
...
let y = i32::try_from(x)?;
...
}

which will transform the `TryFromIntError` into an `Err(EINVAL)`.

Co-developed-by: Adam Bratschi-Kaye <ark.email@gmail.com>
Signed-off-by: Adam Bratschi-Kaye <ark.email@gmail.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>
Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
Reviewed-by: Finn Behrens <me@kloenk.dev>
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

authored by

Wedson Almeida Filho and committed by
Miguel Ojeda
76e2c2d9 266def2a

+45 -1
+44 -1
rust/kernel/error.rs
··· 4 4 //! 5 5 //! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h) 6 6 7 - use alloc::collections::TryReserveError; 7 + use alloc::{ 8 + alloc::{AllocError, LayoutError}, 9 + collections::TryReserveError, 10 + }; 11 + 12 + use core::convert::From; 13 + use core::num::TryFromIntError; 14 + use core::str::Utf8Error; 8 15 9 16 /// Contains the C-compatible error codes. 10 17 pub mod code { ··· 78 71 } 79 72 } 80 73 74 + impl From<AllocError> for Error { 75 + fn from(_: AllocError) -> Error { 76 + code::ENOMEM 77 + } 78 + } 79 + 80 + impl From<TryFromIntError> for Error { 81 + fn from(_: TryFromIntError) -> Error { 82 + code::EINVAL 83 + } 84 + } 85 + 86 + impl From<Utf8Error> for Error { 87 + fn from(_: Utf8Error) -> Error { 88 + code::EINVAL 89 + } 90 + } 91 + 81 92 impl From<TryReserveError> for Error { 82 93 fn from(_: TryReserveError) -> Error { 83 94 code::ENOMEM 95 + } 96 + } 97 + 98 + impl From<LayoutError> for Error { 99 + fn from(_: LayoutError) -> Error { 100 + code::ENOMEM 101 + } 102 + } 103 + 104 + impl From<core::fmt::Error> for Error { 105 + fn from(_: core::fmt::Error) -> Error { 106 + code::EINVAL 107 + } 108 + } 109 + 110 + impl From<core::convert::Infallible> for Error { 111 + fn from(e: core::convert::Infallible) -> Error { 112 + match e {} 84 113 } 85 114 } 86 115
+1
rust/kernel/lib.rs
··· 12 12 //! do so first instead of bypassing this crate. 13 13 14 14 #![no_std] 15 + #![feature(allocator_api)] 15 16 #![feature(core_ffi_c)] 16 17 17 18 // Ensure conditional compilation based on the kernel configuration works;