logicgate 0.3.2

Implementation of logic gates with NAND gates as the underlying layer Turing Complete
Documentation
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
///逻辑门
pub trait LogicGate {
    fn get_result(&self) -> Option<bool>;
}



///与非门 全为true则为false
/// # Examples
/// ```
/// use logicgate::Nand;
/// use logicgate::LogicGate;
/// let a = Nand{input1: &Some(true), input2: &Some(false)};
/// assert_eq!(a.get_result(),Some(true))
/// ```
#[derive(PartialEq)]
pub struct Nand<'a> {
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
}


impl<'a> LogicGate for Nand<'a> {

    fn get_result(&self) -> Option<bool> {
	if *self.input1 == Some(true){
	    if *self.input2 == Some(true) {
		return Some(false);
	    }
	}
	Some(true)
    }
}

///非门 反转真值表
///非门由一个输入一致的与非门组成
/// # Examples
/// ```
/// use logicgate::Not;
/// use logicgate::LogicGate;
/// let a = Not{input: &Some(true)};
/// let b = Not{input: &Some(false)};
/// assert_eq!(a.get_result(),Some(false));
/// assert_eq!(b.get_result(),Some(true));
/// ```
pub struct Not<'a> {
    pub input: &'a Option<bool>,
}


impl<'a> LogicGate for Not<'a> {
    fn get_result(&self) -> Option<bool> {
	let a = Nand{input1: &self.input, input2: &self.input};
	a.get_result()
    }
}
///或门 有true则为true
///或门由三个与非门组成
/// # Examples
/// ```
/// use logicgate::Or;
/// use logicgate::LogicGate;
/// let a = Or{input1: &Some(true),input2: &Some(false)};
/// let b = Or{input1: &Some(false),input2: &Some(false)};
/// assert_eq!(a.get_result(),Some(true));
/// assert_eq!(b.get_result(),Some(false));
/// ```
pub struct Or<'a> {
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
}

impl<'a> LogicGate for Or<'a> {
    fn get_result(&self) -> Option<bool> {
	let a = Nand{input1: self.input1, input2: self.input1};
	let b = Nand{input1: self.input2, input2: self.input2};
	let c = Nand{input1: &a.get_result(),input2: &b.get_result()};
	c.get_result()
    }
}

///或非门 有true则为Some(false)
///或非门由四个与非门组成
/// # Examples
/// ```
/// use logicgate::Nor;
/// use logicgate::LogicGate;
/// let a = Nor{input1: &Some(true),input2: &Some(false)};
/// let b = Nor{input1: &Some(false),input2: &Some(false)};
/// assert_eq!(a.get_result(),Some(false));
/// assert_eq!(b.get_result(),Some(true));
/// ```
pub struct Nor<'a>{
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
}

impl<'a> LogicGate for Nor<'a> {
    fn get_result(&self) -> Option<bool> {
	let a = Nand{input1: self.input1, input2: self.input1};
	let b = Nand{input1: self.input2, input2: self.input2};
	let c = Nand{input1: &a.get_result(),input2: &b.get_result()};
	let d = Nand{input1: &c.get_result(),input2: &c.get_result()};
	d.get_result()
    }
}
///与门 全为true则为true
///与门由两个与非门组成
/// # Examples
/// ```
/// use logicgate::And;
/// use logicgate::LogicGate;
/// let a = And{input1: &Some(true),input2: &Some(false)};
/// let b = And{input1: &Some(true),input2: &Some(true)};
/// assert_eq!(a.get_result(),Some(false));
/// assert_eq!(b.get_result(),Some(true));
/// ```
pub struct And<'a> {
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
}

impl<'a> LogicGate for And<'a> {
    fn get_result(&self) -> Option<bool> {
	let a = Nand{input1: self.input1, input2: self.input2};
	let b = Nand{input1: &a.get_result(), input2: &a.get_result()};
	b.get_result()
    }
}
///高电平 输出true
/// # Examples
/// ```
/// use logicgate::HighLevel;
/// use logicgate::LogicGate;
/// let a = HighLevel{};
/// assert_eq!(a.get_result(),Some(true));
/// ```
pub struct HighLevel{
}
impl LogicGate for HighLevel {
    fn get_result(&self) -> Option<bool> {
	Some(true)
    }
}

///低电平 输出Some(false)
/// # Examples
/// ```
/// use logicgate::LowLevel;
/// use logicgate::LogicGate;
/// let a = LowLevel{};
/// assert_eq!(a.get_result(),Some(false));
/// ```
pub struct LowLevel{
}
impl LogicGate for LowLevel {
    fn get_result(&self) -> Option<bool> {
	Some(false)
    }
}

///异或门 输入不一样true
/// # Examples
/// ```
/// use logicgate::Xor;
/// use logicgate::LogicGate;
/// let a = Xor{input1:& Some(true),input2:& Some(false)};
/// let b = Xor{input1:& Some(true),input2:& Some(true)};
/// let c = Xor{input1:& Some(false),input2:& Some(false)};
/// assert_eq!(a.get_result(),Some(true));
/// assert_eq!(b.get_result(),Some(false));
/// assert_eq!(c.get_result(),Some(false));
/// ```
pub struct Xor<'a>{
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
}
impl<'a> LogicGate for Xor<'a>{
    fn get_result(&self) ->Option<bool> {
	let a:And = And{input1:self.input1,input2:self.input2};
	let b:Nor = Nor{input1:self.input1,input2:self.input2};
	let c:Nor = Nor{input1:&a.get_result(),input2:&b.get_result()};
	c.get_result()
    }
}


///三路或门
/// # Examples
/// ```
/// use logicgate::ThreeOr;
/// use logicgate::LogicGate;
/// let a = ThreeOr{input1:& Some(true),input2:& Some(false),input3:& Some(false)};
/// let b = ThreeOr{input1:& Some(false),input2:& Some(true),input3:& Some(false)};
/// let c = ThreeOr{input1:& Some(false),input2:& Some(false),input3:& Some(true)};
/// let d = ThreeOr{input1:& Some(false),input2:& Some(false),input3:& Some(false)};
/// assert_eq!(a.get_result(),Some(true));
/// assert_eq!(b.get_result(),Some(true));
/// assert_eq!(c.get_result(),Some(true));
/// assert_eq!(d.get_result(),Some(false));
/// ```
pub struct ThreeOr<'a>{
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
    pub input3: &'a Option<bool>,
}

impl<'a> LogicGate for ThreeOr<'a>{
    fn get_result(&self) ->Option<bool> {
	let a:Or = Or{input1:self.input1,input2:self.input2};
	let b:Or = Or{input1:self.input2,input2:self.input3};
	let c:Or = Or{input1:&a.get_result(),input2:&b.get_result()};
	c.get_result()
    }
}


///三路与门
/// # Examples
/// ```
/// use logicgate::ThreeAnd;
/// use logicgate::LogicGate;
/// let a = ThreeAnd{input1:& Some(true),input2:& Some(false),input3:& Some(false)};
/// let b = ThreeAnd{input1:& Some(false),input2:& Some(true),input3:& Some(false)};
/// let c = ThreeAnd{input1:& Some(false),input2:& Some(false),input3:& Some(true)};
/// let d = ThreeAnd{input1:& Some(false),input2:& Some(false),input3:& Some(false)};
/// let e = ThreeAnd{input1:& Some(true),input2:& Some(true),input3:& Some(true)};
/// assert_eq!(a.get_result(),Some(false));
/// assert_eq!(b.get_result(),Some(false));
/// assert_eq!(c.get_result(),Some(false));
/// assert_eq!(d.get_result(),Some(false));
/// assert_eq!(e.get_result(),Some(true));
/// ```
pub struct ThreeAnd<'a>{
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
    pub input3: &'a Option<bool>,
}

impl<'a> LogicGate for ThreeAnd<'a>{
    fn get_result(&self) ->Option<bool> {
	let a:And = And{input1:self.input1,input2:self.input2};
	let b:And = And{input1:self.input2,input2:self.input3};
	let c:And = And{input1:&a.get_result(),input2:&b.get_result()};
	c.get_result()
    }
}
///同或门
///相同则为Some(true)
/// # Examples
/// ```
/// use logicgate::Xnor;
/// use logicgate::LogicGate;
/// let a = Xnor{input1:&Some(true),input2:&Some(false)};
/// let b = Xnor{input1:&Some(true),input2:&Some(true)};
/// let c = Xnor{input1:&Some(false),input2:&Some(false)};
/// assert_eq!(a.get_result(),Some(false));
/// assert_eq!(b.get_result(),Some(true));
/// assert_eq!(c.get_result(),Some(true));
/// ```
pub struct Xnor<'a>{
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
}

impl<'a> LogicGate for Xnor<'a>{
    fn get_result(&self) ->Option<bool> {
	let a:Xor = Xor{input1:self.input1,input2:self.input2};
	let b:Not = Not{input: &a.get_result()};
	b.get_result()
    }
}

use std::time::Duration;
///延迟线
///# Examples
///```
/// use logicgate::DelayLine;
/// let a = DelayLine{delay: 20,input: &Some(true)};
/// 
/// a.get_result();
///```

pub struct DelayLine<'a> {
    /// 延迟时间,单位为毫秒
    pub delay: u64,
    pub input: &'a Option<bool>,
}

impl<'a> DelayLine<'a> {
    pub fn get_result(&self) -> Option<bool> {
        std::thread::sleep(Duration::from_millis(self.delay));
        *self.input
    }
}

///半加器
///输出一个包含低位结果与进位的元组
/// # Examples
///```
///use logicgate::HalfAdder;
///let a:HalfAdder = HalfAdder{input1:&Some(true),input2:&Some(false)};
///assert_eq!(a.get_result(),(Some(true),Some(false)));
///let a:HalfAdder = HalfAdder{input1:&Some(true),input2:&Some(true)};
///assert_eq!(a.get_result(),(Some(false),Some(true)));
///```
pub struct HalfAdder<'a> {
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
}



impl<'a> HalfAdder<'a>{
    ///返回(sum,carry)
    pub fn get_result(&self) -> (Option<bool>,Option<bool>) {
	let a:Xor = Xor{input1: self.input1,input2:self.input2};
	let b:And = And{input1: self.input1,input2:self.input2};
	(a.get_result(),b.get_result())
    }
}

///全加器
/// # Examples
///```
///use logicgate::FullAdder;
///let a:FullAdder = FullAdder{input1:&Some(true),input2:&Some(false),input3:&Some(false)};
///assert_eq!(a.get_result(),(Some(true),Some(false)));
///let a:FullAdder = FullAdder{input1:&Some(true),input2:&Some(true),input3:&Some(false)};
///assert_eq!(a.get_result(),(Some(false),Some(true)));
///let a:FullAdder = FullAdder{input1:&Some(true),input2:&Some(true),input3:&Some(true)};
///assert_eq!(a.get_result(),(Some(true),Some(true)));
///let a:FullAdder = FullAdder{input1:&Some(false),input2:&Some(false),input3:&Some(false)};
///assert_eq!(a.get_result(),(Some(false),Some(false)));
///```
pub struct FullAdder<'a> {
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
    pub input3: &'a Option<bool>,
}

impl<'a> FullAdder<'a> {
    ///返回(sum,carry)
    pub fn get_result(&self) ->(Option<bool>,Option<bool>) {
	let a: Xor = Xor{input1: self.input1, input2: self.input2};
	let b: And = And{input1: self.input1, input2: self.input2};
	let c: Xor = Xor{input1: &a.get_result(), input2: self.input3};
	let d: And = And{input1: &a.get_result(), input2: self.input3};
	let e: Or = Or{input1: &b.get_result(),input2: &d.get_result()};
	(c.get_result(),e.get_result())
    }
}
///一位开关
/// # Examples
///```
///use logicgate::Switch;
///use crate::logicgate::LogicGate;
/// let a:Switch = Switch{switch: &Some(true),input:&Some(false)};
///assert_eq!(a.get_result(),Some(false));
///```
pub struct Switch<'a> {
    pub switch: &'a Option<bool>,
    pub input: &'a Option<bool>,
}

impl<'a> LogicGate for Switch<'a> {
    fn get_result(&self) -> Option<bool> {
	if *self.switch == Some(true) {
	    return *self.input;
	}
	None
    }
}

///八位开关
/// # Examples
///```
///use logicgate::Switch;
///use crate::logicgate::LogicGate;
/// let a:Switch = Switch{switch: &Some(true),input:&Some(false)};
///assert_eq!(a.get_result(),Some(false));
///```
pub struct EightSwitch<'a> {
    pub switch: &'a Option<bool>,
    pub input: i32,
}

impl<'a>  EightSwitch<'a> {
    fn get_result(&self) -> Option<i32> {
	if *self.switch == Some(true) {
	    return Some(self.input);
	}
	None
    }
}

///8位分线器
/// # Examples
///```
///use logicgate::EightBitSplitter;
///let a:EightBitSplitter = EightBitSplitter{input: 201};
///let a = a.get_result();
///assert_eq!(a,(Some(true),Some(false),Some(false),Some(true),Some(false),Some(false),Some(true),Some(true)));
/// let a:EightBitSplitter = EightBitSplitter{input:108};
/// let a = a.get_result();
/// assert_eq!(a,(Some(false),Some(false),Some(true),Some(true),Some(false),Some(true),Some(true),Some(false)));
///```
pub struct EightBitSplitter{
    pub input: i32,
}

impl EightBitSplitter {
    pub fn get_result(&self) ->(Option<bool>,Option<bool>,Option<bool>,Option<bool>,Option<bool>,Option<bool>,Option<bool>,Option<bool>) {
	let bit1 = (self.input & 1) != 0;
        let bit2 = (self.input & 2) != 0;
        let bit3 = (self.input & 4) != 0;
        let bit4 = (self.input & 8) != 0;
        let bit5 = (self.input & 16) != 0;
        let bit6 = (self.input & 32) != 0;
        let bit7 = (self.input & 64) != 0;
        let bit8 = (self.input & 128) != 0;

        (Some(bit1), Some(bit2), Some(bit3), Some(bit4), Some(bit5), Some(bit6), Some(bit7),Some(bit8))
    }
}


///8位集线器
/// # Examples
/// ```
/// use logicgate::EightBitMux;
/// let a = EightBitMux{input1:& Some(true),input2:& Some(false),input3:& Some(false),input4:& Some(true),input5:& Some(false),input6:& Some(false),input7:& Some(true),input8:& Some(true)};
/// assert_eq!(a.get_result(),201);
/// let a = EightBitMux{input1:& Some(false),input2: & Some(false),input3: & Some(true),input4: & Some(true),input5: & Some(false),input6: & Some(true),input7: & Some(true),input8: & Some(false)};
/// assert_eq!(a.get_result(),108);
/// ```
pub struct EightBitMux<'a> {
    pub input1: &'a Option<bool>,
    pub input2: &'a Option<bool>,
    pub input3: &'a Option<bool>,
    pub input4: &'a Option<bool>,
    pub input5: &'a Option<bool>,
    pub input6: &'a Option<bool>,
    pub input7: &'a Option<bool>,
    pub input8: &'a Option<bool>,
}

impl<'a> EightBitMux<'a> {
    pub fn get_result(&self) -> i32 {
        let result = (self.input8.unwrap_or(false) as i32) << 7 |
                     (self.input7.unwrap_or(false) as i32) << 6 |
                     (self.input6.unwrap_or(false) as i32) << 5 |
                     (self.input5.unwrap_or(false) as i32) << 4 |
                     (self.input4.unwrap_or(false) as i32) << 3 |
                     (self.input3.unwrap_or(false) as i32) << 2 |
                     (self.input2.unwrap_or(false) as i32) << 1 |
                     (self.input1.unwrap_or(false) as i32);
        result
    }
}

///八位加法器
/// # get 1 bool input and 2 i32 inputs
/// ## return 1 EightBit Output and Carry bool
/// # Examples
/// ```
///    use logicgate::EightBitAdder;
///         let adder = EightBitAdder {
///             input1: &Some(false),
///             input2: 1_i32,
///             input3: 1_i32,
///         };
///         let result = adder.get_result();
///
///         // 预期的加法结果
///         let expected_output = 2;
///
///         // 预期的进位
///         let expected_carry = Some(false);
///
///         // 检查加法器的输出
///         assert_eq!(result, (expected_output, expected_carry));
/// ```
pub struct EightBitAdder<'a>{
    pub input1: &'a Option<bool>,
    pub input2: i32,
    pub input3: i32,
}

impl<'a> EightBitAdder<'a> {
    /// 返回 (低八位结果, 进位)

    pub fn get_result(&self) -> (i32, Option<bool>) {
        // 分割输入
        let splitter_one = EightBitSplitter { input: self.input2 }.get_result();
        let splitter_two = EightBitSplitter { input: self.input3 }.get_result();
        // 逐个全加器进行相加
        let adder_one = FullAdder { input1: self.input1, input2: & splitter_one.0, input3: & splitter_two.0 }.get_result();
        let adder_two = FullAdder { input1: & adder_one.1, input2: & splitter_one.1, input3: & splitter_two.1 }.get_result();
        let adder_three = FullAdder { input1: & adder_two.1, input2: & splitter_one.2, input3: & splitter_two.2 }.get_result();
        let adder_four = FullAdder { input1: & adder_three.1, input2: & splitter_one.3, input3: & splitter_two.3 }.get_result();
        let adder_five = FullAdder { input1: & adder_four.1, input2: & splitter_one.4, input3: & splitter_two.4 }.get_result();
        let adder_six = FullAdder { input1: & adder_five.1, input2: & splitter_one.5, input3: & splitter_two.5 }.get_result();
        let adder_seven = FullAdder { input1: & adder_six.1, input2: & splitter_one.6, input3: & splitter_two.6 }.get_result();
        let adder_eight = FullAdder { input1: & adder_seven.1, input2: & splitter_one.7, input3: & splitter_two.7 }.get_result();
        // 合并全加器的结果
        let selection = EightBitMux{input1:& adder_one.0, input2:& adder_two.0, input3:& adder_three.0, input4:& adder_four.0, input5:& adder_five.0, input6:& adder_six.0, input7:& adder_seven.0, input8:& adder_eight.0 }.get_result();

        // 返回最终结果及进位
        (selection, adder_eight.1)
    }
}

///八位非
/// # Examples
/// ```
///    use logicgate::EightBitNot;
///    let a = EightBitNot{input: 80}.get_result();
///    assert_eq!(a,175);
/// ```
pub struct EightBitNot{
    pub input: i32,
}

impl EightBitNot {
    pub fn get_result(&self) -> i32 {
	let a = EightBitSplitter{input: self.input}.get_result();
	let b = Not{input:& a.0}.get_result();
	let c = Not{input:& a.1}.get_result();
	let d = Not{input:& a.2}.get_result();
	let e = Not{input:& a.3}.get_result();
	let f = Not{input:& a.4}.get_result();
	let g = Not{input:& a.5}.get_result();
	let h = Not{input:& a.6}.get_result();
	let i = Not{input:& a.7}.get_result();
	EightBitMux{input1:& b,input2:& c,input3:& d,input4:& e,input5:& f,input6:& g,input7:& h,input8:& i}.get_result()
    }
}

///八位或
/// # Examples
/// ```
///    use logicgate::EightBitOr;
///    let a = EightBitOr{input1: 80,input2:21}.get_result();
///    assert_eq!(a,85);
/// ```
pub struct EightBitOr{
    pub input1: i32,
    pub input2: i32,
}

impl EightBitOr {
    pub fn get_result(&self) -> i32 {
	let a = EightBitSplitter{input: self.input1}.get_result();
	let b = EightBitSplitter{input: self.input2}.get_result();
	let c = Or{input1:& a.0,input2:& b.0}.get_result();
	let d = Or{input1:& a.1,input2:& b.1}.get_result();
	let e = Or{input1:& a.2,input2:& b.2}.get_result();
	let f = Or{input1:& a.3,input2:& b.3}.get_result();
	let g = Or{input1:& a.4,input2:& b.4}.get_result();
	let h = Or{input1:& a.5,input2:& b.5}.get_result();
	let i = Or{input1:& a.6,input2:& b.6}.get_result();
	let j = Or{input1:& a.7,input2:& b.7}.get_result();
	EightBitMux{input1:& c,input2: & d,input3:& e,input4:& f,input5:& g,input6:& h,input7:& i,input8:& j}.get_result()
    }
}


///八位数据选择器
/// # 当input1为false时输出input2, input1为true输出input3
/// # Examples
/// ```
/// use logicgate::DataSelector;
/// let a = DataSelector{input1:&Some(false),input2:20,input3:10}.get_result();
/// assert_eq!(a,20);
/// let a = DataSelector{input1:&Some(true),input2:20,input3:10}.get_result();
/// assert_eq!(a,10);
///```
pub struct DataSelector<'a>{
    pub input1: &'a Option<bool>,
    pub input2: i32,
    pub input3: i32,
}

impl<'a> DataSelector<'a>{
    pub fn get_result(&self) -> i32 {
	if *self.input1 ==Some(true) {
	    return self.input3;
	}
	self.input2
    }
}