1use std::future::Future;
4use std::pin::Pin;
5
6pub struct Command<M> {
11 pub(crate) kind: CommandKind<M>,
12}
13
14pub(crate) enum CommandKind<M> {
15 None,
17 Quit,
19 Batch(Vec<Command<M>>),
21 #[allow(dead_code)]
23 Task(Pin<Box<dyn Future<Output = M> + Send + 'static>>),
24 Message(M),
26}
27
28impl<M> Command<M> {
29 #[must_use]
31 pub fn none() -> Self {
32 Self {
33 kind: CommandKind::None,
34 }
35 }
36
37 #[must_use]
39 pub fn quit() -> Self {
40 Self {
41 kind: CommandKind::Quit,
42 }
43 }
44
45 #[must_use]
47 pub fn message(msg: M) -> Self {
48 Self {
49 kind: CommandKind::Message(msg),
50 }
51 }
52
53 #[must_use]
55 pub fn batch(commands: Vec<Command<M>>) -> Self {
56 Self {
57 kind: CommandKind::Batch(commands),
58 }
59 }
60
61 pub fn perform<F, Fut>(task: F) -> Self
65 where
66 F: FnOnce() -> Fut,
67 Fut: Future<Output = M> + Send + 'static,
68 M: Send + 'static,
69 {
70 Self {
71 kind: CommandKind::Task(Box::pin(task())),
72 }
73 }
74
75 #[must_use]
77 pub fn is_quit(&self) -> bool {
78 matches!(self.kind, CommandKind::Quit)
79 }
80
81 #[must_use]
83 pub fn is_none(&self) -> bool {
84 matches!(self.kind, CommandKind::None)
85 }
86}
87
88impl<M> Default for Command<M> {
89 fn default() -> Self {
90 Self::none()
91 }
92}
93
94pub type Cmd<M> = Command<M>;