//! Define the configuration of types used by the Raft, such as [`NodeId`], log [`Entry`], etc. //! //! [`NodeId`]: `RaftTypeConfig::NodeId` //! [`Entry`]: `RaftTypeConfig::Entry` pub(crate) mod util; use std::fmt::Debug; pub use util::TypeConfigExt; use crate::entry::FromAppData; use crate::entry::RaftEntry; use crate::raft::responder::Responder; use crate::AppData; use crate::AppDataResponse; use crate::AsyncRuntime; use crate::Node; use crate::NodeId; use crate::OptionalSend; use crate::OptionalSync; /// Configuration of types used by the [`Raft`] core engine. /// /// The (empty) implementation structure defines request/response types, node ID type /// and the like. Refer to the documentation of associated types for more information. /// /// ## Note /// /// Since Rust cannot automatically infer traits for various inner types using this config /// type as a parameter, this trait simply uses all the traits required for various types /// as its supertraits as a workaround. To ease the declaration, the macro /// `declare_raft_types` is provided, which can be used to declare the type easily. /// /// Example: /// ```ignore /// openraft::declare_raft_types!( /// pub TypeConfig: /// D = ClientRequest, /// R = ClientResponse, /// NodeId = u64, /// Node = openraft::BasicNode, /// Entry = openraft::Entry, /// SnapshotData = Cursor>, /// AsyncRuntime = openraft::TokioRuntime, /// ); /// ``` /// [`Raft`]: crate::Raft pub trait RaftTypeConfig: Sized + OptionalSend + OptionalSync + Debug + Clone + Copy + Default + Eq + PartialEq + Ord + PartialOrd + 'static { /// Application-specific request data passed to the state machine. type D: AppData; /// Application-specific response data returned by the state machine. type R: AppDataResponse; /// A Raft node's ID. type NodeId: NodeId; /// Raft application level node data type Node: Node; /// Raft log entry, which can be built from an AppData. type Entry: RaftEntry + FromAppData; /// Snapshot data for exposing a snapshot for reading & writing. /// /// See the [storage chapter of the guide][sto] for details on log compaction / snapshotting. /// /// [sto]: crate::docs::getting_started#3-implement-raftlogstorage-and-raftstatemachine #[cfg(not(feature = "generic-snapshot-data"))] type SnapshotData: tokio::io::AsyncRead + tokio::io::AsyncWrite + tokio::io::AsyncSeek + OptionalSend + Unpin + 'static; #[cfg(feature = "generic-snapshot-data")] type SnapshotData: OptionalSend + 'static; /// Asynchronous runtime type. type AsyncRuntime: AsyncRuntime; /// Send the response or error of a client write request([`WriteResult`]). /// /// For example, return [`WriteResult`] the to the caller of [`Raft::client_write`], or send to /// some application defined channel. /// /// [`Raft::client_write`]: `crate::raft::Raft::client_write` /// [`WriteResult`]: `crate::raft::message::ClientWriteResult` type Responder: Responder; } #[allow(dead_code)] /// Type alias for types used in `RaftTypeConfig`. pub(crate) mod alias { use crate::raft::responder::Responder; use crate::RaftTypeConfig; pub type DOf = ::D; pub type ROf = ::R; pub type NodeIdOf = ::NodeId; pub type NodeOf = ::Node; pub type EntryOf = ::Entry; pub type SnapshotDataOf = ::SnapshotData; pub type AsyncRuntimeOf = ::AsyncRuntime; pub type ResponderOf = ::Responder; pub type ResponderReceiverOf = as Responder>::Receiver; pub type JoinErrorOf = as crate::AsyncRuntime>::JoinError; pub type JoinHandleOf = as crate::AsyncRuntime>::JoinHandle; pub type SleepOf = as crate::AsyncRuntime>::Sleep; pub type InstantOf = as crate::AsyncRuntime>::Instant; pub type TimeoutErrorOf = as crate::AsyncRuntime>::TimeoutError; pub type TimeoutOf = as crate::AsyncRuntime>::Timeout; pub type OneshotSenderOf = as crate::AsyncRuntime>::OneshotSender; pub type OneshotReceiverErrorOf = as crate::AsyncRuntime>::OneshotReceiverError; pub type OneshotReceiverOf = as crate::AsyncRuntime>::OneshotReceiver; // Usually used types pub type LogIdOf = crate::LogId>; pub type VoteOf = crate::Vote>; pub type LeaderIdOf = crate::LeaderId>; pub type CommittedLeaderIdOf = crate::CommittedLeaderId>; }