1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
use std::cmp::Ordering; #[derive(Default, Eq, Clone, Copy, Serialize, Deserialize, Debug)] pub struct Coord { pub x: i32, pub y: i32, } impl Coord { pub fn new(x: i32, y: i32) -> Coord { Coord { x: x, y: y } } pub fn matches(&self, x: i32, y: i32) -> bool { self.x == x && self.y == y } pub fn distance(&self, other: &Self) -> i32 { (self.x - other.x).abs() + (self.y - other.y).abs() } pub fn to_flat_repr(coords: Vec<Coord>) -> Vec<i32> { let mut flat_result = vec![]; for coord in coords { flat_result.push(coord.x); flat_result.push(coord.y); } flat_result } } impl Ord for Coord { fn cmp(&self, other: &Self) -> Ordering { match self.x.cmp(&other.x) { Ordering::Equal => self.y.cmp(&other.y), ord => ord, } } } impl PartialOrd for Coord { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) } } impl PartialEq for Coord { fn eq(&self, other: &Self) -> bool { (self.x == other.x) & (self.y == other.y) } }