Skip to content

Commit 852882c

Browse files
committed
Remove AnyAction
1 parent 3a58393 commit 852882c

File tree

2 files changed

+14
-46
lines changed

2 files changed

+14
-46
lines changed

src/dispatcher.rs

+11-44
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
use crate::EnablingCondition;
2-
use std::collections::VecDeque;
1+
use std::{collections::VecDeque, marker::PhantomData};
32

43
pub struct Dispatcher<Action, State> {
5-
queue: VecDeque<AnyAction<Action, State>>,
4+
queue: VecDeque<Action>,
5+
_marker: PhantomData<State>,
66
}
77

8-
impl<Action, State> Dispatcher<Action, State> {
8+
impl<Action, State> Dispatcher<Action, State>
9+
where
10+
Action: crate::EnablingCondition<State>,
11+
{
912
pub fn new() -> Self {
1013
Self {
1114
queue: VecDeque::new(),
15+
_marker: Default::default(),
1216
}
1317
}
1418

1519
pub fn push<T>(&mut self, action: T)
1620
where
17-
T: 'static + EnablingCondition<State> + Into<Action> + Clone + Send,
21+
T: Into<Action>,
1822
{
19-
let wrapped_action = AnyAction::<Action, State>::new(action);
20-
self.queue.push_back(wrapped_action);
23+
self.queue.push_back(action.into());
2124
}
2225

23-
pub(crate) fn pop(&mut self) -> Option<AnyAction<Action, State>> {
26+
pub(crate) fn pop(&mut self) -> Option<Action> {
2427
self.queue.pop_front()
2528
}
2629

@@ -32,39 +35,3 @@ impl<Action, State> Dispatcher<Action, State> {
3235
.for_each(|action| self.queue.push_front(action));
3336
}
3437
}
35-
36-
trait ActionConvertible<A, S>: EnablingCondition<S> {
37-
fn convert(&self) -> A;
38-
}
39-
40-
pub(crate) struct AnyAction<A, S> {
41-
inner: Box<dyn ActionConvertible<A, S> + Send>,
42-
}
43-
44-
impl<A, S> AnyAction<A, S> {
45-
fn new<T>(action: T) -> Self
46-
where
47-
T: 'static + ActionConvertible<A, S> + Send,
48-
{
49-
Self {
50-
inner: Box::new(action),
51-
}
52-
}
53-
54-
pub fn is_enabled(&self, state: &S, time: crate::Timestamp) -> bool {
55-
self.inner.is_enabled(state, time)
56-
}
57-
58-
pub fn convert(&self) -> A {
59-
self.inner.convert()
60-
}
61-
}
62-
63-
impl<T, A, S> ActionConvertible<A, S> for T
64-
where
65-
T: Clone + Into<A> + EnablingCondition<S>,
66-
{
67-
fn convert(&self) -> A {
68-
self.clone().into()
69-
}
70-
}

src/store.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub struct Store<State, Service, Action> {
8383
impl<State, Service, Action> Store<State, Service, Action>
8484
where
8585
Service: TimeService,
86+
Action: EnablingCondition<State>,
8687
{
8788
/// Creates a new store.
8889
pub fn new(
@@ -218,7 +219,7 @@ where
218219
// Then dispatch all actions enqueued by the reducer
219220
while let Some(action) = self.dispatch_queue.pop() {
220221
if action.is_enabled(self.state(), self.last_action_id.into()) {
221-
self.dispatch_enabled(action.convert());
222+
self.dispatch_enabled(action);
222223
}
223224
}
224225
}
@@ -228,7 +229,7 @@ impl<State, Service, Action> Clone for Store<State, Service, Action>
228229
where
229230
State: Clone,
230231
Service: Clone,
231-
Action: Clone,
232+
Action: Clone + EnablingCondition<State>,
232233
{
233234
fn clone(&self) -> Self {
234235
Self {

0 commit comments

Comments
 (0)