Skip to main content

CellSpec

Struct CellSpec 

Source
pub struct CellSpec {
    pub policy: Policy,
    pub range: Range,
    pub initial: f64,
    pub exact_bit: bool,
    pub grid: Option<f64>,
}

Fields§

§policy: Policy§range: Range§initial: f64§exact_bit: bool§grid: Option<f64>

Implementations§

Source§

impl CellSpec

Source

pub fn initial(self, value: f64) -> CellSpec

Examples found in repository?
examples/toggle.rs (line 71)
67    pub fn new() -> Toggle {
68        // Every number the program reads, or keeps from one evaluation to the next.
69        let mut table = SourceTable::new();
70        let coordinate = || input(-16_384.0, 16_384.0).grid(1.0 / 64.0);
71        let viewport_width = table.declare("viewport.width", input(0.0, 16_384.0).grid(1.0).initial(1280.0));
72        let viewport_height = table.declare("viewport.height", input(0.0, 16_384.0).grid(1.0).initial(800.0));
73        let mut pointer = |name: &str| Pointer {
74            active: table.declare(format!("{name}.active"), event()),
75            x: table.declare(format!("{name}.x"), coordinate()),
76            y: table.declare(format!("{name}.y"), coordinate()),
77        };
78        let down = pointer("down");
79        let up = pointer("up");
80        let cancel = table.declare("cancel", event());
81        let on_cell = table.declare("on", state_bit());
82        let armed_cell = table.declare("armed", state_bit());
83        let mut circuit = table.create_circuit();
84        let c = &mut circuit;
85
86        // Layout. trackLeft = viewportWidth / 2 - trackWidth / 2
87        let track_left = c.affine(viewport_width * 0.5 - TRACK_WIDTH / 2.0);
88        let track_top = c.affine(viewport_height * 0.5 - TRACK_HEIGHT / 2.0);
89        let track_right = c.affine(track_left + TRACK_WIDTH);
90        let track_bottom = c.affine(track_top + TRACK_HEIGHT);
91        // x >= trackLeft && x < trackLeft + trackWidth && y >= trackTop && y < trackTop + trackHeight
92        let inside_track = |c: &mut Circuit, point: &Pointer| {
93            let bits = [
94                emit_compare(c, point.x, Relation::AtLeast, track_left),
95                emit_compare(c, point.x, Relation::Less, track_right),
96                emit_compare(c, point.y, Relation::AtLeast, track_top),
97                emit_compare(c, point.y, Relation::Less, track_bottom),
98            ];
99            emit_and(c, &bits)
100        };
101
102        // Handle inputs in a fixed order: down, then up, then cancel.
103        // if (down) armed = insideTrack(down)
104        let down_inside = inside_track(c, &down);
105        let armed_after_down = emit_select(c, down.active, armed_cell, down_inside);
106        // if (up && armed && insideTrack(up)) on = !on
107        let up_inside = inside_track(c, &up);
108        let flips = emit_and(c, &[up.active, armed_after_down, up_inside]);
109        let flipped = emit_not(c, on_cell);
110        let on = emit_select(c, flips, on_cell, flipped);
111        // if (up || cancel) armed = false
112        let no_up = emit_not(c, up.active);
113        let no_cancel = emit_not(c, cancel);
114        let armed = emit_and(c, &[armed_after_down, no_up, no_cancel]);
115
116        // Commit state: each state cell gets exactly one next value.
117        table.next(on_cell, on);
118        table.next(armed_cell, armed);
119        table.finish();
120        let outputs = [track_left, track_top, on, armed];
121        let mut roots = outputs.to_vec();
122        roots.extend(table.next_wires());
123        let program = circuit.compile(Some(&roots));
124        let source = table.initial();
125        Toggle {
126            table,
127            circuit,
128            program,
129            source,
130            viewport: (viewport_width, viewport_height),
131            down,
132            up,
133            cancel,
134            outputs,
135        }
136    }
Source

pub fn grid(self, step: f64) -> CellSpec

Every value is a multiple of step, a power of two, e.g. 1/64 for pointer coordinates.

Examples found in repository?
examples/toggle.rs (line 70)
67    pub fn new() -> Toggle {
68        // Every number the program reads, or keeps from one evaluation to the next.
69        let mut table = SourceTable::new();
70        let coordinate = || input(-16_384.0, 16_384.0).grid(1.0 / 64.0);
71        let viewport_width = table.declare("viewport.width", input(0.0, 16_384.0).grid(1.0).initial(1280.0));
72        let viewport_height = table.declare("viewport.height", input(0.0, 16_384.0).grid(1.0).initial(800.0));
73        let mut pointer = |name: &str| Pointer {
74            active: table.declare(format!("{name}.active"), event()),
75            x: table.declare(format!("{name}.x"), coordinate()),
76            y: table.declare(format!("{name}.y"), coordinate()),
77        };
78        let down = pointer("down");
79        let up = pointer("up");
80        let cancel = table.declare("cancel", event());
81        let on_cell = table.declare("on", state_bit());
82        let armed_cell = table.declare("armed", state_bit());
83        let mut circuit = table.create_circuit();
84        let c = &mut circuit;
85
86        // Layout. trackLeft = viewportWidth / 2 - trackWidth / 2
87        let track_left = c.affine(viewport_width * 0.5 - TRACK_WIDTH / 2.0);
88        let track_top = c.affine(viewport_height * 0.5 - TRACK_HEIGHT / 2.0);
89        let track_right = c.affine(track_left + TRACK_WIDTH);
90        let track_bottom = c.affine(track_top + TRACK_HEIGHT);
91        // x >= trackLeft && x < trackLeft + trackWidth && y >= trackTop && y < trackTop + trackHeight
92        let inside_track = |c: &mut Circuit, point: &Pointer| {
93            let bits = [
94                emit_compare(c, point.x, Relation::AtLeast, track_left),
95                emit_compare(c, point.x, Relation::Less, track_right),
96                emit_compare(c, point.y, Relation::AtLeast, track_top),
97                emit_compare(c, point.y, Relation::Less, track_bottom),
98            ];
99            emit_and(c, &bits)
100        };
101
102        // Handle inputs in a fixed order: down, then up, then cancel.
103        // if (down) armed = insideTrack(down)
104        let down_inside = inside_track(c, &down);
105        let armed_after_down = emit_select(c, down.active, armed_cell, down_inside);
106        // if (up && armed && insideTrack(up)) on = !on
107        let up_inside = inside_track(c, &up);
108        let flips = emit_and(c, &[up.active, armed_after_down, up_inside]);
109        let flipped = emit_not(c, on_cell);
110        let on = emit_select(c, flips, on_cell, flipped);
111        // if (up || cancel) armed = false
112        let no_up = emit_not(c, up.active);
113        let no_cancel = emit_not(c, cancel);
114        let armed = emit_and(c, &[armed_after_down, no_up, no_cancel]);
115
116        // Commit state: each state cell gets exactly one next value.
117        table.next(on_cell, on);
118        table.next(armed_cell, armed);
119        table.finish();
120        let outputs = [track_left, track_top, on, armed];
121        let mut roots = outputs.to_vec();
122        roots.extend(table.next_wires());
123        let program = circuit.compile(Some(&roots));
124        let source = table.initial();
125        Toggle {
126            table,
127            circuit,
128            program,
129            source,
130            viewport: (viewport_width, viewport_height),
131            down,
132            up,
133            cancel,
134            outputs,
135        }
136    }

Trait Implementations§

Source§

impl Clone for CellSpec

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for CellSpec

Source§

impl Debug for CellSpec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<E> StoreElem<usize> for E

Source§

fn store(items: &mut [E], index: usize, value: E)

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.