构建 Rust Protobuf

描述如何使用 Blaze 构建 Rust Protobuf。

为 Protobuf 定义构建 Rust 库的过程与其他编程语言类似

  1. 使用语言无关的 proto_library 规则

    proto_library(
        name = "person_proto",
        srcs = ["person.proto"],
    )
    
  2. 创建一个 Rust 库

    load("//third_party/protobuf/rust:defs.bzl", "rust_proto_library")
    
    proto_library(
        name = "person_proto",
        srcs = ["person.proto"],
    )
    
    rust_proto_library(
        name = "person_rust_proto",
        deps = [":person_proto"],
    )
    
  3. 通过将其包含在 Rust 二进制文件中来使用该库

    load("//third_party/bazel_rules/rules_rust/rust:defs.bzl", "rust_binary")
    load("//third_party/protobuf/rust:defs.bzl", "rust_proto_library")
    
    proto_library(
        name = "person_proto",
        srcs = ["person.proto"],
    )
    
    rust_proto_library(
        name = "person_rust_proto",
        deps = [":person_proto"],
    )
    
    rust_binary(
        name = "greet",
        srcs = ["greet.rs"],
        deps = [
            ":person_rust_proto",
        ],
    )