Skip to content

Commit 28f612d

Browse files
committed
Update the repr type for C enums
Partial cherry pick of 192bccb ("ctest: add suport for c enum").
1 parent ffa9170 commit 28f612d

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/macros.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ macro_rules! prelude {
8787
pub(crate) use mem::{align_of, align_of_val, size_of, size_of_val};
8888

8989
#[allow(unused_imports)]
90-
pub(crate) use crate::types::Padding;
90+
pub(crate) use crate::types::{CEnumRepr, Padding};
9191
// Commonly used types defined in this crate
9292
#[allow(unused_imports)]
9393
pub(crate) use crate::{
@@ -274,9 +274,9 @@ macro_rules! c_enum {
274274
c_enum!(@one; $ty_name; $variant + 1; $($tail)*);
275275
};
276276

277-
// Use a specific type if provided, otherwise default to `c_uint`
277+
// Use a specific type if provided, otherwise default to `CEnumRepr`
278278
(@ty $repr:ty) => { $repr };
279-
(@ty) => { $crate::c_uint };
279+
(@ty) => { $crate::prelude::CEnumRepr };
280280
}
281281

282282
// This is a pretty horrible hack to allow us to conditionally mark some functions as 'const',
@@ -431,6 +431,8 @@ macro_rules! deprecated_mach {
431431

432432
#[cfg(test)]
433433
mod tests {
434+
use crate::types::CEnumRepr;
435+
434436
#[test]
435437
fn c_enumbasic() {
436438
// By default, variants get sequential values.
@@ -442,9 +444,9 @@ mod tests {
442444
}
443445
}
444446

445-
assert_eq!(VAR0, 0_u32);
446-
assert_eq!(VAR1, 1_u32);
447-
assert_eq!(VAR2, 2_u32);
447+
assert_eq!(VAR0, 0 as CEnumRepr);
448+
assert_eq!(VAR1, 1 as CEnumRepr);
449+
assert_eq!(VAR2, 2 as CEnumRepr);
448450
}
449451

450452
#[test]
@@ -471,9 +473,9 @@ mod tests {
471473
}
472474
}
473475

474-
assert_eq!(VAR2, 2_u32);
475-
assert_eq!(VAR3, 3_u32);
476-
assert_eq!(VAR4, 4_u32);
476+
assert_eq!(VAR2, 2 as CEnumRepr);
477+
assert_eq!(VAR3, 3 as CEnumRepr);
478+
assert_eq!(VAR4, 4 as CEnumRepr);
477479
}
478480

479481
#[test]
@@ -492,12 +494,12 @@ mod tests {
492494
}
493495
}
494496

495-
assert_eq!(VAR0, 0_u32);
496-
assert_eq!(VAR2_0, 2_u32);
497-
assert_eq!(VAR3_0, 3_u32);
498-
assert_eq!(VAR4_0, 4_u32);
499-
assert_eq!(VAR2_1, 2_u32);
500-
assert_eq!(VAR3_1, 3_u32);
501-
assert_eq!(VAR4_1, 4_u32);
497+
assert_eq!(VAR0, 0 as CEnumRepr);
498+
assert_eq!(VAR2_0, 2 as CEnumRepr);
499+
assert_eq!(VAR3_0, 3 as CEnumRepr);
500+
assert_eq!(VAR4_0, 4 as CEnumRepr);
501+
assert_eq!(VAR2_1, 2 as CEnumRepr);
502+
assert_eq!(VAR3_1, 3 as CEnumRepr);
503+
assert_eq!(VAR4_1, 4 as CEnumRepr);
502504
}
503505
}

src/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ impl<T: Copy> Default for Padding<T> {
1616
Self(MaybeUninit::zeroed())
1717
}
1818
}
19+
20+
/// The default repr type used for C style enums in Rust.
21+
#[cfg(target_env = "msvc")]
22+
#[allow(unused)]
23+
pub(crate) type CEnumRepr = c_int;
24+
#[cfg(not(target_env = "msvc"))]
25+
#[allow(unused)]
26+
pub(crate) type CEnumRepr = c_uint;

0 commit comments

Comments
 (0)