關於protobuf
Google Protocol Buffer(簡稱 Protobuf)是一種輕便高效的結構化資料存放區格式,平台無關、語言無關、可擴充,可用於通訊協議和資料存放區等領域。
學習資料: Developer Guide rust中使用protobuf
下面以Ubuntu-16.04LTS為例: 一、安裝protoc 預先安裝
sudo apt-get install autoconf automake libtool curl make g++ unzip
擷取源碼,產生configure
git clone https://github.com/google/protobuf.gitcd protobufgit submodule update --init --recursive./autogen.sh
編譯安裝
./configure #By default, the package will be installed to /usr/localmakemake checksudo make installsudo ldconfig # refresh shared library cache.
安裝步驟可參考:https://github.com/google/protobuf/blob/master/src/README.md 二、安裝protoc-gen-rust外掛程式
使用cargo 安裝:
cargo install protobuf --vers 1.4.4 #1.4.4為版本號碼,可選填。預設安裝到~/.cargo/bin目錄中
還可使用源碼安裝,從github上clone源碼,編譯安裝,加入環境變數。
安裝步驟可參考:https://github.com/stepancheg/rust-protobuf/tree/master/protobuf-codegen 三、編寫proto檔案產生對應rust檔案
舉例說明(在目前的目錄下產生foo.proto對應的rust檔案):
protoc --rust_out . foo.proto
四、工程應用 在rust工程中Cargo.toml中的添加protobuf
[dependencies]protobuf = "1.4"
添加引用的crate:
extern crate protobuf;
引用相關api…… 其他 使用情境
“Protocol Buffers are not designed to handle large messages.”。protobuf對於1M以下的message有很高的效率,但是當message是大於1M的大塊資料時,protobuf的表現不是很好,使用時應當注意。
參考文檔:
Protocol Buffers - Google’s data interchange format
rust 使用 protobuf
序列化和還原序列化