🎨 cargo fmt

main
Shad Amethyst 2 years ago
parent b6b31502cf
commit ec1660d006
Signed by: amethyst
GPG Key ID: D970C8DD1D6DEE36

@ -55,16 +55,20 @@ pub struct UpdateContext<'a> {
pane: &'a Pane, pane: &'a Pane,
state: State, state: State,
signal: Option<Signal>, signal: Option<Signal>,
commit: &'a mut UpdateCommit commit: &'a mut UpdateCommit,
} }
// SAFETY: self.pane.tiles[self.position] may not be accessed from any method // SAFETY: self.pane.tiles[self.position] may not be accessed from any method
impl<'a> UpdateContext<'a> { impl<'a> UpdateContext<'a> {
/// Returns `None` if the tile was already updated or is empty /// Returns `None` if the tile was already updated or is empty
pub(crate) fn new(pane: &'a mut Pane, position: (usize, usize), commit: &'a mut UpdateCommit) -> Option<(UpdateContext<'a>, &'a mut AnyTile)> { pub(crate) fn new(
pane: &'a mut Pane,
position: (usize, usize),
commit: &'a mut UpdateCommit,
) -> Option<(UpdateContext<'a>, &'a mut AnyTile)> {
let mut tile = pane.get_mut(position)?; let mut tile = pane.get_mut(position)?;
if tile.updated { if tile.updated {
return None return None;
} }
tile.updated = true; // prevent duplicate updates tile.updated = true; // prevent duplicate updates
commit.updates.push(position); commit.updates.push(position);
@ -76,14 +80,12 @@ impl<'a> UpdateContext<'a> {
state: tile.state(), state: tile.state(),
signal: tile.take_signal(), signal: tile.take_signal(),
pane, pane,
commit commit,
}; };
// SAFETY: ptr is a valid pointer // SAFETY: ptr is a valid pointer
// SAFETY: aliasing is prevented by the invariants of UpdateContext // SAFETY: aliasing is prevented by the invariants of UpdateContext
Some((res, unsafe { Some((res, unsafe { &mut *ptr }))
&mut *ptr
}))
} }
/// Returns the position of the currently updated tile. /// Returns the position of the currently updated tile.
@ -94,7 +96,10 @@ impl<'a> UpdateContext<'a> {
/// Returns the [signal](crate::FullTile::signal) of the currently updated tile. /// Returns the [signal](crate::FullTile::signal) of the currently updated tile.
#[inline] #[inline]
pub fn signal<'b>(&'b self) -> Option<&'b Signal> where 'a: 'b { pub fn signal<'b>(&'b self) -> Option<&'b Signal>
where
'a: 'b,
{
self.signal.as_ref() self.signal.as_ref()
} }
@ -125,7 +130,10 @@ impl<'a> UpdateContext<'a> {
/// Returns an immutable reference to the [FullTile] at `pos` in the current [Pane]. /// Returns an immutable reference to the [FullTile] at `pos` in the current [Pane].
/// Returns `None` if the tile is borrowed mutably, if it is the current tile or if it does not exist. /// Returns `None` if the tile is borrowed mutably, if it is the current tile or if it does not exist.
#[inline] #[inline]
pub fn get<'b>(&'b self, pos: (usize, usize)) -> Option<&'b FullTile> where 'a: 'b { pub fn get<'b>(&'b self, pos: (usize, usize)) -> Option<&'b FullTile>
where
'a: 'b,
{
if self.position == pos { if self.position == pos {
None None
} else { } else {
@ -141,8 +149,12 @@ impl<'a> UpdateContext<'a> {
/// Shortcut for calling both `ctx.offset(offset)` and `ctx.get(pos)` /// Shortcut for calling both `ctx.offset(offset)` and `ctx.get(pos)`
#[inline] #[inline]
pub fn get_offset<'b>(&'b self, offset: (i8, i8)) -> Option<((usize, usize), &'b FullTile)> where 'a: 'b { pub fn get_offset<'b>(&'b self, offset: (i8, i8)) -> Option<((usize, usize), &'b FullTile)>
self.offset(offset).and_then(|pos| self.get(pos).map(|tile| (pos, tile))) where
'a: 'b,
{
self.offset(offset)
.and_then(|pos| self.get(pos).map(|tile| (pos, tile)))
} }
/// Returns whether or not the tile at `pos` accepts a signal coming from `direction`. /// Returns whether or not the tile at `pos` accepts a signal coming from `direction`.
@ -151,7 +163,7 @@ impl<'a> UpdateContext<'a> {
pub fn accepts_signal(&self, pos: (usize, usize), direction: Direction) -> bool { pub fn accepts_signal(&self, pos: (usize, usize), direction: Direction) -> bool {
match self.get(pos) { match self.get(pos) {
Some(tile) => tile.accepts_signal(direction), Some(tile) => tile.accepts_signal(direction),
None => false None => false,
} }
} }
@ -163,7 +175,7 @@ impl<'a> UpdateContext<'a> {
signal.set_position(pos); signal.set_position(pos);
if !self.pane.in_bounds(pos) { if !self.pane.in_bounds(pos) {
return None return None;
} }
self.commit.send(pos, signal); self.commit.send(pos, signal);

@ -29,11 +29,11 @@ pub struct World {
} }
pub mod prelude { pub mod prelude {
pub use crate::World;
pub use crate::pane::Pane; pub use crate::pane::Pane;
pub use crate::World;
pub use crate::utils::*;
pub use crate::signal::Signal;
pub use crate::context::UpdateContext; pub use crate::context::UpdateContext;
pub use crate::signal::Signal;
pub use crate::tile::Tile; pub use crate::tile::Tile;
pub use crate::utils::*;
} }

@ -76,12 +76,16 @@ impl Pane {
return None; return None;
} }
self.tiles.get_mut(position.1 * self.width.get() + position.0) self.tiles
.get_mut(position.1 * self.width.get() + position.0)
} }
/// Sets the tile at `position` to `tile`. `T` must either implement [`Tile`] or be `()`. /// Sets the tile at `position` to `tile`. `T` must either implement [`Tile`] or be `()`.
#[inline] #[inline]
pub fn set_tile<T>(&mut self, position: (usize, usize), tile: T) -> Option<()> where FullTile: From<T> { pub fn set_tile<T>(&mut self, position: (usize, usize), tile: T) -> Option<()>
where
FullTile: From<T>,
{
let full_tile = self.get_mut(position)?; let full_tile = self.get_mut(position)?;
*full_tile = FullTile::from(tile); *full_tile = FullTile::from(tile);
@ -147,9 +151,10 @@ impl Pane {
/// Returns an iterator over the tiles and their coordinates /// Returns an iterator over the tiles and their coordinates
#[inline] #[inline]
pub fn tiles<'b>(&'b self) -> impl Iterator<Item=(usize, usize, &FullTile)> + 'b { pub fn tiles<'b>(&'b self) -> impl Iterator<Item = (usize, usize, &FullTile)> + 'b {
self.tiles.iter().enumerate().filter_map(move |(i, v)| { self.tiles
Some((i % self.width, i / self.width, v)) .iter()
}) .enumerate()
.filter_map(move |(i, v)| Some((i % self.width, i / self.width, v)))
} }
} }

@ -10,7 +10,7 @@ impl Signal {
pub fn empty(position: (usize, usize), direction: Direction) -> Self { pub fn empty(position: (usize, usize), direction: Direction) -> Self {
Self { Self {
direction, direction,
position position,
} }
} }

@ -28,7 +28,7 @@ impl FullTile {
cell, cell,
signal: None, signal: None,
state: State::default(), state: State::default(),
updated: false updated: false,
} }
} }
@ -88,10 +88,6 @@ impl FullTile {
pub fn next_state(&mut self) { pub fn next_state(&mut self) {
self.state = self.state.next(); self.state = self.state.next();
} }
pub fn get_raw_mut<'b>(&'b mut self) -> (&'b mut Option<AnyTile>, &'b mut Option<Signal>, &'b mut State) {
(&mut self.cell, &mut self.signal, &mut self.state)
}
} }
impl Default for FullTile { impl Default for FullTile {
@ -186,13 +182,15 @@ mod crate_macros {
macro_rules! test_set_signal { macro_rules! test_set_signal {
( $pane:expr, $pos:expr, $dir:expr ) => { ( $pane:expr, $pos:expr, $dir:expr ) => {
$pane.set_signal($pos, Signal::empty($pos, $dir)).unwrap(); $pane.set_signal($pos, Signal::empty($pos, $dir)).unwrap();
} };
} }
#[macro_export] #[macro_export]
macro_rules! assert_signal { macro_rules! assert_signal {
( $pane:expr, $pos:expr ) => {{ ( $pane:expr, $pos:expr ) => {{
let guard = $pane.get($pos).expect(&format!("Couldn't get tile at {:?}", $pos)); let guard = $pane
.get($pos)
.expect(&format!("Couldn't get tile at {:?}", $pos));
let signal = guard.signal(); let signal = guard.signal();
assert!(signal.is_some()); assert!(signal.is_some());
signal signal
@ -201,15 +199,17 @@ mod crate_macros {
( $pane:expr, $pos:expr, [ $( $data:expr ),* ] ) => {{ ( $pane:expr, $pos:expr, [ $( $data:expr ),* ] ) => {{
let signal = assert_signal!($pane, $pos); let signal = assert_signal!($pane, $pos);
// TODO: check that signal.data == data // TODO: check that signal.data == data
}} }};
} }
#[macro_export] #[macro_export]
macro_rules! assert_no_signal { macro_rules! assert_no_signal {
( $pane:expr, $pos:expr) => {{ ( $pane:expr, $pos:expr) => {{
let guard = $pane.get($pos).expect(&format!("Couldn't get tile at {:?}", $pos)); let guard = $pane
.get($pos)
.expect(&format!("Couldn't get tile at {:?}", $pos));
let signal = guard.signal(); let signal = guard.signal();
assert!(signal.is_none()); assert!(signal.is_none());
}} }};
} }
} }

@ -77,7 +77,7 @@ impl Resistor {
pub fn new(direction: Direction) -> Self { pub fn new(direction: Direction) -> Self {
Self { Self {
direction, direction,
signal: None signal: None,
} }
} }
} }
@ -111,10 +111,18 @@ mod test {
fn test_wire_transmit() { fn test_wire_transmit() {
use crate::Orientation::*; use crate::Orientation::*;
let mut pane = test_tile_setup!(3, 2, [ let mut pane = test_tile_setup!(
Wire::new(Horizontal), Wire::new(Any), Wire::new(Horizontal), 3,
(), Wire::new(Vertical), () 2,
]); [
Wire::new(Horizontal),
Wire::new(Any),
Wire::new(Horizontal),
(),
Wire::new(Vertical),
()
]
);
// Test the signal going from left to right // Test the signal going from left to right
test_set_signal!(pane, (0, 0), Direction::Right); test_set_signal!(pane, (0, 0), Direction::Right);
@ -166,10 +174,18 @@ mod test {
fn test_diode_transmit() { fn test_diode_transmit() {
use crate::Direction::*; use crate::Direction::*;
let mut pane = test_tile_setup!(3, 2, [ let mut pane = test_tile_setup!(
Diode::new(Right), Diode::new(Right), Diode::new(Down), 3,
(), Diode::new(Up), Diode::new(Left) 2,
]); [
Diode::new(Right),
Diode::new(Right),
Diode::new(Down),
(),
Diode::new(Up),
Diode::new(Left)
]
);
// Test the signal going from left to right // Test the signal going from left to right
test_set_signal!(pane, (0, 0), Direction::Right); test_set_signal!(pane, (0, 0), Direction::Right);
@ -178,12 +194,7 @@ mod test {
assert_signal!(pane, (1, 0)); assert_signal!(pane, (1, 0));
assert_no_signal!(pane, (0, 0)); assert_no_signal!(pane, (0, 0));
let positions = [ let positions = [(2, 0), (2, 1), (1, 1), (1, 0)];
(2, 0),
(2, 1),
(1, 1),
(1, 0)
];
for &pos in positions.iter().cycle().take(16) { for &pos in positions.iter().cycle().take(16) {
pane.step(); pane.step();
@ -191,7 +202,7 @@ mod test {
assert_signal!(pane, pos); assert_signal!(pane, pos);
for &pos2 in positions.iter() { for &pos2 in positions.iter() {
if pos == pos2 { if pos == pos2 {
continue continue;
} }
assert_no_signal!(pane, pos2); assert_no_signal!(pane, pos2);
} }
@ -202,9 +213,16 @@ mod test {
fn test_resistor_transmit() { fn test_resistor_transmit() {
use crate::Direction::*; use crate::Direction::*;
let mut pane = test_tile_setup!(4, 1, [ let mut pane = test_tile_setup!(
Diode::new(Right), Resistor::new(Right), Resistor::new(Right), Diode::new(Right) 4,
]); 1,
[
Diode::new(Right),
Resistor::new(Right),
Resistor::new(Right),
Diode::new(Right)
]
);
test_set_signal!(pane, (0, 0), Direction::Right); test_set_signal!(pane, (0, 0), Direction::Right);

Loading…
Cancel
Save