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
use crate::{Coord, CoordNum, MapCoords};
pub trait Convert<T, U> {
type Output;
fn convert(&self) -> Self::Output;
}
impl<G, T: CoordNum, U: CoordNum> Convert<T, U> for G
where
G: MapCoords<T, U>,
U: From<T>,
{
type Output = <Self as MapCoords<T, U>>::Output;
fn convert(&self) -> Self::Output {
self.map_coords(|Coord { x, y }| Coord {
x: x.into(),
y: y.into(),
})
}
}
pub trait TryConvert<T, U> {
type Output;
fn try_convert(&self) -> Self::Output;
}
impl<G, T: CoordNum, U: CoordNum> TryConvert<T, U> for G
where
G: MapCoords<T, U>,
U: TryFrom<T>,
{
type Output = Result<<Self as MapCoords<T, U>>::Output, <U as TryFrom<T>>::Error>;
fn try_convert(&self) -> Self::Output {
self.try_map_coords(|Coord { x, y }| {
Ok(Coord {
x: x.try_into()?,
y: y.try_into()?,
})
})
}
}