Skip to content

Commit 0ecd2f3

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 a556b42 commit 0ecd2f3

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
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,

0 commit comments

Comments
 (0)