remote_raft.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use async_trait::async_trait;
  2. use crate::{
  3. AppendEntriesArgs, AppendEntriesReply, InstallSnapshotArgs,
  4. InstallSnapshotReply, RequestVoteArgs, RequestVoteReply,
  5. };
  6. #[async_trait]
  7. pub trait RemoteRaft<Command>: Send + Sync {
  8. async fn request_vote(
  9. &self,
  10. args: RequestVoteArgs,
  11. ) -> std::io::Result<RequestVoteReply>;
  12. async fn append_entries(
  13. &self,
  14. args: AppendEntriesArgs<Command>,
  15. ) -> std::io::Result<AppendEntriesReply>;
  16. async fn install_snapshot(
  17. &self,
  18. args: InstallSnapshotArgs,
  19. ) -> std::io::Result<InstallSnapshotReply>;
  20. }
  21. #[async_trait]
  22. impl<Command, R> RemoteRaft<Command> for R
  23. where
  24. Command: Send + 'static,
  25. R: AsRef<dyn RemoteRaft<Command>> + Send + Sync,
  26. {
  27. async fn request_vote(
  28. &self,
  29. args: RequestVoteArgs,
  30. ) -> std::io::Result<RequestVoteReply> {
  31. self.as_ref().request_vote(args).await
  32. }
  33. async fn append_entries(
  34. &self,
  35. args: AppendEntriesArgs<Command>,
  36. ) -> std::io::Result<AppendEntriesReply> {
  37. self.as_ref().append_entries(args).await
  38. }
  39. async fn install_snapshot(
  40. &self,
  41. args: InstallSnapshotArgs,
  42. ) -> std::io::Result<InstallSnapshotReply> {
  43. self.as_ref().install_snapshot(args).await
  44. }
  45. }