Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 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. enum Node {
  16. Not(NotGate),
  17. Signal(SignalGate)
  18. }
  19. impl Node {
  20. fn get_output(&self) -> Output {
  21. match self {
  22. Node::Not(gate) => gate.get_output(),
  23. Node::Signal(gate) => gate.get_output()
  24. }
  25. }
  26. }
  27.  
  28. struct NotGate {
  29. inputs: Vec<&Node>
  30. }
  31. struct SignalGate {
  32. signal: Output
  33. }
  34.  
  35. trait NodeTrait {
  36. fn get_output(&self) -> Output;
  37. }
  38.  
  39. impl NodeTrait for NotGate {
  40. fn get_output(&self) -> Output {
  41. self.inputs[0].get_output().invert()
  42. }
  43. }
  44.  
  45. impl NodeTrait for SignalGate {
  46. fn get_output(&self) -> Output {
  47. self.signal.clone()
  48. }
  49. }
  50.  
  51. fn main() {
  52. let x = Node::Not(NotGate {
  53. inputs: Vec::new()
  54. });
  55. let y = Node::Not(NotGate {
  56. inputs: vec![&x]
  57. });
  58. let z = Node
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement