Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the enum value to Rust as a field #182

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,16 @@ fn expand_enum(enm: &Enum) -> TokenStream {
.unwrap_or_else(|| prev_discriminant.map_or(0, |n| n + 1));
*prev_discriminant = Some(discriminant);
Some(quote! {
pub const #variant_ident: Self = #ident(#discriminant);
pub const #variant_ident: Self = #ident { repr: #discriminant };
})
});
quote! {
#doc
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct #ident(u32);
pub struct #ident {
pub repr: u32,
}

#[allow(non_upper_case_globals)]
impl #ident {
Expand Down
13 changes: 10 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ fn test_c_return() {
assert_eq!(2020, ffi::c_return_identity(2020));
assert_eq!(2021, ffi::c_return_sum(2020, 1));
match ffi::c_return_enum(0) {
ffi::Enum::AVal => {}
enm @ ffi::Enum::AVal => assert_eq!(0, enm.repr),
_ => assert!(false),
}
match ffi::c_return_enum(1) {
ffi::Enum::BVal => {}
enm @ ffi::Enum::BVal => assert_eq!(2020, enm.repr),
_ => assert!(false),
}
match ffi::c_return_enum(2021) {
ffi::Enum::CVal => {}
enm @ ffi::Enum::CVal => assert_eq!(2021, enm.repr),
_ => assert!(false),
}
}
Expand Down Expand Up @@ -160,6 +160,13 @@ fn test_c_method_calls() {
assert_eq!(old_value, unique_ptr.get2())
}

#[test]
fn test_enum_representations() {
assert_eq!(0, ffi::Enum::AVal.repr);
assert_eq!(2020, ffi::Enum::BVal.repr);
assert_eq!(2021, ffi::Enum::CVal.repr);
}

#[no_mangle]
extern "C" fn cxx_test_suite_get_box() -> *mut cxx_test_suite::R {
Box::into_raw(Box::new(2020usize))
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/enum_match_without_wildcard.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0004]: non-exhaustive patterns: `A(2u32..=std::u32::MAX)` not covered
error[E0004]: non-exhaustive patterns: `A { repr: 2u32..=std::u32::MAX }` not covered
--> $DIR/enum_match_without_wildcard.rs:12:11
|
1 | #[cxx::bridge]
| -------------- `ffi::A` defined here
...
12 | match a {
| ^ pattern `A(2u32..=std::u32::MAX)` not covered
| ^ pattern `A { repr: 2u32..=std::u32::MAX }` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `ffi::A`