Back to Comprehensive Rust

Shared Enums

src/android/interoperability/cpp/shared-enums.md

latest847 B
Original Source
<!-- Copyright 2023 Google LLC SPDX-License-Identifier: CC-BY-4.0 -->

Shared Enums

rust,ignore
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include ../../../../third_party/cxx/book/snippets.rs:shared_enums_bridge}}

Generated Rust:

rust
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include ../../../../third_party/cxx/book/snippets.rs:shared_enums_rust}}

Generated C++:

c++
{{#include ../../../../third_party/cxx/book/snippets.cc:shared_enums_cpp}}
<details>
  • On the Rust side, the code generated for shared enums is actually a struct wrapping a numeric value. This is because it is not UB in C++ for an enum class to hold a value different from all of the listed variants, and our Rust representation needs to have the same behavior.
</details>