From 5a1df22a783ee0348a4c862e36480a50829a95f5 Mon Sep 17 00:00:00 2001
From: Josh Triplett <josh@joshtriplett.org>
Date: Wed, 19 Aug 2020 23:32:39 -0700
Subject: [PATCH] Make some inline functions like WIFEXITED and WEXITSTATUS
 const and safe

---
 src/macros.rs              | 30 ++++++++++++++++++++++++++++++
 src/unix/linux_like/mod.rs | 32 +++++++++++++++++---------------
 2 files changed, 47 insertions(+), 15 deletions(-)

diff --git a/src/macros.rs b/src/macros.rs
index 378da7cc..b314f60f 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -195,6 +195,21 @@ cfg_if! {
             )*)
         }
 
+        #[allow(unused_macros)]
+        macro_rules! safe_f {
+            ($(pub $({$constness:ident})* fn $i:ident(
+                        $($arg:ident: $argty:ty),*
+            ) -> $ret:ty {
+                $($body:stmt);*
+            })*) => ($(
+                #[inline]
+                pub $($constness)* extern fn $i($($arg: $argty),*
+                ) -> $ret {
+                    $($body);*
+                }
+            )*)
+        }
+
         #[allow(unused_macros)]
         macro_rules! const_fn {
             ($($({$constness:ident})* fn $i:ident(
@@ -226,6 +241,21 @@ cfg_if! {
             )*)
         }
 
+        #[allow(unused_macros)]
+        macro_rules! safe_f {
+            ($(pub $({$constness:ident})* fn $i:ident(
+                        $($arg:ident: $argty:ty),*
+            ) -> $ret:ty {
+                $($body:stmt);*
+            })*) => ($(
+                #[inline]
+                pub extern fn $i($($arg: $argty),*
+                ) -> $ret {
+                    $($body);*
+                }
+            )*)
+        }
+
         #[allow(unused_macros)]
         macro_rules! const_fn {
             ($($({$constness:ident})* fn $i:ident(
diff --git a/src/unix/linux_like/mod.rs b/src/unix/linux_like/mod.rs
index a48c3aaa..feb3f0fc 100644
--- a/src/unix/linux_like/mod.rs
+++ b/src/unix/linux_like/mod.rs
@@ -1233,64 +1233,66 @@ f! {
             *slot = 0;
         }
     }
+}
 
-    pub fn WIFSTOPPED(status: ::c_int) -> bool {
+safe_f! {
+    pub {const} fn WIFSTOPPED(status: ::c_int) -> bool {
         (status & 0xff) == 0x7f
     }
 
-    pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
+    pub {const} fn WSTOPSIG(status: ::c_int) -> ::c_int {
         (status >> 8) & 0xff
     }
 
-    pub fn WIFCONTINUED(status: ::c_int) -> bool {
+    pub {const} fn WIFCONTINUED(status: ::c_int) -> bool {
         status == 0xffff
     }
 
-    pub fn WIFSIGNALED(status: ::c_int) -> bool {
+    pub {const} fn WIFSIGNALED(status: ::c_int) -> bool {
         ((status & 0x7f) + 1) as i8 >= 2
     }
 
-    pub fn WTERMSIG(status: ::c_int) -> ::c_int {
+    pub {const} fn WTERMSIG(status: ::c_int) -> ::c_int {
         status & 0x7f
     }
 
-    pub fn WIFEXITED(status: ::c_int) -> bool {
+    pub {const} fn WIFEXITED(status: ::c_int) -> bool {
         (status & 0x7f) == 0
     }
 
-    pub fn WEXITSTATUS(status: ::c_int) -> ::c_int {
+    pub {const} fn WEXITSTATUS(status: ::c_int) -> ::c_int {
         (status >> 8) & 0xff
     }
 
-    pub fn WCOREDUMP(status: ::c_int) -> bool {
+    pub {const} fn WCOREDUMP(status: ::c_int) -> bool {
         (status & 0x80) != 0
     }
 
-    pub fn W_EXITCODE(ret: ::c_int, sig: ::c_int) -> ::c_int {
+    pub {const} fn W_EXITCODE(ret: ::c_int, sig: ::c_int) -> ::c_int {
         (ret << 8) | sig
     }
 
-    pub fn W_STOPCODE(sig: ::c_int) -> ::c_int {
+    pub {const} fn W_STOPCODE(sig: ::c_int) -> ::c_int {
         (sig << 8) | 0x7f
     }
 
-    pub fn QCMD(cmd: ::c_int, type_: ::c_int) -> ::c_int {
+    pub {const} fn QCMD(cmd: ::c_int, type_: ::c_int) -> ::c_int {
         (cmd << 8) | (type_ & 0x00ff)
     }
 
-    pub fn IPOPT_COPIED(o: u8) -> u8 {
+    pub {const} fn IPOPT_COPIED(o: u8) -> u8 {
         o & IPOPT_COPY
     }
 
-    pub fn IPOPT_CLASS(o: u8) -> u8 {
+    pub {const} fn IPOPT_CLASS(o: u8) -> u8 {
         o & IPOPT_CLASS_MASK
     }
 
-    pub fn IPOPT_NUMBER(o: u8) -> u8 {
+    pub {const} fn IPOPT_NUMBER(o: u8) -> u8 {
         o & IPOPT_NUMBER_MASK
     }
 
-    pub fn IPTOS_ECN(x: u8) -> u8 {
+    pub {const} fn IPTOS_ECN(x: u8) -> u8 {
         x & ::IPTOS_ECN_MASK
     }
 }
-- 
GitLab