forked from databendlabs/openraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: refine code based on PR feedback
- Loading branch information
1 parent
2e02fc1
commit 90162d0
Showing
7 changed files
with
41 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,54 @@ | ||
use std::fmt; | ||
use std::ops::RangeInclusive; | ||
|
||
use crate::type_config::alias::LogIdOf; | ||
use crate::RaftTypeConfig; | ||
|
||
/// The first and the last log id belonging to a Leader. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub(crate) struct LeaderLogIds<C: RaftTypeConfig> { | ||
first_last: Option<(LogIdOf<C>, LogIdOf<C>)>, | ||
log_id_range: Option<RangeInclusive<LogIdOf<C>>>, | ||
} | ||
|
||
impl<C> fmt::Display for LeaderLogIds<C> | ||
where C: RaftTypeConfig | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match &self.first_last { | ||
match &self.log_id_range { | ||
None => write!(f, "None"), | ||
Some((first, last)) => write!(f, "({}, {})", first, last), | ||
Some(rng) => write!(f, "({}, {})", rng.start(), rng.end()), | ||
} | ||
} | ||
} | ||
|
||
impl<C> LeaderLogIds<C> | ||
where C: RaftTypeConfig | ||
{ | ||
pub(crate) fn new(log_ids: Option<(LogIdOf<C>, LogIdOf<C>)>) -> Self { | ||
Self { first_last: log_ids } | ||
pub(crate) fn new(log_id_range: Option<RangeInclusive<LogIdOf<C>>>) -> Self { | ||
Self { log_id_range } | ||
} | ||
|
||
/// Used only in tests | ||
#[allow(dead_code)] | ||
pub(crate) fn new1(log_id: LogIdOf<C>) -> Self { | ||
pub(crate) fn new_single(log_id: LogIdOf<C>) -> Self { | ||
Self { | ||
first_last: Some((log_id.clone(), log_id)), | ||
log_id_range: Some(log_id.clone()..=log_id), | ||
} | ||
} | ||
|
||
/// Used only in tests | ||
#[allow(dead_code)] | ||
pub(crate) fn new2(first: LogIdOf<C>, last: LogIdOf<C>) -> Self { | ||
pub(crate) fn new_start_end(first: LogIdOf<C>, last: LogIdOf<C>) -> Self { | ||
Self { | ||
first_last: Some((first, last)), | ||
log_id_range: Some(first..=last), | ||
} | ||
} | ||
|
||
pub(crate) fn first(&self) -> Option<&LogIdOf<C>> { | ||
self.first_last.as_ref().map(|x| &x.0) | ||
self.log_id_range.as_ref().map(|x| x.start()) | ||
} | ||
|
||
pub(crate) fn last(&self) -> Option<&LogIdOf<C>> { | ||
self.first_last.as_ref().map(|x| &x.1) | ||
self.log_id_range.as_ref().map(|x| x.end()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters