Skip to content

Commit

Permalink
refactor: simplify display impl & use the wrapper ty not the trait
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC committed Jul 20, 2024
1 parent 4812120 commit b3a23a8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion openraft/src/display_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub(crate) mod display_option;
pub(crate) mod display_result;
pub(crate) mod display_slice;

#[allow(unused_imports)]
pub(crate) use display_btreemap_opt_value::DisplayBTreeMapOptValue;
#[allow(unused_imports)]
pub(crate) use display_btreemap_opt_value::DisplayBtreeMapOptValueExt;
#[allow(unused_imports)]
pub(crate) use display_instant::DisplayInstant;
Expand Down
17 changes: 6 additions & 11 deletions openraft/src/display_ext/display_btreemap_opt_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,19 @@ pub(crate) struct DisplayBTreeMapOptValue<'a, K: fmt::Display, V: fmt::Display>(

impl<'a, K: fmt::Display, V: fmt::Display> fmt::Display for DisplayBTreeMapOptValue<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let opt_idx_of_last_one = self.0.len().checked_sub(1);

match opt_idx_of_last_one {
Some(idx_of_last_one) => {
for (idx, (key, value)) in self.0.iter().enumerate() {
write!(f, "{}:{}", key, DisplayOption(value))?;
if idx != idx_of_last_one {
write!(f, ",")?;
}
}
let len = self.0.len();
for (idx, (key, value)) in self.0.iter().enumerate() {
write!(f, "{}:{}", key, DisplayOption(value))?;
if idx + 1 != len {
write!(f, ",")?;
}
None => { /* do nothing, as map length is 0 */ }
}

Ok(())
}
}

#[allow(unused)]
pub(crate) trait DisplayBtreeMapOptValueExt<'a, K: fmt::Display, V: fmt::Display> {
fn display(&'a self) -> DisplayBTreeMapOptValue<'a, K, V>;
}
Expand Down
10 changes: 5 additions & 5 deletions openraft/src/metrics/raft_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;
use std::sync::Arc;

use crate::core::ServerState;
use crate::display_ext::DisplayBtreeMapOptValueExt;
use crate::display_ext::DisplayBTreeMapOptValue;
use crate::display_ext::DisplayOption;
use crate::display_ext::DisplayOptionExt;
use crate::error::Fatal;
Expand Down Expand Up @@ -116,8 +116,8 @@ where C: RaftTypeConfig
self.membership_config,
DisplayOption(&self.snapshot),
DisplayOption(&self.purged),
DisplayOption(&self.replication.as_ref().map(DisplayBtreeMapOptValueExt::display)),
DisplayOption(&self.heartbeat.as_ref().map(DisplayBtreeMapOptValueExt::display)),
DisplayOption(&self.replication.as_ref().map(DisplayBTreeMapOptValue)),
DisplayOption(&self.heartbeat.as_ref().map(DisplayBTreeMapOptValue)),
)?;

write!(f, "}}")?;
Expand Down Expand Up @@ -200,8 +200,8 @@ where C: RaftTypeConfig
DisplayOption(&self.snapshot),
DisplayOption(&self.purged),
self.millis_since_quorum_ack.display(),
DisplayOption(&self.replication.as_ref().map(DisplayBtreeMapOptValueExt::display)),
DisplayOption(&self.heartbeat.as_ref().map(DisplayBtreeMapOptValueExt::display)),
DisplayOption(&self.replication.as_ref().map(DisplayBTreeMapOptValue)),
DisplayOption(&self.heartbeat.as_ref().map(DisplayBTreeMapOptValue)),
)?;

write!(f, "}}")?;
Expand Down

0 comments on commit b3a23a8

Please sign in to comment.