1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{
objects::{Object, Objects, WithHandle},
storage::Handle,
};
use super::{Service, State};
impl State for Objects {
type Command = InsertObject;
type Event = ObjectToInsert;
fn decide(&self, command: Self::Command, events: &mut Vec<Self::Event>) {
let event = ObjectToInsert {
object: command.object,
};
events.push(event);
}
fn evolve(&mut self, event: &Self::Event) {
event.object.clone().insert(self);
}
}
#[derive(Clone, Debug)]
pub struct InsertObject {
pub object: Object<WithHandle>,
}
#[derive(Clone, Debug)]
pub struct ObjectToInsert {
pub object: Object<WithHandle>,
}
pub trait ServiceObjectsExt {
fn insert<T>(&mut self, handle: Handle<T>, object: T)
where
(Handle<T>, T): Into<Object<WithHandle>>;
}
impl ServiceObjectsExt for Service<Objects> {
fn insert<T>(&mut self, handle: Handle<T>, object: T)
where
(Handle<T>, T): Into<Object<WithHandle>>,
{
self.execute(InsertObject {
object: (handle, object).into(),
});
}
}