Trait logic_circus::GateLike [−][src]
pub trait GateLike { unsafe fn compute(&mut self, input: Vec<Bit>) -> Vec<Bit>; fn num_of_inputs(&self) -> usize; fn reset(&mut self, full: bool) { ... } }
Expand description
Implement a gate in rust
if you want to implement a custom Gate
or a node of a Component
in
rust code instead of as a component with logic (whether the goal is
efficiancy or you’re just lazy, I won’t judge) all you have to do is
implement this trait on your type and stick it in the generic <Rust>
parameter of your Component
, then construct the full gate with
Gate::rust
you yourself are not supposed to ever need to use the functions in this trait
Example
use logic_circus::{Bit, Component, Gate, GateLike}; struct And; impl GateLike for And { fn compute(&mut self, input: Vec<Bit>) -> Vec<Bit> { vec![input[0] && input[1]] } fn num_of_inputs(&self) -> usize { 2 } } let gate = Gate::rust(And); let mut component = Component::single_gate(gate, 1); assert_eq!(component.compute(vec![true, true]), vec![true]); // note that I must `reset` the component in between each call to compute even // though `And` doesn't implement the `reset` function, this is due to internal // logic of the `compute` function component.reset(false); assert_eq!(component.compute(vec![true, false]), vec![false]);
Required methods
This function computes the outputs of a gate for it’s given inputs,
these are in Vec
s as a gate can have multiple inputs and outputs,
for example an And
gate has two inputs and one output
you yourself are not supposed to ever need to use this function
Safety
input
s length must be equal to num_of_inputs()
fn num_of_inputs(&self) -> usize
fn num_of_inputs(&self) -> usize
This function counts the amount of inputs a gate has
you yourself are not supposed to ever need to use this function