Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #[derive(Debug, Clone)]
  2. pub enum Output {
  3. True,
  4. False
  5. }
  6. impl Output {
  7. pub fn invert(&self) -> Output {
  8. match self {
  9. Output::True => Output::False,
  10. Output::False => Output::True
  11. }
  12. }
  13. }
  14.  
  15. trait NodeTrait {
  16. fn get_output(&self) -> Output;
  17. }
  18.  
  19. enum Node<'a> {
  20. Not(NotGate<'a>),
  21. Signal(SignalGate)
  22. }
  23. impl<'a> NodeTrait for Node<'a> {
  24. fn get_output(&self) -> Output {
  25. match self {
  26. Node::Not(gate) => gate.get_output(),
  27. Node::Signal(gate) => gate.get_output()
  28. }
  29. }
  30.  
  31. }
  32.  
  33. impl<'a> Node<'a> {
  34. fn add_input(&mut self, node: &'a Node) {
  35. match self {
  36. Node::Not(gate) => gate.inputs.push(node),
  37. _ => println!("hoi")
  38. }
  39. }
  40. }
  41.  
  42. struct NotGate<'a> {
  43. inputs: Vec<&'a Node<'a>>
  44. }
  45. struct SignalGate {
  46. signal: Output
  47. }
  48.  
  49. impl<'a> NodeTrait for NotGate<'a> {
  50. fn get_output(&self) -> Output {
  51. self.inputs[0].get_output().invert()
  52. }
  53. }
  54.  
  55. impl NodeTrait for SignalGate {
  56. fn get_output(&self) -> Output {
  57. self.signal.clone()
  58. }
  59. }
  60.  
  61. fn lol_received_a_node_must_add_new_input(node: &mut Node) {
  62. let w = Node::Signal(SignalGate {
  63. signal: Output::False
  64. });
  65. node.add_input(&w);
  66. }
  67.  
  68. fn main() {
  69. let q = Node::Signal(SignalGate {
  70. signal: Output::False
  71. });
  72. let w = Node::Not(NotGate {
  73. inputs: vec![&q]
  74. });
  75. let e = Node::Not(NotGate {
  76. inputs: vec![&w]
  77. });
  78. let r = Node::Not(NotGate {
  79. inputs: vec![&e]
  80. });
  81.  
  82. println!("{:?}",r.get_output())
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement