Skip to content

Instantly share code, notes, and snippets.

@evaporei
Created January 8, 2022 18:40
Show Gist options
  • Save evaporei/b65533fa0a3824fa88278c1378c349ff to your computer and use it in GitHub Desktop.
Save evaporei/b65533fa0a3824fa88278c1378c349ff to your computer and use it in GitHub Desktop.
Xor in Rust cause HDL sucks
struct In {
a: bool,
b: bool,
}
#[derive(Debug, PartialEq)]
struct Out {
out: bool,
}
struct Xor;
trait Chip {
type Input;
type Output;
fn run(input: Self::Input) -> Self::Output;
}
impl Chip for Xor {
type Input = In;
type Output = Out;
fn run(input: Self::Input) -> Self::Output {
let In { a, b } = input;
let not_a = !a;
let not_b = !b;
let w1 = a && not_b;
let w2 = not_a && b;
let out = w1 || w2;
Out { out }
}
}
fn main() {
let test_table = [
(In { a: false, b: false }, Out { out: false }),
(In { a: false, b: true }, Out { out: true }),
(In { a: true, b: false }, Out { out: true }),
(In { a: true, b: true }, Out { out: false }),
];
for scenario in test_table {
let (input, output) = scenario;
assert_eq!(Xor::run(input), output);
}
println!("Tests passed!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment