diff src/constants.rs @ 185:fb8b547b36b7

Banish al(most al)l use of `i32` in favor of `c_int`.
author Paul Fisher <paul@pfish.zone>
date Thu, 31 Jul 2025 14:45:38 -0400
parents a1bb1d013567
children 5e4ea9650f87
line wrap: on
line diff
--- a/src/constants.rs	Thu Jul 31 14:36:50 2025 -0400
+++ b/src/constants.rs	Thu Jul 31 14:45:38 2025 -0400
@@ -3,6 +3,7 @@
 use crate::_doc::{linklist, man7, manbsd, mansun, xsso};
 use bitflags::bitflags;
 use std::error::Error;
+use std::ffi::c_int;
 use std::fmt;
 use std::result::Result as StdResult;
 
@@ -14,14 +15,14 @@
         $(#[$m])*
         #[derive(Clone, Copy, Debug, PartialEq, Eq)]
         #[repr(transparent)]
-        $viz struct $name(i32);
+        $viz struct $name($wraps);
 
-        impl From<i32> for $name {
-            fn from(value: i32) -> Self {
+        impl From<$wraps> for $name {
+            fn from(value: $wraps) -> Self {
                 Self(value)
             }
         }
-        impl From<$name> for i32 {
+        impl From<$name> for $wraps {
             fn from(value: $name) -> Self {
                 value.0
             }
@@ -31,11 +32,11 @@
 
 wrapper! {
     /// Type of the flags that PAM passes to us (or that we pass to PAM).
-    pub RawFlags(i32);
+    pub RawFlags(c_int);
 }
 wrapper! {
     /// The error code that we return to PAM.
-    pub ReturnCode(i32);
+    pub ReturnCode(c_int);
 }
 
 impl ReturnCode {
@@ -68,7 +69,7 @@
         impl From<RawFlags> for $name {
             #[allow(unused_doc_comments)]
             fn from(value: RawFlags) -> Self {
-                let value: i32 = value.into();
+                let value: c_int = value.into();
                 let result = Self::empty();
                 $(
                     $(#[$m_ident $($m_arg)*])*
@@ -184,7 +185,7 @@
 
         #[cfg(feature = "link")]
         impl $name {
-            const ALL_VALUES: i32 = 0 $( | $item_value)*;
+            const ALL_VALUES: c_int = 0 $( | $item_value)*;
 
             fn split(value: RawFlags) -> Result<(Option<Self>, RawFlags)> {
                 let me = value.0 & Self::ALL_VALUES;
@@ -379,12 +380,13 @@
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         use std::ffi::CStr;
         use std::ptr;
+        let retcode: ReturnCode = (*self).into();
         // SAFETY: PAM impls don't care about the PAM handle and always return
         // static strings.
-        let got = unsafe { libpam_sys::pam_strerror(ptr::null(), *self as i32) };
+        let got = unsafe { libpam_sys::pam_strerror(ptr::null(), retcode.into()) };
         if got.is_null() {
             // This shouldn't happen.
-            write!(f, "PAM error: {self:?} ({:?})", *self as i32)
+            write!(f, "PAM error: {self:?} ({:?})", retcode)
         } else {
             // SAFETY: We just got this back from PAM and we checked if it's null.
             f.write_str(&unsafe { CStr::from_ptr(got) }.to_string_lossy())
@@ -414,7 +416,7 @@
     #[cfg(not(feature = "openpam-ext"))]
     pub const BAD_CONST: ErrorCode = ErrorCode::SystemError;
 
-    pub(crate) fn result_from(ret: i32) -> Result<()> {
+    pub(crate) fn result_from(ret: c_int) -> Result<()> {
         match ret {
             0 => Ok(()),
             value => Err(ReturnCode(value).try_into().unwrap_or(Self::BAD_CONST)),