🎨 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,
state: State,
signal: Option<Signal>,
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);

@ -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::*;
}

@ -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<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)?;
*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<Item=(usize, usize, &FullTile)> + '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<Item = (usize, usize, &FullTile)> + 'b {
self.tiles
.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 {
Self {
direction,
position
position,
}
}

@ -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<AnyTile>, &'b mut Option<Signal>, &'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());
}}
}};
}
}

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

Loading…
Cancel
Save