Skip to content

Commit

Permalink
Use i32 instead of usize for bson document
Browse files Browse the repository at this point in the history
The generic Coords is waiting  on reoslution for: rust-lang/rust#49415
  • Loading branch information
tobz1000 committed May 9, 2018
1 parent 26e6adb commit 21e13a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
27 changes: 14 additions & 13 deletions src/coords.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
use std::fmt;
use std::convert::{TryFrom, TryInto};

#[derive(Clone, Serialize, Deserialize)]
pub struct Coords(pub Vec<usize>);
pub struct Coords<T = usize>(pub Vec<T>);

impl Coords {
impl<T> Coords<T>
where T: TryFrom<usize> + TryInto<usize>,
<T as TryFrom<usize>>::Error: fmt::Debug,
<T as TryInto<usize>>::Error: fmt::Debug,
{
pub fn from_index(index: usize, dims: &[usize]) -> Self {
let mut coords = vec![0; dims.len()];
let mut remainder = index;

for i in (0..dims.len()).rev() {
coords[i] = remainder % dims[i];
remainder /= dims[i];
}

Coords(coords)
Coords(dims.iter().scan(index, |remainder, &dim| {
let coord = T::try_from(*remainder % dim).unwrap();
*remainder /= dim;
Some(coord)
}).collect())
}

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

impl fmt::Debug for Coords {
impl<T: fmt::Debug> fmt::Debug for Coords<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
Expand Down
20 changes: 10 additions & 10 deletions src/server/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum CellState { Empty, Cleared, Mine }
pub struct CellInfo {
pub surrounding: usize,
pub state: CellState,
pub coords: Coords
pub coords: Coords<i32>
}

#[derive(Serialize, Deserialize)]
Expand All @@ -27,10 +27,10 @@ pub struct Game {
pub id: Option<ObjectId>,
pub created_at: DateTime<Utc>,
pub pass: Option<String>,
pub seed: u32,
pub dims: Vec<usize>,
pub size: usize,
pub mines: usize,
pub seed: i32,
pub dims: Vec<i32>,
pub size: i32,
pub mines: i32,
pub autoclear: bool,
pub turns: Vec<Turn>,
pub clients: Vec<String>,
Expand All @@ -42,20 +42,20 @@ pub struct Game {
#[serde(rename_all = "camelCase")]
pub struct Turn {
pub turn_taken_at: DateTime<Utc>,
pub clear_req: Vec<Coords>,
pub clear_req: Vec<Coords<i32>>,
pub clear_actual: Vec<CellInfo>,
pub flagged: Vec<Coords>,
pub unflagged: Vec<Coords>,
pub flagged: Vec<Coords<i32>>,
pub unflagged: Vec<Coords<i32>>,
pub game_over: bool,
pub win: bool,
pub cells_rem: usize,
pub cells_rem: i32,
}

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

fn id(&self) -> Option<ObjectId> {
return self.id.clone();
self.id.clone()
}

fn set_id(&mut self, oid: ObjectId) {
Expand Down
12 changes: 6 additions & 6 deletions src/server/native_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct TurnInfo {

impl TurnInfo {
fn to_document(&self, server: &NativeServer) -> db::Turn {
let to_coords_vec = |indices: &[usize]| {
let to_coords_vec = |indices: &[usize]| -> Vec<Coords<i32>> {
indices.iter()
.map(|&i| Coords::from_index(i, &server.dims))
.collect()
Expand Down Expand Up @@ -103,7 +103,7 @@ impl TurnInfo {
unflagged: to_coords_vec(&self.unflagged),
game_over: self.game_state != GameState::Ongoing,
win: self.game_state == GameState::Win,
cells_rem: self.cells_rem
cells_rem: self.cells_rem as i32
}
}
}
Expand Down Expand Up @@ -271,10 +271,10 @@ impl NativeServer {
id: None,
created_at,
pass: None,
seed,
dims: dims.clone(),
size: dims.iter().fold(1, |acc, &d| acc * d),
mines,
seed: seed as i32,
dims: dims.iter().map(|&d| d as i32).collect(),
size: dims.iter().fold(1, |acc, &d| acc * d) as i32,
mines: mines as i32,
autoclear,
turns,
clients: vec!["RustoBusto".to_owned()],
Expand Down

0 comments on commit 21e13a9

Please sign in to comment.