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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! `bevy` systems and higher-order system constructors related to effects piping and composition.
use StaticSystemParam;
use *;
use crate;
use crate::;
/// `bevy` system that accepts [`Effect`]s as pipe input and performs their state transition.
///
/// Technically, this actually accepts `IntoEffectOut: Into<EffectOut<E, O>>` as pipe-input, and
/// performs the conversion implicitly (with `O=()` in the simple converted-effect case). The
/// `output: O` of the [`EffectOut<E, O>`] is returned, so this system can be piped into more
/// systems if [`EffectOut<E, O>`] output exists.
///
/// # Examples
/// ```
/// use bevy::ecs::system::assert_is_system;
/// use bevy::prelude::*;
/// use bevy_pipe_affect::prelude::*;
///
/// fn system_with_effects() -> impl Effect {
/// res_set(ClearColor(Color::BLACK))
/// }
///
/// assert_is_system(system_with_effects.pipe(affect))
/// ```
///
/// [`EffectOut<E, O>>`]: EffectOut
/// Higher-order `bevy` system constructor for composing two systems with effects via piping.
///
/// Accepts an effect-returning system `s` and returns a system that composes the effects of the
/// piped-in system and `s` using the `compose` function.
///
/// Some basic effect composition functions are provided by this library in the
/// [`effect_composition`] module.
///
/// See [`in_and_then`] for a short-hand of `in_and_then_compose(s, combine)`.
///
/// If the piped-in system returns [`EffectOut<E, O>`] instead of a simple effect, then the
/// `output: O` is passed into the given system `s`. This allows for a monad-ish API of chaining
/// many systems and piping their outputs while composing their effects.
///
/// # Examples
/// ## No output piping
/// ```
/// use bevy::ecs::system::assert_is_system;
/// use bevy::prelude::*;
/// use bevy_pipe_affect::effect_composition;
/// use bevy_pipe_affect::prelude::*;
///
/// fn system_with_effects() -> impl Effect {
/// res_set(ClearColor(Color::BLACK))
/// }
///
/// fn another_system_with_effects() -> impl Effect {
/// res_set(UiScale(2.0))
/// }
///
/// assert_is_system(
/// system_with_effects
/// .pipe(in_and_then_compose(
/// another_system_with_effects,
/// effect_composition::enibmoc, // |e1, e2| (e2, e1)
/// ))
/// .pipe(affect), // applies both effects, in reverse
/// )
/// ```
///
/// ## [`EffectOut<E, O>`] piping
/// ```
/// use bevy::ecs::system::assert_is_system;
/// use bevy::prelude::*;
/// use bevy_pipe_affect::effect_composition;
/// use bevy_pipe_affect::prelude::*;
///
/// fn system_with_effects() -> EffectOut<impl Effect, f32> {
/// effect_out(res_set(ClearColor(Color::BLACK)), 2.0)
/// }
///
/// fn another_system_with_effects(In(value): In<f32>) -> impl Effect {
/// res_set(UiScale(value))
/// }
///
/// assert_is_system(
/// system_with_effects
/// .pipe(in_and_then_compose(
/// another_system_with_effects,
/// effect_composition::enibmoc, // |e1, e2| (e2, e1)
/// ))
/// .pipe(affect), // applies both effects, in reverse
/// )
/// ```
///
/// [`EffectOut<E, O>`]: EffectOut
/// [`effect_composition`]: crate::effect_composition
/// Higher-order `bevy` system constructor for combining the effects of two systems via piping.
///
/// Accepts an effect-returning system `s` and returns a system that combines the effects of the
/// piped-in system and `s`.
///
/// To "combine" these effects just means to apply them in order, first the effect of the piped-in
/// system, then the effect of the given system. See [`in_and_then_compose`] for more effect
/// composition flexibility.
///
/// If the piped-in system returns [`EffectOut<E, O>`] instead of a simple effect, then the
/// `output: O` is passed into the given system `s`. This allows for a monad-ish API of chaining
/// many systems and piping their outputs while combining their effects.
///
/// # Examples
/// ## No output
/// ```
/// use bevy::ecs::system::assert_is_system;
/// use bevy::prelude::*;
/// use bevy_pipe_affect::prelude::*;
///
/// fn system_with_effects() -> impl Effect {
/// res_set(ClearColor(Color::BLACK))
/// }
///
/// fn another_system_with_effects() -> impl Effect {
/// res_set(UiScale(2.0))
/// }
///
/// assert_is_system(
/// system_with_effects
/// .pipe(in_and_then(another_system_with_effects))
/// .pipe(affect), // applies both effects
/// )
/// ```
///
/// ## [`EffectOut<E, O>`] piping
/// ```
/// use bevy::ecs::system::assert_is_system;
/// use bevy::prelude::*;
/// use bevy_pipe_affect::effect_composition;
/// use bevy_pipe_affect::prelude::*;
///
/// fn system_with_effects() -> EffectOut<impl Effect, f32> {
/// effect_out(res_set(ClearColor(Color::BLACK)), 2.0)
/// }
///
/// fn another_system_with_effects(In(value): In<f32>) -> impl Effect {
/// res_set(UiScale(value))
/// }
///
/// assert_is_system(
/// system_with_effects
/// .pipe(in_and_then(another_system_with_effects))
/// .pipe(affect), // applies both effects
/// )
/// ```
///
/// [`EffectOut<E, O>`]: EffectOut
/// Higher-order `bevy` system constructor for concatenating the iterator effects of two systems
/// via piping.
///
/// Accepts an iterator-effect-returning system `s` and returns a system that concatenates the
/// extendable-iterator effect of the piped-in system and `s`.
///
/// See [`in_and_then`] if you want to combine effects of heterogenous type. See
/// [`in_and_then_compose`] for more effect composition flexibility.
///
/// If the piped-in system returns [`EffectOut<E, O>`] instead of a simple effect, then the
/// `output: O` is passed into the given system `s`. This allows for a monad-ish API of chaining
/// many systems and piping their outputs while concatenating their effects.
/// Identity function for read-only-systems.
///
/// This totally-optional function can be used if you want the pureness of your systems to be
/// checked at compile time.
///
/// This only fails for bevy system parameters that aren't read-only. There may be other side
/// effects in your system still that may be unrelated to bevy, like print statements, or global
/// rust state like `OnceCell`s. There are even some `bevy` things with interior mutability that
/// will not get caught, notably `Res<AssetServer>`.
///
/// # Examples
/// An anti-example that failes to compile:
/// ```compile_fail
/// use bevy::prelude::*;
/// use bevy_pipe_affect::prelude::*;
///
/// fn my_non_read_only_system(_color: ResMut<ClearColor>) -> impl Effect + use <> {
/// // potential mutation
///
/// res_set(ClearColor::default())
/// }
///
/// App::new()
/// .add_systems(Update, pure(my_non_read_only_system).pipe(affect))
/// .run();
/// ```