Skip to content

Commit f29e25d

Browse files
committed
Allow anonymous c_enum!s
1 parent f2fe0f0 commit f29e25d

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

ci/style.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ while IFS= read -r file; do
3737
# rust-lang/rustfmt#5464).
3838
perl -pi -e 's/^(\s*)(.*)\{const\}/$1\/\* FMT-CONST \*\/\n$1$2const/g' "$file"
3939

40+
# The `c_enum!` macro allows anonymous enums without names, which isn't
41+
# valid syntax. Replace it with a dummy name and an indicator comment.
42+
perl -pi -e 's/^(\s*)(.*)enum(\s*\{)/$1\/\* FMT-ANON-ENUM \*\/\n$1$2enum _Anon$3/g' "$file"
43+
4044
# Format the file. We need to invoke `rustfmt` directly since `cargo fmt`
4145
# can't figure out the module tree with the hacks in place.
4246
failed=false
@@ -46,6 +50,7 @@ while IFS= read -r file; do
4650
perl -pi -e 's/fn (\w+)_fmt_tmp\(\)/$1!/g' "$file"
4751
perl -pi -0777 -e 's/cfg_tmp!\(\[(.*?)\]\)/#[cfg($1)]/gms' "$file"
4852
perl -pi -0777 -e 's/\/\* FMT-CONST \*\/(?:\n\s*)?(.*?)const/$1\{const\}/gms' "$file"
53+
perl -pi -0777 -e 's/\/\* FMT-ANON-ENUM \*\/(?:\n\s*)?(.*?)enum _Anon(\s*\{)/$1enum$2/gms' "$file"
4954

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

src/macros.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -233,32 +233,33 @@ macro_rules! e {
233233
macro_rules! c_enum {
234234
($(
235235
$(#[repr($repr:ty)])?
236-
pub enum $ty_name:ident {
236+
pub enum $($ty_name:ident)? {
237237
$($variant:ident $(= $value:expr)?,)+
238238
}
239239
)+) => {
240-
$(c_enum!(@expand;
241-
$(#[repr($repr)])?
242-
pub enum $ty_name {
243-
$($variant $(= $value)?,)+
244-
}
245-
);)+
240+
$(c_enum!(@expand_repr; $($repr)?; $($ty_name)?; $($variant $(= $value)?,)+);)+
246241
};
247242

248-
(@expand;
249-
$(#[repr($repr:ty)])?
250-
pub enum $ty_name:ident {
251-
$($variant:ident $(= $value:expr)?,)+
252-
}
253-
) => {
254-
pub type $ty_name = c_enum!(@ty $($repr)?);
255-
c_enum!(@one; $ty_name; 0; $($variant $(= $value)?,)+);
243+
(@expand_repr; ; $($ty_name:ident)?; $($variants:tt)*) => {
244+
// Use a specific type if provided, otherwise default to `CEnumRepr`
245+
c_enum!(@expand_tyname; $crate::prelude::CEnumRepr; $($ty_name)?; $($variants)*);
246+
};
247+
(@expand_repr; $repr:ty; $($ty_name:ident)?; $($variants:tt)*) => {
248+
c_enum!(@expand_tyname; $repr; $($ty_name)?; $($variants)*);
249+
};
250+
251+
(@expand_tyname; $repr:ty; ; $($variants:tt)*) => {
252+
c_enum!(@one; $repr; 0; $($variants)*);
253+
};
254+
(@expand_tyname; $repr:ty; $ty_name:ident; $($variants:tt)*) => {
255+
pub type $ty_name = $repr;
256+
c_enum!(@one; $ty_name; 0; $($variants)*);
256257
};
257258

258259
// Matcher for a single variant
259-
(@one; $_ty_name:ident; $_idx:expr;) => {};
260+
(@one; $_ty_name:ty; $_idx:expr;) => {};
260261
(
261-
@one; $ty_name:ident; $default_val:expr;
262+
@one; $ty_name:ty; $default_val:expr;
262263
$variant:ident $(= $value:expr)?,
263264
$($tail:tt)*
264265
) => {
@@ -273,10 +274,6 @@ macro_rules! c_enum {
273274
// set explicitly.
274275
c_enum!(@one; $ty_name; $variant + 1; $($tail)*);
275276
};
276-
277-
// Use a specific type if provided, otherwise default to `CEnumRepr`
278-
(@ty $repr:ty) => { $repr };
279-
(@ty) => { $crate::prelude::CEnumRepr };
280277
}
281278

282279
// This is a pretty horrible hack to allow us to conditionally mark some functions as 'const',
@@ -449,7 +446,7 @@ mod tests {
449446
// C enums always take one more than the previous value, unless set to a specific
450447
// value. Duplicates are allowed.
451448
c_enum! {
452-
pub enum e {
449+
pub enum {
453450
VAR0,
454451
VAR2_0 = 2,
455452
VAR3_0,

0 commit comments

Comments
 (0)