use openraft_macros::since;
use crate::LogIndexOptionExt;
use crate::log_id::raft_log_id::RaftLogId;
#[since(
version = "0.10.2",
change = "`next_index()` panics on index `u64::MAX` in all builds"
)]
#[since(version = "0.10.0", change = "removed `C: RaftTypeConfig` generic parameter")]
pub trait LogIdOptionExt {
fn index(&self) -> Option<u64>;
fn next_index(&self) -> u64;
}
impl<T> LogIdOptionExt for Option<T>
where T: RaftLogId
{
fn index(&self) -> Option<u64> {
self.as_ref().map(|x| x.index())
}
fn next_index(&self) -> u64 {
self.index().next_index()
}
}
#[cfg(test)]
mod tests {
use super::LogIdOptionExt;
use crate::engine::testing::UTConfig;
use crate::engine::testing::log_id;
use crate::type_config::alias::LogIdOf;
#[test]
fn test_next_index_returns_representable_successors() {
assert_eq!(0, None::<LogIdOf<UTConfig>>.next_index());
assert_eq!(u64::MAX, Some(log_id(1, 1, u64::MAX - 1)).next_index());
}
#[test]
#[should_panic(expected = "u64::MAX has no next log index")]
fn test_next_index_panics_at_max() {
Some(log_id(1, 1, u64::MAX)).next_index();
}
}