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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use num::{ FromPrimitive, ToPrimitive };
#[derive(Copy, Clone, RustcDecodable, RustcEncodable, PartialEq,
Eq, Ord, PartialOrd, Hash, Debug)]
pub enum MouseButton {
Unknown,
Left,
Right,
Middle,
X1,
X2,
Button6,
Button7,
Button8,
}
impl FromPrimitive for MouseButton {
fn from_u64(n: u64) -> Option<MouseButton> {
match n {
0 => Some(MouseButton::Unknown),
1 => Some(MouseButton::Left),
2 => Some(MouseButton::Right),
3 => Some(MouseButton::Middle),
4 => Some(MouseButton::X1),
5 => Some(MouseButton::X2),
6 => Some(MouseButton::Button6),
7 => Some(MouseButton::Button7),
8 => Some(MouseButton::Button8),
_ => Some(MouseButton::Unknown),
}
}
#[inline(always)]
fn from_i64(n: i64) -> Option<MouseButton> {
FromPrimitive::from_u64(n as u64)
}
#[inline(always)]
fn from_isize(n: isize) -> Option<MouseButton> {
FromPrimitive::from_u64(n as u64)
}
}
impl ToPrimitive for MouseButton {
fn to_u64(&self) -> Option<u64> {
match self {
&MouseButton::Unknown => Some(0),
&MouseButton::Left => Some(1),
&MouseButton::Right => Some(2),
&MouseButton::Middle => Some(3),
&MouseButton::X1 => Some(4),
&MouseButton::X2 => Some(5),
&MouseButton::Button6 => Some(6),
&MouseButton::Button7 => Some(7),
&MouseButton::Button8 => Some(8),
}
}
#[inline(always)]
fn to_i64(&self) -> Option<i64> {
self.to_u64().map(|x| x as i64)
}
#[inline(always)]
fn to_isize(&self) -> Option<isize> {
self.to_u64().map(|x| x as isize)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mouse_button_primitives() {
use num::{ FromPrimitive, ToPrimitive };
for i in 0u64..9 {
let button: MouseButton = FromPrimitive::from_u64(i).unwrap();
let j = ToPrimitive::to_u64(&button).unwrap();
assert_eq!(i, j);
}
}
}