7.2.5 共享类型

05-共享类型 — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/android/interoperability/cpp/shared-types.html

7.2.5 共享类型

// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
#[cxx::bridge]
mod ffi {
    #[derive(Clone, Debug, Hash)]
    struct PlayingCard {
        suit: Suit,
        value: u8,  // A=1, J=11, Q=12, K=13
    }

    enum Suit {
        Clubs,
        Diamonds,
        Hearts,
        Spades,
    }
}
  • 只支持类 C(unit)枚举。
  • 共享类型上的 #[derive()] 只支持有限数量的 trait。对应功能也会为 C++ 代码生成,例如若你 derive Hash,也会为对应的 C++ 类型生成 std::hash 实现。
最后修改 August 11, 2026: 更新 (70a5af133)