Skip to content

Commit

Permalink
Refactor: Node no longer requires Default
Browse files Browse the repository at this point in the history
Openraft internally does not require `Node` to implement `Default`.
`Default for Node` is only required in tests. Therefore in this commit
the `Default` is removed from trait `Node` and the contraint is added to
test suite codes where `Default` is required to build a `node` for
testing.
  • Loading branch information
drmingdrmer committed Dec 18, 2024
1 parent c875b95 commit 409ef4b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
10 changes: 9 additions & 1 deletion openraft/src/engine/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ use crate::RaftTypeConfig;

/// Trivial Raft type config for Engine related unit tests,
/// with an optional custom node type `N` for Node type.
#[derive(Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub(crate) struct UTConfig<N = ()> {
_p: std::marker::PhantomData<N>,
}

impl<N> Default for UTConfig<N> {
fn default() -> Self {
Self {
_p: std::marker::PhantomData,
}
}
}

impl<N> Clone for UTConfig<N> {
fn clone(&self) -> Self {
*self
Expand Down
13 changes: 12 additions & 1 deletion openraft/src/membership/membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::RaftTypeConfig;
///
/// It could be a joint of one, two or more configs, i.e., a quorum is a node set that is superset
/// of a majority of every config.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))]
pub struct Membership<C>
where C: RaftTypeConfig
Expand All @@ -35,6 +35,17 @@ where C: RaftTypeConfig
pub(crate) nodes: BTreeMap<C::NodeId, C::Node>,
}

impl<C> Default for Membership<C>
where C: RaftTypeConfig
{
fn default() -> Self {
Membership {
configs: vec![],
nodes: BTreeMap::new(),
}
}
}

impl<C> From<BTreeMap<C::NodeId, C::Node>> for Membership<C>
where C: RaftTypeConfig
{
Expand Down
5 changes: 1 addition & 4 deletions openraft/src/metrics/wait_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ where C: RaftTypeConfig {
current_leader: None,
millis_since_quorum_ack: None,
last_quorum_acked: None,
membership_config: Arc::new(StoredMembership::new(
None,
Membership::new_with_defaults(vec![btreeset! {}], []),
)),
membership_config: Arc::new(StoredMembership::new(None, Membership::default())),
heartbeat: None,

snapshot: None,
Expand Down
4 changes: 2 additions & 2 deletions openraft/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ impl<T> NodeId for T where T: Sized
/// network address, but the used [`Node`] implementation can be customized to include additional
/// information.
pub trait Node
where Self: Sized + OptionalFeatures + Eq + PartialEq + Debug + Clone + Default + 'static
where Self: Sized + OptionalFeatures + Eq + PartialEq + Debug + Clone + 'static
{
}

impl<T> Node for T where T: Sized + OptionalFeatures + Eq + PartialEq + Debug + Clone + Default + 'static {}
impl<T> Node for T where T: Sized + OptionalFeatures + Eq + PartialEq + Debug + Clone + 'static {}

/// EmptyNode is an implementation of trait [`Node`] that contains nothing.
///
Expand Down
5 changes: 4 additions & 1 deletion openraft/src/testing/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ pub fn membership_ent<C: RaftTypeConfig>(
node_id: C::NodeId,
index: u64,
config: Vec<BTreeSet<C::NodeId>>,
) -> crate::Entry<C> {
) -> crate::Entry<C>
where
C::Node: Default,
{
crate::Entry::new_membership(
LogId::new(CommittedLeaderId::new(term, node_id), index),
crate::Membership::new_with_defaults(config, []),
Expand Down
6 changes: 5 additions & 1 deletion openraft/src/testing/log/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ where
C::D: Debug,
C::R: Debug,
C::NodeId: From<u64>,
C::Node: Default,
LS: RaftLogStorage<C>,
SM: RaftStateMachine<C>,
B: StoreBuilder<C, LS, SM, G>,
Expand Down Expand Up @@ -1391,7 +1392,10 @@ where C::NodeId: From<u64> {

/// Create a membership entry with node_id 0 for test.
fn membership_ent_0<C: RaftTypeConfig>(term: u64, index: u64, bs: BTreeSet<C::NodeId>) -> C::Entry
where C::NodeId: From<u64> {
where
C::NodeId: From<u64>,
C::Node: Default,
{
C::Entry::new_membership(log_id_0(term, index), Membership::new_with_defaults(vec![bs], []))
}

Expand Down

0 comments on commit 409ef4b

Please sign in to comment.