🔥 Remove `position` from signal

main
Shad Amethyst 2 years ago
parent 63245aeb69
commit 27b3dca6a9
Signed by: amethyst
GPG Key ID: D970C8DD1D6DEE36

@ -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;

@ -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());
});

@ -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(())

@ -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();

@ -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<Value>,
}
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
}};

@ -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();
};
}

Loading…
Cancel
Save