From 1d46d91cae65bdd6eb008038e893180dbf313a46 Mon Sep 17 00:00:00 2001 From: angelos Date: Fri, 5 Aug 2022 13:06:32 +0300 Subject: [PATCH] The patch adds core::num::NonZeroI16 as Error inner type. Solves Issue: error.rs: use a tighter inner type for Error #296 Signed-off-by: angelos --- rust/kernel/error.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index f968aa91ddf2cc..b250e4cd09c8fd 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -12,7 +12,7 @@ use alloc::{ }; use core::convert::From; use core::fmt; -use core::num::TryFromIntError; +use core::num::{NonZeroI16, TryFromIntError}; use core::str::{self, Utf8Error}; /// Contains the C-compatible error codes. @@ -22,7 +22,7 @@ pub mod code { $( #[doc = $doc] )* - pub const $err: super::Error = super::Error(-(crate::bindings::$err as i32)); + pub const $err: super::Error = super::Error(unsafe { super::NonZeroI16::new_unchecked(-(crate::bindings::$err as i16)) }); }; } @@ -317,7 +317,7 @@ pub mod code { /// /// The value is a valid `errno` (i.e. `>= -MAX_ERRNO && < 0`). #[derive(Clone, Copy, PartialEq, Eq)] -pub struct Error(core::ffi::c_int); +pub struct Error(NonZeroI16); impl Error { /// Creates an [`Error`] from a kernel error code. @@ -336,7 +336,7 @@ impl Error { // INVARIANT: The check above ensures the type invariant // will hold. - Error(errno) + Error(unsafe { NonZeroI16::new_unchecked(errno as i16) }) } /// Creates an [`Error`] from a kernel error code. @@ -347,19 +347,19 @@ impl Error { pub(crate) unsafe fn from_kernel_errno_unchecked(errno: core::ffi::c_int) -> Error { // INVARIANT: The contract ensures the type invariant // will hold. - Error(errno) + Error(unsafe { NonZeroI16::new_unchecked(errno as i16) }) } /// Returns the kernel error code. pub fn to_kernel_errno(self) -> core::ffi::c_int { - self.0 + self.0.get() as i32 } /// 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) }; + let ptr = unsafe { bindings::errname(-(self.0.get() as i32)) }; if ptr.is_null() { None } else { @@ -383,7 +383,10 @@ 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(), + None => f + .debug_tuple("Error") + .field(&-(self.0.get() as i32)) + .finish(), // SAFETY: These strings are ASCII-only. Some(name) => f .debug_tuple(unsafe { str::from_utf8_unchecked(name) })