Skip to content

Commit 21e13a9

Browse files
committed
Use i32 instead of usize for bson document
The generic Coords is waiting on reoslution for: rust-lang/rust#49415
1 parent 26e6adb commit 21e13a9

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

src/coords.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
use std::fmt;
2+
use std::convert::{TryFrom, TryInto};
23

34
#[derive(Clone, Serialize, Deserialize)]
4-
pub struct Coords(pub Vec<usize>);
5+
pub struct Coords<T = usize>(pub Vec<T>);
56

6-
impl Coords {
7+
impl<T> Coords<T>
8+
where T: TryFrom<usize> + TryInto<usize>,
9+
<T as TryFrom<usize>>::Error: fmt::Debug,
10+
<T as TryInto<usize>>::Error: fmt::Debug,
11+
{
712
pub fn from_index(index: usize, dims: &[usize]) -> Self {
8-
let mut coords = vec![0; dims.len()];
9-
let mut remainder = index;
10-
11-
for i in (0..dims.len()).rev() {
12-
coords[i] = remainder % dims[i];
13-
remainder /= dims[i];
14-
}
15-
16-
Coords(coords)
13+
Coords(dims.iter().scan(index, |remainder, &dim| {
14+
let coord = T::try_from(*remainder % dim).unwrap();
15+
*remainder /= dim;
16+
Some(coord)
17+
}).collect())
1718
}
1819

1920
pub fn to_index(&self, dims: &[usize]) -> usize {
2021
self.0.iter().zip(dims.iter())
21-
.fold(0, |acc, (&coord, &dim)| (acc * dim) + coord)
22+
.fold(0, |acc, (&coord, &dim)| (acc * dim) + coord.try_into().unwrap())
2223
}
2324
}
2425

25-
impl fmt::Debug for Coords {
26+
impl<T: fmt::Debug> fmt::Debug for Coords<T> {
2627
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2728
self.0.fmt(f)
2829
}

src/server/db.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum CellState { Empty, Cleared, Mine }
1717
pub struct CellInfo {
1818
pub surrounding: usize,
1919
pub state: CellState,
20-
pub coords: Coords
20+
pub coords: Coords<i32>
2121
}
2222

2323
#[derive(Serialize, Deserialize)]
@@ -27,10 +27,10 @@ pub struct Game {
2727
pub id: Option<ObjectId>,
2828
pub created_at: DateTime<Utc>,
2929
pub pass: Option<String>,
30-
pub seed: u32,
31-
pub dims: Vec<usize>,
32-
pub size: usize,
33-
pub mines: usize,
30+
pub seed: i32,
31+
pub dims: Vec<i32>,
32+
pub size: i32,
33+
pub mines: i32,
3434
pub autoclear: bool,
3535
pub turns: Vec<Turn>,
3636
pub clients: Vec<String>,
@@ -42,20 +42,20 @@ pub struct Game {
4242
#[serde(rename_all = "camelCase")]
4343
pub struct Turn {
4444
pub turn_taken_at: DateTime<Utc>,
45-
pub clear_req: Vec<Coords>,
45+
pub clear_req: Vec<Coords<i32>>,
4646
pub clear_actual: Vec<CellInfo>,
47-
pub flagged: Vec<Coords>,
48-
pub unflagged: Vec<Coords>,
47+
pub flagged: Vec<Coords<i32>>,
48+
pub unflagged: Vec<Coords<i32>>,
4949
pub game_over: bool,
5050
pub win: bool,
51-
pub cells_rem: usize,
51+
pub cells_rem: i32,
5252
}
5353

5454
impl<'a> Model<'a> for Game {
5555
const COLLECTION_NAME: &'static str = "games";
5656

5757
fn id(&self) -> Option<ObjectId> {
58-
return self.id.clone();
58+
self.id.clone()
5959
}
6060

6161
fn set_id(&mut self, oid: ObjectId) {

src/server/native_server.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct TurnInfo {
6767

6868
impl TurnInfo {
6969
fn to_document(&self, server: &NativeServer) -> db::Turn {
70-
let to_coords_vec = |indices: &[usize]| {
70+
let to_coords_vec = |indices: &[usize]| -> Vec<Coords<i32>> {
7171
indices.iter()
7272
.map(|&i| Coords::from_index(i, &server.dims))
7373
.collect()
@@ -103,7 +103,7 @@ impl TurnInfo {
103103
unflagged: to_coords_vec(&self.unflagged),
104104
game_over: self.game_state != GameState::Ongoing,
105105
win: self.game_state == GameState::Win,
106-
cells_rem: self.cells_rem
106+
cells_rem: self.cells_rem as i32
107107
}
108108
}
109109
}
@@ -271,10 +271,10 @@ impl NativeServer {
271271
id: None,
272272
created_at,
273273
pass: None,
274-
seed,
275-
dims: dims.clone(),
276-
size: dims.iter().fold(1, |acc, &d| acc * d),
277-
mines,
274+
seed: seed as i32,
275+
dims: dims.iter().map(|&d| d as i32).collect(),
276+
size: dims.iter().fold(1, |acc, &d| acc * d) as i32,
277+
mines: mines as i32,
278278
autoclear,
279279
turns,
280280
clients: vec!["RustoBusto".to_owned()],

0 commit comments

Comments
 (0)