font_types/
point.rs

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

/// Two dimensional point with a generic coordinate type.
#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
#[repr(C)]
pub struct Point<T> {
    /// X coordinate.
    pub x: T,
    /// Y coordinate.
    pub y: T,
}

/// SAFETY:
/// This trait has four preconditions:
///
/// 1. All fields in the struct must implement `NoUninit`
/// 2. The struct must be `#[repr(C)]` or `#[repr(transparent)]`
/// 3. The struct must not contain any padding bytes
/// 4. The struct must contain no generic parameters
///
/// We satisfy the first and second preconditions trivially. The third
/// condition is satisfied because the struct is repr(C) and contains
/// two fields of the same type which guarantees no padding.
///
/// The fourth condition is obviously not satisfied which is what
/// requires implementing this trait manually rather than deriving
/// it. This condition only exists because the bytemuck derive
/// macro cannot guarantee the first three conditions in a type
/// with generic parameters.
#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::NoUninit for Point<T> where T: bytemuck::NoUninit {}

impl<T> Point<T> {
    /// Creates a new point with the given x and y coordinates.
    #[inline(always)]
    pub const fn new(x: T, y: T) -> Self {
        Self { x, y }
    }

    /// Creates a new point from a single value assigned to both coordinates.
    pub const fn broadcast(xy: T) -> Self
    where
        T: Copy,
    {
        Self { x: xy, y: xy }
    }

    /// Maps `Point<T>` to `Point<U>` by applying a function to each coordinate.
    #[inline(always)]
    pub fn map<U>(self, mut f: impl FnMut(T) -> U) -> Point<U> {
        Point {
            x: f(self.x),
            y: f(self.y),
        }
    }
}

impl<T> Add for Point<T>
where
    T: Add<Output = T>,
{
    type Output = Self;

    #[inline(always)]
    fn add(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}

impl<T> AddAssign for Point<T>
where
    T: AddAssign,
{
    #[inline(always)]
    fn add_assign(&mut self, rhs: Self) {
        self.x += rhs.x;
        self.y += rhs.y;
    }
}

impl<T> Sub for Point<T>
where
    T: Sub<Output = T>,
{
    type Output = Self;

    #[inline(always)]
    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

impl<T> SubAssign for Point<T>
where
    T: SubAssign,
{
    #[inline(always)]
    fn sub_assign(&mut self, rhs: Self) {
        self.x -= rhs.x;
        self.y -= rhs.y;
    }
}

impl<T> Mul for Point<T>
where
    T: Mul<Output = T>,
{
    type Output = Self;

    #[inline(always)]
    fn mul(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x * rhs.x,
            y: self.y * rhs.y,
        }
    }
}

impl<T> Mul<T> for Point<T>
where
    T: Mul<Output = T> + Copy,
{
    type Output = Self;

    #[inline(always)]
    fn mul(self, rhs: T) -> Self::Output {
        Self {
            x: self.x * rhs,
            y: self.y * rhs,
        }
    }
}

impl<T> MulAssign for Point<T>
where
    T: MulAssign,
{
    #[inline(always)]
    fn mul_assign(&mut self, rhs: Self) {
        self.x *= rhs.x;
        self.y *= rhs.y;
    }
}

impl<T> MulAssign<T> for Point<T>
where
    T: MulAssign + Copy,
{
    #[inline(always)]
    fn mul_assign(&mut self, rhs: T) {
        self.x *= rhs;
        self.y *= rhs;
    }
}

impl<T> Div for Point<T>
where
    T: Div<Output = T>,
{
    type Output = Self;

    #[inline(always)]
    fn div(self, rhs: Self) -> Self::Output {
        Self {
            x: self.x / rhs.x,
            y: self.y / rhs.y,
        }
    }
}

impl<T> Div<T> for Point<T>
where
    T: Div<Output = T> + Copy,
{
    type Output = Self;

    #[inline(always)]
    fn div(self, rhs: T) -> Self::Output {
        Self {
            x: self.x / rhs,
            y: self.y / rhs,
        }
    }
}

impl<T> DivAssign for Point<T>
where
    T: DivAssign,
{
    #[inline(always)]
    fn div_assign(&mut self, rhs: Self) {
        self.x /= rhs.x;
        self.y /= rhs.y;
    }
}

impl<T> DivAssign<T> for Point<T>
where
    T: DivAssign + Copy,
{
    #[inline(always)]
    fn div_assign(&mut self, rhs: T) {
        self.x /= rhs;
        self.y /= rhs;
    }
}

impl<T> Neg for Point<T>
where
    T: Neg<Output = T>,
{
    type Output = Self;

    #[inline(always)]
    fn neg(self) -> Self::Output {
        Self {
            x: -self.x,
            y: -self.y,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Point;
    use crate::F26Dot6;

    #[test]
    fn map() {
        assert_eq!(
            Point::new(42.5, 20.1).map(F26Dot6::from_f64),
            Point::new(F26Dot6::from_f64(42.5), F26Dot6::from_f64(20.1))
        );
    }

    #[test]
    fn add() {
        assert_eq!(Point::new(1, 2) + Point::new(3, 4), Point::new(4, 6));
        let mut point = Point::new(1, 2);
        point += Point::new(3, 4);
        assert_eq!(point, Point::new(4, 6));
    }

    #[test]
    fn sub() {
        assert_eq!(Point::new(1, 2) - Point::new(3, 4), Point::new(-2, -2));
        let mut point = Point::new(1, 2);
        point -= Point::new(3, 4);
        assert_eq!(point, Point::new(-2, -2));
    }

    #[test]
    fn mul() {
        assert_eq!(Point::new(1, 2) * Point::new(3, 4), Point::new(3, 8));
        let mut point = Point::new(1, 2);
        point *= Point::new(3, 4);
        assert_eq!(point, Point::new(3, 8));
        assert_eq!(Point::new(1, 2) * 8, Point::new(8, 16));
    }

    #[test]
    fn div() {
        assert_eq!(Point::new(10, 16) / Point::new(2, 3), Point::new(5, 5));
        let mut point = Point::new(10, 16);
        point /= Point::new(2, 3);
        assert_eq!(point, Point::new(5, 5));
        assert_eq!(Point::new(10, 16) / 2, Point::new(5, 8));
    }

    #[test]
    fn neg() {
        assert_eq!(-Point::new(1, -2), Point::new(-1, 2));
    }
}