FIDL 除冰箱

必要條件

在本教學課程中,您將使用「建立 FIDL 程式庫」教學課程中的 fuchsia.examples FIDL 程式庫。這個 FIDL 程式庫的程式碼位於 //examples/fidl/fuchsia.examples。請花點時間檢查程式碼,再繼續下一個步驟。

總覽

本教學課程將詳細說明如何從 Rust 使用 FIDL,方法是建立單元測試,做為探索 Rust 繫結的「遊樂場」。

本文說明如何完成下列工作:

範例程式碼位於 Fuchsia 結帳頁面的 //examples/fidl/rust/fidl_crates/ 中。如果您想在學習本教學課程時撰寫所有程式碼,可以移除範例程式碼:

rm -r examples/fidl/rust/fidl_crates/*

編寫「Hello World」程式

  1. 將主要函式新增至 examples/fidl/rust/fidl_crates/src/main.rs

    fn main() {
        println!("Hello, world!");
    }
    
  2. 定義 rustc_binary,然後透過 $host_toolchain 建立測試的依附元件,這會建構主機的二進位檔。 如要這麼做,請在 examples/fidl/rust/fidl_crates/BUILD.gn 中加入下列項目:

    import("//build/rust/rustc_binary.gni")
    
    
    rustc_binary("fidl_crates_bin") {
      edition = "2024"
      sources = [ "src/main.rs" ]
    }
    
    group("fidl_crates") {
       testonly = true
       deps = [ ":fidl_crates_bin($host_toolchain)" ]
    }
    
  3. 在建構作業中加入範例

    fx set core.x64 --with //examples/fidl/rust/fidl_crates
    
  4. 建構範例

    fx build
    
  5. 執行二進位檔

    out/default/host_x64/fidl_crates_bin
    

    您應該會看到列印的 Hello World 訊息。

將 Rust FIDL 繫結新增為依附元件

針對每個 FIDL 程式庫宣告 (包括「編譯 FIDL」中的宣告),系統會在原始目標名稱後方附加 _rust,並在該名稱下產生包含該程式庫 Rust 繫結程式碼的 FIDL Crate。

參考這個產生的 Crate,新增 Rust 繫結的依附元件。新的 rustc_binary 目標應如下所示:

rustc_binary("fidl_crates_bin") {
  edition = "2024"
  deps = [ "//examples/fidl/fuchsia.examples:fuchsia.examples_rust" ]

  sources = [ "src/main.rs" ]
}

(選用) 如要查看新產生的繫結:

  1. 使用 fx build 重新建構。
  2. 切換至產生的檔案目錄: out/default/fidling/gen/examples/fidl/fuchsia.examples/fuchsia.examples。產生的程式碼位於 fidl_fuchsia_examples.rs 中。如果您已設定其他建構輸出目錄,可能需要變更 out/default。您可以使用 cat .fx-build-dir 檢查建構輸出目錄。

如要進一步瞭解如何尋找產生的繫結程式碼,請參閱「查看產生的繫結程式碼」。

在專案中使用 FIDL Rust Crate

新增測試模組和預留位置測試,建立可隨意使用產生的 FIDL Crate 的位置:

#[cfg(test)]
mod test {
   #[test]
   fn fidl_crates_usage() {

   }
}

接著,您需要設定 with_unit_tests 引數,使用測試進行建構:

rustc_binary("fidl_crates_bin") {
  edition = "2024"
  test_deps = [ "//examples/fidl/fuchsia.examples:fuchsia.examples_rust" ]
  with_unit_tests = true

  sources = [ "src/main.rs" ]
}

這會產生 fidl_crates_bin_test 目標,然後應將該目標新增至建構群組:

group("fidl_crates") {
  testonly = true
  deps = [
    ":fidl_crates_bin($host_toolchain)",
    ":fidl_crates_bin_test($host_toolchain)",
  ]
}

如要匯入 Crate,請在 tests 模組頂端新增以下內容:在 Fuchsia 樹狀結構中,FIDL Crate 通常會別名為較短的名稱,以求簡潔:

    use fidl_fuchsia_examples as fex;

使用產生的繫結程式碼

您現在可以使用產生的繫結程式碼編寫一些程式碼。如要進一步瞭解繫結,請參閱「Rust Bindings Reference」。

如要開始使用,也可以使用下方的程式碼範例。您可以在 fidl_crates_usage 測試中新增這項內容:

let flags = fex::FileMode::READ | fex::FileMode::WRITE;
println!("{:?}", flags);

let from_raw = fex::LocationType::from_primitive(1).expect("Could not create LocationType");
assert_eq!(from_raw, fex::LocationType::Museum);
assert_eq!(fex::LocationType::Restaurant.into_primitive(), 3);

let red = fex::Color { id: 0u32, name: "red".to_string() };
println!("{:?}", red);

let int_val = fex::JsonValue::IntValue(1);
let str_val = fex::JsonValue::StringValue("1".to_string());
println!("{:?}", int_val);
assert_ne!(int_val, str_val);

let user = fex::User { age: Some(20), ..Default::default() };
println!("{:?}", user);
assert!(user.age.is_some());

如要重建並重新執行測試,請執行下列指令:

fx test -vo fidl_crates_bin_test