From ec1660d00657fc11837381299c19137b0228090e Mon Sep 17 00:00:00 2001 From: Adrien Burgun Date: Thu, 23 Jun 2022 09:18:13 +0200 Subject: [PATCH] :art: cargo fmt --- stackline/src/context.rs | 38 +++++++++++++++++--------- stackline/src/lib.rs | 6 ++-- stackline/src/pane.rs | 17 ++++++++---- stackline/src/signal.rs | 2 +- stackline/src/tile/mod.rs | 20 +++++++------- stackline/src/tile/wire.rs | 56 +++++++++++++++++++++++++------------- 6 files changed, 87 insertions(+), 52 deletions(-) diff --git a/stackline/src/context.rs b/stackline/src/context.rs index d3958d2..1198c46 100644 --- a/stackline/src/context.rs +++ b/stackline/src/context.rs @@ -55,16 +55,20 @@ pub struct UpdateContext<'a> { pane: &'a Pane, state: State, signal: Option, - commit: &'a mut UpdateCommit + commit: &'a mut UpdateCommit, } // SAFETY: self.pane.tiles[self.position] may not be accessed from any method impl<'a> UpdateContext<'a> { /// 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)?; if tile.updated { - return None + return None; } tile.updated = true; // prevent duplicate updates commit.updates.push(position); @@ -76,14 +80,12 @@ impl<'a> UpdateContext<'a> { state: tile.state(), signal: tile.take_signal(), pane, - commit + commit, }; // SAFETY: ptr is a valid pointer // SAFETY: aliasing is prevented by the invariants of UpdateContext - Some((res, unsafe { - &mut *ptr - })) + Some((res, unsafe { &mut *ptr })) } /// 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. #[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() } @@ -125,7 +130,10 @@ impl<'a> UpdateContext<'a> { /// 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. #[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 { None } else { @@ -141,8 +149,12 @@ impl<'a> UpdateContext<'a> { /// Shortcut for calling both `ctx.offset(offset)` and `ctx.get(pos)` #[inline] - pub fn get_offset<'b>(&'b self, offset: (i8, i8)) -> Option<((usize, usize), &'b FullTile)> where 'a: 'b { - self.offset(offset).and_then(|pos| self.get(pos).map(|tile| (pos, tile))) + pub fn get_offset<'b>(&'b self, offset: (i8, i8)) -> Option<((usize, usize), &'b FullTile)> + 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`. @@ -151,7 +163,7 @@ impl<'a> UpdateContext<'a> { pub fn accepts_signal(&self, pos: (usize, usize), direction: Direction) -> bool { match self.get(pos) { Some(tile) => tile.accepts_signal(direction), - None => false + None => false, } } @@ -163,7 +175,7 @@ impl<'a> UpdateContext<'a> { signal.set_position(pos); if !self.pane.in_bounds(pos) { - return None + return None; } self.commit.send(pos, signal); diff --git a/stackline/src/lib.rs b/stackline/src/lib.rs index 08f0cf9..0282f5c 100644 --- a/stackline/src/lib.rs +++ b/stackline/src/lib.rs @@ -29,11 +29,11 @@ pub struct World { } pub mod prelude { - pub use crate::World; 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::signal::Signal; pub use crate::tile::Tile; + pub use crate::utils::*; } diff --git a/stackline/src/pane.rs b/stackline/src/pane.rs index a15d078..8bffbc0 100644 --- a/stackline/src/pane.rs +++ b/stackline/src/pane.rs @@ -76,12 +76,16 @@ impl Pane { 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 `()`. #[inline] - pub fn set_tile(&mut self, position: (usize, usize), tile: T) -> Option<()> where FullTile: From { + pub fn set_tile(&mut self, position: (usize, usize), tile: T) -> Option<()> + where + FullTile: From, + { let full_tile = self.get_mut(position)?; *full_tile = FullTile::from(tile); @@ -147,9 +151,10 @@ impl Pane { /// Returns an iterator over the tiles and their coordinates #[inline] - pub fn tiles<'b>(&'b self) -> impl Iterator + 'b { - self.tiles.iter().enumerate().filter_map(move |(i, v)| { - Some((i % self.width, i / self.width, v)) - }) + pub fn tiles<'b>(&'b self) -> impl Iterator + 'b { + self.tiles + .iter() + .enumerate() + .filter_map(move |(i, v)| Some((i % self.width, i / self.width, v))) } } diff --git a/stackline/src/signal.rs b/stackline/src/signal.rs index 2f0ce27..8b6e6a7 100644 --- a/stackline/src/signal.rs +++ b/stackline/src/signal.rs @@ -10,7 +10,7 @@ impl Signal { pub fn empty(position: (usize, usize), direction: Direction) -> Self { Self { direction, - position + position, } } diff --git a/stackline/src/tile/mod.rs b/stackline/src/tile/mod.rs index 0162468..f6fc8e3 100644 --- a/stackline/src/tile/mod.rs +++ b/stackline/src/tile/mod.rs @@ -28,7 +28,7 @@ impl FullTile { cell, signal: None, state: State::default(), - updated: false + updated: false, } } @@ -88,10 +88,6 @@ impl FullTile { pub fn next_state(&mut self) { self.state = self.state.next(); } - - pub fn get_raw_mut<'b>(&'b mut self) -> (&'b mut Option, &'b mut Option, &'b mut State) { - (&mut self.cell, &mut self.signal, &mut self.state) - } } impl Default for FullTile { @@ -186,13 +182,15 @@ mod crate_macros { macro_rules! test_set_signal { ( $pane:expr, $pos:expr, $dir:expr ) => { $pane.set_signal($pos, Signal::empty($pos, $dir)).unwrap(); - } + }; } #[macro_export] macro_rules! assert_signal { ( $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(); assert!(signal.is_some()); signal @@ -201,15 +199,17 @@ mod crate_macros { ( $pane:expr, $pos:expr, [ $( $data:expr ),* ] ) => {{ let signal = assert_signal!($pane, $pos); // TODO: check that signal.data == data - }} + }}; } #[macro_export] macro_rules! assert_no_signal { ( $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(); assert!(signal.is_none()); - }} + }}; } } diff --git a/stackline/src/tile/wire.rs b/stackline/src/tile/wire.rs index 6988ec9..8d09e08 100644 --- a/stackline/src/tile/wire.rs +++ b/stackline/src/tile/wire.rs @@ -77,7 +77,7 @@ impl Resistor { pub fn new(direction: Direction) -> Self { Self { direction, - signal: None + signal: None, } } } @@ -111,10 +111,18 @@ mod test { fn test_wire_transmit() { use crate::Orientation::*; - let mut pane = test_tile_setup!(3, 2, [ - Wire::new(Horizontal), Wire::new(Any), Wire::new(Horizontal), - (), Wire::new(Vertical), () - ]); + let mut pane = test_tile_setup!( + 3, + 2, + [ + Wire::new(Horizontal), + Wire::new(Any), + Wire::new(Horizontal), + (), + Wire::new(Vertical), + () + ] + ); // Test the signal going from left to right test_set_signal!(pane, (0, 0), Direction::Right); @@ -166,10 +174,18 @@ mod test { fn test_diode_transmit() { use crate::Direction::*; - let mut pane = test_tile_setup!(3, 2, [ - Diode::new(Right), Diode::new(Right), Diode::new(Down), - (), Diode::new(Up), Diode::new(Left) - ]); + let mut pane = test_tile_setup!( + 3, + 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_set_signal!(pane, (0, 0), Direction::Right); @@ -178,12 +194,7 @@ mod test { assert_signal!(pane, (1, 0)); assert_no_signal!(pane, (0, 0)); - let positions = [ - (2, 0), - (2, 1), - (1, 1), - (1, 0) - ]; + let positions = [(2, 0), (2, 1), (1, 1), (1, 0)]; for &pos in positions.iter().cycle().take(16) { pane.step(); @@ -191,7 +202,7 @@ mod test { assert_signal!(pane, pos); for &pos2 in positions.iter() { if pos == pos2 { - continue + continue; } assert_no_signal!(pane, pos2); } @@ -202,9 +213,16 @@ mod test { fn test_resistor_transmit() { use crate::Direction::*; - let mut pane = test_tile_setup!(4, 1, [ - Diode::new(Right), Resistor::new(Right), Resistor::new(Right), Diode::new(Right) - ]); + let mut pane = test_tile_setup!( + 4, + 1, + [ + Diode::new(Right), + Resistor::new(Right), + Resistor::new(Right), + Diode::new(Right) + ] + ); test_set_signal!(pane, (0, 0), Direction::Right);