rust: error: integrate Rust error type with `errname`
Alice Ryhl
reviewerPATCH 1/1 rust: error: integrate Rust error type with `errname`
From: Gary Guo <[email protected]>
This integrates the Error type with the errname by making it accessible via the name method or via the Debug trait.
--- rust/bindings/bindings_helper.h | 1 + rust/kernel/error.rs | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+)
rust/bindings/bindings_helper.h +1 -0
@@ -10,6 +10,7 @@#include <linux/refcount.h> #include <linux/wait.h> #include <linux/sched.h> +#include <linux/errname.h> /* `bindgen` gets confused at certain things. */ const gfp_t BINDINGS_GFP_KERNEL =GFP_KERNEL ;
rust/kernel/error.rs +39 -0
@@ -4,12 +4,15 @@//! //! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h) +use crate ::str::CStr ; +use alloc::{ alloc::{AllocError ,LayoutError }, collections::TryReserveError , };use core::convert::From ; +use core::fmt;use core::num::TryFromIntError ;use core::str::Utf8Error ;
@@ -113,6 +116,42 @@ impl Error {// SAFETY: self.0 is a valid error due to its invariant. unsafe { bindings::ERR_PTR (self.0 .into ())as *mut _ } } + +/// Returns a string representing the error, if one exists. +#[cfg(not(testlib))] +pub fn name (&self) ->Option <&'staticCStr > { +// SAFETY: Just an FFI call, there are no extra safety requirements. +let ptr =unsafe { bindings::errname (-self.0 ) }; +if ptr.is_null () { +None + }else { +// SAFETY: The string returned by `errname` is static and `NUL`-terminated. +Some (unsafe {CStr ::from_char_ptr (ptr) }) + } + } + +/// Returns a string representing the error, if one exists. +/// +/// When `testlib` is configured, this always returns `None` to avoid the dependency on a +/// kernel function so that tests that use this (e.g., by calling [`Result::unwrap`]) can still +/// run in userspace. +#[cfg(testlib)] +pub fn name (&self) ->Option <&'staticCStr > { +None + } +} + +impl fmt::Debug for Error { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { +match self.name () { +// Print out number if no name can be found. +None => f.debug_tuple ("Error" ).field (&-self.0 ).finish (), +// SAFETY: These strings are ASCII-only. +Some (name) => f + .debug_tuple (unsafe { core::str::from_utf8_unchecked (name) }) + .finish (), + } + } }impl From <AllocError >for Error {
base-commit: ac9a78681b921877518763ba0e89202254349d1b
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Quoted 5 lines · Alice Ryhl
From: Gary Guo <[email protected]>
This integrates the
Errortype with theerrnameby making it accessible via thenamemethod or via theDebugtrait.
Quoted 8 lines
Co-Developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Co-Developed-by Sven Van Asbroeck <[email protected]>Signed-off-by Sven Van Asbroeck <[email protected]>Signed-off-by Gary Guo <[email protected]>Signed-off-by Alice Ryhl <[email protected]>---
[...]
Hello Alice, as we discussed through a separate channel, you have my permission to use my name in Co-Developed-by and Signed-off-by tags.
Quoted 12 lines · Alice Ryhl
From: Gary Guo <[email protected]>
This integrates the
Errortype with theerrnameby making it accessible via thenamemethod or via theDebugtrait.Co-Developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Co-Developed-by Sven Van Asbroeck <[email protected]>Signed-off-by Sven Van Asbroeck <[email protected]>Signed-off-by Gary Guo <[email protected]>Signed-off-by Alice Ryhl <[email protected]>
Miguel Ojeda
AppliedQuoted 4 lines · Sven Van Asbroeck
Hello Alice, as we discussed through a separate channel, you have my permission to use my name in Co-Developed-by and Signed-off-by tags.
Thanks a lot Sven for the confirmation!
Applied to rust-next -- reworded for clarity. Thanks!
Cheers,
Miguel
Quoted 2 lines · Miguel Ojeda
Applied to
rust-next-- reworded for clarity. Thanks!
When working on the KUnit changes that require this, I noticed this didn't include the CONFIG_SYMBOLIC_ERRNAME=n support we originally had on the rust branch.
So I added the errname helper and force-pushed.
Cheers,
Miguel