1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

pub use crate::protos::eraftpb;

#[allow(dead_code)]
#[allow(unknown_lints)]
#[allow(clippy::all)]
#[allow(renamed_and_removed_lints)]
#[allow(bare_trait_objects)]
mod protos {
    include!(concat!(env!("OUT_DIR"), "/protos/mod.rs"));
}

pub mod prelude {
    pub use crate::eraftpb::{
        ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, MessageType,
        Snapshot, SnapshotMetadata,
    };
}

pub mod util {
    use crate::eraftpb::{ConfChange, ConfChangeType, ConfState};

    // Bring some consistency to things. The protobuf has `nodes` and it's not really a term that's used anymore.
    impl ConfState {
        /// Get the voters. This is identical to `nodes`.
        #[inline]
        pub fn get_voters(&self) -> &[u64] {
            &self.nodes
        }
    }

    impl<Iter1, Iter2> From<(Iter1, Iter2)> for ConfState
    where
        Iter1: IntoIterator<Item = u64>,
        Iter2: IntoIterator<Item = u64>,
    {
        fn from((voters, learners): (Iter1, Iter2)) -> Self {
            let mut conf_state = ConfState::default();
            conf_state.mut_nodes().extend(voters.into_iter());
            conf_state.mut_learners().extend(learners.into_iter());
            conf_state
        }
    }

    impl From<(u64, ConfState)> for ConfChange {
        fn from((start_index, state): (u64, ConfState)) -> Self {
            let mut change = ConfChange::default();
            change.set_change_type(ConfChangeType::BeginMembershipChange);
            change.set_configuration(state);
            change.start_index = start_index;
            change
        }
    }
}