-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added an associated type `Vote: RaftVote` to `RaftTypeConfig`, allowing applications to customize the `Vote` implementation. - Introduced the `OrdBy` trait with the method `fn ord_by() -> Option<Ordering>` to enable customized ordering for types. This trait is used internally only for determine `RaftVote` order. The ordering logic is consistent across different `Vote` implementations and does not require the application to implement `PartialOrd` directly for `Vote`. Instead, this ordering property is provided by OpenRaft. - Implemented `RaftVote` for the struct `Vote`, which serves as the default `RaftVote` implementation. This ensures that applications upgrading OpenRaft do not need to make any changes. - Part of #1278
- Loading branch information
1 parent
f8dd4d5
commit d93bd17
Showing
49 changed files
with
468 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::fmt; | ||
|
||
use openraft::vote::RaftVote; | ||
|
||
use crate::typ::*; | ||
use crate::TypeConfig; | ||
|
||
impl RaftVote<TypeConfig> for Vote { | ||
fn from_leader_id(leader_id: LeaderId, committed: bool) -> Self { | ||
Vote { | ||
leader_id: Some(leader_id), | ||
committed, | ||
} | ||
} | ||
|
||
fn leader_id(&self) -> Option<&LeaderId> { | ||
self.leader_id.as_ref() | ||
} | ||
|
||
fn is_committed(&self) -> bool { | ||
self.committed | ||
} | ||
} | ||
|
||
impl fmt::Display for pb::Vote { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!( | ||
f, | ||
"<{}:{}>", | ||
self.leader_id.as_ref().unwrap(), | ||
if self.is_committed() { "Q" } else { "-" } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//! Implements traits for protobuf types | ||
mod impl_leader_id; | ||
mod impl_vote; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/// A trait for types whose order can be determined by a key. | ||
/// | ||
/// Types implementing this trait define how they should be compared by providing a key | ||
/// that implements [`PartialOrd`]. | ||
/// | ||
/// OpenRaft uses this trait to compare types that may not be [`PartialOrd`] themselves. | ||
/// | ||
/// # Type Parameters | ||
/// - `Key<'k>`: The type of the comparison key, which must be partially ordered and must not out | ||
/// live the value. | ||
/// | ||
/// # Examples | ||
/// ```rust,ignore | ||
/// # use openraft::base::ord_by::OrdBy; | ||
/// | ||
/// struct Person { | ||
/// name: String, | ||
/// age: u32, | ||
/// } | ||
/// | ||
/// impl OrdBy<()> for Person { | ||
/// type By<'k> = &'k str; | ||
/// | ||
/// fn ord_by(&self) -> Self::By<'_> { | ||
/// &self.name | ||
/// } | ||
/// } | ||
/// ``` | ||
pub(crate) trait OrdBy<C> { | ||
/// The key type used for comparison. | ||
type By<'k>: PartialOrd + 'k | ||
where Self: 'k; | ||
|
||
/// Returns the key used for comparing this value. | ||
fn ord_by(&self) -> Self::By<'_>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.