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
//! Logic Circus is a logic circut simulator written in Rust
//!
//! It is nowhere near finished and I'd like to one day even add a GUI
//! You can build circuits by composing other circuits together (with
//! [`Component`]) or by fully implementing their logic (with the [`GateLike`]
//! trait)
use Infallible;
use GateKind;
use SequalsExtension;
use ContainerSizeGames;
/// A semantic alias for a boolean
pub type Bit = bool;
/// A circuit
///
/// use [`builder()`][Self::builder] to construct
///
/// use compute to run the logic
///
/// stick inside a [`Gate::component`] to use as part of a bigger component and
/// compose with other gates
///
/// put a gate implemented in rust (or an enum of them) in `<Rust>` to use it
/// (or them)
/// The constructor for [`Component`]
///
/// you create one with [`Component::builder()`]
///
/// use [`gate`][ComponentBuilder::gate] to add another inner gate
///
/// use [`inputs`][ComponentBuilder::inputs] to set the input locations and
/// finalize construction (get your final [`Component`])
///
/// put a gate implemented in rust (or an enum of them) in `<Rust>` to use it
/// (or them)
/// A wire
///
/// connects the outputs of the current [`Gate`] (or the inputs of the
/// [`Component`]) to either the inputs of another gate or the outputs of the
/// containing component.
///
/// if you want to send an output to several places it must be done
/// indirectly, you should take a look at [`Gate::dup`]
/// A gate, or rather a single node of a [`Component`]
///
/// put a gate implemented in rust (or an enum of them) in `<Rust>` to use it
/// (or them)
/// Implement a gate in rust
///
/// if you want to implement a custom [`Gate`] or a node of a [`Component`] in
/// rust code instead of as a component with logic (whether the goal is
/// efficiancy or you're just lazy, I won't judge) all you have to do is
/// implement this trait on your type and stick it in the generic `<Rust>`
/// parameter of your [`Component`], then construct the full gate with
/// [`Gate::rust`]
///
/// you yourself are not supposed to ever need to use the functions in this
/// trait
///
/// # Example
/// ```
/// use logic_circus::{Bit, Component, Gate, GateLike};
/// struct And;
///
/// impl GateLike for And {
/// fn compute(&mut self, input: Vec<Bit>) -> Vec<Bit> {
/// vec![input[0] && input[1]]
/// }
/// fn num_of_inputs(&self) -> usize {
/// 2
/// }
/// }
///
/// let gate = Gate::rust(And);
/// let mut component = Component::single_gate(gate, 1);
/// assert_eq!(component.compute(vec![true, true]), vec![true]);
/// // note that I must `reset` the component in between each call to compute even
/// // though `And` doesn't implement the `reset` function, this is due to internal
/// // logic of the `compute` function
/// component.reset(false);
/// assert_eq!(component.compute(vec![true, false]), vec![false]);
/// ```