Skip to content

Commit 54d5823

Browse files
tgross35mbyx
andcommitted
Update the c_enum macro to take multiple values
Partial cherry pick of a541bf4 ("libc: remove uses of enum as per #4419"). Co-authored-by: mbyx <[email protected]>
1 parent 65bd617 commit 54d5823

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/macros.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,24 @@ macro_rules! e {
223223
///
224224
/// See <https://github.com/rust-lang/libc/issues/4419> for more.
225225
macro_rules! c_enum {
226-
(
226+
($(
227+
$(#[repr($repr:ty)])?
228+
pub enum $ty_name:ident {
229+
$($variant:ident $(= $value:expr)?,)+
230+
}
231+
)+) => {
232+
$(c_enum!(@expand;
233+
$(#[repr($repr)])?
234+
pub enum $ty_name {
235+
$($variant $(= $value)?,)+
236+
}
237+
);)+
238+
};
239+
240+
(@expand;
227241
$(#[repr($repr:ty)])?
228-
enum $ty_name:ident {
229-
$($variant:ident $(= $value:literal)?,)+
242+
pub enum $ty_name:ident {
243+
$($variant:ident $(= $value:expr)?,)+
230244
}
231245
) => {
232246
pub type $ty_name = c_enum!(@ty $($repr)?);
@@ -237,7 +251,7 @@ macro_rules! c_enum {
237251
(@one; $_ty_name:ident; $_idx:expr;) => {};
238252
(
239253
@one; $ty_name:ident; $default_val:expr;
240-
$variant:ident $(= $value:literal)?,
254+
$variant:ident $(= $value:expr)?,
241255
$($tail:tt)*
242256
) => {
243257
pub const $variant: $ty_name = {
@@ -413,7 +427,7 @@ mod tests {
413427
fn c_enumbasic() {
414428
// By default, variants get sequential values.
415429
c_enum! {
416-
enum e {
430+
pub enum e {
417431
VAR0,
418432
VAR1,
419433
VAR2,
@@ -430,7 +444,7 @@ mod tests {
430444
// By default, variants get sequential values.
431445
c_enum! {
432446
#[repr(u16)]
433-
enum e {
447+
pub enum e {
434448
VAR0,
435449
}
436450
}
@@ -442,7 +456,7 @@ mod tests {
442456
fn c_enumset_value() {
443457
// Setting an explicit value resets the count.
444458
c_enum! {
445-
enum e {
459+
pub enum e {
446460
VAR2 = 2,
447461
VAR3,
448462
VAR4,
@@ -459,7 +473,7 @@ mod tests {
459473
// C enums always take one more than the previous value, unless set to a specific
460474
// value. Duplicates are allowed.
461475
c_enum! {
462-
enum e {
476+
pub enum e {
463477
VAR0,
464478
VAR2_0 = 2,
465479
VAR3_0,

src/unix/linux_like/linux/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ e! {
8484
}
8585

8686
c_enum! {
87-
enum pid_type {
87+
pub enum pid_type {
8888
PIDTYPE_PID,
8989
PIDTYPE_TGID,
9090
PIDTYPE_PGID,
@@ -4468,14 +4468,12 @@ pub const RTNLGRP_STATS: c_uint = 0x24;
44684468

44694469
// linux/cn_proc.h
44704470
c_enum! {
4471-
enum proc_cn_mcast_op {
4471+
pub enum proc_cn_mcast_op {
44724472
PROC_CN_MCAST_LISTEN = 1,
44734473
PROC_CN_MCAST_IGNORE = 2,
44744474
}
4475-
}
44764475

4477-
c_enum! {
4478-
enum proc_cn_event {
4476+
pub enum proc_cn_event {
44794477
PROC_EVENT_NONE = 0x00000000,
44804478
PROC_EVENT_FORK = 0x00000001,
44814479
PROC_EVENT_EXEC = 0x00000002,

0 commit comments

Comments
 (0)