1use core::array;
4use serde::{Deserialize, Serialize};
5
6pub const COLUMNS: usize = 15;
8
9pub const PERMUTS: usize = 7;
11
12pub const WIRES: [usize; COLUMNS] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
14
15#[derive(PartialEq, Default, Eq, Clone, Copy, Debug, Serialize, Deserialize)]
19#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
20#[cfg_attr(feature = "wasm_types", wasm_bindgen::prelude::wasm_bindgen)]
21pub struct Wire {
22 pub row: usize,
24 pub col: usize,
25}
26
27impl Wire {
28 pub fn new(row: usize, col: usize) -> Self {
30 Self { row, col }
31 }
32
33 pub fn for_row(row: usize) -> [Self; PERMUTS] {
35 GateWires::new(row)
36 }
37}
38
39pub type GateWires = [Wire; PERMUTS];
43
44pub trait Wirable: Sized {
47 fn new(row: usize) -> Self;
49
50 fn wire(self, col: usize, to: Wire) -> Self;
52}
53
54impl Wirable for GateWires {
55 fn new(row: usize) -> Self {
56 array::from_fn(|col| Wire { row, col })
57 }
58
59 fn wire(mut self, col: usize, to: Wire) -> Self {
60 assert!(col < PERMUTS);
61 self[col] = to;
62 self
63 }
64}
65
66#[cfg(feature = "ocaml_types")]
67pub mod caml {
68 use super::*;
69 use core::convert::TryInto;
70
71 #[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
72 pub struct CamlWire {
73 pub row: ocaml::Int,
74 pub col: ocaml::Int,
75 }
76
77 impl From<Wire> for CamlWire {
78 fn from(w: Wire) -> Self {
79 Self {
80 row: w.row.try_into().expect("usize -> isize"),
81 col: w.col.try_into().expect("usize -> isize"),
82 }
83 }
84 }
85
86 impl From<CamlWire> for Wire {
87 fn from(w: CamlWire) -> Self {
88 Self {
89 row: w.row.try_into().expect("isize -> usize"),
90 col: w.col.try_into().expect("isize -> usize"),
91 }
92 }
93 }
94}
95
96#[cfg(feature = "wasm_types")]
97pub mod wasm {
98 use super::*;
99
100 #[wasm_bindgen::prelude::wasm_bindgen]
101 impl Wire {
102 #[wasm_bindgen::prelude::wasm_bindgen]
103 pub fn create(row: i32, col: i32) -> Self {
104 Self {
105 row: row as usize,
106 col: col as usize,
107 }
108 }
109 }
110}