From aab7dad3945ba0eb73a75e4782748d751d056029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Sun, 24 Mar 2024 10:38:24 +0800 Subject: [PATCH] Refactor: impl Display for Membership --- openraft/src/membership/membership.rs | 55 ++++++++++++---------- openraft/src/membership/membership_test.rs | 6 +-- openraft/src/type_config.rs | 2 +- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/openraft/src/membership/membership.rs b/openraft/src/membership/membership.rs index ff1c65fc8..4a196771d 100644 --- a/openraft/src/membership/membership.rs +++ b/openraft/src/membership/membership.rs @@ -47,50 +47,57 @@ impl fmt::Display for Membership where C: RaftTypeConfig { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.summary()) - } -} + write!(f, "{{voters:[",)?; -impl MessageSummary> for Membership -where C: RaftTypeConfig -{ - fn summary(&self) -> String { - let mut res = vec!["voters:[".to_string()]; for (i, c) in self.configs.iter().enumerate() { if i > 0 { - res.push(",".to_string()); + write!(f, ",",)?; } - res.push("{".to_string()); + write!(f, "{{",)?; for (i, node_id) in c.iter().enumerate() { if i > 0 { - res.push(",".to_string()); + write!(f, ",",)?; } - res.push(format!("{}", node_id)); + write!(f, "{node_id}:")?; - let n = self.get_node(node_id).map(|x| format!("{:?}", x)).unwrap_or_else(|| "None".to_string()); - res.push(format!(":{{{}}}", n)); + if let Some(n) = self.get_node(node_id) { + write!(f, "{n:?}")?; + } else { + write!(f, "None")?; + } } - res.push("}".to_string()); + write!(f, "}}")?; } - res.push("]".to_string()); + write!(f, "]")?; let all_node_ids = self.nodes.keys().cloned().collect::>(); let members = self.voter_ids().collect::>(); - res.push(", learners:[".to_string()); + write!(f, ", learners:[")?; + for (learner_cnt, learner_id) in all_node_ids.difference(&members).enumerate() { if learner_cnt > 0 { - res.push(",".to_string()); + write!(f, ",")?; } - res.push(format!("{}", learner_id)); - - let n = self.get_node(learner_id).map(|x| format!("{:?}", x)).unwrap_or_else(|| "None".to_string()); - res.push(format!(":{{{}}}", n)); + write!(f, "{learner_id}:")?; + if let Some(n) = self.get_node(learner_id) { + write!(f, "{n:?}")?; + } else { + write!(f, "None")?; + } } - res.push("]".to_string()); - res.join("") + write!(f, "]}}")?; + Ok(()) + } +} + +impl MessageSummary> for Membership +where C: RaftTypeConfig +{ + fn summary(&self) -> String { + self.to_string() } } diff --git a/openraft/src/membership/membership_test.rs b/openraft/src/membership/membership_test.rs index eb01af1b0..2a5ab818b 100644 --- a/openraft/src/membership/membership_test.rs +++ b/openraft/src/membership/membership_test.rs @@ -38,10 +38,10 @@ fn test_membership_summary() -> anyhow::Result<()> { }; let m = Membership::::new(vec![btreeset! {1,2}, btreeset! {3}], None); - assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}], learners:[]", m.summary()); + assert_eq!("{voters:[{1:(),2:()},{3:()}], learners:[]}", m.summary()); let m = Membership::::new(vec![btreeset! {1,2}, btreeset! {3}], Some(btreeset! {4})); - assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}], learners:[4:{()}]", m.summary()); + assert_eq!("{voters:[{1:(),2:()},{3:()}], learners:[4:()]}", m.summary()); let m = Membership::>::new_unchecked(vec![btreeset! {1,2}, btreeset! {3}], btreemap! { 1=>node("127.0.0.1", "k1"), @@ -51,7 +51,7 @@ fn test_membership_summary() -> anyhow::Result<()> { }); assert_eq!( - r#"voters:[{1:{TestNode { addr: "127.0.0.1", data: {"k1": "k1"} }},2:{TestNode { addr: "127.0.0.2", data: {"k2": "k2"} }}},{3:{TestNode { addr: "127.0.0.3", data: {"k3": "k3"} }}}], learners:[4:{TestNode { addr: "127.0.0.4", data: {"k4": "k4"} }}]"#, + r#"{voters:[{1:TestNode { addr: "127.0.0.1", data: {"k1": "k1"} },2:TestNode { addr: "127.0.0.2", data: {"k2": "k2"} }},{3:TestNode { addr: "127.0.0.3", data: {"k3": "k3"} }}], learners:[4:TestNode { addr: "127.0.0.4", data: {"k4": "k4"} }]}"#, m.summary() ); diff --git a/openraft/src/type_config.rs b/openraft/src/type_config.rs index c84a204f4..6559c4ccd 100644 --- a/openraft/src/type_config.rs +++ b/openraft/src/type_config.rs @@ -74,8 +74,8 @@ pub trait RaftTypeConfig: } #[allow(dead_code)] +/// Type alias for types used in `RaftTypeConfig`. pub mod alias { - //! Type alias for types used in `RaftTypeConfig`. pub type DOf = ::D; pub type ROf = ::R;