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
use std::mem; use crate::{constructors, Bit, Gate, GateKind, GateLike}; impl<Rust> Gate<Rust> where Rust: GateLike, { pub(super) fn fill_input(&mut self, with: Bit, at: usize) -> Option<Vec<Bit>> { debug_assert!( self.inputs.len() > at, "tried to insert at [{}], but length was {}", at, self.inputs.len() ); *unsafe { self.inputs.get_unchecked_mut(at) } = with; self.inputs_filled += 1; if self.inputs_filled == self.inputs.len() { Some(self.compute()) } else { None } } fn compute(&mut self) -> Vec<Bit> { let inputs = mem::take(&mut self.inputs); self.kind.compute(inputs) } pub(super) fn reset(&mut self) { self.inputs_filled = 0; self.inputs = constructors::zeroed_vec(self.kind.num_of_inputs()); if let GateKind::Custom(inner) = &mut self.kind { inner.reset(); } } } impl<Rust> GateLike for GateKind<Rust> where Rust: GateLike, { fn compute(&mut self, input: Vec<Bit>) -> Vec<Bit> { match self { GateKind::Custom(component) => component.compute(input), GateKind::Rust(component) => component.compute(input), } } fn num_of_inputs(&self) -> usize { match self { Self::Custom(component) => component.num_of_inputs(), Self::Rust(component) => component.num_of_inputs(), } } }