changeset 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 42f747774d94
children 5e4ea9650f87
files src/constants.rs src/libpam/handle.rs src/libpam/items.rs src/libpam/memory.rs src/libpam/question.rs
diffstat 5 files changed, 27 insertions(+), 23 deletions(-) [+]
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)),
--- a/src/libpam/handle.rs	Thu Jul 31 14:36:50 2025 -0400
+++ b/src/libpam/handle.rs	Thu Jul 31 14:45:38 2025 -0400
@@ -149,8 +149,8 @@
     }
 
     /// Internal "end" function, which binary-ORs the status with `or_with`.
-    fn end_internal(&mut self, or_with: i32) {
-        let last: i32 = ReturnCode::from(self.last_return.get()).into();
+    fn end_internal(&mut self, or_with: c_int) {
+        let last: c_int = ReturnCode::from(self.last_return.get()).into();
         let result = last | or_with;
         unsafe { libpam_sys::pam_end(self.handle.cast(), result) };
     }
@@ -297,7 +297,7 @@
     #[doc = guide!(adg: "adg-interface-by-app-expected.html#adg-pam_end")]
     #[doc = stdlinks!(3 pam_end)]
     pub fn end_silent(&mut self, result: Result<()>) {
-        let result: i32 = ReturnCode::from(result).into();
+        let result: c_int = ReturnCode::from(result).into();
         #[cfg(pam_impl = "LinuxPam")]
         let result = result | libpam_sys::PAM_DATA_SILENT;
         unsafe {
--- a/src/libpam/items.rs	Thu Jul 31 14:36:50 2025 -0400
+++ b/src/libpam/items.rs	Thu Jul 31 14:45:38 2025 -0400
@@ -10,7 +10,7 @@
     /// Identifies what is being gotten or set with `pam_get_item`
     /// or `pam_set_item`.
     #[non_exhaustive]
-    pub enum ItemType(i32) {
+    pub enum ItemType {
         /// The PAM service name.
         Service = libpam_sys::PAM_SERVICE,
         /// The user's login name.
--- a/src/libpam/memory.rs	Thu Jul 31 14:36:50 2025 -0400
+++ b/src/libpam/memory.rs	Thu Jul 31 14:45:38 2025 -0400
@@ -13,16 +13,18 @@
 macro_rules! num_enum {
     (
         $(#[$m:meta])*
-        $viz:vis enum $name:ident($repr:ty) {
+        $viz:vis enum $name:ident {
             $(
                 $(#[$im:meta])*
                 $item_name:ident = $item_value:path,
             )*
         }
     ) => {
+        // This is the one place where we depend upon c_int being i32.
+        // Ideally, we would be able to say `repr(c_int)` but we can't.
         $(#[$m])*
         #[derive(Clone, Copy, Debug, Eq, PartialEq)]
-        #[repr($repr)]
+        #[repr(i32)]
         $viz enum $name {
             $(
                 $(#[$im])*
@@ -30,11 +32,11 @@
             )*
         }
 
-        impl TryFrom<$repr> for $name {
+        impl TryFrom<c_int> for $name {
             type Error = crate::constants::ErrorCode;
 
             #[allow(unused_doc_comments)]
-            fn try_from(value: $repr) -> crate::constants::Result<$name> {
+            fn try_from(value: c_int) -> crate::constants::Result<$name> {
                 match value {
                     $(
                         $(#[$im])*
@@ -45,9 +47,9 @@
             }
         }
 
-        impl From<$name> for $repr {
-            fn from(value: $name) -> $repr {
-                value as $repr
+        impl From<$name> for c_int {
+            fn from(value: $name) -> c_int {
+                value as c_int
             }
         }
     }
--- a/src/libpam/question.rs	Thu Jul 31 14:36:50 2025 -0400
+++ b/src/libpam/question.rs	Thu Jul 31 14:45:38 2025 -0400
@@ -12,7 +12,7 @@
 
 memory::num_enum! {
     /// The C enum values for messages shown to the user.
-    enum Style(i32) {
+    enum Style {
         /// Requests information from the user; will be masked when typing.
         PromptEchoOff = libpam_sys::PAM_PROMPT_ECHO_OFF,
         /// Requests information from the user; will not be masked.