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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::error::Error;
use std::fmt;
use std::marker::PhantomData;
use {Command, Merge};
#[macro_export]
macro_rules! merge {
($cmd1:expr, $cmd2:expr) => (
$crate::Merged::new($cmd1, $cmd2)
);
($cmd1:expr, $cmd2:expr, $($tail:expr),+) => (
merge![$crate::Merged::new($cmd1, $cmd2), $($tail),*]
);
}
pub struct Merged<R, C1: Command<R> + 'static, C2: Command<R> + 'static> {
cmd1: C1,
cmd2: C2,
_marker: PhantomData<Box<dyn Command<R> + 'static>>,
}
impl<R, C1: Command<R> + 'static, C2: Command<R> + 'static> Merged<R, C1, C2> {
#[inline]
pub fn new(cmd1: C1, cmd2: C2) -> Merged<R, C1, C2> {
Merged {
cmd1,
cmd2,
_marker: PhantomData,
}
}
#[inline]
pub fn into_commands(self) -> (C1, C2) {
(self.cmd1, self.cmd2)
}
}
impl<R, C1: Command<R> + 'static, C2: Command<R> + 'static> Command<R> for Merged<R, C1, C2> {
#[inline]
fn apply(&mut self, receiver: &mut R) -> Result<(), Box<dyn Error + Send + Sync>> {
self.cmd1.apply(receiver)?;
self.cmd2.apply(receiver)
}
#[inline]
fn undo(&mut self, receiver: &mut R) -> Result<(), Box<dyn Error + Send + Sync>> {
self.cmd2.undo(receiver)?;
self.cmd1.undo(receiver)
}
#[inline]
fn redo(&mut self, receiver: &mut R) -> Result<(), Box<dyn Error + Send + Sync>> {
self.cmd1.redo(receiver)?;
self.cmd2.redo(receiver)
}
#[inline]
fn merge(&self) -> Merge {
self.cmd1.merge()
}
}
impl<R, C1: Command<R> + 'static, C2: Command<R> + 'static> fmt::Debug for Merged<R, C1, C2> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Merged")
.field("cmd1", &self.cmd1)
.field("cmd2", &self.cmd2)
.finish()
}
}
#[cfg(feature = "display")]
impl<R, C1: Command<R> + 'static, C2: Command<R> + 'static> fmt::Display for Merged<R, C1, C2> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{cmd1} + {cmd2}", cmd1 = self.cmd1, cmd2 = self.cmd2)
}
}