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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
//! Rectangles and points.

use crate::sys;
use std::convert::{AsMut, AsRef};
use std::hash::{Hash, Hasher};
use std::mem;
use std::ops::{
    Add, AddAssign, BitAnd, BitOr, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Neg, Sub,
    SubAssign,
};
use std::ptr;

/// The maximal integer value that can be used for rectangles.
///
/// This value is smaller than strictly needed, but is useful in ensuring that
/// rect sizes will never have to be truncated when clamping.
pub fn max_int_value() -> u32 {
    i32::max_value() as u32 / 2
}

/// The minimal integer value that can be used for rectangle positions
/// and points.
///
/// This value is needed, because otherwise the width of a rectangle created
/// from a point would be able to exceed the maximum width.
pub fn min_int_value() -> i32 {
    i32::min_value() / 2
}

fn clamp_size(val: u32) -> u32 {
    if val == 0 {
        1
    } else if val > max_int_value() {
        max_int_value()
    } else {
        val
    }
}

fn clamp_position(val: i32) -> i32 {
    if val > max_int_value() as i32 {
        max_int_value() as i32
    } else if val < min_int_value() {
        min_int_value()
    } else {
        val
    }
}

fn clamped_mul(a: i32, b: i32) -> i32 {
    match a.checked_mul(b) {
        Some(val) => val,
        None => {
            if (a < 0) ^ (b < 0) {
                min_int_value()
            } else {
                max_int_value() as i32
            }
        }
    }
}

/// A (non-empty) rectangle.
///
/// The width and height of a `Rect` must always be strictly positive (never
/// zero).  In cases where empty rects may need to represented, it is
/// recommended to use `Option<Rect>`, with `None` representing an empty
/// rectangle (see, for example, the output of the
/// [`intersection`](#method.intersection) method).
#[derive(Clone, Copy)]
pub struct Rect {
    raw: sys::SDL_Rect,
}

impl ::std::fmt::Debug for Rect {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        return write!(
            fmt,
            "Rect {{ x: {}, y: {}, w: {}, h: {} }}",
            self.raw.x, self.raw.y, self.raw.w, self.raw.h
        );
    }
}

impl PartialEq for Rect {
    fn eq(&self, other: &Rect) -> bool {
        self.raw.x == other.raw.x
            && self.raw.y == other.raw.y
            && self.raw.w == other.raw.w
            && self.raw.h == other.raw.h
    }
}

impl Eq for Rect {}

impl Hash for Rect {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.raw.x.hash(state);
        self.raw.y.hash(state);
        self.raw.w.hash(state);
        self.raw.h.hash(state);
    }
}

impl Rect {
    /// Creates a new rectangle from the given values.
    ///
    /// The width and height are clamped to ensure that the right and bottom
    /// sides of the rectangle does not exceed i32::max_value() (the value
    /// 2147483647, the maximal positive size of an i32).  This means that the
    /// rect size will behave oddly if you move it very far to the right or
    /// downwards on the screen.
    ///
    /// `Rect`s must always be non-empty, so a `width` and/or `height` argument
    /// of 0 will be replaced with 1.
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> Rect {
        let raw = sys::SDL_Rect {
            x: clamp_position(x),
            y: clamp_position(y),
            w: clamp_size(width) as i32,
            h: clamp_size(height) as i32,
        };
        Rect { raw }
    }

    /// Creates a new rectangle centered on the given position.
    ///
    /// The width and height are clamped to ensure that the right and bottom
    /// sides of the rectangle does not exceed i32::max_value() (the value
    /// 2147483647, the maximal positive size of an i32).  This means that the
    /// rect size will behave oddly if you move it very far to the right or
    /// downwards on the screen.
    ///
    /// `Rect`s must always be non-empty, so a `width` and/or `height` argument
    /// of 0 will be replaced with 1.
    pub fn from_center<P>(center: P, width: u32, height: u32) -> Rect
    where
        P: Into<Point>,
    {
        let raw = sys::SDL_Rect {
            x: 0,
            y: 0,
            w: clamp_size(width) as i32,
            h: clamp_size(height) as i32,
        };
        let mut rect = Rect { raw };
        rect.center_on(center.into());
        rect
    }

    /// The horizontal position of this rectangle.
    #[inline]
    pub fn x(&self) -> i32 {
        self.raw.x
    }

    /// The vertical position of this rectangle.
    #[inline]
    pub fn y(&self) -> i32 {
        self.raw.y
    }

    /// The width of this rectangle.
    pub fn width(&self) -> u32 {
        self.raw.w as u32
    }

    /// The height of this rectangle.
    pub fn height(&self) -> u32 {
        self.raw.h as u32
    }

    /// Returns the width and height of this rectangle.
    pub fn size(&self) -> (u32, u32) {
        (self.width(), self.height())
    }

    /// Sets the horizontal position of this rectangle to the given value,
    /// clamped to be less than or equal to i32::max_value() / 2.
    pub fn set_x(&mut self, x: i32) {
        self.raw.x = clamp_position(x);
    }

    /// Sets the vertical position of this rectangle to the given value,
    /// clamped to be less than or equal to i32::max_value() / 2.
    pub fn set_y(&mut self, y: i32) {
        self.raw.y = clamp_position(y);
    }

    /// Sets the width of this rectangle to the given value,
    /// clamped to be less than or equal to i32::max_value() / 2.
    ///
    /// `Rect`s must always be non-empty, so a `width` argument of 0 will be
    /// replaced with 1.
    pub fn set_width(&mut self, width: u32) {
        self.raw.w = clamp_size(width) as i32;
    }

    /// Sets the height of this rectangle to the given value,
    /// clamped to be less than or equal to i32::max_value() / 2.
    ///
    /// `Rect`s must always be non-empty, so a `height` argument of 0 will be
    /// replaced with 1.
    pub fn set_height(&mut self, height: u32) {
        self.raw.h = clamp_size(height) as i32;
    }

    /// Returns the x-position of the left side of this rectangle.
    pub fn left(&self) -> i32 {
        self.raw.x
    }

    /// Returns the x-position of the right side of this rectangle.
    pub fn right(&self) -> i32 {
        self.raw.x + self.raw.w
    }

    /// Returns the y-position of the top side of this rectangle.
    pub fn top(&self) -> i32 {
        self.raw.y
    }

    /// Returns the y-position of the bottom side of this rectangle.
    pub fn bottom(&self) -> i32 {
        self.raw.y + self.raw.h
    }

    /// Shifts this rectangle to the left by `offset`.
    ///
    /// # Example
    ///
    /// ```
    /// use sdl2::rect::Rect;
    /// assert_eq!(Rect::new(0, 0, 10, 10).left_shifted(5), Rect::new(-5, 0, 10, 10));
    /// ```
    pub fn left_shifted(mut self, offset: i32) -> Rect {
        self.offset(-offset, self.y());
        self
    }

    /// Shifts this rectangle to the right by `offset`.
    ///
    /// # Example
    ///
    /// ```
    /// use sdl2::rect::Rect;
    /// assert_eq!(Rect::new(0, 0, 10, 10).right_shifted(5), Rect::new(5, 0, 10, 10));
    /// ```
    pub fn right_shifted(mut self, offset: i32) -> Rect {
        self.offset(offset, self.y());
        self
    }

    /// Shifts this rectangle to the top by `offset`.
    ///
    /// # Example
    ///
    /// ```
    /// use sdl2::rect::Rect;
    /// assert_eq!(Rect::new(0, 0, 10, 10).top_shifted(5), Rect::new(0, -5, 10, 10));
    /// ```
    pub fn top_shifted(mut self, offset: i32) -> Rect {
        self.offset(self.x(), -offset);
        self
    }

    /// Shifts this rectangle to the bottom by `offset`.
    ///
    /// # Example
    ///
    /// ```
    /// use sdl2::rect::Rect;
    /// assert_eq!(Rect::new(0, 0, 10, 10).bottom_shifted(5), Rect::new(0, 5, 10, 10));
    /// ```
    pub fn bottom_shifted(mut self, offset: i32) -> Rect {
        self.offset(self.x(), offset);
        self
    }

    /// Returns the center position of this rectangle.
    ///
    /// Note that if the width or height is not a multiple of two,
    /// the center will be rounded down.
    ///
    /// # Example
    ///
    /// ```
    /// use sdl2::rect::{Rect,Point};
    /// let rect = Rect::new(1,0,2,3);
    /// assert_eq!(Point::new(2,1),rect.center());
    /// ```
    pub fn center(&self) -> Point {
        let x = self.raw.x + (self.raw.w / 2);
        let y = self.raw.y + (self.raw.h / 2);
        Point::new(x, y)
    }

    /// Returns the top-left corner of this rectangle.
    ///
    /// # Example
    ///
    /// ```
    /// use sdl2::rect::{Rect, Point};
    /// let rect = Rect::new(1, 0, 2, 3);
    /// assert_eq!(Point::new(1, 0), rect.top_left());
    /// ```
    pub fn top_left(&self) -> Point {
        Point::new(self.left(), self.top())
    }

    /// Returns the top-right corner of this rectangle.
    ///
    /// # Example
    ///
    /// ```
    /// use sdl2::rect::{Rect, Point};
    /// let rect = Rect::new(1, 0, 2, 3);
    /// assert_eq!(Point::new(3, 0), rect.top_right());
    /// ```
    pub fn top_right(&self) -> Point {
        Point::new(self.right(), self.top())
    }

    /// Returns the bottom-left corner of this rectangle.
    ///
    /// # Example
    ///
    /// ```
    /// use sdl2::rect::{Rect, Point};
    /// let rect = Rect::new(1, 0, 2, 3);
    /// assert_eq!(Point::new(1, 3), rect.bottom_left());
    /// ```
    pub fn bottom_left(&self) -> Point {
        Point::new(self.left(), self.bottom())
    }

    /// Returns the bottom-right corner of this rectangle.
    ///
    /// # Example
    ///
    /// ```
    /// use sdl2::rect::{Rect, Point};
    /// let rect = Rect::new(1, 0, 2, 3);
    /// assert_eq!(Point::new(3, 3), rect.bottom_right());
    /// ```
    pub fn bottom_right(&self) -> Point {
        Point::new(self.right(), self.bottom())
    }

    /// Sets the position of the right side of this rectangle to the given
    /// value, clamped to be less than or equal to i32::max_value() / 2.
    pub fn set_right(&mut self, right: i32) {
        self.raw.x = clamp_position(clamp_position(right) - self.raw.w);
    }

    /// Sets the position of the bottom side of this rectangle to the given
    /// value, clamped to be less than or equal to i32::max_value() / 2.
    pub fn set_bottom(&mut self, bottom: i32) {
        self.raw.y = clamp_position(clamp_position(bottom) - self.raw.h);
    }

    /// Centers the rectangle on the given point (in place).
    #[inline]
    pub fn center_on<P>(&mut self, point: P)
    where
        P: Into<(i32, i32)>,
    {
        let (x, y) = point.into();
        self.raw.x = clamp_position(clamp_position(x) - self.raw.w / 2);
        self.raw.y = clamp_position(clamp_position(y) - self.raw.h / 2);
    }

    /// Centers the rectangle on the given point.
    #[inline]
    pub fn centered_on<P>(mut self, point: P) -> Rect
    where
        P: Into<(i32, i32)>,
    {
        self.center_on(point);
        self
    }

    /// Move this rect and clamp the positions to prevent over/underflow.
    /// This also clamps the size to prevent overflow.
    #[inline]
    pub fn offset(&mut self, x: i32, y: i32) {
        match self.raw.x.checked_add(x) {
            Some(val) => self.raw.x = clamp_position(val),
            None => {
                if x >= 0 {
                    self.raw.x = max_int_value() as i32;
                } else {
                    self.raw.x = i32::min_value();
                }
            }
        }
        match self.raw.y.checked_add(y) {
            Some(val) => self.raw.y = clamp_position(val),
            None => {
                if y >= 0 {
                    self.raw.y = max_int_value() as i32;
                } else {
                    self.raw.y = i32::min_value();
                }
            }
        }
    }

    /// Moves this rect to the given position after clamping the values.
    pub fn reposition<P>(&mut self, point: P)
    where
        P: Into<(i32, i32)>,
    {
        let (x, y) = point.into();
        self.raw.x = clamp_position(x);
        self.raw.y = clamp_position(y);
    }

    /// Resizes this rect to the given size after clamping the values.
    pub fn resize(&mut self, width: u32, height: u32) {
        self.raw.w = clamp_size(width) as i32;
        self.raw.h = clamp_size(height) as i32;
    }

    /// Checks whether this rectangle contains a given point.
    ///
    /// Points along the right and bottom edges are not considered to be inside
    /// the rectangle; this way, a 1-by-1 rectangle contains only a single
    /// point.  Another way to look at it is that this method returns true if
    /// and only if the given point would be painted by a call to
    /// [`Renderer::fill_rect`](
    /// ../render/struct.Renderer.html#method.fill_rect).
    ///
    /// # Examples
    ///
    /// ```
    /// use sdl2::rect::{Rect, Point};
    /// let rect = Rect::new(1, 2, 3, 4);
    /// assert!(rect.contains_point(Point::new(1, 2)));
    /// assert!(!rect.contains_point(Point::new(0, 1)));
    /// assert!(rect.contains_point(Point::new(3, 5)));
    /// assert!(!rect.contains_point(Point::new(4, 6)));
    /// ```
    pub fn contains_point<P>(&self, point: P) -> bool
    where
        P: Into<(i32, i32)>,
    {
        let (x, y) = point.into();
        let inside_x = x >= self.left() && x < self.right();
        inside_x && (y >= self.top() && y < self.bottom())
    }

    /// Checks whether this rectangle completely contains another rectangle.
    ///
    /// This method returns true if and only if every point contained by
    /// `other` is also contained by `self`; in other words, if the
    /// intersection of `self` and `other` is equal to `other`.
    ///
    /// # Examples
    ///
    /// ```
    /// use sdl2::rect::Rect;
    /// let rect = Rect::new(1, 2, 3, 4);
    /// assert!(rect.contains_rect(rect));
    /// assert!(rect.contains_rect(Rect::new(3, 3, 1, 1)));
    /// assert!(!rect.contains_rect(Rect::new(2, 1, 1, 1)));
    /// assert!(!rect.contains_rect(Rect::new(3, 3, 2, 1)));
    /// ```
    pub fn contains_rect(&self, other: Rect) -> bool {
        other.left() >= self.left()
            && other.right() <= self.right()
            && other.top() >= self.top()
            && other.bottom() <= self.bottom()
    }

    /// Returns the underlying C Rect.
    // this can prevent introducing UB until
    // https://github.com/rust-lang/rust-clippy/issues/5953 is fixed
    #[allow(clippy::trivially_copy_pass_by_ref)]
    pub fn raw(&self) -> *const sys::SDL_Rect {
        &self.raw
    }

    pub fn raw_mut(&mut self) -> *mut sys::SDL_Rect {
        self.raw() as *mut _
    }

    #[doc(alias = "SDL_Rect")]
    pub fn raw_slice(slice: &[Rect]) -> *const sys::SDL_Rect {
        slice.as_ptr() as *const sys::SDL_Rect
    }

    pub fn from_ll(raw: sys::SDL_Rect) -> Rect {
        Rect::new(raw.x, raw.y, raw.w as u32, raw.h as u32)
    }

    /// Calculate a minimal rectangle enclosing a set of points.
    /// If a clipping rectangle is given, only points that are within it will be
    /// considered.
    #[doc(alias = "SDL_EnclosePoints")]
    pub fn from_enclose_points<R: Into<Option<Rect>>>(
        points: &[Point],
        clipping_rect: R,
    ) -> Option<Rect>
    where
        R: Into<Option<Rect>>,
    {
        let clipping_rect = clipping_rect.into();

        if points.is_empty() {
            return None;
        }

        let mut out = mem::MaybeUninit::uninit();

        let clip_ptr = match clipping_rect.as_ref() {
            Some(r) => r.raw(),
            None => ptr::null(),
        };

        let result = unsafe {
            sys::SDL_EnclosePoints(
                Point::raw_slice(points),
                points.len() as i32,
                clip_ptr,
                out.as_mut_ptr(),
            ) != sys::SDL_bool::SDL_FALSE
        };

        if result {
            let out = unsafe { out.assume_init() };

            // Return an error if the dimensions are too large.
            Some(Rect::from_ll(out))
        } else {
            None
        }
    }

    /// Determines whether two rectangles intersect.
    ///
    /// Rectangles that share an edge but don't actually overlap are not
    /// considered to intersect.
    ///
    /// # Examples
    ///
    /// ```
    /// use sdl2::rect::Rect;
    /// let rect = Rect::new(0, 0, 5, 5);
    /// assert!(rect.has_intersection(rect));
    /// assert!(rect.has_intersection(Rect::new(2, 2, 5, 5)));
    /// assert!(!rect.has_intersection(Rect::new(5, 0, 5, 5)));
    /// ```
    #[doc(alias = "SDL_HasIntersection")]
    pub fn has_intersection(&self, other: Rect) -> bool {
        unsafe { sys::SDL_HasIntersection(self.raw(), other.raw()) != sys::SDL_bool::SDL_FALSE }
    }

    /// Calculates the intersection of two rectangles.
    ///
    /// Returns `None` if the two rectangles don't intersect.  Rectangles that
    /// share an edge but don't actually overlap are not considered to
    /// intersect.
    ///
    /// The bitwise AND operator `&` can also be used.
    ///
    /// # Examples
    ///
    /// ```
    /// use sdl2::rect::Rect;
    /// let rect = Rect::new(0, 0, 5, 5);
    /// assert_eq!(rect.intersection(rect), Some(rect));
    /// assert_eq!(rect.intersection(Rect::new(2, 2, 5, 5)),
    ///            Some(Rect::new(2, 2, 3, 3)));
    /// assert_eq!(rect.intersection(Rect::new(5, 0, 5, 5)), None);
    /// ```
    #[doc(alias = "SDL_IntersectRect")]
    pub fn intersection(&self, other: Rect) -> Option<Rect> {
        let mut out = mem::MaybeUninit::uninit();

        let success = unsafe {
            sys::SDL_IntersectRect(self.raw(), other.raw(), out.as_mut_ptr())
                != sys::SDL_bool::SDL_FALSE
        };

        if success {
            let out = unsafe { out.assume_init() };
            Some(Rect::from_ll(out))
        } else {
            None
        }
    }

    /// Calculates the union of two rectangles (i.e. the smallest rectangle
    /// that contains both).
    ///
    /// The bitwise OR operator `|` can also be used.
    ///
    /// # Examples
    ///
    /// ```
    /// use sdl2::rect::Rect;
    /// let rect = Rect::new(0, 0, 5, 5);
    /// assert_eq!(rect.union(rect), rect);
    /// assert_eq!(rect.union(Rect::new(2, 2, 5, 5)), Rect::new(0, 0, 7, 7));
    /// assert_eq!(rect.union(Rect::new(5, 0, 5, 5)), Rect::new(0, 0, 10, 5));
    /// ```
    #[doc(alias = "SDL_UnionRect")]
    pub fn union(&self, other: Rect) -> Rect {
        let mut out = mem::MaybeUninit::uninit();

        unsafe {
            // If `self` and `other` are both empty, `out` remains uninitialized.
            // Because empty rectangles aren't allowed in Rect, we don't need to worry about this.
            sys::SDL_UnionRect(self.raw(), other.raw(), out.as_mut_ptr())
        };

        let out = unsafe { out.assume_init() };

        Rect::from_ll(out)
    }

    /// Calculates the intersection of a rectangle and a line segment and
    /// returns the points of their intersection.
    #[doc(alias = "SDL_IntersectRectAndLine")]
    pub fn intersect_line(&self, start: Point, end: Point) -> Option<(Point, Point)> {
        let (mut start_x, mut start_y) = (start.x(), start.y());
        let (mut end_x, mut end_y) = (end.x(), end.y());

        let intersected = unsafe {
            sys::SDL_IntersectRectAndLine(
                self.raw(),
                &mut start_x,
                &mut start_y,
                &mut end_x,
                &mut end_y,
            ) != sys::SDL_bool::SDL_FALSE
        };

        if intersected {
            Some((Point::new(start_x, start_y), Point::new(end_x, end_y)))
        } else {
            None
        }
    }
}

impl Deref for Rect {
    type Target = sys::SDL_Rect;

    /// # Example
    ///
    /// ```rust
    /// use sdl2::rect::Rect;
    /// let rect = Rect::new(2, 3, 4, 5);
    /// assert_eq!(2, rect.x);
    /// ```
    fn deref(&self) -> &sys::SDL_Rect {
        &self.raw
    }
}

impl DerefMut for Rect {
    /// # Example
    ///
    /// ```rust
    /// use sdl2::rect::Rect;
    /// let mut rect = Rect::new(2, 3, 4, 5);
    /// rect.x = 60;
    /// assert_eq!(60, rect.x);
    /// ```
    fn deref_mut(&mut self) -> &mut sys::SDL_Rect {
        &mut self.raw
    }
}

impl Into<sys::SDL_Rect> for Rect {
    fn into(self) -> sys::SDL_Rect {
        self.raw
    }
}

impl Into<(i32, i32, u32, u32)> for Rect {
    fn into(self) -> (i32, i32, u32, u32) {
        (self.raw.x, self.raw.y, self.raw.w as u32, self.raw.h as u32)
    }
}

impl From<sys::SDL_Rect> for Rect {
    fn from(raw: sys::SDL_Rect) -> Rect {
        Rect { raw }
    }
}

impl From<(i32, i32, u32, u32)> for Rect {
    fn from((x, y, width, height): (i32, i32, u32, u32)) -> Rect {
        Rect::new(x, y, width, height)
    }
}

impl AsRef<sys::SDL_Rect> for Rect {
    fn as_ref(&self) -> &sys::SDL_Rect {
        &self.raw
    }
}

impl AsMut<sys::SDL_Rect> for Rect {
    fn as_mut(&mut self) -> &mut sys::SDL_Rect {
        &mut self.raw
    }
}

// Intersection
impl BitAnd<Rect> for Rect {
    type Output = Option<Rect>;
    #[doc(alias = "SDL_Point")]
    fn bitand(self, rhs: Rect) -> Option<Rect> {
        self.intersection(rhs)
    }
}

// Union
impl BitOr<Rect> for Rect {
    type Output = Rect;
    fn bitor(self, rhs: Rect) -> Rect {
        self.union(rhs)
    }
}

/// Immutable point type, consisting of x and y.
#[derive(Copy, Clone)]
pub struct Point {
    raw: sys::SDL_Point,
}

impl ::std::fmt::Debug for Point {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        return write!(fmt, "Point {{ x: {}, y: {} }}", self.raw.x, self.raw.y);
    }
}

impl PartialEq for Point {
    fn eq(&self, other: &Point) -> bool {
        self.raw.x == other.raw.x && self.raw.y == other.raw.y
    }
}

impl Eq for Point {}

impl Hash for Point {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.raw.x.hash(state);
        self.raw.y.hash(state);
    }
}

impl Deref for Point {
    type Target = sys::SDL_Point;

    /// # Example
    ///
    /// ```rust
    /// use sdl2::rect::Point;
    /// let point = Point::new(2, 3);
    /// assert_eq!(2, point.x);
    /// ```
    fn deref(&self) -> &sys::SDL_Point {
        &self.raw
    }
}

impl DerefMut for Point {
    /// # Example
    ///
    /// ```rust
    /// use sdl2::rect::Point;
    /// let mut point = Point::new(2, 3);
    /// point.x = 4;
    /// assert_eq!(4, point.x);
    /// ```
    fn deref_mut(&mut self) -> &mut sys::SDL_Point {
        &mut self.raw
    }
}

impl AsRef<sys::SDL_Point> for Point {
    fn as_ref(&self) -> &sys::SDL_Point {
        &self.raw
    }
}

impl AsMut<sys::SDL_Point> for Point {
    fn as_mut(&mut self) -> &mut sys::SDL_Point {
        &mut self.raw
    }
}

impl From<sys::SDL_Point> for Point {
    fn from(prim: sys::SDL_Point) -> Point {
        Point { raw: prim }
    }
}

impl From<(i32, i32)> for Point {
    fn from((x, y): (i32, i32)) -> Point {
        Point::new(x, y)
    }
}

impl Into<sys::SDL_Point> for Point {
    fn into(self) -> sys::SDL_Point {
        self.raw
    }
}

impl Into<(i32, i32)> for Point {
    fn into(self) -> (i32, i32) {
        (self.x(), self.y())
    }
}

impl Point {
    /// Creates a new point from the given coordinates.
    pub fn new(x: i32, y: i32) -> Point {
        Point {
            raw: sys::SDL_Point {
                x: clamp_position(x),
                y: clamp_position(y),
            },
        }
    }

    pub fn from_ll(raw: sys::SDL_Point) -> Point {
        Point::new(raw.x, raw.y)
    }

    #[doc(alias = "SDL_Point")]
    pub fn raw_slice(slice: &[Point]) -> *const sys::SDL_Point {
        slice.as_ptr() as *const sys::SDL_Point
    }
    // this can prevent introducing UB until
    // https://github.com/rust-lang/rust-clippy/issues/5953 is fixed
    #[allow(clippy::trivially_copy_pass_by_ref)]
    pub fn raw(&self) -> *const sys::SDL_Point {
        &self.raw
    }

    /// Returns a new point by shifting this point's coordinates by the given
    /// x and y values.
    pub fn offset(self, x: i32, y: i32) -> Point {
        let x = match self.raw.x.checked_add(x) {
            Some(val) => val,
            None => {
                if x < 0 {
                    min_int_value()
                } else {
                    max_int_value() as i32
                }
            }
        };
        let y = match self.raw.y.checked_add(y) {
            Some(val) => val,
            None => {
                if y < 0 {
                    min_int_value()
                } else {
                    max_int_value() as i32
                }
            }
        };
        Point::new(x, y)
    }

    /// Returns a new point by multiplying this point's coordinates by the
    /// given scale factor.
    pub fn scale(self, f: i32) -> Point {
        Point::new(clamped_mul(self.raw.x, f), clamped_mul(self.raw.y, f))
    }

    /// Returns the x-coordinate of this point.
    pub fn x(self) -> i32 {
        self.raw.x
    }

    /// Returns the y-coordinate of this point.
    pub fn y(self) -> i32 {
        self.raw.y
    }
}

impl Add for Point {
    type Output = Point;

    fn add(self, rhs: Point) -> Point {
        self.offset(rhs.x(), rhs.y())
    }
}

impl AddAssign for Point {
    fn add_assign(&mut self, rhs: Point) {
        self.raw.x = clamp_position(self.x() + rhs.x());
        self.raw.y = clamp_position(self.y() + rhs.y());
    }
}

impl Neg for Point {
    type Output = Point;

    fn neg(self) -> Point {
        Point::new(-self.x(), -self.y())
    }
}

impl Sub for Point {
    type Output = Point;

    fn sub(self, rhs: Point) -> Point {
        self.offset(-rhs.x(), -rhs.y())
    }
}

impl SubAssign for Point {
    fn sub_assign(&mut self, rhs: Point) {
        self.raw.x = clamp_position(self.x() - rhs.x());
        self.raw.y = clamp_position(self.y() - rhs.y());
    }
}

impl Mul<i32> for Point {
    type Output = Point;

    fn mul(self, rhs: i32) -> Point {
        self.scale(rhs)
    }
}

impl MulAssign<i32> for Point {
    fn mul_assign(&mut self, rhs: i32) {
        self.raw.x = clamped_mul(self.x(), rhs);
        self.raw.y = clamped_mul(self.y(), rhs);
    }
}

impl Div<i32> for Point {
    type Output = Point;

    fn div(self, rhs: i32) -> Point {
        Point::new(self.x() / rhs, self.y() / rhs)
    }
}

impl DivAssign<i32> for Point {
    fn div_assign(&mut self, rhs: i32) {
        self.raw.x /= rhs;
        self.raw.y /= rhs;
    }
}

impl std::iter::Sum for Point {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Point::new(0, 0), Point::add)
    }
}

#[cfg(test)]
mod test {
    use super::{max_int_value, min_int_value, Point, Rect};

    /// Used to compare "literal" (unclamped) rect values.
    fn tuple(x: i32, y: i32, w: u32, h: u32) -> (i32, i32, u32, u32) {
        (x, y, w, h)
    }

    #[test]
    fn centered() {
        // Tests both center_on and centered_on
        assert_eq!(
            Rect::new(0, 0, 10, 10).centered_on((0, 0)),
            Rect::new(-5, -5, 10, 10)
        );
    }

    #[test]
    fn enclose_points_valid() {
        assert_eq!(
            Some(tuple(2, 4, 4, 6)),
            Rect::from_enclose_points(&[Point::new(2, 4), Point::new(5, 9)], None)
                .map(|r| r.into())
        );
    }

    #[test]
    fn enclose_points_outside_clip_rect() {
        assert_eq!(
            Rect::from_enclose_points(
                &[Point::new(0, 0), Point::new(10, 10)],
                Some(Rect::new(3, 3, 1, 1))
            ),
            None
        );
    }

    #[test]
    fn enclose_points_max_values() {
        // Try to enclose the top-left-most and bottom-right-most points.
        assert_eq!(
            Some(tuple(
                min_int_value(),
                min_int_value(),
                max_int_value(),
                max_int_value()
            )),
            Rect::from_enclose_points(
                &[
                    Point::new(i32::min_value(), i32::min_value()),
                    Point::new(i32::max_value(), i32::max_value())
                ],
                None
            )
            .map(|r| r.into())
        );
    }

    #[test]
    fn has_intersection() {
        let rect = Rect::new(0, 0, 10, 10);
        assert!(rect.has_intersection(Rect::new(9, 9, 10, 10)));
        // edge
        assert!(!rect.has_intersection(Rect::new(10, 10, 10, 10)));
        // out
        assert!(!rect.has_intersection(Rect::new(11, 11, 10, 10)));
    }

    #[test]
    fn intersection() {
        let rect = Rect::new(0, 0, 10, 10);
        assert_eq!(rect & Rect::new(9, 9, 10, 10), Some(Rect::new(9, 9, 1, 1)));
        assert_eq!(rect & Rect::new(11, 11, 10, 10), None);
    }

    #[test]
    fn union() {
        assert_eq!(
            Rect::new(0, 0, 1, 1) | Rect::new(9, 9, 1, 1),
            Rect::new(0, 0, 10, 10)
        );
    }

    #[test]
    fn intersect_line() {
        assert_eq!(
            Rect::new(1, 1, 5, 5).intersect_line(Point::new(0, 0), Point::new(10, 10)),
            Some((Point::new(1, 1), Point::new(5, 5)))
        );
    }

    #[test]
    fn clamp_size_zero() {
        assert_eq!(tuple(0, 0, 1, 1), Rect::new(0, 0, 0, 0).into());
    }

    #[test]
    fn clamp_position_min() {
        assert_eq!(
            tuple(min_int_value(), min_int_value(), 1, 1),
            Rect::new(i32::min_value(), i32::min_value(), 1, 1).into()
        );
    }

    #[test]
    fn clamp_size_max() {
        assert_eq!(
            tuple(0, 0, max_int_value(), max_int_value()),
            Rect::new(0, 0, max_int_value() + 1, max_int_value() + 1).into()
        );
    }

    #[test]
    fn clamp_i32_max() {
        assert_eq!(
            tuple(0, 0, max_int_value(), max_int_value()),
            Rect::new(0, 0, i32::max_value() as u32, i32::max_value() as u32).into()
        )
    }

    #[test]
    fn clamp_position_max() {
        assert_eq!(
            tuple(max_int_value() as i32, max_int_value() as i32, 1, 1),
            Rect::new(max_int_value() as i32 + 1, max_int_value() as i32 + 1, 1, 1).into()
        );
    }

    #[test]
    fn shifted() {
        // Groups all functions into a single assertion
        let rect = Rect::new(0, 0, 10, 10)
            .left_shifted(5)
            .right_shifted(5)
            .top_shifted(5)
            .bottom_shifted(5);
        assert_eq!(rect, Rect::new(0, 0, 10, 10));
    }

    #[test]
    fn rect_into() {
        let test: (i32, i32, u32, u32) = (-11, 5, 50, 20);
        assert_eq!(test, Rect::new(-11, 5, 50, 20).into());
    }

    #[test]
    fn rect_from() {
        assert_eq!(Rect::from((-11, 5, 50, 20)), Rect::new(-11, 5, 50, 20));
    }

    #[test]
    fn point_into() {
        let test: (i32, i32) = (-11, 5);
        assert_eq!(test, Point::new(-11, 5).into());
    }

    #[test]
    fn point_from() {
        let test: (i32, i32) = (-11, 5);
        assert_eq!(test, Point::new(-11, 5).into());
    }

    #[test]
    fn point_add() {
        assert_eq!(Point::new(-5, 7), Point::new(-11, 5) + Point::new(6, 2));
    }

    #[test]
    fn point_add_assign() {
        let mut point = Point::new(-11, 5);
        point += Point::new(6, 2);
        assert_eq!(point, Point::new(-11, 5) + Point::new(6, 2));
    }

    #[test]
    fn point_sub() {
        assert_eq!(Point::new(-17, 3), Point::new(-11, 5) - Point::new(6, 2));
    }

    #[test]
    fn point_sub_assign() {
        let mut point = Point::new(-11, 5);
        point -= Point::new(6, 2);
        assert_eq!(point, Point::new(-11, 5) - Point::new(6, 2));
    }

    #[test]
    fn point_mul() {
        assert_eq!(Point::new(-33, 15), Point::new(-11, 5) * 3);
    }

    #[test]
    fn point_mul_assign() {
        let mut point = Point::new(-11, 5);
        point *= 3;
        assert_eq!(point, Point::new(-11, 5) * 3);
    }

    #[test]
    fn point_mul_clamp() {
        assert_eq!(
            Point::new(0x7fffffff, -0x7fffffff),
            Point::new(-1000000, 5000000) * -3000000
        );
    }

    #[test]
    fn point_mul_assign_clamp() {
        let mut point = Point::new(-1000000, 5000000);
        point *= -3000000;
        assert_eq!(point, Point::new(-1000000, 5000000) * -3000000);
    }

    #[test]
    fn point_div() {
        assert_eq!(Point::new(-3, 1), Point::new(-11, 5) / 3);
    }

    #[test]
    fn point_div_assign() {
        let mut point = Point::new(-11, 5);
        point /= 3;
        assert_eq!(point, Point::new(-11, 5) / 3);
    }

    #[test]
    fn point_sum() {
        let points_sum: Point = vec![Point::new(-11, 5), Point::new(6, 2)].into_iter().sum();
        assert_eq!(Point::new(-5, 7), points_sum);
    }
}