openraft 0.10.2

Advanced Raft consensus
Documentation
use std::fmt::Debug;
use std::fmt::Display;

use openraft_macros::since;

use crate::base::OptionalFeatures;
use crate::base::finalized::Final;
use crate::entry::RaftPayload;
use crate::log_id::LogId;
use crate::vote::RaftCommittedLeaderId;

/// Defines operations on an entry.
///
/// # Methods to inherit
///
/// An implementation defines [`new`](Self::new), [`log_id_parts`](Self::log_id_parts)
/// and [`get_membership`](Self::get_membership). It should inherit the defaults of
/// [`new_blank`](Self::new_blank), [`new_normal`](Self::new_normal),
/// [`new_membership`](Self::new_membership), [`log_id`](Self::log_id) and [`index`](Self::index).
///
/// The five defaults are bounded by `where Self: Final`, a marker private to Openraft, but the
/// bound does not prevent an override: Rust lets an implementation leave out a `where` clause of
/// the trait method. An override must return what the default returns, as each of these methods
/// states. Openraft reads the log id of an entry through `log_id_parts()`, `log_id()` and
/// `index()`, so an entry whose accessors disagree gives Openraft inconsistent log ids.
#[since(version = "0.10.2", change = "removed `set_log_id`")]
#[since(
    version = "0.10.0",
    change = "replaced `D`, `NodeId`, and `Node` associated types with `Payload`"
)]
#[since(
    version = "0.10.0",
    change = "moved membership inspection from `RaftPayload` to `RaftEntry`"
)]
#[since(
    version = "0.10.0",
    change = "removed `C: RaftTypeConfig` generic parameter, added associated types"
)]
pub trait RaftEntry
where Self: OptionalFeatures + Debug + Display
{
    /// The committed leader ID type used in log IDs.
    #[since(version = "0.10.0")]
    type CommittedLeaderId: RaftCommittedLeaderId;

    /// The payload stored in log entries.
    #[since(version = "0.10.0")]
    type Payload: RaftPayload;

    /// Create a new log entry with a log ID and configured payload.
    #[since(version = "0.10.0", change = "accept configured payload type")]
    #[since(version = "0.10.0")]
    fn new(log_id: LogId<Self::CommittedLeaderId>, payload: Self::Payload) -> Self;

    /// Returns references to the components of this entry's log ID: the committed leader ID and
    /// index.
    ///
    /// The returned tuple contains:
    /// - A reference to the committed leader ID that proposed this log entry.
    /// - The index position of this entry in the log.
    ///
    /// Note: Although these components constitute a `LogId`, this method returns them separately
    /// rather than as a reference to `LogId`. This allows implementations to store these
    /// components directly without requiring a `LogId` field in their data structure.
    #[since(version = "0.10.0")]
    fn log_id_parts(&self) -> (&Self::CommittedLeaderId, u64);

    /// Return `Some(Membership)` if this entry contains a membership payload.
    #[since(version = "0.10.0", change = "derive membership type from `Payload`")]
    #[since(version = "0.10.0")]
    fn get_membership(&self) -> Option<<Self::Payload as RaftPayload>::Membership>;

    /// Create a new blank log entry.
    ///
    /// Keep this default: an override must build the entry that
    /// `Self::new(log_id, Self::Payload::blank())` builds.
    #[since(version = "0.10.0", change = "construct configured payload type")]
    #[since(version = "0.10.0", change = "become a default method")]
    fn new_blank(log_id: LogId<Self::CommittedLeaderId>) -> Self
    where Self: Final + Sized {
        Self::new(log_id, Self::Payload::blank())
    }

    /// Create a new normal log entry that contains application data.
    ///
    /// Keep this default: an override must build the entry that
    /// `Self::new(log_id, Self::Payload::normal(data))` builds.
    #[since(version = "0.10.0", change = "construct configured payload type")]
    #[since(version = "0.10.0", change = "become a default method")]
    fn new_normal(log_id: LogId<Self::CommittedLeaderId>, data: <Self::Payload as RaftPayload>::D) -> Self
    where Self: Final + Sized {
        Self::new(log_id, Self::Payload::normal(data))
    }

    /// Create a new membership log entry.
    ///
    /// The returned instance must return `Some()` for `Self::get_membership()`.
    ///
    /// Keep this default: an override must build the entry that
    /// `Self::new(log_id, Self::Payload::membership(m))` builds.
    #[since(version = "0.10.0", change = "construct configured payload type")]
    #[since(version = "0.10.0", change = "become a default method")]
    fn new_membership(log_id: LogId<Self::CommittedLeaderId>, m: <Self::Payload as RaftPayload>::Membership) -> Self
    where Self: Final + Sized {
        Self::new(log_id, Self::Payload::membership(m))
    }

    /// Returns the `LogId` of this entry.
    ///
    /// Keep this default: an override must return the log id made of the committed leader id and
    /// the index that [`log_id_parts`](Self::log_id_parts) returns.
    #[since(version = "0.10.0")]
    fn log_id(&self) -> LogId<Self::CommittedLeaderId>
    where Self: Final {
        let (leader_id, index) = self.log_id_parts();
        LogId::new(leader_id.clone(), index)
    }

    /// Returns the index of this log entry.
    ///
    /// Keep this default: an override must return `self.log_id_parts().1`.
    #[since(version = "0.10.0")]
    fn index(&self) -> u64
    where Self: Final {
        self.log_id_parts().1
    }
}