Skip to content

Commit e860257

Browse files
committed
cleanup: Simplify the syntax of f! and similar macros
We no longer need to support gating `const` behind `libc_const_extern_fn`, so remove the awkward `{const}` syntax.
1 parent e4b64bb commit e860257

File tree

30 files changed

+264
-265
lines changed

30 files changed

+264
-265
lines changed

ci/style.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ while IFS= read -r file; do
3131
# wouldn't be correct for something like `all(any(...), ...)`).
3232
perl -pi -0777 -e 's/if #\[cfg\((.*?)\)\]/if cfg_tmp!([$1])/gms' "$file"
3333

34-
# We have some instances of `{const}` that make macros happy but aren't
35-
# valid syntax. Replace this with just the keyword, plus an indicator
36-
# comment on the preceding line (which is where rustfmt puts it. Also
37-
# rust-lang/rustfmt#5464).
38-
perl -pi -e 's/^(\s*)(.*)\{const\}/$1\/\* FMT-CONST \*\/\n$1$2const/g' "$file"
39-
4034
# Format the file. We need to invoke `rustfmt` directly since `cargo fmt`
4135
# can't figure out the module tree with the hacks in place.
4236
failed=false
@@ -45,7 +39,6 @@ while IFS= read -r file; do
4539
# Restore all changes to the files.
4640
perl -pi -e 's/fn (\w+)_fmt_tmp\(\)/$1!/g' "$file"
4741
perl -pi -0777 -e 's/cfg_tmp!\(\[(.*?)\]\)/#[cfg($1)]/gms' "$file"
48-
perl -pi -0777 -e 's/\/\* FMT-CONST \*\/(?:\n\s*)?(.*?)const/$1\{const\}/gms' "$file"
4942

5043
# Defer emitting the failure until after the files get reset
5144
if [ "$failed" != "false" ]; then

src/fuchsia/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3374,57 +3374,57 @@ f! {
33743374
}
33753375
}
33763376

3377-
pub {const} fn CMSG_ALIGN(len: size_t) -> size_t {
3377+
pub const fn CMSG_ALIGN(len: size_t) -> size_t {
33783378
(len + size_of::<size_t>() - 1) & !(size_of::<size_t>() - 1)
33793379
}
33803380

3381-
pub {const} fn CMSG_SPACE(len: c_uint) -> c_uint {
3381+
pub const fn CMSG_SPACE(len: c_uint) -> c_uint {
33823382
(CMSG_ALIGN(len as size_t) + CMSG_ALIGN(size_of::<cmsghdr>())) as c_uint
33833383
}
33843384

3385-
pub {const} fn CMSG_LEN(len: c_uint) -> c_uint {
3385+
pub const fn CMSG_LEN(len: c_uint) -> c_uint {
33863386
(CMSG_ALIGN(size_of::<cmsghdr>()) + len as size_t) as c_uint
33873387
}
33883388
}
33893389

33903390
safe_f! {
3391-
pub {const} fn WIFSTOPPED(status: c_int) -> bool {
3391+
pub const fn WIFSTOPPED(status: c_int) -> bool {
33923392
(status & 0xff) == 0x7f
33933393
}
33943394

3395-
pub {const} fn WSTOPSIG(status: c_int) -> c_int {
3395+
pub const fn WSTOPSIG(status: c_int) -> c_int {
33963396
(status >> 8) & 0xff
33973397
}
33983398

3399-
pub {const} fn WIFCONTINUED(status: c_int) -> bool {
3399+
pub const fn WIFCONTINUED(status: c_int) -> bool {
34003400
status == 0xffff
34013401
}
34023402

3403-
pub {const} fn WIFSIGNALED(status: c_int) -> bool {
3403+
pub const fn WIFSIGNALED(status: c_int) -> bool {
34043404
((status & 0x7f) + 1) as i8 >= 2
34053405
}
34063406

3407-
pub {const} fn WTERMSIG(status: c_int) -> c_int {
3407+
pub const fn WTERMSIG(status: c_int) -> c_int {
34083408
status & 0x7f
34093409
}
34103410

3411-
pub {const} fn WIFEXITED(status: c_int) -> bool {
3411+
pub const fn WIFEXITED(status: c_int) -> bool {
34123412
(status & 0x7f) == 0
34133413
}
34143414

3415-
pub {const} fn WEXITSTATUS(status: c_int) -> c_int {
3415+
pub const fn WEXITSTATUS(status: c_int) -> c_int {
34163416
(status >> 8) & 0xff
34173417
}
34183418

3419-
pub {const} fn WCOREDUMP(status: c_int) -> bool {
3419+
pub const fn WCOREDUMP(status: c_int) -> bool {
34203420
(status & 0x80) != 0
34213421
}
34223422

3423-
pub {const} fn QCMD(cmd: c_int, type_: c_int) -> c_int {
3423+
pub const fn QCMD(cmd: c_int, type_: c_int) -> c_int {
34243424
(cmd << 8) | (type_ & 0x00ff)
34253425
}
34263426

3427-
pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
3427+
pub const fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
34283428
let major = major as crate::dev_t;
34293429
let minor = minor as crate::dev_t;
34303430
let mut dev = 0;
@@ -3435,14 +3435,14 @@ safe_f! {
34353435
dev
34363436
}
34373437

3438-
pub {const} fn major(dev: crate::dev_t) -> c_uint {
3438+
pub const fn major(dev: crate::dev_t) -> c_uint {
34393439
let mut major = 0;
34403440
major |= (dev & 0x00000000000fff00) >> 8;
34413441
major |= (dev & 0xfffff00000000000) >> 32;
34423442
major as c_uint
34433443
}
34443444

3445-
pub {const} fn minor(dev: crate::dev_t) -> c_uint {
3445+
pub const fn minor(dev: crate::dev_t) -> c_uint {
34463446
let mut minor = 0;
34473447
minor |= (dev & 0x00000000000000ff) >> 0;
34483448
minor |= (dev & 0x00000ffffff00000) >> 12;

src/macros.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,40 +296,46 @@ macro_rules! c_enum {
296296
macro_rules! f {
297297
($(
298298
$(#[$attr:meta])*
299-
pub $({$constness:ident})? fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
299+
// Less than ideal hack to match either `fn` or `const fn`.
300+
pub $(fn $i:ident)? $(const fn $const_i:ident)?
301+
($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
300302
$body:block
301-
)*) => ($(
303+
)+) => {$(
302304
#[inline]
303305
$(#[$attr])*
304-
pub $($constness)? unsafe extern "C" fn $i($($arg: $argty),*) -> $ret
306+
pub $(unsafe extern "C" fn $i)? $(const unsafe extern "C" fn $const_i)?
307+
($($arg: $argty),*) -> $ret
305308
$body
306-
)*)
309+
)+};
307310
}
308311

309312
/// Define a safe function.
310313
macro_rules! safe_f {
311314
($(
312315
$(#[$attr:meta])*
313-
pub $({$constness:ident})? fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
316+
// Less than ideal hack to match either `fn` or `const fn`.
317+
pub $(fn $i:ident)? $(const fn $const_i:ident)?
318+
($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
314319
$body:block
315-
)*) => ($(
320+
)+) => {$(
316321
#[inline]
317322
$(#[$attr])*
318-
pub $($constness)? extern "C" fn $i($($arg: $argty),*) -> $ret
323+
pub $(extern "C" fn $i)? $(const extern "C" fn $const_i)?
324+
($($arg: $argty),*) -> $ret
319325
$body
320-
)*)
326+
)+};
321327
}
322328

323329
/// Define a nonpublic function.
324330
macro_rules! const_fn {
325331
($(
326332
$(#[$attr:meta])*
327-
$({$constness:ident})? fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
333+
const fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
328334
$body:block
329335
)*) => ($(
330336
#[inline]
331337
$(#[$attr])*
332-
$($constness)? fn $i($($arg: $argty),*) -> $ret
338+
const fn $i($($arg: $argty),*) -> $ret
333339
$body
334340
)*)
335341
}

src/unix/aix/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,11 +2457,11 @@ f! {
24572457
(cmsg as *mut c_uchar).offset(size_of::<cmsghdr>() as isize)
24582458
}
24592459

2460-
pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
2460+
pub const fn CMSG_LEN(length: c_uint) -> c_uint {
24612461
size_of::<cmsghdr>() as c_uint + length
24622462
}
24632463

2464-
pub {const} fn CMSG_SPACE(length: c_uint) -> c_uint {
2464+
pub const fn CMSG_SPACE(length: c_uint) -> c_uint {
24652465
size_of::<cmsghdr>() as c_uint + length
24662466
}
24672467

@@ -2493,62 +2493,62 @@ f! {
24932493
}
24942494

24952495
safe_f! {
2496-
pub {const} fn WIFSTOPPED(status: c_int) -> bool {
2496+
pub const fn WIFSTOPPED(status: c_int) -> bool {
24972497
(status & _W_STOPPED) != 0
24982498
}
24992499

2500-
pub {const} fn WSTOPSIG(status: c_int) -> c_int {
2500+
pub const fn WSTOPSIG(status: c_int) -> c_int {
25012501
if WIFSTOPPED(status) {
25022502
(((status as c_uint) >> 8) & 0xff) as c_int
25032503
} else {
25042504
-1
25052505
}
25062506
}
25072507

2508-
pub {const} fn WIFEXITED(status: c_int) -> bool {
2508+
pub const fn WIFEXITED(status: c_int) -> bool {
25092509
(status & 0xFF) == 0
25102510
}
25112511

2512-
pub {const} fn WEXITSTATUS(status: c_int) -> c_int {
2512+
pub const fn WEXITSTATUS(status: c_int) -> c_int {
25132513
if WIFEXITED(status) {
25142514
(((status as c_uint) >> 8) & 0xff) as c_int
25152515
} else {
25162516
-1
25172517
}
25182518
}
25192519

2520-
pub {const} fn WIFSIGNALED(status: c_int) -> bool {
2520+
pub const fn WIFSIGNALED(status: c_int) -> bool {
25212521
!WIFEXITED(status) && !WIFSTOPPED(status)
25222522
}
25232523

2524-
pub {const} fn WTERMSIG(status: c_int) -> c_int {
2524+
pub const fn WTERMSIG(status: c_int) -> c_int {
25252525
if WIFSIGNALED(status) {
25262526
(((status as c_uint) >> 16) & 0xff) as c_int
25272527
} else {
25282528
-1
25292529
}
25302530
}
25312531

2532-
pub {const} fn WIFCONTINUED(status: c_int) -> bool {
2532+
pub const fn WIFCONTINUED(status: c_int) -> bool {
25332533
(status & WCONTINUED) != 0
25342534
}
25352535

25362536
// AIX doesn't have native WCOREDUMP.
2537-
pub {const} fn WCOREDUMP(_status: c_int) -> bool {
2537+
pub const fn WCOREDUMP(_status: c_int) -> bool {
25382538
false
25392539
}
25402540

2541-
pub {const} fn major(dev: crate::dev_t) -> c_uint {
2541+
pub const fn major(dev: crate::dev_t) -> c_uint {
25422542
let x = dev >> 16;
25432543
x as c_uint
25442544
}
25452545

2546-
pub {const} fn minor(dev: crate::dev_t) -> c_uint {
2546+
pub const fn minor(dev: crate::dev_t) -> c_uint {
25472547
let y = dev & 0xFFFF;
25482548
y as c_uint
25492549
}
25502550

2551-
pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
2551+
pub const fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
25522552
let major = major as crate::dev_t;
25532553
let minor = minor as crate::dev_t;
25542554
let mut dev = 0;

src/unix/bsd/apple/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5022,49 +5022,49 @@ f! {
50225022
(cmsg as *mut c_uchar).add(__DARWIN_ALIGN32(size_of::<cmsghdr>()))
50235023
}
50245024

5025-
pub {const} fn CMSG_SPACE(length: c_uint) -> c_uint {
5025+
pub const fn CMSG_SPACE(length: c_uint) -> c_uint {
50265026
(__DARWIN_ALIGN32(size_of::<cmsghdr>()) + __DARWIN_ALIGN32(length as usize)) as c_uint
50275027
}
50285028

5029-
pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
5029+
pub const fn CMSG_LEN(length: c_uint) -> c_uint {
50305030
(__DARWIN_ALIGN32(size_of::<cmsghdr>()) + length as usize) as c_uint
50315031
}
50325032

5033-
pub {const} fn VM_MAKE_TAG(id: u8) -> u32 {
5033+
pub const fn VM_MAKE_TAG(id: u8) -> u32 {
50345034
(id as u32) << 24u32
50355035
}
50365036
}
50375037

50385038
safe_f! {
5039-
pub {const} fn WSTOPSIG(status: c_int) -> c_int {
5039+
pub const fn WSTOPSIG(status: c_int) -> c_int {
50405040
status >> 8
50415041
}
50425042

5043-
pub {const} fn _WSTATUS(status: c_int) -> c_int {
5043+
pub const fn _WSTATUS(status: c_int) -> c_int {
50445044
status & 0x7f
50455045
}
50465046

5047-
pub {const} fn WIFCONTINUED(status: c_int) -> bool {
5047+
pub const fn WIFCONTINUED(status: c_int) -> bool {
50485048
_WSTATUS(status) == _WSTOPPED && WSTOPSIG(status) == 0x13
50495049
}
50505050

5051-
pub {const} fn WIFSIGNALED(status: c_int) -> bool {
5051+
pub const fn WIFSIGNALED(status: c_int) -> bool {
50525052
_WSTATUS(status) != _WSTOPPED && _WSTATUS(status) != 0
50535053
}
50545054

5055-
pub {const} fn WIFSTOPPED(status: c_int) -> bool {
5055+
pub const fn WIFSTOPPED(status: c_int) -> bool {
50565056
_WSTATUS(status) == _WSTOPPED && WSTOPSIG(status) != 0x13
50575057
}
50585058

5059-
pub {const} fn makedev(major: i32, minor: i32) -> dev_t {
5059+
pub const fn makedev(major: i32, minor: i32) -> dev_t {
50605060
(major << 24) | minor
50615061
}
50625062

5063-
pub {const} fn major(dev: dev_t) -> i32 {
5063+
pub const fn major(dev: dev_t) -> i32 {
50645064
(dev >> 24) & 0xff
50655065
}
50665066

5067-
pub {const} fn minor(dev: dev_t) -> i32 {
5067+
pub const fn minor(dev: dev_t) -> i32 {
50685068
dev & 0xffffff
50695069
}
50705070
}

src/unix/bsd/freebsdlike/dragonfly/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ pub const RTAX_MPLS3: c_int = 10;
14221422
pub const RTAX_MAX: c_int = 11;
14231423

14241424
const_fn! {
1425-
{const} fn _CMSG_ALIGN(n: usize) -> usize {
1425+
const fn _CMSG_ALIGN(n: usize) -> usize {
14261426
(n + (size_of::<c_long>() - 1)) & !(size_of::<c_long>() - 1)
14271427
}
14281428
}
@@ -1432,7 +1432,7 @@ f! {
14321432
(cmsg as *mut c_uchar).offset(_CMSG_ALIGN(size_of::<cmsghdr>()) as isize)
14331433
}
14341434

1435-
pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
1435+
pub const fn CMSG_LEN(length: c_uint) -> c_uint {
14361436
(_CMSG_ALIGN(size_of::<cmsghdr>()) + length as usize) as c_uint
14371437
}
14381438

@@ -1448,7 +1448,7 @@ f! {
14481448
}
14491449
}
14501450

1451-
pub {const} fn CMSG_SPACE(length: c_uint) -> c_uint {
1451+
pub const fn CMSG_SPACE(length: c_uint) -> c_uint {
14521452
(_CMSG_ALIGN(size_of::<cmsghdr>()) + _CMSG_ALIGN(length as usize)) as c_uint
14531453
}
14541454

@@ -1477,11 +1477,11 @@ f! {
14771477
}
14781478

14791479
safe_f! {
1480-
pub {const} fn WIFSIGNALED(status: c_int) -> bool {
1480+
pub const fn WIFSIGNALED(status: c_int) -> bool {
14811481
(status & 0o177) != 0o177 && (status & 0o177) != 0
14821482
}
14831483

1484-
pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
1484+
pub const fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
14851485
let major = major as crate::dev_t;
14861486
let minor = minor as crate::dev_t;
14871487
let mut dev = 0;
@@ -1490,11 +1490,11 @@ safe_f! {
14901490
dev
14911491
}
14921492

1493-
pub {const} fn major(dev: crate::dev_t) -> c_int {
1493+
pub const fn major(dev: crate::dev_t) -> c_int {
14941494
((dev >> 8) & 0xff) as c_int
14951495
}
14961496

1497-
pub {const} fn minor(dev: crate::dev_t) -> c_int {
1497+
pub const fn minor(dev: crate::dev_t) -> c_int {
14981498
(dev & 0xffff00ff) as c_int
14991499
}
15001500
}

src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,17 @@ pub const MINCORE_SUPER: c_int = 0x20;
385385
pub const SPECNAMELEN: c_int = 63;
386386

387387
safe_f! {
388-
pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
388+
pub const fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
389389
let major = major as crate::dev_t;
390390
let minor = minor as crate::dev_t;
391391
(major << 8) | minor
392392
}
393393

394-
pub {const} fn major(dev: crate::dev_t) -> c_int {
394+
pub const fn major(dev: crate::dev_t) -> c_int {
395395
((dev >> 8) & 0xff) as c_int
396396
}
397397

398-
pub {const} fn minor(dev: crate::dev_t) -> c_int {
398+
pub const fn minor(dev: crate::dev_t) -> c_int {
399399
(dev & 0xffff00ff) as c_int
400400
}
401401
}

0 commit comments

Comments
 (0)