rust: error: `impl Debug` for `Error` with `errname()` integration
Integrate the `Error` type with `errname()` by providing a new `name()` method. Then, implement `Debug` for the type using the new method. [ Miguel: under `CONFIG_SYMBOLIC_ERRNAME=n`, `errname()` is a `static inline`, so added a helper to support that case, like we had in the `rust` branch. Also moved `#include` up and reworded commit message for clarity. ] Co-developed-by: Wedson Almeida Filho <walmeida@microsoft.com> Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com> Co-developed-by: Sven Van Asbroeck <thesven73@gmail.com> Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com> Signed-off-by: Gary Guo <gary@garyguo.net> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com> Link: https://lore.kernel.org/r/20230531174450.3733220-1-aliceryhl@google.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
parent
d09a61024f
commit
d2e3115d71
|
@ -6,6 +6,7 @@
|
||||||
* Sorted alphabetically.
|
* Sorted alphabetically.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <linux/errname.h>
|
||||||
#include <linux/slab.h>
|
#include <linux/slab.h>
|
||||||
#include <linux/refcount.h>
|
#include <linux/refcount.h>
|
||||||
#include <linux/wait.h>
|
#include <linux/wait.h>
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include <linux/bug.h>
|
#include <linux/bug.h>
|
||||||
#include <linux/build_bug.h>
|
#include <linux/build_bug.h>
|
||||||
#include <linux/err.h>
|
#include <linux/err.h>
|
||||||
|
#include <linux/errname.h>
|
||||||
#include <linux/refcount.h>
|
#include <linux/refcount.h>
|
||||||
#include <linux/mutex.h>
|
#include <linux/mutex.h>
|
||||||
#include <linux/spinlock.h>
|
#include <linux/spinlock.h>
|
||||||
|
@ -110,6 +111,12 @@ long rust_helper_PTR_ERR(__force const void *ptr)
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
|
EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
|
||||||
|
|
||||||
|
const char *rust_helper_errname(int err)
|
||||||
|
{
|
||||||
|
return errname(err);
|
||||||
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(rust_helper_errname);
|
||||||
|
|
||||||
struct task_struct *rust_helper_get_current(void)
|
struct task_struct *rust_helper_get_current(void)
|
||||||
{
|
{
|
||||||
return current;
|
return current;
|
||||||
|
|
|
@ -4,12 +4,15 @@
|
||||||
//!
|
//!
|
||||||
//! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h)
|
//! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h)
|
||||||
|
|
||||||
|
use crate::str::CStr;
|
||||||
|
|
||||||
use alloc::{
|
use alloc::{
|
||||||
alloc::{AllocError, LayoutError},
|
alloc::{AllocError, LayoutError},
|
||||||
collections::TryReserveError,
|
collections::TryReserveError,
|
||||||
};
|
};
|
||||||
|
|
||||||
use core::convert::From;
|
use core::convert::From;
|
||||||
|
use core::fmt;
|
||||||
use core::num::TryFromIntError;
|
use core::num::TryFromIntError;
|
||||||
use core::str::Utf8Error;
|
use core::str::Utf8Error;
|
||||||
|
|
||||||
|
@ -133,6 +136,42 @@ impl Error {
|
||||||
// SAFETY: self.0 is a valid error due to its invariant.
|
// SAFETY: self.0 is a valid error due to its invariant.
|
||||||
unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ }
|
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<&'static CStr> {
|
||||||
|
// 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<&'static CStr> {
|
||||||
|
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 {
|
impl From<AllocError> for Error {
|
||||||
|
|
Loading…
Reference in New Issue