Browse Source

cargo fmt

Getty Ritter 2 years ago
parent
commit
97b2fd8a0c
6 changed files with 161 additions and 99 deletions
  1. 57 42
      carpet/src/board.rs
  2. 1 2
      carpet/src/lib.rs
  3. 6 7
      carpet/src/types.rs
  4. 36 17
      ch2/src/main.rs
  5. 38 20
      ch3/src/main.rs
  6. 23 11
      ch4/src/main.rs

+ 57 - 42
carpet/src/board.rs

@@ -12,22 +12,34 @@ pub struct Board<T> {
 impl<T: Clone> Board<T> {
     pub fn new_with_default(width: usize, height: usize, default: T) -> Board<T> {
         let mut storage = Vec::with_capacity(width * height);
-        for _ in 0..width*height {
+        for _ in 0..width * height {
             storage.push(default.clone())
         }
-        Board { width, height, storage }
+        Board {
+            width,
+            height,
+            storage,
+        }
     }
 }
 
 impl<T> Board<T> {
-    pub fn new_from(width: usize, height: usize, mut func: impl FnMut(usize, usize) -> T) -> Board<T> {
+    pub fn new_from(
+        width: usize,
+        height: usize,
+        mut func: impl FnMut(usize, usize) -> T,
+    ) -> Board<T> {
         let mut storage = Vec::with_capacity(width * height);
         for y in 0..height {
             for x in 0..width {
                 storage.push(func(x, y))
             }
         }
-        Board { width, height, storage }
+        Board {
+            width,
+            height,
+            storage,
+        }
     }
 
     /// Returns a reference to an element at the given location, or
@@ -124,7 +136,8 @@ impl<T> std::ops::Index<Coord> for Board<T> {
     type Output = T;
 
     fn index(&self, pos: Coord) -> &Self::Output {
-        self.get(pos.x, pos.y).unwrap_or_else(|| panic!("Coordinate {:?} out of range", pos))
+        self.get(pos.x, pos.y)
+            .unwrap_or_else(|| panic!("Coordinate {:?} out of range", pos))
     }
 }
 
@@ -132,37 +145,41 @@ impl<T> std::ops::Index<(usize, usize)> for Board<T> {
     type Output = T;
 
     fn index(&self, (x, y): (usize, usize)) -> &Self::Output {
-        self.get(x, y).unwrap_or_else(|| panic!("Coordinate {:?} out of range", (x, y)))
+        self.get(x, y)
+            .unwrap_or_else(|| panic!("Coordinate {:?} out of range", (x, y)))
     }
 }
 
-impl<T> std::ops::Index<[usize;2]> for Board<T> {
+impl<T> std::ops::Index<[usize; 2]> for Board<T> {
     type Output = T;
 
-    fn index(&self, [x, y]: [usize;2]) -> &Self::Output {
-        self.get(x, y).unwrap_or_else(|| panic!("Coordinate {:?} out of range", (x, y)))
+    fn index(&self, [x, y]: [usize; 2]) -> &Self::Output {
+        self.get(x, y)
+            .unwrap_or_else(|| panic!("Coordinate {:?} out of range", (x, y)))
     }
 }
 
 impl<T> std::ops::IndexMut<Coord> for Board<T> {
     fn index_mut(&mut self, pos: Coord) -> &mut Self::Output {
-        self.get_mut(pos.x, pos.y).unwrap_or_else(|| panic!("Coordinate {:?} out of range", pos))
+        self.get_mut(pos.x, pos.y)
+            .unwrap_or_else(|| panic!("Coordinate {:?} out of range", pos))
     }
 }
 
 impl<T> std::ops::IndexMut<(usize, usize)> for Board<T> {
     fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Self::Output {
-        self.get_mut(x, y).unwrap_or_else(|| panic!("Coordinate {:?} out of range", (x, y)))
+        self.get_mut(x, y)
+            .unwrap_or_else(|| panic!("Coordinate {:?} out of range", (x, y)))
     }
 }
 
-impl<T> std::ops::IndexMut<[usize;2]> for Board<T> {
-    fn index_mut(&mut self, [x, y]: [usize;2]) -> &mut Self::Output {
-        self.get_mut(x, y).unwrap_or_else(|| panic!("Coordinate {:?} out of range", (x, y)))
+impl<T> std::ops::IndexMut<[usize; 2]> for Board<T> {
+    fn index_mut(&mut self, [x, y]: [usize; 2]) -> &mut Self::Output {
+        self.get_mut(x, y)
+            .unwrap_or_else(|| panic!("Coordinate {:?} out of range", (x, y)))
     }
 }
 
-
 /// An iterator over the elements of a `Board`. This returns values in
 /// row-major order but each element is also accompanied by its
 /// indices.
@@ -187,7 +204,6 @@ impl<'a, T> Iterator for BoardIter<'a, T> {
     }
 }
 
-
 /// An iterator over the elements of a `Board`. This returns values in
 /// row-major order but each element is also accompanied by its
 /// indices.
@@ -212,7 +228,6 @@ impl<'a, T> Iterator for BoardIterMut<'a, T> {
     }
 }
 
-
 pub struct BoardWindowIter<'a, T> {
     n: usize,
     width: usize,
@@ -236,8 +251,6 @@ impl<'a, T> Iterator for BoardWindowIter<'a, T> {
     }
 }
 
-
-
 pub struct BoardWindowIterMut<'a, T> {
     n: usize,
     width: usize,
@@ -261,7 +274,6 @@ impl<'a, T> Iterator for BoardWindowIterMut<'a, T> {
     }
 }
 
-
 #[cfg(test)]
 mod test {
     use super::*;
@@ -339,11 +351,12 @@ mod test {
             ]
         ];
 
-        let mut iter = b.window_iter(
-            Rect {
+        let mut iter = b
+            .window_iter(Rect {
                 origin: [1, 1].into(),
                 size: [2, 2].into(),
-            }).expect("Did not find expected BoardWindowIter");
+            })
+            .expect("Did not find expected BoardWindowIter");
         // in-bounds tests
         assert_eq!(iter.next(), Some((1, 1, &6)));
         assert_eq!(iter.next(), Some((2, 1, &7)));
@@ -352,34 +365,36 @@ mod test {
         assert_eq!(iter.next(), None);
     }
 
-
     #[test]
     fn window_iter_mut() {
         let mut b: Board<isize> = board_from_vec![
-            4,4;
-            [1,2,3,4,
-             5,6,7,8,
-             8,7,6,5,
-             4,3,2,1,
-            ]];
-
-        let iter = b.window_iter_mut(
-            Rect {
+        4,4;
+        [1,2,3,4,
+         5,6,7,8,
+         8,7,6,5,
+         4,3,2,1,
+        ]];
+
+        let iter = b
+            .window_iter_mut(Rect {
                 origin: [1, 1].into(),
                 size: [2, 2].into(),
-            }).expect("Did not find expected BoardWindowIterMut");
+            })
+            .expect("Did not find expected BoardWindowIterMut");
         for (x, y, v) in iter {
             *v = -(2 * x as isize + 2 * y as isize);
         }
 
-        assert_eq!(b, board_from_vec![
-            4,4;
-            [ 1, 2, 3, 4,
-              5,-4,-6, 8,
-              8,-6,-8, 5,
-              4, 3, 2, 1,
+        assert_eq!(
+            b,
+            board_from_vec![
+                4,4;
+                [ 1, 2, 3, 4,
+                  5,-4,-6, 8,
+                  8,-6,-8, 5,
+                  4, 3, 2, 1,
+                ]
             ]
-        ]);
+        );
     }
-
 }

+ 1 - 2
carpet/src/lib.rs

@@ -4,8 +4,8 @@ use ggez::{Context, GameError};
 use specs::WorldExt;
 use std::path::Path;
 
-pub use winit::VirtualKeyCode;
 pub use ggez::input::keyboard::KeyMods;
+pub use winit::VirtualKeyCode;
 
 mod board;
 mod types;
@@ -321,7 +321,6 @@ impl<Idx: Tile + 'static> Game<Idx> {
         self.world.world.create_entity()
     }
 
-
     pub fn on_key(&mut self, kc: Keycode, update: impl FnMut(&mut specs::World) + 'static) {
         self.world.on_keycode(kc, update);
     }

+ 6 - 7
carpet/src/types.rs

@@ -1,4 +1,4 @@
-use std::cmp::{min, max};
+use std::cmp::{max, min};
 
 #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
 pub struct Size {
@@ -32,7 +32,6 @@ impl From<[usize; 2]> for Coord {
     }
 }
 
-
 #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
 pub struct Rect {
     pub origin: Coord,
@@ -40,7 +39,7 @@ pub struct Rect {
 }
 
 impl Rect {
-    pub fn new(origin: impl Into<Coord>, size: impl Into<Size>) -> Rect{
+    pub fn new(origin: impl Into<Coord>, size: impl Into<Size>) -> Rect {
         let origin = origin.into();
         let size = size.into();
         Rect { origin, size }
@@ -82,10 +81,10 @@ impl Rect {
 
     pub fn contains(&self, pt: impl Into<Coord>) -> bool {
         let pt = pt.into();
-        pt.x >= self.origin.x &&
-            pt.y >= self.origin.y &&
-            pt.x < self.origin.x + self.size.width &&
-            pt.y < self.origin.y + self.size.height
+        pt.x >= self.origin.x
+            && pt.y >= self.origin.y
+            && pt.x < self.origin.x + self.size.width
+            && pt.y < self.origin.y + self.size.height
     }
 
     pub fn overlaps(&self, other: Rect) -> bool {

+ 36 - 17
ch2/src/main.rs

@@ -27,7 +27,11 @@ pub struct Player;
 impl Player {
     fn get_entity(world: &mut specs::World) -> Entity {
         let storage = (&world.read_component::<Player>(), &world.entities());
-        storage.join().next().expect("No entities tagged as Player").1
+        storage
+            .join()
+            .next()
+            .expect("No entities tagged as Player")
+            .1
     }
 }
 
@@ -40,7 +44,10 @@ pub struct Motion {
 impl Motion {
     fn move_player(world: &mut specs::World, down: i8, right: i8) {
         let player = Player::get_entity(world);
-        world.write_component::<Motion>().insert(player, Motion { down, right }).unwrap();
+        world
+            .write_component::<Motion>()
+            .insert(player, Motion { down, right })
+            .unwrap();
     }
 }
 
@@ -124,21 +131,33 @@ fn main() -> Result<(), GameError> {
             .build();
     }
 
-    game.on_key((carpet::VirtualKeyCode::W, carpet::KeyMods::NONE), |world| {
-        Motion::move_player(world, -1, 0);
-    });
-
-    game.on_key((carpet::VirtualKeyCode::A, carpet::KeyMods::NONE), |world| {
-        Motion::move_player(world, 0, -1);
-    });
-
-    game.on_key((carpet::VirtualKeyCode::S, carpet::KeyMods::NONE), |world| {
-        Motion::move_player(world, 1, 0);
-    });
-
-    game.on_key((carpet::VirtualKeyCode::D, carpet::KeyMods::NONE), |world| {
-        Motion::move_player(world, 0, 1);
-    });
+    game.on_key(
+        (carpet::VirtualKeyCode::W, carpet::KeyMods::NONE),
+        |world| {
+            Motion::move_player(world, -1, 0);
+        },
+    );
+
+    game.on_key(
+        (carpet::VirtualKeyCode::A, carpet::KeyMods::NONE),
+        |world| {
+            Motion::move_player(world, 0, -1);
+        },
+    );
+
+    game.on_key(
+        (carpet::VirtualKeyCode::S, carpet::KeyMods::NONE),
+        |world| {
+            Motion::move_player(world, 1, 0);
+        },
+    );
+
+    game.on_key(
+        (carpet::VirtualKeyCode::D, carpet::KeyMods::NONE),
+        |world| {
+            Motion::move_player(world, 0, 1);
+        },
+    );
 
     game.run_with_systems(|world| {
         Draw.run_now(&world);

+ 38 - 20
ch3/src/main.rs

@@ -4,8 +4,8 @@ extern crate specs_derive;
 extern crate specs_system_macro;
 
 use ggez::GameError;
-use specs::prelude::*;
 use rand::Rng;
+use specs::prelude::*;
 
 #[derive(PartialEq, Clone, Copy, Debug)]
 pub enum TileType {
@@ -64,7 +64,11 @@ pub struct Player;
 impl Player {
     fn get_entity(world: &mut specs::World) -> Entity {
         let storage = (&world.read_component::<Player>(), &world.entities());
-        storage.join().next().expect("No entities tagged as Player").1
+        storage
+            .join()
+            .next()
+            .expect("No entities tagged as Player")
+            .1
     }
 }
 
@@ -77,7 +81,10 @@ pub struct Motion {
 impl Motion {
     fn move_player(world: &mut specs::World, down: i8, right: i8) {
         let player = Player::get_entity(world);
-        world.write_component::<Motion>().insert(player, Motion { down, right }).unwrap();
+        world
+            .write_component::<Motion>()
+            .insert(player, Motion { down, right })
+            .unwrap();
     }
 }
 
@@ -116,8 +123,7 @@ system! {
 }
 
 fn main() -> Result<(), GameError> {
-    let mut game: carpet::Game<carpet::CP437> =
-        carpet::GameBuilder::new()
+    let mut game: carpet::Game<carpet::CP437> = carpet::GameBuilder::new()
         .name("game")
         .author("me")
         .resource_path({
@@ -147,21 +153,33 @@ fn main() -> Result<(), GameError> {
         })
         .build();
 
-    game.on_key((carpet::VirtualKeyCode::W, carpet::KeyMods::NONE), |world| {
-        Motion::move_player(world, -1, 0);
-    });
-
-    game.on_key((carpet::VirtualKeyCode::A, carpet::KeyMods::NONE), |world| {
-        Motion::move_player(world, 0, -1);
-    });
-
-    game.on_key((carpet::VirtualKeyCode::S, carpet::KeyMods::NONE), |world| {
-        Motion::move_player(world, 1, 0);
-    });
-
-    game.on_key((carpet::VirtualKeyCode::D, carpet::KeyMods::NONE), |world| {
-        Motion::move_player(world, 0, 1);
-    });
+    game.on_key(
+        (carpet::VirtualKeyCode::W, carpet::KeyMods::NONE),
+        |world| {
+            Motion::move_player(world, -1, 0);
+        },
+    );
+
+    game.on_key(
+        (carpet::VirtualKeyCode::A, carpet::KeyMods::NONE),
+        |world| {
+            Motion::move_player(world, 0, -1);
+        },
+    );
+
+    game.on_key(
+        (carpet::VirtualKeyCode::S, carpet::KeyMods::NONE),
+        |world| {
+            Motion::move_player(world, 1, 0);
+        },
+    );
+
+    game.on_key(
+        (carpet::VirtualKeyCode::D, carpet::KeyMods::NONE),
+        |world| {
+            Motion::move_player(world, 0, 1);
+        },
+    );
 
     game.run_with_systems(|world| {
         Draw.run_now(&world);

+ 23 - 11
ch4/src/main.rs

@@ -5,8 +5,8 @@ extern crate specs_system_macro;
 
 use carpet::Coord;
 use ggez::GameError;
-use specs::prelude::*;
 use rand::Rng;
+use specs::prelude::*;
 
 #[derive(PartialEq, Clone, Copy, Debug)]
 pub enum TileType {
@@ -69,7 +69,9 @@ impl Map {
     }
 
     fn carve(&mut self, rect: carpet::Rect) {
-        let iter = self.tiles.window_iter_mut(rect)
+        let iter = self
+            .tiles
+            .window_iter_mut(rect)
             .expect(&format!("Rect {:?} of map bounds", rect));
         for (_, _, t) in iter {
             *t = TileType::Floor;
@@ -93,7 +95,11 @@ pub struct Player;
 impl Player {
     fn get_entity(world: &mut specs::World) -> Entity {
         let storage = (&world.read_component::<Player>(), &world.entities());
-        storage.join().next().expect("No entities tagged as Player").1
+        storage
+            .join()
+            .next()
+            .expect("No entities tagged as Player")
+            .1
     }
 }
 
@@ -106,7 +112,10 @@ pub struct Motion {
 impl Motion {
     fn move_player(world: &mut specs::World, down: i8, right: i8) {
         let player = Player::get_entity(world);
-        world.write_component::<Motion>().insert(player, Motion { down, right }).unwrap();
+        world
+            .write_component::<Motion>()
+            .insert(player, Motion { down, right })
+            .unwrap();
     }
 }
 
@@ -145,8 +154,7 @@ system! {
 }
 
 fn main() -> Result<(), GameError> {
-    let mut game: carpet::Game<carpet::CP437> =
-        carpet::GameBuilder::new()
+    let mut game: carpet::Game<carpet::CP437> = carpet::GameBuilder::new()
         .name("game")
         .author("me")
         .resource_path({
@@ -166,7 +174,11 @@ fn main() -> Result<(), GameError> {
     game.register::<Player>();
 
     let map = Map::new();
-    let player_start = map.rooms.first().map(|r| r.center()).unwrap_or([40, 25].into());
+    let player_start = map
+        .rooms
+        .first()
+        .map(|r| r.center())
+        .unwrap_or([40, 25].into());
     game.insert(map);
 
     game.create_entity()
@@ -182,10 +194,10 @@ fn main() -> Result<(), GameError> {
         // set up all the keybindings
         use carpet::VirtualKeyCode::*;
         let none = carpet::KeyMods::NONE;
-        game.on_key((W, none), |world| Motion::move_player(world, -1,  0));
-        game.on_key((A, none), |world| Motion::move_player(world,  0, -1));
-        game.on_key((S, none), |world| Motion::move_player(world,  1,  0));
-        game.on_key((D, none), |world| Motion::move_player(world,  0,  1));
+        game.on_key((W, none), |world| Motion::move_player(world, -1, 0));
+        game.on_key((A, none), |world| Motion::move_player(world, 0, -1));
+        game.on_key((S, none), |world| Motion::move_player(world, 1, 0));
+        game.on_key((D, none), |world| Motion::move_player(world, 0, 1));
     }
 
     game.run_with_systems(|world| {