Skip to content

Commit 18cd757

Browse files
authored
Merge pull request #182 from jgalenson/enums
Expose the enum value to Rust as a field
2 parents e720e85 + db1ec31 commit 18cd757

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

macro/src/expand.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,16 @@ fn expand_enum(enm: &Enum) -> TokenStream {
137137
.unwrap_or_else(|| prev_discriminant.map_or(0, |n| n + 1));
138138
*prev_discriminant = Some(discriminant);
139139
Some(quote! {
140-
pub const #variant_ident: Self = #ident(#discriminant);
140+
pub const #variant_ident: Self = #ident { repr: #discriminant };
141141
})
142142
});
143143
quote! {
144144
#doc
145145
#[derive(Copy, Clone, PartialEq, Eq)]
146146
#[repr(transparent)]
147-
pub struct #ident(u32);
147+
pub struct #ident {
148+
pub repr: u32,
149+
}
148150

149151
#[allow(non_upper_case_globals)]
150152
impl #ident {

tests/test.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ fn test_c_return() {
5252
assert_eq!(2020, ffi::c_return_identity(2020));
5353
assert_eq!(2021, ffi::c_return_sum(2020, 1));
5454
match ffi::c_return_enum(0) {
55-
ffi::Enum::AVal => {}
55+
enm @ ffi::Enum::AVal => assert_eq!(0, enm.repr),
5656
_ => assert!(false),
5757
}
5858
match ffi::c_return_enum(1) {
59-
ffi::Enum::BVal => {}
59+
enm @ ffi::Enum::BVal => assert_eq!(2020, enm.repr),
6060
_ => assert!(false),
6161
}
6262
match ffi::c_return_enum(2021) {
63-
ffi::Enum::CVal => {}
63+
enm @ ffi::Enum::CVal => assert_eq!(2021, enm.repr),
6464
_ => assert!(false),
6565
}
6666
}
@@ -160,6 +160,13 @@ fn test_c_method_calls() {
160160
assert_eq!(old_value, unique_ptr.get2())
161161
}
162162

163+
#[test]
164+
fn test_enum_representations() {
165+
assert_eq!(0, ffi::Enum::AVal.repr);
166+
assert_eq!(2020, ffi::Enum::BVal.repr);
167+
assert_eq!(2021, ffi::Enum::CVal.repr);
168+
}
169+
163170
#[no_mangle]
164171
extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
165172
Box::into_raw(Box::new(2020usize))
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0004]: non-exhaustive patterns: `A(2u32..=std::u32::MAX)` not covered
1+
error[E0004]: non-exhaustive patterns: `A { repr: 2u32..=std::u32::MAX }` not covered
22
--> $DIR/enum_match_without_wildcard.rs:12:11
33
|
44
1 | #[cxx::bridge]
55
| -------------- `ffi::A` defined here
66
...
77
12 | match a {
8-
| ^ pattern `A(2u32..=std::u32::MAX)` not covered
8+
| ^ pattern `A { repr: 2u32..=std::u32::MAX }` not covered
99
|
1010
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1111
= note: the matched value is of type `ffi::A`

0 commit comments

Comments
 (0)