From 27b3dca6a9a358ee642d4fe17e04808a515d951b Mon Sep 17 00:00:00 2001 From: Adrien Burgun Date: Wed, 10 Aug 2022 17:28:17 +0200 Subject: [PATCH] :fire: Remove `position` from signal --- stackline-cli/src/main.rs | 13 ++++++------ stackline/benches/dispatch.rs | 8 +++---- stackline/src/context.rs | 4 +--- stackline/src/pane.rs | 13 ++++++------ stackline/src/signal.rs | 40 +++++++++++++---------------------- stackline/src/tile/macros.rs | 2 +- 6 files changed, 33 insertions(+), 47 deletions(-) diff --git a/stackline-cli/src/main.rs b/stackline-cli/src/main.rs index b21f063..a560ee6 100644 --- a/stackline-cli/src/main.rs +++ b/stackline-cli/src/main.rs @@ -410,9 +410,9 @@ fn remove(world: &mut World, x: i32, y: i32) { } fn signal(world: &mut World, x: i32, y: i32) { - match world.get_mut_with_pos((x, y)) { - Some((tile, x, y)) => { - tile.set_signal(Some(Signal::empty((x, y), Direction::Right))); + match world.get_mut((x, y)) { + Some(tile) => { + tile.set_signal(Some(Signal::empty(Direction::Right))); tile.set_state(State::Active); } None => { @@ -586,10 +586,9 @@ fn copy(world: &mut World, x: i32, y: i32, x2: i32, y2: i32) { }; // TODO: register signal in Pane::signals - match world.get_mut_with_pos((x2, y2)) { - Some((tile, x2, y2)) => { - if let Some(mut signal) = from.take_signal() { - signal.set_position((x2, y2)); + match world.get_mut((x2, y2)) { + Some(tile) => { + if let Some(signal) = from.take_signal() { from.set_signal(Some(signal)); } *tile = from; diff --git a/stackline/benches/dispatch.rs b/stackline/benches/dispatch.rs index 471982f..df1a643 100644 --- a/stackline/benches/dispatch.rs +++ b/stackline/benches/dispatch.rs @@ -18,13 +18,13 @@ fn benchmark_step(c: &mut Criterion) { pane.set_tile((3, n), Wire::new(Orientation::Vertical)); } - pane.set_signal((0, 0), stackline::signal!((0, 0), Direction::Right, [])); + pane.set_signal((0, 0), stackline::signal!(Direction::Right)); - pane.set_signal((3, 0), stackline::signal!((3, 0), Direction::Down, [])); + pane.set_signal((3, 0), stackline::signal!(Direction::Down)); - pane.set_signal((3, 3), stackline::signal!((3, 3), Direction::Left, [])); + pane.set_signal((3, 3), stackline::signal!(Direction::Left)); - pane.set_signal((0, 3), stackline::signal!((0, 3), Direction::Up, [])); + pane.set_signal((0, 3), stackline::signal!(Direction::Up)); b.iter(|| pane.step()); }); diff --git a/stackline/src/context.rs b/stackline/src/context.rs index 6a9b134..7b238cd 100644 --- a/stackline/src/context.rs +++ b/stackline/src/context.rs @@ -290,14 +290,12 @@ impl<'a> UpdateContext<'a> { pub fn force_send( &mut self, position: (usize, usize), - mut signal: Signal, + signal: Signal, ) -> Result<(), SendError> { if !self.in_bounds(position) { return Err(SendError(signal)); } - signal.set_position(position); - self.commit.send(position, signal); Ok(()) diff --git a/stackline/src/pane.rs b/stackline/src/pane.rs index b261c07..15e8b81 100644 --- a/stackline/src/pane.rs +++ b/stackline/src/pane.rs @@ -32,7 +32,7 @@ impl Pane { /// pane.set_tile((3, 1), Wire::new(Orientation::Horizontal)); /// /// // Put a signal on (2, 1) - /// pane.set_signal((2, 1), stackline::signal!((2, 1))); + /// pane.set_signal((2, 1), stackline::signal!()); /// /// // Perform a simulation step /// pane.step(); @@ -395,13 +395,12 @@ impl Pane { /// assert!(pane.get((0, 0)).unwrap().signal().is_none()); /// /// // We set it to something else - /// pane.set_signal((0, 0), stackline::signal!((0, 0), Direction::Right)).unwrap(); + /// pane.set_signal((0, 0), stackline::signal!(Direction::Right)).unwrap(); /// /// assert!(pane.get((0, 0)).unwrap().signal().is_some()); /// ``` #[inline] - pub fn set_signal(&mut self, position: (usize, usize), mut signal: Signal) -> Option<()> { - signal.set_position(position); + pub fn set_signal(&mut self, position: (usize, usize), signal: Signal) -> Option<()> { if let Some(tile) = self.get_mut(position) { tile.set_signal(Some(signal))?; tile.set_state(State::Active); @@ -477,7 +476,7 @@ impl Pane { /// // ^< /// /// // Initialize the circuit with a signal at (0, 0) - /// pane.set_signal((0, 0), stackline::signal!((0, 0), Direction::Right)); + /// pane.set_signal((0, 0), stackline::signal!(Direction::Right)); /// /// // Do an update step /// pane.step(); @@ -669,10 +668,10 @@ mod test { pane.get_mut((0, 0)) .unwrap() - .set_signal(Some(Signal::empty((0, 0), Direction::Right))); + .set_signal(Some(Signal::empty(Direction::Right))); pane.get_mut((1, 0)) .unwrap() - .set_signal(Some(Signal::empty((1, 0), Direction::Right))); + .set_signal(Some(Signal::empty(Direction::Right))); pane.init_signals(); diff --git a/stackline/src/signal.rs b/stackline/src/signal.rs index d36d25a..cfe059f 100644 --- a/stackline/src/signal.rs +++ b/stackline/src/signal.rs @@ -59,15 +59,13 @@ impl<'a> From<&'a str> for Value { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Signal { direction: Direction, - position: (usize, usize), stack: Vec, } impl Signal { - pub fn empty(position: (usize, usize), direction: Direction) -> Self { + pub fn empty(direction: Direction) -> Self { Self { direction, - position, stack: Vec::new(), } } @@ -115,14 +113,6 @@ impl Signal { self.direction } - pub fn position(&self) -> (usize, usize) { - self.position - } - - pub fn set_position(&mut self, position: (usize, usize)) { - self.position = position; - } - /// Pushes a value onto the stack of the signal. /// Signals are pushed on top of the stack and can be [`pop`ped](Signal::pop) in reverse order. /// @@ -130,7 +120,7 @@ impl Signal { /// /// ``` /// # use stackline::prelude::*; - /// let mut signal = Signal::empty((0, 0), Direction::Down); + /// let mut signal = Signal::empty(Direction::Down); /// assert_eq!(signal.len(), 0); /// /// signal.push(Value::Number(1.0)); @@ -150,7 +140,7 @@ impl Signal { /// /// ``` /// # use stackline::prelude::*; - /// let mut signal = Signal::empty((0, 0), Direction::Down); + /// let mut signal = Signal::empty(Direction::Down); /// /// signal.push(Value::Number(1.0)); /// signal.push(Value::Number(2.0)); @@ -170,7 +160,7 @@ impl Signal { /// /// ``` /// # use stackline::prelude::*; - /// let signal = stackline::signal!((0, 0), Direction::Down, [1.0, -1.5]); + /// let signal = stackline::signal!(Direction::Down, [1.0, -1.5]); /// /// assert_eq!(signal.len(), 2); /// ``` @@ -204,18 +194,18 @@ impl Signal { /// ``` /// # use stackline::prelude::*; /// // Creates an empty signal at (1, 2) -/// let signal = stackline::signal!((1, 2)); +/// let signal = stackline::signal!(); /// /// // Creates an empty signal going right at (2, 2) -/// let signal = stackline::signal!((2, 2), Direction::Right); +/// let signal = stackline::signal!(Direction::Right); /// /// // Creates a signal with the values 10 and 12 on its stack -/// let signal = stackline::signal!((0, 0), [10, 12]); +/// let signal = stackline::signal!([10, 12]); /// ``` #[macro_export] macro_rules! signal { - ( $pos:expr, $dir:expr, [ $( $x:expr ),* ] ) => {{ - let mut res = Signal::empty($pos, $dir); + ( $dir:expr, [ $( $x:expr ),* ] ) => {{ + let mut res = Signal::empty($dir); $({ res.push(Value::from($x)); @@ -224,8 +214,8 @@ macro_rules! signal { res }}; - ( $pos:expr, [ $( $x:expr ),* ] ) => {{ - let mut res = Signal::empty($pos, Direction::default()); + ( [ $( $x:expr ),* ] ) => {{ + let mut res = Signal::empty(Direction::default()); $({ res.push(Value::from($x)); @@ -234,14 +224,14 @@ macro_rules! signal { res }}; - ( $pos:expr, $dir:expr) => {{ - let mut res = Signal::empty($pos, $dir); + ( $dir:expr ) => {{ + let mut res = Signal::empty($dir); res }}; - ( $pos:expr) => {{ - let mut res = Signal::empty($pos, Direction::default()); + () => {{ + let mut res = Signal::empty(Direction::default()); res }}; diff --git a/stackline/src/tile/macros.rs b/stackline/src/tile/macros.rs index b50299e..f478517 100644 --- a/stackline/src/tile/macros.rs +++ b/stackline/src/tile/macros.rs @@ -25,7 +25,7 @@ macro_rules! test_tile_setup { macro_rules! test_set_signal { ( $pane:expr, $pos:expr, $dir:expr ) => { $pane - .set_signal($pos, crate::signal::Signal::empty($pos, $dir)) + .set_signal($pos, crate::signal::Signal::empty($dir)) .unwrap(); }; }