CXX 的限制

使用 CXX 時,最實用的頁面是型別參照

CXX 基本上適用下列情況:

  • 您的 Rust-C++ 介面非常簡單,您可以宣告其中所有項目。
  • 您只使用 CXX 已原生支援的型別,例如 std::unique_ptrstd::string&[u8] 等。

它有許多限制,例如不支援 Rust 的 Option 型別。

這些限制會導致我們只能在 Chromium 中將 Rust 用於妥善隔離的「葉節點」,而非用於任意 Rust-C++ 互通情形。考慮 Chromium 中 Rust 的用途時,建議先草擬語言邊界的 CXX 繫結,瞭解是否足夠簡單。

In addition, right now, Rust code in one component cannot depend on Rust code in another, due to linking details in our component build. That's another reason to restrict Rust to use in leaf nodes.

您也應討論一些 CXX 的其他棘手問題,例如:

  • 其錯誤處理方式是以 C++ 例外狀況為根據 (請見下一張投影片)
  • 函式指標不容易使用。