Skip to content

Commit

Permalink
Replace rust-protobuf with prost (tikv#201)
Browse files Browse the repository at this point in the history
* Replace script with build script, use features

See pingcap/kvproto#349

Signed-off-by: ice1000 <[email protected]>

* Cargo fmt

Signed-off-by: ice1000 <[email protected]>

* Apply some suggestions from rust clippy

Signed-off-by: ice1000 <[email protected]>

* Remove everything related to prost

Signed-off-by: ice1000 <[email protected]>

* Refactor build.rs

Signed-off-by: ice1000 <[email protected]>

* Save progress

Signed-off-by: ice1000 <[email protected]>

* Refactor build.rs

Signed-off-by: ice1000 <[email protected]>

* Refactor build.rs

Signed-off-by: ice1000 <[email protected]>

* Save progress

Signed-off-by: ice1000 <[email protected]>

* Documentation in src/lib.rs

* Add [cfg(feature = "lib-rust-protobuf")] to every protobuf places

* Replace common.sh with rust code

Signed-off-by: ice1000 <[email protected]>

* Fix compilation error

Signed-off-by: ice1000 <[email protected]>

* Fix typo

Signed-off-by: ice1000 <[email protected]>

* Cargo fmt

Signed-off-by: ice1000 <[email protected]>

* Fix typo-caused compilation error

Signed-off-by: ice1000 <[email protected]>

* Fix compilation errors

Signed-off-by: ice1000 <[email protected]>

* Add rsprost file to git repo

Signed-off-by: ice1000 <[email protected]>

* Save progress

* Save progress

* Successfully generate!

* Refactor, only one error is left now

* Fix all errors occurred in `src`, start working on `tests`

* Test compiles, remove rust-protobuf support

* Compatible with stable rust, pass 150 tests now

Signed-off-by: ice1000 <[email protected]>

* Sounds like all tests are passed

Signed-off-by: ice1000 <[email protected]>

* Passing all doc-tests

Signed-off-by: ice1000 <[email protected]>

* Use git dependency instead of relative path

Signed-off-by: ice1000 <[email protected]>

* Partially address comments

Signed-off-by: ice1000 <[email protected]>

* Fix tests after merge

Signed-off-by: ice1000 <[email protected]>

* Rename generated mod to `prost` from `rsprost`

Signed-off-by: ice1000 <[email protected]>

* Remove `RepeatedField::from_vec`

Signed-off-by: ice1000 <[email protected]>

* Remove all uses of `protobuf::Message as Msg`

Signed-off-by: ice1000 <[email protected]>

* Remove `Codec` error and remove tests (not added for prost's errors because their constructors are private

Signed-off-by: ice1000 <[email protected]>

* Remove even more protobuf stuff, apply some minor suggestions by clippy

Signed-off-by: ice1000 <[email protected]>

* Clean up build script

Signed-off-by: ice1000 <[email protected]>

* Fix clippy warnings

Signed-off-by: ice1000 <[email protected]>

* Use `merge`

Signed-off-by: ice1000 <[email protected]>

* Add rust-protobuf codes back

Signed-off-by: ice1000 <[email protected]>

* Revert unexpected comment changes

Signed-off-by: ice1000 <[email protected]>
  • Loading branch information
ice1000 authored and nrc committed Apr 11, 2019
1 parent ca6b392 commit fc6c059
Show file tree
Hide file tree
Showing 21 changed files with 1,416 additions and 2,966 deletions.
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ documentation = "https://docs.rs/raft"
description = "The rust language implementation of Raft algorithm."
categories = ["algorithms", "database-implementations"]
edition = "2018"
build = "build.rs"

[build-dependencies]
protobuf-build = "0.3"

[features]
default = []
gen = []
# Enable failpoints
failpoint = ["fail"]

# Make sure to synchronize updates with Harness.
[dependencies]
log = ">0.2"
protobuf = "~2.0-2.2"
protobuf = "~2.1-2.2"
lazy_static = "1.3.0"
prost = "0.5"
prost-derive = "0.5"
bytes = "0.4.11"
quick-error = "1.2.2"
rand = "0.5.4"
hashbrown = "0.1"
Expand Down
81 changes: 81 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

use protobuf_build::*;
use std::fs::{read_dir, File};
use std::io::Write;

fn main() {
// This build script creates files in the `src` directory. Since that is
// outside Cargo's OUT_DIR it will cause an error when this crate is used
// as a dependency. Therefore, the user must opt-in to regenerating the
// Rust files.
if !cfg!(feature = "gen") {
println!("cargo:rerun-if-changed=build.rs");
return;
}

check_protoc_version();

let file_names: Vec<_> = read_dir("proto")
.expect("Couldn't read proto directory")
.filter_map(|e| {
let e = e.expect("Couldn't list file");
if e.file_type().expect("File broken").is_dir() {
None
} else {
Some(format!("proto/{}", e.file_name().to_string_lossy()))
}
})
.collect();

for f in &file_names {
println!("cargo:rerun-if-changed={}", f);
}

// Generate Prost files.
generate_prost_files(&file_names, "src/prost");
let mod_names = module_names_for_dir("src/prost");
generate_wrappers(
&mod_names
.iter()
.map(|m| format!("src/prost/{}.rs", m))
.collect::<Vec<_>>(),
"src/prost",
);
generate_prost_rs(&mod_names);
}

fn generate_prost_rs(mod_names: &[String]) {
let mut text = "#![allow(dead_code)]\n\
#![allow(missing_docs)]\n\
#![allow(clippy::all)]\n\n"
.to_owned();

for mod_name in mod_names {
text.push_str("pub mod ");
text.push_str(mod_name);
text.push_str("{\n");
text.push_str("include!(\"prost/");
text.push_str(mod_name);
text.push_str(".rs\");");
text.push_str("include!(\"prost/wrapper_");
text.push_str(mod_name);
text.push_str(".rs\");");
text.push_str("}\n\n");
}

let mut lib = File::create("src/prost.rs").expect("Could not create prost.rs");
lib.write_all(text.as_bytes())
.expect("Could not write prost.rs");
}
8 changes: 4 additions & 4 deletions examples/five_mem_node/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate protobuf;
extern crate prost;
extern crate raft;
extern crate regex;

Expand All @@ -23,7 +23,7 @@ use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use std::{str, thread};

use protobuf::Message as PbMessage;
use prost::Message as ProstMsg;
use raft::eraftpb::ConfState;
use raft::storage::MemStorage;
use raft::{prelude::*, StateRole};
Expand Down Expand Up @@ -224,8 +224,8 @@ fn on_ready(
}
if let EntryType::EntryConfChange = entry.get_entry_type() {
// For conf change messages, make them effective.
let mut cc = ConfChange::new();
cc.merge_from_bytes(entry.get_data()).unwrap();
let mut cc = ConfChange::new_();
ProstMsg::merge(&mut cc, entry.get_data()).unwrap();
let node_id = cc.get_node_id();
match cc.get_change_type() {
ConfChangeType::AddNode => raft_group.raft.add_node(node_id).unwrap(),
Expand Down
15 changes: 0 additions & 15 deletions generate-proto.sh

This file was deleted.

Loading

0 comments on commit fc6c059

Please sign in to comment.