1mod log_id_option_ext;
5mod log_index_option_ext;
6mod raft_log_id;
7
8use std::fmt::Display;
9use std::fmt::Formatter;
10
11pub use log_id_option_ext::LogIdOptionExt;
12pub use log_index_option_ext::LogIndexOptionExt;
13pub use raft_log_id::RaftLogId;
14
15use crate::CommittedLeaderId;
16use crate::MessageSummary;
17use crate::NodeId;
18
19#[derive(Debug, Default, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
24#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))]
25pub struct LogId<NID: NodeId> {
26 pub leader_id: CommittedLeaderId<NID>,
28 pub index: u64,
32}
33
34impl<NID: NodeId> RaftLogId<NID> for LogId<NID> {
35 fn get_log_id(&self) -> &LogId<NID> {
36 self
37 }
38
39 fn set_log_id(&mut self, log_id: &LogId<NID>) {
40 *self = log_id.clone()
41 }
42}
43
44impl<NID: NodeId> Display for LogId<NID> {
45 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46 write!(f, "{}-{}", self.leader_id, self.index)
47 }
48}
49
50impl<NID: NodeId> MessageSummary<LogId<NID>> for LogId<NID> {
51 fn summary(&self) -> String {
52 format!("{}", self)
53 }
54}
55
56impl<NID: NodeId> LogId<NID> {
57 pub fn new(leader_id: CommittedLeaderId<NID>, index: u64) -> Self {
59 LogId { leader_id, index }
60 }
61
62 pub fn committed_leader_id(&self) -> &CommittedLeaderId<NID> {
64 &self.leader_id
65 }
66}