Skip to content

Commit 5a1df22

Browse files
committed
Make some inline functions like WIFEXITED and WEXITSTATUS const and safe
1 parent 6bd95e4 commit 5a1df22

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

Diff for: src/macros.rs

+30
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ cfg_if! {
195195
)*)
196196
}
197197

198+
#[allow(unused_macros)]
199+
macro_rules! safe_f {
200+
($(pub $({$constness:ident})* fn $i:ident(
201+
$($arg:ident: $argty:ty),*
202+
) -> $ret:ty {
203+
$($body:stmt);*
204+
})*) => ($(
205+
#[inline]
206+
pub $($constness)* extern fn $i($($arg: $argty),*
207+
) -> $ret {
208+
$($body);*
209+
}
210+
)*)
211+
}
212+
198213
#[allow(unused_macros)]
199214
macro_rules! const_fn {
200215
($($({$constness:ident})* fn $i:ident(
@@ -226,6 +241,21 @@ cfg_if! {
226241
)*)
227242
}
228243

244+
#[allow(unused_macros)]
245+
macro_rules! safe_f {
246+
($(pub $({$constness:ident})* fn $i:ident(
247+
$($arg:ident: $argty:ty),*
248+
) -> $ret:ty {
249+
$($body:stmt);*
250+
})*) => ($(
251+
#[inline]
252+
pub extern fn $i($($arg: $argty),*
253+
) -> $ret {
254+
$($body);*
255+
}
256+
)*)
257+
}
258+
229259
#[allow(unused_macros)]
230260
macro_rules! const_fn {
231261
($($({$constness:ident})* fn $i:ident(

Diff for: src/unix/linux_like/mod.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1233,64 +1233,66 @@ f! {
12331233
*slot = 0;
12341234
}
12351235
}
1236+
}
12361237

1237-
pub fn WIFSTOPPED(status: ::c_int) -> bool {
1238+
safe_f! {
1239+
pub {const} fn WIFSTOPPED(status: ::c_int) -> bool {
12381240
(status & 0xff) == 0x7f
12391241
}
12401242

1241-
pub fn WSTOPSIG(status: ::c_int) -> ::c_int {
1243+
pub {const} fn WSTOPSIG(status: ::c_int) -> ::c_int {
12421244
(status >> 8) & 0xff
12431245
}
12441246

1245-
pub fn WIFCONTINUED(status: ::c_int) -> bool {
1247+
pub {const} fn WIFCONTINUED(status: ::c_int) -> bool {
12461248
status == 0xffff
12471249
}
12481250

1249-
pub fn WIFSIGNALED(status: ::c_int) -> bool {
1251+
pub {const} fn WIFSIGNALED(status: ::c_int) -> bool {
12501252
((status & 0x7f) + 1) as i8 >= 2
12511253
}
12521254

1253-
pub fn WTERMSIG(status: ::c_int) -> ::c_int {
1255+
pub {const} fn WTERMSIG(status: ::c_int) -> ::c_int {
12541256
status & 0x7f
12551257
}
12561258

1257-
pub fn WIFEXITED(status: ::c_int) -> bool {
1259+
pub {const} fn WIFEXITED(status: ::c_int) -> bool {
12581260
(status & 0x7f) == 0
12591261
}
12601262

1261-
pub fn WEXITSTATUS(status: ::c_int) -> ::c_int {
1263+
pub {const} fn WEXITSTATUS(status: ::c_int) -> ::c_int {
12621264
(status >> 8) & 0xff
12631265
}
12641266

1265-
pub fn WCOREDUMP(status: ::c_int) -> bool {
1267+
pub {const} fn WCOREDUMP(status: ::c_int) -> bool {
12661268
(status & 0x80) != 0
12671269
}
12681270

1269-
pub fn W_EXITCODE(ret: ::c_int, sig: ::c_int) -> ::c_int {
1271+
pub {const} fn W_EXITCODE(ret: ::c_int, sig: ::c_int) -> ::c_int {
12701272
(ret << 8) | sig
12711273
}
12721274

1273-
pub fn W_STOPCODE(sig: ::c_int) -> ::c_int {
1275+
pub {const} fn W_STOPCODE(sig: ::c_int) -> ::c_int {
12741276
(sig << 8) | 0x7f
12751277
}
12761278

1277-
pub fn QCMD(cmd: ::c_int, type_: ::c_int) -> ::c_int {
1279+
pub {const} fn QCMD(cmd: ::c_int, type_: ::c_int) -> ::c_int {
12781280
(cmd << 8) | (type_ & 0x00ff)
12791281
}
12801282

1281-
pub fn IPOPT_COPIED(o: u8) -> u8 {
1283+
pub {const} fn IPOPT_COPIED(o: u8) -> u8 {
12821284
o & IPOPT_COPY
12831285
}
12841286

1285-
pub fn IPOPT_CLASS(o: u8) -> u8 {
1287+
pub {const} fn IPOPT_CLASS(o: u8) -> u8 {
12861288
o & IPOPT_CLASS_MASK
12871289
}
12881290

1289-
pub fn IPOPT_NUMBER(o: u8) -> u8 {
1291+
pub {const} fn IPOPT_NUMBER(o: u8) -> u8 {
12901292
o & IPOPT_NUMBER_MASK
12911293
}
12921294

1293-
pub fn IPTOS_ECN(x: u8) -> u8 {
1295+
pub {const} fn IPTOS_ECN(x: u8) -> u8 {
12941296
x & ::IPTOS_ECN_MASK
12951297
}
12961298
}

0 commit comments

Comments
 (0)