diff src/constants.rs @ 84:a638a45e5f1f

do some real irritating i32/u32 juggling to make bindgen happy
author Paul Fisher <paul@pfish.zone>
date Tue, 10 Jun 2025 02:35:11 -0400
parents 5aa1a010f1e8
children 5e14bb093851
line wrap: on
line diff
--- a/src/constants.rs	Tue Jun 10 01:56:41 2025 -0400
+++ b/src/constants.rs	Tue Jun 10 02:35:11 2025 -0400
@@ -5,6 +5,7 @@
 use bitflags::bitflags;
 use libc::c_int;
 use num_enum::{IntoPrimitive, TryFromPrimitive};
+use std::ffi::c_uint;
 use std::result::Result as StdResult;
 
 /// Arbitrary values for PAM constants when not linking against system PAM.
@@ -18,13 +19,13 @@
         ($(#[$attr:meta])* $($name:ident = $value:expr),+) => {
             define!(
                 @meta { $(#[$attr])* }
-                $(pub const $name: i32 = $value;)+
+                $(pub const $name: u32 = $value;)+
             );
         };
         (@meta $m:tt $($i:item)+) => { define!(@expand $($m $i)+); };
         (@expand $({ $(#[$m:meta])* } $i:item)+) => {$($(#[$m])* $i)+};
     }
-    const fn bit(n: i8) -> i32 {
+    const fn bit(n: u8) -> u32 {
         1 << n
     }
     define!(
@@ -81,7 +82,7 @@
     /// See `/usr/include/security/pam_modules.h` for more details.
     #[derive(Debug, PartialEq)]
     #[repr(transparent)]
-    pub struct Flags: c_int {
+    pub struct Flags: c_uint {
         /// The module should not generate any messages.
         const SILENT = pam_ffi::PAM_SILENT;
 
@@ -138,7 +139,7 @@
 #[allow(non_camel_case_types, dead_code)]
 #[derive(Copy, Clone, Debug, PartialEq, thiserror::Error, TryFromPrimitive, IntoPrimitive)]
 #[non_exhaustive] // C might give us anything!
-#[repr(i32)]
+#[repr(u32)]
 pub enum ErrorCode {
     #[error("dlopen() failure when dynamically loading a service module")]
     OpenError = pam_ffi::PAM_OPEN_ERR,
@@ -212,7 +213,7 @@
     pub fn result_to_c<T>(value: Result<T>) -> c_int {
         match value {
             Ok(_) => 0, // PAM_SUCCESS
-            Err(otherwise) => otherwise.into(),
+            Err(otherwise) => u32::from(otherwise) as i32,
         }
     }
 
@@ -221,7 +222,7 @@
     pub fn result_from(value: c_int) -> Result<()> {
         match value {
             0 => Ok(()),
-            value => Err(value.try_into().unwrap_or(Self::SystemError)),
+            value => Err((value as u32).try_into().unwrap_or(Self::SystemError)),
         }
     }
 }
@@ -237,12 +238,12 @@
     fn test_enums() {
         assert_eq!(Ok(()), ErrorCode::result_from(0));
         assert_eq!(
-            pam_ffi::PAM_BAD_ITEM,
+            pam_ffi::PAM_BAD_ITEM as i32,
             ErrorCode::result_to_c::<()>(Err(ErrorCode::BadItem))
         );
         assert_eq!(
             Err(ErrorCode::Abort),
-            ErrorCode::result_from(pam_ffi::PAM_ABORT)
+            ErrorCode::result_from(pam_ffi::PAM_ABORT as i32)
         );
         assert_eq!(Err(ErrorCode::SystemError), ErrorCode::result_from(423));
     }