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
106
107
108
109
110
111
112
113
114
115
116
117
/// A rectangle structure.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(C)]
pub struct Rect<T> {
/// X coordinate of the rectangle.
pub x: T,
/// Y coordinate of the rectangle.
pub y: T,
/// Width of the rectangle.
pub width: T,
/// Height of the rectangle.
pub height: T,
}
impl<T> Rect<T> {
/// Creates a new rectangle with the specified coordinates and size.
#[inline]
pub fn new(x: T, y: T, width: T, height: T) -> Self {
Rect {
x,
y,
width,
height,
}
}
/// Returns the origin point of the rectangle.
#[inline]
pub fn origin(&self) -> Point<T>
where
T: Copy,
{
Point::new(self.x, self.y)
}
/// Returns the size of the rectangle.
pub fn size(&self) -> Size<T>
where
T: Copy,
{
Size::new(self.width, self.height)
}
/// Maps the rectangle to a new type using the provided function.
pub fn map<F, U>(&self, f: F) -> Rect<U>
where
T: Copy,
F: Fn(T) -> U,
{
Rect {
x: f(self.x),
y: f(self.y),
width: f(self.width),
height: f(self.height),
}
}
}
/// A point structure.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(C)]
pub struct Point<T> {
/// X coordinate of the point.
pub x: T,
/// Y coordinate of the point.
pub y: T,
}
impl<T> Point<T> {
/// Creates a new point with the specified coordinates.
#[inline]
pub fn new(x: T, y: T) -> Self {
Point { x, y }
}
/// Maps the point to a new type using the provided function.
pub fn map<F, U>(&self, f: F) -> Point<U>
where
T: Copy,
F: Fn(T) -> U,
{
Point {
x: f(self.x),
y: f(self.y),
}
}
}
/// A size structure.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(C)]
pub struct Size<T> {
/// Width of the size.
pub width: T,
/// Height of the size.
pub height: T,
}
impl<T> Size<T> {
/// Creates a new size with the specified width and height.
#[inline]
pub fn new(width: T, height: T) -> Self {
Size { width, height }
}
/// Maps the size to a new type using the provided function.
pub fn map<F, U>(&self, f: F) -> Size<U>
where
T: Copy,
F: Fn(T) -> U,
{
Size {
width: f(self.width),
height: f(self.height),
}
}
}