pub struct PersistentState {
pub current_term: Term,
pub voted_for: Option<NodeId>,
pub log: RaftLog,
}Expand description
Persistent state on all servers
Updated on stable storage before responding to RPCs
Fields§
§current_term: TermLatest term server has seen (initialized to 0, increases monotonically)
voted_for: Option<NodeId>Candidate ID that received vote in current term (or None)
log: RaftLogLog entries (each entry contains command and term)
Implementations§
Source§impl PersistentState
impl PersistentState
Sourcepub fn increment_term(&mut self)
pub fn increment_term(&mut self)
Increment the current term
Sourcepub fn update_term(&mut self, term: Term) -> bool
pub fn update_term(&mut self, term: Term) -> bool
Update term if the given term is higher
Sourcepub fn can_vote_for(&self, candidate_id: &NodeId) -> bool
pub fn can_vote_for(&self, candidate_id: &NodeId) -> bool
Check if vote can be granted for the given candidate
Sourcepub fn to_bytes(&self) -> Result<Vec<u8>, EncodeError>
pub fn to_bytes(&self) -> Result<Vec<u8>, EncodeError>
Serialize state to bytes for persistence
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, DecodeError>
Deserialize state from bytes
Trait Implementations§
Source§impl Clone for PersistentState
impl Clone for PersistentState
Source§fn clone(&self) -> PersistentState
fn clone(&self) -> PersistentState
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for PersistentState
impl Debug for PersistentState
Source§impl Default for PersistentState
impl Default for PersistentState
Source§impl<'de> Deserialize<'de> for PersistentState
impl<'de> Deserialize<'de> for PersistentState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for PersistentState
impl RefUnwindSafe for PersistentState
impl Send for PersistentState
impl Sync for PersistentState
impl Unpin for PersistentState
impl UnwindSafe for PersistentState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more