openraft/
node.rs

1use std::fmt::Debug;
2use std::fmt::Display;
3use std::fmt::Formatter;
4use std::hash::Hash;
5
6use crate::OptionalSend;
7use crate::OptionalSync;
8
9/// Essential trait bound for node-id, except serde.
10#[doc(hidden)]
11pub trait NodeIdEssential:
12    Sized
13    + OptionalSend
14    + OptionalSync
15    + Eq
16    + PartialEq
17    + Ord
18    + PartialOrd
19    + Debug
20    + Display
21    + Hash
22    + Clone
23    + Default
24    + 'static
25{
26}
27
28impl<T> NodeIdEssential for T where T: Sized
29        + OptionalSend
30        + OptionalSync
31        + Eq
32        + PartialEq
33        + Ord
34        + PartialOrd
35        + Debug
36        + Display
37        + Hash
38        + Copy
39        + Clone
40        + Default
41        + 'static
42{
43}
44
45/// A Raft node's ID.
46///
47/// A `NodeId` uniquely identifies a node in the Raft cluster.
48#[cfg(feature = "serde")]
49pub trait NodeId: NodeIdEssential + serde::Serialize + for<'a> serde::Deserialize<'a> {}
50
51#[cfg(feature = "serde")]
52impl<T> NodeId for T where T: NodeIdEssential + serde::Serialize + for<'a> serde::Deserialize<'a> {}
53
54#[cfg(not(feature = "serde"))]
55pub trait NodeId: NodeIdEssential {}
56
57#[cfg(not(feature = "serde"))]
58impl<T> NodeId for T where T: NodeIdEssential {}
59
60/// Essential trait bound for application level node-data, except serde.
61pub trait NodeEssential:
62    Sized + OptionalSend + OptionalSync + Eq + PartialEq + Debug + Clone + Default + 'static
63{
64}
65impl<T> NodeEssential for T where T: Sized + OptionalSend + OptionalSync + Eq + PartialEq + Debug + Clone + Default + 'static
66{}
67
68/// A Raft `Node`, this trait holds all relevant node information.
69///
70/// For the most generic case `BasicNode` provides an example implementation including the node's
71/// network address, but the used `Node` implementation can be customized to include additional
72/// information.
73#[cfg(feature = "serde")]
74pub trait Node: NodeEssential + serde::Serialize + for<'a> serde::Deserialize<'a> {}
75
76#[cfg(feature = "serde")]
77impl<T> Node for T where T: NodeEssential + serde::Serialize + for<'a> serde::Deserialize<'a> {}
78
79#[cfg(not(feature = "serde"))]
80pub trait Node: NodeEssential {}
81
82#[cfg(not(feature = "serde"))]
83impl<T> Node for T where T: NodeEssential {}
84
85/// EmptyNode is an implementation of trait [`Node`] that contains nothing.
86///
87/// Such a node store nothing but is just a place holder.
88#[derive(Debug, Clone, Default, PartialEq, Eq)]
89#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
90pub struct EmptyNode {}
91
92impl EmptyNode {
93    /// Creates an [`EmptyNode`].
94    pub fn new() -> Self {
95        Self {}
96    }
97}
98
99impl Display for EmptyNode {
100    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
101        write!(f, "{{}}")
102    }
103}
104
105/// An implementation of trait [`Node`] that contains minimal node information.
106///
107/// The most common usage is to store the connecting address of a node.
108/// So that an application does not need an additional store to support its
109/// [`RaftNetwork`](crate::RaftNetwork) implementation.
110///
111/// An application is also free not to use this storage and implements its own node-id to address
112/// mapping.
113#[derive(Debug, Clone, Default, PartialEq, Eq)]
114#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
115pub struct BasicNode {
116    /// User defined string that represent the endpoint of the target node.
117    ///
118    /// It is used by [`RaftNetwork`](crate::RaftNetwork) for connecting to target node.
119    pub addr: String,
120}
121
122impl BasicNode {
123    /// Creates as [`BasicNode`].
124    pub fn new(addr: impl ToString) -> Self {
125        Self { addr: addr.to_string() }
126    }
127}
128
129impl Display for BasicNode {
130    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
131        write!(f, "{}", self.addr)
132    }
133}