chessrs/game/
index_extensions.rs

1use crate::*;
2use std::ops::{Index, IndexMut};
3
4impl Index<Coords> for Game {
5    type Output = Piece;
6
7    fn index(&self, coords: Coords) -> &Self::Output {
8        &self.board[7 - coords.y][coords.x]
9    }
10}
11
12impl IndexMut<Coords> for Game {
13    fn index_mut(&mut self, coords: Coords) -> &mut Self::Output {
14        &mut self.board[7 - coords.y][coords.x]
15    }
16}
17
18impl Index<(usize, usize)> for Game {
19    type Output = Piece;
20
21    fn index(&self, coords: (usize, usize)) -> &Self::Output {
22        &self.board[7 - coords.1][coords.0]
23    }
24}
25
26impl IndexMut<(usize, usize)> for Game {
27    fn index_mut(&mut self, coords: (usize, usize)) -> &mut Self::Output {
28        &mut self.board[7 - coords.0][coords.0]
29    }
30}
31
32impl Index<usize> for Game {
33    type Output = [Piece; 8];
34
35    fn index(&self, index: usize) -> &Self::Output {
36        &self.board[7 - index]
37    }
38}
39
40impl IndexMut<usize> for Game {
41    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
42        &mut self.board[7 - index]
43    }
44}