raftstore: move duplicated code into exists macro
@@ -55,7 +55,7 @@ enum FsmTypes<N, C> {
// A macro to introduce common definition of scheduler.
macro_rules! impl_sched {
- ($name:ident) => {
+ ($name:ident, $ty:path, Fsm = $fsm:tt) => {
pub struct $name<N, C> {
sender: channel::Sender<FsmTypes<N, C>>,
}
@@ -68,21 +68,19 @@ macro_rules! impl_sched {
}
}
}
- };
-}
-
-impl_sched!(NormalScheduler);
-impl_sched!(ControlScheduler);
-impl<N: Fsm, C> FsmScheduler for NormalScheduler<N, C> {
- type Fsm = N;
+ impl<N, C> FsmScheduler for $name<N, C>
+ where
+ $fsm: Fsm,
+ {
+ type Fsm = $fsm;
#[inline]
- fn schedule(&self, fsm: Box<N>) {
- match self.sender.send(FsmTypes::Normal(fsm)) {
+ fn schedule(&self, fsm: Box<Self::Fsm>) {
+ match self.sender.send($ty(fsm)) {
Ok(()) => return,
// TODO: use debug instead.
- Err(SendError(FsmTypes::Normal(fsm))) => warn!("failed to schedule fsm {:p}", fsm),
+ Err(SendError($ty(fsm))) => warn!("failed to schedule fsm {:p}", fsm),
_ => unreachable!(),
}
}
@@ -95,25 +93,11 @@ impl<N: Fsm, C> FsmScheduler for NormalScheduler<N, C> {
}
}
}
-
-impl<N, C: Fsm> FsmScheduler for ControlScheduler<N, C> {
- type Fsm = C;
-
- #[inline]
- fn schedule(&self, fsm: Box<C>) {
- match self.sender.send(FsmTypes::Control(fsm)) {
- Ok(()) => return,
- Err(SendError(FsmTypes::Control(fsm))) => warn!("failed to schedule fsm {:p}", fsm),
- _ => unreachable!(),
- }
+ };
}
- fn shutdown(&self) {
- for _ in 0..100 {
- let _ = self.sender.send(FsmTypes::Empty);
- }
- }
-}
+impl_sched!(NormalScheduler, FsmTypes::Normal, Fsm = N);
+impl_sched!(ControlScheduler, FsmTypes::Control, Fsm = C);
/// A basic struct for a round of polling.
#[allow(clippy::vec_box)]
