From c875b955b007b98c29e2e588a4ba6107e1bbcc55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Sat, 14 Dec 2024 12:44:04 +0800 Subject: [PATCH] Refactor: remove NodeIdEssential and NodeEssential traits These two traits are used to simplify NodeId and Node constraints. But with `Optional*` traits, these two are no longer needed. --- openraft/src/base/mod.rs | 19 +++++++++- openraft/src/node.rs | 82 +++++++++++++--------------------------- 2 files changed, 43 insertions(+), 58 deletions(-) diff --git a/openraft/src/base/mod.rs b/openraft/src/base/mod.rs index 5fffeeda5..43d621ab8 100644 --- a/openraft/src/base/mod.rs +++ b/openraft/src/base/mod.rs @@ -14,9 +14,13 @@ mod threaded { use std::future::Future; use std::pin::Pin; + /// A trait that is empty if the `singlethreaded` feature flag is enabled, + /// otherwise it extends `Send`. pub trait OptionalSend: Send {} impl OptionalSend for T {} + /// A trait that is empty if the `singlethreaded` feature flag is enabled, + /// otherwise it extends `Sync`. pub trait OptionalSync: Sync {} impl OptionalSync for T {} @@ -32,9 +36,13 @@ mod threaded { use std::future::Future; use std::pin::Pin; + /// A trait that is empty if the `singlethreaded` feature flag is enabled, + /// otherwise it extends `Send`. pub trait OptionalSend {} impl OptionalSend for T {} + /// A trait that is empty if the `singlethreaded` feature flag is enabled, + /// otherwise it extends `Sync`. pub trait OptionalSync {} impl OptionalSync for T {} @@ -46,14 +54,21 @@ mod threaded { #[cfg(not(feature = "serde"))] mod serde_able { - #[doc(hidden)] + /// A trait that extends `Serialize` and `Deserialize` if the `serde` feature flag + /// is enabled, otherwise it is empty trait. pub trait OptionalSerde {} impl OptionalSerde for T {} } #[cfg(feature = "serde")] mod serde_able { - #[doc(hidden)] + /// A trait that extends `Serialize` and `Deserialize` if the `serde` feature flag + /// is enabled, otherwise it is empty trait. pub trait OptionalSerde: serde::Serialize + for<'a> serde::Deserialize<'a> {} impl OptionalSerde for T where T: serde::Serialize + for<'a> serde::Deserialize<'a> {} } + +/// A trait that combines all optional features. +pub trait OptionalFeatures: OptionalSend + OptionalSync + OptionalSerde {} + +impl OptionalFeatures for T where T: OptionalSend + OptionalSync + OptionalSerde {} diff --git a/openraft/src/node.rs b/openraft/src/node.rs index e512a623d..ef3b16696 100644 --- a/openraft/src/node.rs +++ b/openraft/src/node.rs @@ -3,31 +3,29 @@ use std::fmt::Display; use std::fmt::Formatter; use std::hash::Hash; -use crate::OptionalSend; -use crate::OptionalSync; +use crate::base::OptionalFeatures; -/// Essential trait bound for node-id, except serde. -#[doc(hidden)] -pub trait NodeIdEssential: - Sized - + OptionalSend - + OptionalSync - + Eq - + PartialEq - + Ord - + PartialOrd - + Debug - + Display - + Hash - + Clone - + Default - + 'static +/// A Raft node's ID. +/// +/// A `NodeId` uniquely identifies a node in the Raft cluster. +pub trait NodeId +where Self: Sized + + OptionalFeatures + + Eq + + PartialEq + + Ord + + PartialOrd + + Debug + + Display + + Hash + + Clone + + Default + + 'static { } -impl NodeIdEssential for T where T: Sized - + OptionalSend - + OptionalSync +impl NodeId for T where T: Sized + + OptionalFeatures + Eq + PartialEq + Ord @@ -42,45 +40,17 @@ impl NodeIdEssential for T where T: Sized { } -/// A Raft node's ID. -/// -/// A `NodeId` uniquely identifies a node in the Raft cluster. -#[cfg(feature = "serde")] -pub trait NodeId: NodeIdEssential + serde::Serialize + for<'a> serde::Deserialize<'a> {} - -#[cfg(feature = "serde")] -impl NodeId for T where T: NodeIdEssential + serde::Serialize + for<'a> serde::Deserialize<'a> {} - -#[cfg(not(feature = "serde"))] -pub trait NodeId: NodeIdEssential {} - -#[cfg(not(feature = "serde"))] -impl NodeId for T where T: NodeIdEssential {} - -/// Essential trait bound for application level node-data, except serde. -pub trait NodeEssential: - Sized + OptionalSend + OptionalSync + Eq + PartialEq + Debug + Clone + Default + 'static -{ -} -impl NodeEssential for T where T: Sized + OptionalSend + OptionalSync + Eq + PartialEq + Debug + Clone + Default + 'static -{} - /// A Raft `Node`, this trait holds all relevant node information. /// -/// For the most generic case `BasicNode` provides an example implementation including the node's -/// network address, but the used `Node` implementation can be customized to include additional +/// For the most generic case [`BasicNode`] provides an example implementation including the node's +/// network address, but the used [`Node`] implementation can be customized to include additional /// information. -#[cfg(feature = "serde")] -pub trait Node: NodeEssential + serde::Serialize + for<'a> serde::Deserialize<'a> {} - -#[cfg(feature = "serde")] -impl Node for T where T: NodeEssential + serde::Serialize + for<'a> serde::Deserialize<'a> {} - -#[cfg(not(feature = "serde"))] -pub trait Node: NodeEssential {} +pub trait Node +where Self: Sized + OptionalFeatures + Eq + PartialEq + Debug + Clone + Default + 'static +{ +} -#[cfg(not(feature = "serde"))] -impl Node for T where T: NodeEssential {} +impl Node for T where T: Sized + OptionalFeatures + Eq + PartialEq + Debug + Clone + Default + 'static {} /// EmptyNode is an implementation of trait [`Node`] that contains nothing. ///