blob: 23e0433f59823f29d55e6e54853a6a6cd0401149 (
plain)
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
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Point {
pub x: u8,
pub y: u8
}
impl Point {
pub fn new(x: u8, y: u8) -> Point {
Point { x, y }
}
pub fn move_left(&self) -> Option<Point> {
self.x.checked_sub(1).map(|x| Point {
x: x,
..*self
})
}
pub fn move_right(&self, size: &Point) -> Option<Point> {
if self.x + 1 >= size.x {
None
} else {
Some(Point {
x: self.x + 1,
..*self
})
}
}
}
use std::cmp::Ord;
use std::cmp::Ordering;
impl PartialOrd for Point {
fn partial_cmp(&self, other: &Point) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Point {
fn cmp(&self, other: &Point) -> Ordering {
self.x.cmp(&other.x).then(self.y.cmp(&other.y))
}
}
|