Newer
Older
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
98
99
100
101
102
103
104
105
use crate::TileStatus;
pub trait IntoTile {
fn into_tile(self) -> i32;
}
// Implementations for unsigned integer types
impl IntoTile for u8 {
fn into_tile(self) -> i32 {
self as i32
}
}
impl IntoTile for u16 {
fn into_tile(self) -> i32 {
self as i32
}
}
impl IntoTile for u32 {
fn into_tile(self) -> i32 {
self as i32
}
}
impl IntoTile for u64 {
fn into_tile(self) -> i32 {
self as i32
}
}
impl IntoTile for u128 {
fn into_tile(self) -> i32 {
self as i32
}
}
impl IntoTile for usize {
fn into_tile(self) -> i32 {
self as i32
}
}
// Implementations for signed integer types
impl IntoTile for i8 {
fn into_tile(self) -> i32 {
self as i32
}
}
impl IntoTile for i16 {
fn into_tile(self) -> i32 {
self as i32
}
}
impl IntoTile for i32 {
fn into_tile(self) -> i32 {
self
}
}
impl IntoTile for i64 {
fn into_tile(self) -> i32 {
self as i32
}
}
impl IntoTile for i128 {
fn into_tile(self) -> i32 {
self as i32
}
}
impl IntoTile for isize {
fn into_tile(self) -> i32 {
self as i32
}
}
// We don't actually implement IntoTile for TileStatus, as this would conflict with a later From impl
impl TileStatus {
pub fn into_tile(self) -> i32 {
match self {
Self::Ignore => 0,
Self::Nothing => -1000001,
Self::Anything => 1000001,
Self::Is(value) => value,
Self::IsNot(value) => -(value),
}
}
}
impl From<TileStatus> for i32 {
fn from(value: TileStatus) -> Self {
value.into_tile()
}
}
impl From<TileStatus> for i64 {
fn from(value: TileStatus) -> Self {
value.into_tile() as i64
}
}
impl <T: IntoTile> IntoTile for Option<T> {
fn into_tile(self) -> i32 {
match self {
Some(value) => value.into_tile(),
None => 0,
}
}
}
impl <T: IntoTile, E> IntoTile for Result<T, E> {
fn into_tile(self) -> i32 {
match self {
Ok(value) => value.into_tile(),
Err(_) => 0,
}
}
}
impl <I: IntoTile> From<I> for TileStatus {
fn from(value: I) -> Self {
let value = value.into_tile();
match value {
0 => Self::Ignore,
1000001 => Self::Anything,
-1000001 => Self::Nothing,
other => {
if other > 0 {
Self::Is(other.into_tile())
} else {
Self::IsNot(other.abs().into_tile())
}
}
}
}
}