syntax = "proto3";
package tutorial;
message Person {
string name = 1;
int32 id = 2; // unique id number for this person
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneNumber type = 2;
}
repeated PhoneNumber phones = 4;
}
// our address book file is just one of these
message AddressBook {
repeated Person people = 1;
}
生成的代码
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Person {
#[prost(string, tag="1")]
pub name: ::prost::alloc::string::String,
/// unique id number for this person
#[prost(int32, tag="2")]
pub id: i32,
#[prost(string, tag="3")]
pub email: ::prost::alloc::string::String,
#[prost(message, repeated, tag="4")]
pub phones: ::prost::alloc::vec::Vec<person::PhoneNumber>,
}
/// Nested message and enum types in `Person`.
pub mod person {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PhoneNumber {
#[prost(string, tag="1")]
pub number: ::prost::alloc::string::String,
#[prost(message, optional, boxed, tag="2")]
pub r#type: ::core::option::Option<::prost::alloc::boxed::Box<PhoneNumber>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum PhoneType {
Mobile = 0,
Home = 1,
Work = 2,
}
}
/// our address book file is just one of these
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AddressBook {
#[prost(message, repeated, tag="1")]
pub people: ::prost::alloc::vec::Vec<Person>,
}
PS. cargo build script doc cargo build 在编译之前会先编译 build.rs 文件并执行,The script may communicate with Cargo by printing specially formatted commands prefixed with cargo: to stdout,比如 println!("cargo:rerun-if-changed={}", file_name);,默认 package 文件任何一个变更都会重新执行