7.2.6 共享枚举
06-共享枚举 — Comprehensive Rust
1分钟内可阅读完
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/android/interoperability/cpp/shared-enums.html
7.2.6 共享枚举
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
#[cxx::bridge]
mod ffi {
enum Suit {
Clubs,
Diamonds,
Hearts,
Spades,
}
}
生成的 Rust:
| |
生成的 C++:
| |
- 在 Rust 一侧,为共享枚举生成的代码实际上是包装数值的结构体。这是因为在 C++ 中,enum class 持有与所有列出变体不同的值并不是 UB,而我们的 Rust 表示需要有相同行为。