Преглед изворни кода

Create a dedicated trait for request snapshot function.

Jing Yang пре 4 година
родитељ
комит
cc8f061609
2 измењених фајлова са 14 додато и 12 уклоњено
  1. 4 7
      src/lib.rs
  2. 10 5
      src/snapshot.rs

+ 4 - 7
src/lib.rs

@@ -24,7 +24,7 @@ pub(crate) use crate::raft_state::RaftState;
 pub(crate) use crate::raft_state::State;
 pub use crate::rpcs::RpcClient;
 pub use crate::snapshot::Snapshot;
-use crate::snapshot::SnapshotDaemon;
+use crate::snapshot::{RequestSnapshotFnMut, SnapshotDaemon};
 use crate::utils::retry_rpc;
 
 mod apply_command;
@@ -133,17 +133,14 @@ where
     ///
     /// Each instance will create at least 3 + (number of peers) threads. The
     /// extensive usage of threads is to minimize latency.
-    pub fn new<RequestSnapshotFunc>(
+    pub fn new(
         peers: Vec<RpcClient>,
         me: usize,
         persister: Arc<dyn Persister>,
         apply_command: impl ApplyCommandFnMut<Command>,
         max_state_size_bytes: Option<usize>,
-        request_snapshot: RequestSnapshotFunc,
-    ) -> Self
-    where
-        RequestSnapshotFunc: 'static + Send + FnMut(Index) -> Snapshot,
-    {
+        request_snapshot: impl RequestSnapshotFnMut,
+    ) -> Self {
         let peer_size = peers.len();
         let mut state = RaftState {
             current_term: Term(0),

+ 10 - 5
src/snapshot.rs

@@ -12,6 +12,13 @@ pub(crate) struct SnapshotDaemon {
     unparker: Option<Unparker>,
 }
 
+pub trait RequestSnapshotFnMut:
+    'static + Send + FnMut(Index) -> Snapshot
+{
+}
+
+impl<T: 'static + Send + FnMut(Index) -> Snapshot> RequestSnapshotFnMut for T {}
+
 impl SnapshotDaemon {
     pub(crate) fn trigger(&self) {
         match &self.unparker {
@@ -22,13 +29,11 @@ impl SnapshotDaemon {
 }
 
 impl<C: 'static + Clone + Default + Send + serde::Serialize> Raft<C> {
-    pub(crate) fn run_snapshot_daemon<Func>(
+    pub(crate) fn run_snapshot_daemon(
         &mut self,
         max_state_size: Option<usize>,
-        mut request_snapshot: Func,
-    ) where
-        Func: 'static + Send + FnMut(Index) -> Snapshot,
-    {
+        mut request_snapshot: impl RequestSnapshotFnMut,
+    ) {
         let max_state_size = match max_state_size {
             Some(max_state_size) => max_state_size,
             None => return,