openraft/log_id/
log_id_option_ext.rs

1use crate::LogId;
2use crate::NodeId;
3
4/// This helper trait extracts information from an `Option<LogId>`.
5pub trait LogIdOptionExt {
6    /// Returns the log index if it is not a `None`.
7    fn index(&self) -> Option<u64>;
8
9    /// Returns the next log index.
10    ///
11    /// If self is `None`, it returns 0.
12    fn next_index(&self) -> u64;
13}
14
15impl<NID: NodeId> LogIdOptionExt for Option<LogId<NID>> {
16    fn index(&self) -> Option<u64> {
17        self.as_ref().map(|x| x.index)
18    }
19
20    fn next_index(&self) -> u64 {
21        match self {
22            None => 0,
23            Some(log_id) => log_id.index + 1,
24        }
25    }
26}
27
28impl<NID: NodeId> LogIdOptionExt for Option<&LogId<NID>> {
29    fn index(&self) -> Option<u64> {
30        self.map(|x| x.index)
31    }
32
33    fn next_index(&self) -> u64 {
34        match self {
35            None => 0,
36            Some(log_id) => log_id.index + 1,
37        }
38    }
39}