Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug, Clone)]
- pub enum Output {
- True,
- False
- }
- impl Output {
- pub fn invert(&self) -> Output {
- match self {
- Output::True => Output::False,
- Output::False => Output::True
- }
- }
- }
- trait NodeTrait {
- fn get_output(&self) -> Output;
- }
- enum Node<'a> {
- Not(NotGate<'a>),
- Signal(SignalGate)
- }
- impl<'a> NodeTrait for Node<'a> {
- fn get_output(&self) -> Output {
- match self {
- Node::Not(gate) => gate.get_output(),
- Node::Signal(gate) => gate.get_output()
- }
- }
- }
- impl<'a> Node<'a> {
- fn add_input(&mut self, node: &'a Node) {
- match self {
- Node::Not(gate) => gate.inputs.push(node),
- _ => println!("hoi")
- }
- }
- }
- struct NotGate<'a> {
- inputs: Vec<&'a Node<'a>>
- }
- struct SignalGate {
- signal: Output
- }
- impl<'a> NodeTrait for NotGate<'a> {
- fn get_output(&self) -> Output {
- self.inputs[0].get_output().invert()
- }
- }
- impl NodeTrait for SignalGate {
- fn get_output(&self) -> Output {
- self.signal.clone()
- }
- }
- fn lol_received_a_node_must_add_new_input(node: &mut Node) {
- let w = Node::Signal(SignalGate {
- signal: Output::False
- });
- node.add_input(&w);
- }
- fn main() {
- let q = Node::Signal(SignalGate {
- signal: Output::False
- });
- let w = Node::Not(NotGate {
- inputs: vec![&q]
- });
- let e = Node::Not(NotGate {
- inputs: vec![&w]
- });
- let r = Node::Not(NotGate {
- inputs: vec![&e]
- });
- println!("{:?}",r.get_output())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement