FIDL 除冰箱

必要條件

在本教學課程中,您將使用建立 FIDL 程式庫教學課程中的 fuchsia.examples FIDL 程式庫。這個 FIDL 程式庫的程式碼可在 //examples/fidl/fuchsia.example 取得。請花一點時間查看程式碼,然後再繼續課程。

總覽

本教學課程詳細說明如何藉由建立單元測試來使用 Rust 的 FIDL,這個測試可做為探索 Rust 繫結的「遊樂場」。

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

範例程式碼位於 //examples/fidl/rust/fidl_crates/ 的 Fuchsia 結帳畫面中。如要按照本教學課程操作來編寫所有程式碼,您可以移除範例程式碼:

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 = "2021"
      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 繫結程式碼的 FIDL Crate 會在原始目標名稱後產生,附加在 _rust 後方。

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

rustc_binary("fidl_crates_bin") {
  edition = "2021"
  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 = "2021"
  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 繫結參考資料

您也可以使用下方的程式碼範例開始操作。您可以在 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