use std::io;
use futures_util::Stream;
use openraft_macros::add_async_trait;
use openraft_macros::since;
use crate::OptionalSend;
use crate::OptionalSync;
use crate::RaftSnapshotBuilder;
use crate::RaftTypeConfig;
use crate::storage::EntryResponder;
use crate::type_config::alias::LogIdOf;
use crate::type_config::alias::SnapshotMetaOf;
use crate::type_config::alias::SnapshotOf;
use crate::type_config::alias::StoredMembershipOf;
#[add_async_trait]
pub trait RaftStateMachine<C>: OptionalSend + OptionalSync + 'static
where C: RaftTypeConfig
{
#[since(version = "0.10.0", change = "moved from RaftTypeConfig to RaftStateMachine")]
type SnapshotData: OptionalSend + 'static;
#[since(version = "0.10.0", change = "uses the state machine SnapshotData")]
type SnapshotBuilder: RaftSnapshotBuilder<C, SnapshotData = Self::SnapshotData>;
async fn applied_state(&mut self) -> Result<(Option<LogIdOf<C>>, StoredMembershipOf<C>), io::Error>;
#[since(version = "0.10.0", change = "Entry-Responder-Result stream")]
async fn apply<Strm>(&mut self, entries: Strm) -> Result<(), io::Error>
where Strm: Stream<Item = Result<EntryResponder<C>, io::Error>> + Unpin + OptionalSend;
#[since(version = "0.10.0")]
async fn try_create_snapshot_builder(&mut self, force: bool) -> Option<Self::SnapshotBuilder> {
let _ = force;
Some(self.get_snapshot_builder().await)
}
#[since(version = "0.10.0", change = "deprecated, use `try_create_snapshot_builder` instead")]
async fn get_snapshot_builder(&mut self) -> Self::SnapshotBuilder;
#[since(version = "0.10.0", change = "SnapshotData without Box")]
async fn begin_receiving_snapshot(&mut self) -> Result<Self::SnapshotData, io::Error>;
#[since(version = "0.10.0", change = "SnapshotData without Box")]
async fn install_snapshot(
&mut self,
meta: &SnapshotMetaOf<C>,
snapshot: Self::SnapshotData,
) -> Result<(), io::Error>;
async fn get_current_snapshot(&mut self) -> Result<Option<SnapshotOf<C, Self::SnapshotData>>, io::Error>;
}
#[cfg(test)]
fn unit_state_machine_error() -> io::Error {
io::Error::new(
io::ErrorKind::Unsupported,
"unit type does not implement a usable state machine",
)
}
#[cfg(test)]
impl<C> RaftSnapshotBuilder<C> for ()
where C: RaftTypeConfig
{
type SnapshotData = ();
async fn build_snapshot(&mut self) -> Result<SnapshotOf<C, ()>, io::Error> {
Err(unit_state_machine_error())
}
}
#[cfg(test)]
impl<C> RaftStateMachine<C> for ()
where C: RaftTypeConfig
{
type SnapshotData = ();
type SnapshotBuilder = ();
async fn applied_state(&mut self) -> Result<(Option<LogIdOf<C>>, StoredMembershipOf<C>), io::Error> {
Err(unit_state_machine_error())
}
async fn apply<Strm>(&mut self, _entries: Strm) -> Result<(), io::Error>
where Strm: Stream<Item = Result<EntryResponder<C>, io::Error>> + Unpin + OptionalSend {
Err(unit_state_machine_error())
}
async fn get_snapshot_builder(&mut self) -> Self::SnapshotBuilder {}
async fn begin_receiving_snapshot(&mut self) -> Result<Self::SnapshotData, io::Error> {
Err(unit_state_machine_error())
}
async fn install_snapshot(
&mut self,
_meta: &SnapshotMetaOf<C>,
_snapshot: Self::SnapshotData,
) -> Result<(), io::Error> {
Err(unit_state_machine_error())
}
async fn get_current_snapshot(&mut self) -> Result<Option<SnapshotOf<C, Self::SnapshotData>>, io::Error> {
Err(unit_state_machine_error())
}
}