Skip to content

Commit 7ba7760

Browse files
authored
Move protos to a separate crate (#247)
* Move protos to a separate crate Makes Raft a workspace project Signed-off-by: Nick Cameron <[email protected]> * Satisfy Rustfmt Signed-off-by: Nick Cameron <[email protected]>
1 parent 4fd768d commit 7ba7760

File tree

11 files changed

+83
-59
lines changed

11 files changed

+83
-59
lines changed

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ documentation = "https://docs.rs/raft"
1111
description = "The rust language implementation of Raft algorithm."
1212
categories = ["algorithms", "database-implementations"]
1313
edition = "2018"
14-
build = "build.rs"
1514

16-
[build-dependencies]
17-
protobuf-build = "0.6"
15+
[workspace]
16+
members = ["proto"]
1817

1918
[features]
2019
default = []
21-
gen = []
2220
# Enable failpoints
2321
failpoint = ["fail"]
2422

@@ -32,6 +30,7 @@ prost-derive = "0.5"
3230
bytes = "0.4.11"
3331
slog = "2.2"
3432
quick-error = "1.2.2"
33+
raft-proto = { path = "proto" }
3534
rand = "0.6.4"
3635
hashbrown = "0.3"
3736
fail = { version = "0.2", optional = true }

harness/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ mod network;
4242
pub use self::{interface::Interface, network::Network};
4343
use slog::{Drain, Logger};
4444

45-
4645
/// Build a logger for tests.
4746
///
4847
/// Currently, this is a terminal log. It ensures it is only initialized once to prevent clobbering.

proto/Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "raft-proto"
3+
version = "0.1.0"
4+
authors = ["The TiKV Project Developers"]
5+
edition = "2018"
6+
license = "Apache-2.0"
7+
keywords = ["raft", "distributed-systems", "ha"]
8+
repository = "https://github.com/pingcap/raft-rs"
9+
homepage = "https://github.com/pingcap/raft-rs"
10+
documentation = "https://docs.rs/raft-proto"
11+
description = "Protocol definitions for the rust language implementation of the Raft algorithm."
12+
categories = ["algorithms", "database-implementations"]
13+
build = "build.rs"
14+
15+
[features]
16+
default = []
17+
gen = []
18+
19+
[build-dependencies]
20+
protobuf-build = "0.6"
21+
22+
[dependencies]
23+
bytes = "0.4.11"
24+
lazy_static = "1.3.0"
25+
prost = "0.5"
26+
prost-derive = "0.5"
27+
protobuf = "2"

build.rs renamed to proto/build.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
// Copyright 2019 PingCAP, Inc.
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// See the License for the specific language governing permissions and
12-
// limitations under the License.
1+
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
132

143
use protobuf_build::*;
154
use std::fs::{read_dir, File};
@@ -44,7 +33,7 @@ fn main() {
4433
println!("cargo:rerun-if-changed={}", f);
4534
}
4635

47-
// Generate Prost files.
36+
// Generate Prost output.
4837
generate_prost_files(&file_names, "src/prost");
4938
let mod_names = module_names_for_dir("src/prost");
5039
generate_wrappers(
File renamed without changes.

proto/src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2+
3+
pub use crate::prost::eraftpb;
4+
5+
mod prost;
6+
7+
pub mod prelude {
8+
pub use crate::eraftpb::{
9+
ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, MessageType,
10+
Snapshot, SnapshotMetadata,
11+
};
12+
}
13+
14+
pub mod util {
15+
use crate::eraftpb::{ConfChange, ConfChangeType, ConfState};
16+
17+
// Bring some consistency to things. The protobuf has `nodes` and it's not really a term that's used anymore.
18+
impl ConfState {
19+
/// Get the voters. This is identical to `nodes`.
20+
#[inline]
21+
pub fn get_voters(&self) -> &[u64] {
22+
&self.nodes
23+
}
24+
}
25+
26+
impl<Iter1, Iter2> From<(Iter1, Iter2)> for ConfState
27+
where
28+
Iter1: IntoIterator<Item = u64>,
29+
Iter2: IntoIterator<Item = u64>,
30+
{
31+
fn from((voters, learners): (Iter1, Iter2)) -> Self {
32+
let mut conf_state = ConfState::default();
33+
conf_state.mut_nodes().extend(voters.into_iter());
34+
conf_state.mut_learners().extend(learners.into_iter());
35+
conf_state
36+
}
37+
}
38+
39+
impl From<(u64, ConfState)> for ConfChange {
40+
fn from((start_index, state): (u64, ConfState)) -> Self {
41+
let mut change = ConfChange::default();
42+
change.set_change_type(ConfChangeType::BeginMembershipChange);
43+
change.set_configuration(state);
44+
change.set_start_index(start_index);
45+
change
46+
}
47+
}
48+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,6 @@ extern crate quick_error;
382382
extern crate getset;
383383

384384
mod config;
385-
mod prost;
386-
/// This module supplies the needed message types. However, it is autogenerated and thus cannot be
387-
/// documented by field.
388-
pub use crate::prost::eraftpb;
389385
mod errors;
390386
mod log_unstable;
391387
mod progress;
@@ -412,6 +408,7 @@ pub use self::raw_node::{is_empty_snap, Peer, RawNode, Ready, SnapshotStatus};
412408
pub use self::read_only::{ReadOnlyOption, ReadState};
413409
pub use self::status::Status;
414410
pub use self::storage::{RaftState, Storage};
411+
pub use raft_proto::eraftpb;
415412
use slog::{Drain, Logger};
416413

417414
pub mod prelude {
@@ -427,10 +424,7 @@ pub mod prelude {
427424
//!
428425
//! The prelude may grow over time as additional items see ubiquitous use.
429426
430-
pub use crate::eraftpb::{
431-
ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, MessageType,
432-
Snapshot, SnapshotMetadata,
433-
};
427+
pub use raft_proto::prelude::*;
434428

435429
pub use crate::config::Config;
436430
pub use crate::raft::Raft;

0 commit comments

Comments
 (0)