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
//! Buffer of asynchronous actions.
mod buf;
pub(crate) use self::buf::CommandsBuf;
use std::future::Future;
use iced_futures::MaybeSend;
use iced_native::Command;
/// Send commands to an iced application.
#[doc(hidden)]
pub trait Commands<T> {
/// Type of the reborrowed command buffer.
///
/// See [`Commands::by_ref`].
type ByRef<'this>: Commands<T>
where
Self: 'this;
/// Helper to generically reborrow the command buffer mutably.
///
/// This is useful if you have a function that takes `mut commands: impl
/// Commands<T>` and you want to use a method such as [Commands::map] which
/// would otherwise consume the command buffer.
///
/// This can still be done through an expression like `(&mut
/// commands).map(/* */)`, but having a method like this reduces the number
/// of references involves in case the `impl Command<T>` is already a
/// reference.
///
/// Note that naming is inspired by [`Iterator::by_ref`].
///
/// # Examples
///
/// ```
/// # use ontv::commands::Commands;
/// enum Message {
/// Component1(component1::Message),
/// Component2(component2::Message),
/// }
///
/// fn update(mut commands: impl Commands<Message>) {
/// component1::update(commands.by_ref().map(Message::Component1));
/// component2::update(commands.by_ref().map(Message::Component2));
/// }
///
/// mod component1 {
/// # use ontv::commands::Commands;
/// pub(crate) enum Message {
/// Tick,
/// }
///
/// pub(crate) fn update(mut commands: impl Commands<Message>) {
/// // emit commands
/// }
/// }
///
/// mod component2 {
/// # use ontv::commands::Commands;
/// pub(crate) enum Message {
/// Tick,
/// }
///
/// pub(crate) fn update(mut commands: impl Commands<Message>) {
/// // emit commands
/// }
/// }
/// ```
///
/// Without this method, you'd have to do the following while also
/// potentially constructing another reference that you don't really need:
///
/// ```
/// # use ontv::commands::Commands;
/// # enum Message { Component1(component1::Message), Component2(component2::Message) }
/// fn update(mut commands: impl Commands<Message>) {
/// component1::update((&mut commands).map(Message::Component1));
/// component2::update((&mut commands).map(Message::Component2));
/// }
/// # mod component1 {
/// # use ontv::commands::Commands;
/// # pub(crate) enum Message { Tick }
/// # pub(crate) fn update(mut commands: impl Commands<Message>) { }
/// # }
/// # mod component2 {
/// # use ontv::commands::Commands;
/// # pub(crate) enum Message { Tick }
/// # pub(crate) fn update(mut commands: impl Commands<Message>) { }
/// # }
/// ```
fn by_ref(&mut self) -> Self::ByRef<'_>;
/// Perform a single asynchronous action and map its output into the
/// expected message type `T`.
///
/// # Examples
///
/// ```
/// # use ontv::commands::Commands;
/// enum Message {
/// Greeting(String),
/// }
///
/// async fn asynchronous_update() -> String {
/// "Hello World".to_string()
/// }
///
/// fn update(mut commands: impl Commands<Message>) {
/// commands.perform(asynchronous_update(), Message::Greeting);
/// }
/// ```
fn perform<F>(&mut self, future: F, map: impl Fn(F::Output) -> T + MaybeSend + Sync + 'static)
where
F: Future + 'static + MaybeSend;
/// Insert a command directly into the command buffer.
///
/// This is primarily used for built-in commands such as window messages.
///
/// # Examples
///
/// ```
/// # // NB: we don't have access to iced here so faking it.
/// # mod iced { pub(crate) mod window { pub(crate) fn close<Message>() -> iced_native::Command<Message> { todo!() } } }
/// # use ontv::commands::Commands;
/// enum Message {
/// /* snip */
/// }
///
/// fn update(mut commands: impl Commands<Message>) {
/// commands.command(iced::window::close());
/// }
/// ```
fn command(&mut self, command: Command<T>);
/// Extend the current command buffer with an iterator.
#[inline]
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = Command<T>>,
{
for command in iter {
self.command(command);
}
}
/// Map the current command buffer so that it can be used with a different
/// message type `U`.
///
/// # Examples
///
/// ```
/// # use ontv::commands::Commands;
/// enum Message {
/// Component1(component1::Message),
/// Component2(component2::Message),
/// }
///
/// fn update(mut commands: impl Commands<Message>) {
/// component1::update(commands.by_ref().map(Message::Component1));
/// component2::update(commands.by_ref().map(Message::Component2));
/// }
///
/// mod component1 {
/// # use ontv::commands::Commands;
/// pub(crate) enum Message {
/// Tick,
/// }
///
/// pub(crate) fn update(mut commands: impl Commands<Message>) {
/// // emit commands
/// }
/// }
///
/// mod component2 {
/// # use ontv::commands::Commands;
/// pub(crate) enum Message {
/// Tick,
/// }
///
/// pub(crate) fn update(mut commands: impl Commands<Message>) {
/// // emit commands
/// }
/// }
/// ```
#[inline]
fn map<M, U>(self, map: M) -> Map<Self, M>
where
Self: Sized,
M: MaybeSend + Sync + Clone + Fn(U) -> T,
{
Map {
commands: self,
map,
}
}
}
/// Wrapper produced by [`Commands::map`].
#[derive(Debug)]
pub struct Map<C, M> {
commands: C,
map: M,
}
impl<T: 'static, C, M: 'static, U: 'static> Commands<U> for Map<C, M>
where
C: Commands<T>,
M: MaybeSend + Sync + Clone + Fn(U) -> T,
{
type ByRef<'this> = &'this mut Self where Self: 'this;
#[inline]
fn by_ref(&mut self) -> Self::ByRef<'_> {
self
}
#[inline]
fn perform<F>(&mut self, future: F, outer: impl Fn(F::Output) -> U + MaybeSend + Sync + 'static)
where
F: Future + 'static + MaybeSend,
{
let map = self.map.clone();
self.commands
.perform(future, move |message| map(outer(message)));
}
#[inline]
fn command(&mut self, command: Command<U>) {
let map = self.map.clone();
self.commands.command(command.map(map));
}
}
impl<C, M> Commands<M> for &mut C
where
C: Commands<M>,
{
type ByRef<'this> = C::ByRef<'this> where Self: 'this;
#[inline]
fn by_ref(&mut self) -> Self::ByRef<'_> {
(*self).by_ref()
}
#[inline]
fn perform<F>(&mut self, future: F, map: impl Fn(F::Output) -> M + MaybeSend + Sync + 'static)
where
F: Future + 'static + MaybeSend,
{
(**self).perform(future, map);
}
#[inline]
fn command(&mut self, command: Command<M>) {
(**self).command(command);
}
}