Skip to main content

openstack_keystone_distributed_storage/proto_impl/
impl_vote.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14use std::fmt;
15
16use openraft::vote::RaftVote;
17
18use crate::TypeConfig;
19use crate::pb;
20use crate::types::LeaderId;
21
22impl RaftVote<TypeConfig> for pb::raft::Vote {
23    fn from_leader_id(leader_id: LeaderId, committed: bool) -> Self {
24        pb::raft::Vote {
25            leader_id: Some(leader_id),
26            committed,
27        }
28    }
29
30    fn leader_id(&self) -> &LeaderId {
31        self.leader_id.as_ref().expect("Vote must have a leader_id")
32    }
33
34    fn is_committed(&self) -> bool {
35        self.committed
36    }
37}
38
39impl fmt::Display for pb::raft::Vote {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        write!(
42            f,
43            "<{}:{}>",
44            self.leader_id(),
45            if self.is_committed() { "Q" } else { "-" }
46        )
47    }
48}