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
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Axis {
    X,
    Y,
}

impl Axis {
    pub fn other(self) -> Self {
        match self {
            Axis::X => Axis::Y,
            Axis::Y => Axis::X,
        }
    }
    pub fn new_coord(self, this_axis: i32, other_axis: i32) -> Coord {
        match self {
            Axis::X => Coord::new(this_axis, other_axis),
            Axis::Y => Coord::new(other_axis, this_axis),
        }
    }
    pub fn try_new_size(
        self,
        this_axis: u32,
        other_axis: u32,
    ) -> Result<Size, DimensionTooLargeForSize> {
        match self {
            Axis::X => Size::try_new(this_axis, other_axis),
            Axis::Y => Size::try_new(other_axis, this_axis),
        }
    }
    pub fn new_size(self, this_axis: u32, other_axis: u32) -> Size {
        match self {
            Axis::X => Size::new(this_axis, other_axis),
            Axis::Y => Size::new(other_axis, this_axis),
        }
    }
}

pub trait StaticAxis: private::Sealed {
    type Other;
    fn axis() -> Axis;
    fn new_coord(this_axis: i32, other_axis: i32) -> Coord;
    fn coord_get(coord: Coord) -> i32;
    fn coord_get_mut(coord: &mut Coord) -> &mut i32;
    fn coord_with_axis<F: FnMut(i32) -> i32>(coord: Coord, f: F) -> Coord;
    fn coord_set(coord: Coord, value: i32) -> Coord;
    fn coord_set_in_place(coord: &mut Coord, value: i32);
    fn try_new_size(this_axis: u32, other_axis: u32) -> Result<Size, DimensionTooLargeForSize>;
    fn size_get(size: Size) -> u32;
    fn size_get_mut(size: &mut Size) -> &mut u32;
    fn size_with_axis<F: FnMut(u32) -> u32>(size: Size, f: F) -> Size;
    fn try_size_set(size: Size, value: u32) -> Result<Size, DimensionTooLargeForSize>;
    fn try_size_set_in_place(size: &mut Size, value: u32) -> Result<(), DimensionTooLargeForSize>;
    fn size_set(size: Size, value: u32) -> Size {
        match Self::try_size_set(size, value) {
            Err(DimensionTooLargeForSize) => {
                panic!("Value is too big: {}. Max is {}.", value, MAX_SIZE_FIELD);
            }
            Ok(size) => size,
        }
    }
    fn size_set_in_place(size: &mut Size, value: u32) {
        match Self::try_size_set_in_place(size, value) {
            Err(DimensionTooLargeForSize) => {
                panic!("Value is too big: {}. Max is {}.", value, MAX_SIZE_FIELD);
            }
            Ok(()) => (),
        }
    }
    fn new_size(this_axis: u32, other_axis: u32) -> Size {
        match Self::try_new_size(this_axis, other_axis) {
            Err(DimensionTooLargeForSize) => {
                panic!(
                    "Size is too big: ({}, {}). Max is {}.",
                    this_axis, other_axis, MAX_SIZE_FIELD
                );
            }
            Ok(size) => size,
        }
    }
}

pub mod static_axis {
    pub struct X;
    pub struct Y;
}

fn check_size_limit(value: u32) -> Result<(), DimensionTooLargeForSize> {
    if value > MAX_SIZE_FIELD {
        Err(DimensionTooLargeForSize)
    } else {
        Ok(())
    }
}

impl StaticAxis for static_axis::X {
    type Other = static_axis::Y;
    fn axis() -> Axis {
        Axis::X
    }
    fn new_coord(this_axis: i32, other_axis: i32) -> Coord {
        Coord::new(this_axis, other_axis)
    }
    fn coord_get(coord: Coord) -> i32 {
        coord.x
    }
    fn coord_get_mut(coord: &mut Coord) -> &mut i32 {
        &mut coord.x
    }
    fn coord_with_axis<F: FnMut(i32) -> i32>(coord: Coord, mut f: F) -> Coord {
        Coord {
            x: f(coord.x),
            ..coord
        }
    }
    fn coord_set(coord: Coord, value: i32) -> Coord {
        Coord { x: value, ..coord }
    }
    fn coord_set_in_place(coord: &mut Coord, value: i32) {
        coord.x = value
    }
    fn try_new_size(this_axis: u32, other_axis: u32) -> Result<Size, DimensionTooLargeForSize> {
        Size::try_new(this_axis, other_axis)
    }
    fn size_get(size: Size) -> u32 {
        size.x
    }
    fn size_get_mut(size: &mut Size) -> &mut u32 {
        &mut size.x
    }
    fn size_with_axis<F: FnMut(u32) -> u32>(size: Size, mut f: F) -> Size {
        Size {
            x: f(size.x),
            ..size
        }
    }
    fn try_size_set(size: Size, value: u32) -> Result<Size, DimensionTooLargeForSize> {
        check_size_limit(value)?;
        Ok(Size { x: value, ..size })
    }
    fn try_size_set_in_place(size: &mut Size, value: u32) -> Result<(), DimensionTooLargeForSize> {
        check_size_limit(value)?;
        size.x = value;
        Ok(())
    }
}

impl StaticAxis for static_axis::Y {
    type Other = static_axis::X;
    fn axis() -> Axis {
        Axis::Y
    }
    fn new_coord(this_axis: i32, other_axis: i32) -> Coord {
        Coord::new(other_axis, this_axis)
    }
    fn coord_get(coord: Coord) -> i32 {
        coord.y
    }
    fn coord_get_mut(coord: &mut Coord) -> &mut i32 {
        &mut coord.y
    }
    fn coord_with_axis<F: FnMut(i32) -> i32>(coord: Coord, mut f: F) -> Coord {
        Coord {
            y: f(coord.y),
            ..coord
        }
    }
    fn coord_set(coord: Coord, value: i32) -> Coord {
        Coord { y: value, ..coord }
    }
    fn coord_set_in_place(coord: &mut Coord, value: i32) {
        coord.y = value
    }
    fn size_get(size: Size) -> u32 {
        size.y
    }
    fn size_get_mut(size: &mut Size) -> &mut u32 {
        &mut size.y
    }
    fn size_with_axis<F: FnMut(u32) -> u32>(size: Size, mut f: F) -> Size {
        Size {
            y: f(size.y),
            ..size
        }
    }
    fn try_size_set(size: Size, value: u32) -> Result<Size, DimensionTooLargeForSize> {
        check_size_limit(value)?;
        Ok(Size { y: value, ..size })
    }
    fn try_size_set_in_place(size: &mut Size, value: u32) -> Result<(), DimensionTooLargeForSize> {
        check_size_limit(value)?;
        size.y = value;
        Ok(())
    }
    fn try_new_size(this_axis: u32, other_axis: u32) -> Result<Size, DimensionTooLargeForSize> {
        Size::try_new(other_axis, this_axis)
    }
}

mod private {
    pub trait Sealed {}

    impl Sealed for super::static_axis::X {}
    impl Sealed for super::static_axis::Y {}
}

/// General purpose coordinate
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default, PartialOrd, Ord)]
pub struct Coord {
    pub x: i32,
    pub y: i32,
}

#[derive(Debug)]
pub struct NegativeDimension;

#[derive(Debug)]
pub struct DimensionTooLargeForSize;

#[derive(Debug)]
pub struct DimensionTooLargeForCoord;

impl Coord {
    pub const fn new(x: i32, y: i32) -> Self {
        Self { x, y }
    }
    pub fn from_size(size: Size) -> Result<Self, DimensionTooLargeForCoord> {
        size.to_coord()
    }
    #[cfg(feature = "rand")]
    pub fn random_within<R: rand::Rng>(size: Size, rng: &mut R) -> Self {
        let x = rng.gen_range(0, size.width() as i32);
        let y = rng.gen_range(0, size.height() as i32);
        Self { x, y }
    }
    pub fn to_size(self) -> Result<Size, NegativeDimension> {
        if self.x < 0 || self.y < 0 {
            Err(NegativeDimension)
        } else {
            Ok(Size::new(self.x as u32, self.y as u32))
        }
    }
    fn normalize_part(value: i32, size: u32) -> i32 {
        let value = value % size as i32;
        if value < 0 {
            value + size as i32
        } else {
            value
        }
    }
    pub fn normalize(self, size: Size) -> Self {
        Self {
            x: Self::normalize_part(self.x, size.x()),
            y: Self::normalize_part(self.y, size.y()),
        }
    }
    pub fn is_valid(self, size: Size) -> bool {
        if self.x < 0 || self.y < 0 {
            return false;
        }
        let x = self.x as u32;
        let y = self.y as u32;
        x < size.x() && y < size.y()
    }
    pub fn get(self, axis: Axis) -> i32 {
        match axis {
            Axis::X => self.x,
            Axis::Y => self.y,
        }
    }
    pub fn get_mut(&mut self, axis: Axis) -> &mut i32 {
        match axis {
            Axis::X => &mut self.x,
            Axis::Y => &mut self.y,
        }
    }
    pub fn with_axis<F: FnMut(i32) -> i32>(self, axis: Axis, mut f: F) -> Self {
        match axis {
            Axis::X => Self {
                x: f(self.x),
                ..self
            },
            Axis::Y => Self {
                y: f(self.y),
                ..self
            },
        }
    }
    pub fn set(self, axis: Axis, value: i32) -> Self {
        match axis {
            Axis::X => Self::new(value, self.y),
            Axis::Y => Self::new(self.x, value),
        }
    }
    pub fn set_in_place(&mut self, axis: Axis, value: i32) {
        match axis {
            Axis::X => self.x = value,
            Axis::Y => self.y = value,
        }
    }
    pub fn new_axis(this_axis: i32, other_axis: i32, axis: Axis) -> Self {
        axis.new_coord(this_axis, other_axis)
    }
    pub fn get_static<A: StaticAxis>(self) -> i32 {
        A::coord_get(self)
    }
    pub fn get_static_mut<A: StaticAxis>(&mut self) -> &mut i32 {
        A::coord_get_mut(self)
    }
    pub fn with_static_axis<A: StaticAxis, F: FnMut(i32) -> i32>(self, f: F) -> Self {
        A::coord_with_axis(self, f)
    }
    pub fn set_static<A: StaticAxis>(self, value: i32) -> Self {
        A::coord_set(self, value)
    }
    pub fn set_static_in_place<A: StaticAxis>(&mut self, value: i32) {
        A::coord_set_in_place(self, value)
    }
    pub fn new_static_axis<A: StaticAxis>(this_axis: i32, other_axis: i32) -> Self {
        A::new_coord(this_axis, other_axis)
    }
    pub fn set_x(self, x: i32) -> Self {
        Self { x, ..self }
    }
    pub fn set_y(self, y: i32) -> Self {
        Self { y, ..self }
    }
    pub fn set_x_in_place(&mut self, x: i32) {
        self.x = x;
    }
    pub fn set_y_in_place(&mut self, y: i32) {
        self.y = y;
    }
    pub fn checked_add(self, rhs: Self) -> Option<Self> {
        self.x
            .checked_add(rhs.x)
            .and_then(|x| self.y.checked_add(rhs.y).map(|y| Self::new(x, y)))
    }
    pub fn checked_sub(self, rhs: Self) -> Option<Self> {
        self.x
            .checked_sub(rhs.x)
            .and_then(|x| self.y.checked_sub(rhs.y).map(|y| Self::new(x, y)))
    }
    pub fn checked_mul(self, rhs: i32) -> Option<Self> {
        self.x
            .checked_mul(rhs)
            .and_then(|x| self.y.checked_mul(rhs).map(|y| Self::new(x, y)))
    }
    pub fn checked_div(self, rhs: i32) -> Option<Self> {
        self.x
            .checked_div(rhs)
            .and_then(|x| self.y.checked_div(rhs).map(|y| Self::new(x, y)))
    }
    pub const fn magnitude2(self) -> u32 {
        (self.x * self.x) as u32 + (self.y * self.y) as u32
    }
    pub const fn distance2(self, other: Self) -> u32 {
        Self {
            x: self.x - other.x,
            y: self.y - other.y,
        }
        .magnitude2()
    }
    pub const fn manhattan_magnitude(self) -> u32 {
        self.x.abs() as u32 + self.y.abs() as u32
    }
    pub const fn manhattan_distance(self, other: Self) -> u32 {
        Self {
            x: self.x - other.x,
            y: self.y - other.y,
        }
        .manhattan_magnitude()
    }
    pub const fn opposite(self) -> Self {
        Self {
            x: -self.x,
            y: -self.y,
        }
    }
    pub const fn left90(self) -> Self {
        Self {
            x: self.y,
            y: -self.x,
        }
    }
    pub const fn right90(self) -> Self {
        Self {
            x: -self.y,
            y: self.x,
        }
    }
    pub const fn cardinal_left45(self) -> Self {
        Self {
            x: self.y + self.x,
            y: self.y - self.x,
        }
    }
    pub const fn cardinal_right45(self) -> Self {
        Self {
            x: self.x - self.y,
            y: self.y + self.x,
        }
    }
    pub const fn cardinal_left135(self) -> Self {
        Self {
            x: self.y - self.x,
            y: -self.x - self.y,
        }
    }
    pub const fn cardinal_right135(self) -> Self {
        Self {
            x: -self.y - self.x,
            y: self.x - self.y,
        }
    }

    pub fn is_zero(self) -> bool {
        self.x == 0 && self.y == 0
    }
}

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

impl From<[i32; 2]> for Coord {
    fn from(array: [i32; 2]) -> Self {
        Coord::new(array[0], array[1])
    }
}

/// A size cannot be created which would contain un-addressable cells.
/// That is, the maximum size has a width and height of one greater than the maximum `i32`.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default, PartialOrd, Ord)]
pub struct Size {
    x: u32,
    y: u32,
}

pub const MAX_SIZE_FIELD: u32 = ::std::i32::MAX as u32 + 1;

pub const MAX_SIZE: Size = Size {
    x: MAX_SIZE_FIELD,
    y: MAX_SIZE_FIELD,
};

impl Size {
    pub fn try_new(width: u32, height: u32) -> Result<Self, DimensionTooLargeForSize> {
        check_size_limit(width)?;
        check_size_limit(height)?;
        Ok(Self {
            x: width,
            y: height,
        })
    }

    /// Creates a new `Size`.
    /// Panics if `width` or `width` is greater than `::std::i32::MAX as u32 + 1`.
    pub fn new(width: u32, height: u32) -> Self {
        match Self::try_new(width, height) {
            Err(DimensionTooLargeForSize) => {
                panic!(
                    "Size is too big: ({}, {}). Max is {}.",
                    width, width, MAX_SIZE_FIELD
                );
            }
            Ok(size) => size,
        }
    }

    /// Like new, but const and never panics as it's impossible to construct an invalid size
    pub const fn new_u16(width: u16, height: u16) -> Self {
        Self {
            x: width as u32,
            y: height as u32,
        }
    }

    pub fn from_coord(coord: Coord) -> Result<Self, NegativeDimension> {
        coord.to_size()
    }

    pub fn to_coord(self) -> Result<Coord, DimensionTooLargeForCoord> {
        if self.x > ::std::i32::MAX as u32 || self.y > ::std::i32::MAX as u32 {
            Err(DimensionTooLargeForCoord)
        } else {
            Ok(Coord::new(self.x as i32, self.y as i32))
        }
    }

    pub fn get(self, axis: Axis) -> u32 {
        match axis {
            Axis::X => self.x,
            Axis::Y => self.y,
        }
    }

    pub fn get_mut(&mut self, axis: Axis) -> &mut u32 {
        match axis {
            Axis::X => &mut self.x,
            Axis::Y => &mut self.y,
        }
    }

    pub fn with_axis<F: FnMut(u32) -> u32>(self, axis: Axis, mut f: F) -> Self {
        match axis {
            Axis::X => Self {
                x: f(self.x),
                ..self
            },
            Axis::Y => Self {
                y: f(self.y),
                ..self
            },
        }
    }

    pub fn try_set(self, axis: Axis, value: u32) -> Result<Self, DimensionTooLargeForSize> {
        check_size_limit(value)?;
        Ok(match axis {
            Axis::X => Self {
                x: value,
                y: self.y,
            },
            Axis::Y => Self {
                x: self.x,
                y: value,
            },
        })
    }

    pub fn set(self, axis: Axis, value: u32) -> Self {
        match self.try_set(axis, value) {
            Err(DimensionTooLargeForSize) => {
                panic!("Value is too big: {}. Max is {}.", value, MAX_SIZE_FIELD);
            }
            Ok(size) => size,
        }
    }

    pub fn try_set_in_place(
        &mut self,
        axis: Axis,
        value: u32,
    ) -> Result<(), DimensionTooLargeForSize> {
        check_size_limit(value)?;
        match axis {
            Axis::X => self.x = value,
            Axis::Y => self.y = value,
        }
        Ok(())
    }

    pub fn set_in_place(&mut self, axis: Axis, value: u32) {
        match self.try_set_in_place(axis, value) {
            Err(DimensionTooLargeForSize) => {
                panic!("Value is too big: {}. Max is {}.", value, MAX_SIZE_FIELD);
            }
            Ok(()) => (),
        }
    }

    pub fn try_new_axis(
        this_axis: u32,
        other_axis: u32,
        axis: Axis,
    ) -> Result<Self, DimensionTooLargeForSize> {
        axis.try_new_size(this_axis, other_axis)
    }

    pub fn new_axis(this_axis: u32, other_axis: u32, axis: Axis) -> Self {
        axis.new_size(this_axis, other_axis)
    }

    pub fn get_static<A: StaticAxis>(self) -> u32 {
        A::size_get(self)
    }
    pub fn get_static_mut<A: StaticAxis>(&mut self) -> &mut u32 {
        A::size_get_mut(self)
    }
    pub fn with_static_axis<A: StaticAxis, F: FnMut(u32) -> u32>(self, f: F) -> Self {
        A::size_with_axis(self, f)
    }
    pub fn try_set_static<A: StaticAxis>(
        self,
        value: u32,
    ) -> Result<Self, DimensionTooLargeForSize> {
        A::try_size_set(self, value)
    }
    pub fn try_set_static_in_place<A: StaticAxis>(
        &mut self,
        value: u32,
    ) -> Result<(), DimensionTooLargeForSize> {
        A::try_size_set_in_place(self, value)
    }

    pub fn set_static<A: StaticAxis>(self, value: u32) -> Self {
        A::size_set(self, value)
    }

    pub fn set_static_in_place<A: StaticAxis>(&mut self, value: u32) {
        A::size_set_in_place(self, value)
    }

    pub fn try_new_static_axis<A: StaticAxis>(
        this_axis: u32,
        other_axis: u32,
    ) -> Result<Self, DimensionTooLargeForSize> {
        A::try_new_size(this_axis, other_axis)
    }

    pub fn new_static_axis<A: StaticAxis>(this_axis: u32, other_axis: u32) -> Self {
        A::new_size(this_axis, other_axis)
    }

    pub fn try_set_width(self, width: u32) -> Result<Self, DimensionTooLargeForSize> {
        self.try_set_static::<static_axis::X>(width)
    }

    pub fn try_set_height(self, height: u32) -> Result<Self, DimensionTooLargeForSize> {
        self.try_set_static::<static_axis::Y>(height)
    }

    pub fn set_width(self, width: u32) -> Self {
        self.set_static::<static_axis::X>(width)
    }

    pub fn set_height(self, height: u32) -> Self {
        self.set_static::<static_axis::Y>(height)
    }

    pub fn try_set_width_in_place(&mut self, width: u32) -> Result<(), DimensionTooLargeForSize> {
        self.try_set_static_in_place::<static_axis::X>(width)
    }

    pub fn try_set_height_in_place(&mut self, height: u32) -> Result<(), DimensionTooLargeForSize> {
        self.try_set_static_in_place::<static_axis::Y>(height)
    }

    pub fn set_width_in_place(&mut self, width: u32) {
        self.set_static_in_place::<static_axis::X>(width)
    }

    pub fn set_height_in_place(&mut self, height: u32) {
        self.set_static_in_place::<static_axis::Y>(height)
    }

    /// Returns the width.
    pub const fn width(self) -> u32 {
        self.x
    }

    /// Alias for `width`.
    pub const fn x(self) -> u32 {
        self.x
    }

    /// Returns the height.
    pub const fn height(self) -> u32 {
        self.y
    }

    /// Alias for `height`.
    pub const fn y(self) -> u32 {
        self.y
    }

    /// Return the number of cells in a 2D grid of this size.
    pub const fn count(self) -> usize {
        (self.x * self.y) as usize
    }

    pub fn checked_sub(self, rhs: Self) -> Option<Self> {
        self.x
            .checked_sub(rhs.x)
            .and_then(|x| self.y.checked_sub(rhs.y).map(|y| Self::new(x, y)))
    }

    pub fn saturating_sub(self, rhs: Self) -> Self {
        let x = self.x.saturating_sub(rhs.x);
        let y = self.y.saturating_sub(rhs.y);
        Self::new(x, y)
    }

    pub const fn max_field() -> u32 {
        MAX_SIZE_FIELD
    }

    pub const fn max() -> Self {
        MAX_SIZE
    }

    pub fn is_zero(self) -> bool {
        self.x == 0 && self.y == 0
    }
}

impl From<(u32, u32)> for Size {
    fn from((x, y): (u32, u32)) -> Self {
        Size::new(x, y)
    }
}

impl From<[u32; 2]> for Size {
    fn from(array: [u32; 2]) -> Self {
        Size::new(array[0], array[1])
    }
}

#[cfg(test)]
mod test {
    use super::{Coord, Size};

    #[test]
    fn normalize() {
        assert_eq!(
            Coord::new(5, 2).normalize(Size::new(2, 3)),
            Coord::new(1, 2)
        );
        assert_eq!(
            Coord::new(-4, 3).normalize(Size::new(3, 1)),
            Coord::new(2, 0)
        );
    }

    #[test]
    fn manhattan_dsitance() {
        assert_eq!(Coord::new(-2, 4).manhattan_distance(Coord::new(5, -2)), 13);
    }

    #[test]
    fn rotation() {
        assert_eq!(Coord::new(2, -3).opposite(), Coord::new(-2, 3));
        assert_eq!(Coord::new(2, -3).left90(), Coord::new(-3, -2));
        assert_eq!(Coord::new(2, -3).right90(), Coord::new(3, 2));
        assert_eq!(Coord::new(0, -1).cardinal_left135(), Coord::new(-1, 1));
        assert_eq!(Coord::new(0, -1).cardinal_right135(), Coord::new(1, 1));
        assert_eq!(Coord::new(-1, 0).cardinal_left135(), Coord::new(1, 1));
        assert_eq!(Coord::new(-1, 0).cardinal_right135(), Coord::new(1, -1));
    }
}