From b3a23a8d66d2f67c5e1a66f5ea1b45997ec06634 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Sat, 20 Jul 2024 09:05:39 +0800 Subject: [PATCH] refactor: simplify display impl & use the wrapper ty not the trait --- openraft/src/display_ext.rs | 2 +- .../display_ext/display_btreemap_opt_value.rs | 17 ++++++----------- openraft/src/metrics/raft_metrics.rs | 10 +++++----- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/openraft/src/display_ext.rs b/openraft/src/display_ext.rs index a03813a7c..aa3936f44 100644 --- a/openraft/src/display_ext.rs +++ b/openraft/src/display_ext.rs @@ -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; diff --git a/openraft/src/display_ext/display_btreemap_opt_value.rs b/openraft/src/display_ext/display_btreemap_opt_value.rs index 0b00babaa..e7e353e5f 100644 --- a/openraft/src/display_ext/display_btreemap_opt_value.rs +++ b/openraft/src/display_ext/display_btreemap_opt_value.rs @@ -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>; } diff --git a/openraft/src/metrics/raft_metrics.rs b/openraft/src/metrics/raft_metrics.rs index 1d97be98c..1226fc07d 100644 --- a/openraft/src/metrics/raft_metrics.rs +++ b/openraft/src/metrics/raft_metrics.rs @@ -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; @@ -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, "}}")?; @@ -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, "}}")?;