retrofire_core/util/
buf.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
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
//! Two-dimensional buffers, with owned and borrowed variants.
//!
//! Useful for storing pixel data of any kind, among other things.

use alloc::{vec, vec::Vec};
use core::fmt::{Debug, Formatter};
use core::ops::{Deref, DerefMut};

use crate::util::Dims;
use inner::Inner;

//
// Traits
//

/// A trait for types that can provide a view of their data as a [`Slice2`].
pub trait AsSlice2<T> {
    /// Returns a borrowed `Slice2` view of `Self`.
    fn as_slice2(&self) -> Slice2<T>;
}

/// A trait for types that can provide a mutable view of their data
/// as a [`MutSlice2`].
pub trait AsMutSlice2<T> {
    /// Returns a mutably borrowed `MutSlice2` view of `Self`.
    fn as_mut_slice2(&mut self) -> MutSlice2<T>;
}

//
// Types
//

/// A rectangular 2D buffer that owns its elements, backed by a `Vec`.
///
/// Unlike `Vec`, however, `Buf2` cannot be resized after construction
/// without explicitly copying the contents to a new, larger buffer.
///
/// `Buf2` stores its elements contiguously, in standard row-major order,
/// such that the coordinate pair (x, y) maps to the index
/// ```text
/// buf.width() * y + x
/// ```
/// in the backing vector.
///
/// # Examples
/// ```
/// # use retrofire_core::util::buf::*;
/// # use retrofire_core::math::vec::*;
/// // Elements initialized with `Default::default()`
/// let mut buf = Buf2::new((4, 4));
/// // Indexing with a 2D vector (x, y) yields element at row y, column x:
/// buf[vec2(2, 1)] = 123;
/// // Indexing with an usize i yields row with index i as a slice:
/// assert_eq!(buf[1usize], [0, 0, 123, 0]);
/// // Thus you can also do this, row first, column second:
/// assert_eq!(buf[1usize][2], 123)
/// ```
#[derive(Clone)]
#[repr(transparent)]
pub struct Buf2<T>(Inner<T, Vec<T>>);

/// An immutable rectangular view to a region of a [`Buf2`], another `Slice2`,
/// or in general any `&[T]` slice of memory. A two-dimensional analog to `&[T]`.
///
/// A `Slice2` may be discontiguous:
/// ```text
/// +------stride-----+
/// |    ____w____    |
/// |   |r0_______|   |
/// |   |r1_______| h |
/// |   |r2_______|   |
/// +-----------------+
/// ```
/// TODO More documentation
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Slice2<'a, T>(Inner<T, &'a [T]>);

/// A mutable rectangular view to a region of a `Buf2`, a `Slice2`,
/// or in general any `&[T]` slice of memory.
#[repr(transparent)]
pub struct MutSlice2<'a, T>(Inner<T, &'a mut [T]>);

//
// Inherent impls
//

impl<T> Buf2<T> {
    /// Returns a buffer with the given dimensions, with elements initialized
    /// in row-major order with values yielded by `init`.
    ///
    /// # Panics
    /// If there are fewer than `w * h` elements in `init`.
    pub fn new_from<I>(dims: Dims, init: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        let len = (dims.0 * dims.1) as usize;
        let data: Vec<_> = init.into_iter().take(len).collect();
        assert_eq!(data.len(), len);
        Self(Inner::new(dims, dims.0, data))
    }
    /// Returns a buffer with size `w` × `h`, with every element
    /// initialized by calling `T::default()`.
    pub fn new(dims: Dims) -> Self
    where
        T: Clone + Default,
    {
        let data = vec![T::default(); (dims.0 * dims.1) as usize];
        Self(Inner::new(dims, dims.0, data))
    }
    /// Returns a buffer with size `w` × `h`, with every element
    /// initialized by calling `init_fn(x, y)` where x is the column index
    /// and y the row index of the element being initialized.
    pub fn new_with<F>(dims: Dims, init_fn: F) -> Self
    where
        F: Clone + FnMut(u32, u32) -> T,
    {
        let init = (0..dims.1).flat_map(move |y| {
            let mut init_fn = init_fn.clone();
            (0..dims.0).map(move |x| init_fn(x, y)) //
        });
        Self::new_from(dims, init)
    }

    /// Returns a view of the backing data of `self`.
    pub fn data(&self) -> &[T] {
        self.0.data()
    }
    /// Returns a mutable view of the backing data of `self`.
    pub fn data_mut(&mut self) -> &mut [T] {
        self.0.data_mut()
    }
}

impl<'a, T> Slice2<'a, T> {
    /// Returns a new `Slice2` view to `data` with the given dimensions
    /// and stride.
    ///
    /// # Examples
    /// ```
    /// # use retrofire_core::util::buf::Slice2;
    /// let data = &[0, 1, 2, 3, 4, 5, 6];
    /// let slice = Slice2::new((2, 2), 3, data);
    /// assert_eq!(&slice[0usize], &[0, 1]);
    /// assert_eq!(&slice[1usize], &[3, 4]);
    /// ```
    /// Above, `slice` represents a 2×2 rectangle with stride 3, such that
    /// the first row maps to `data[0..2]` and the second to `data[3..5]`:
    /// ```text
    ///  slice[0]    slice[1]
    ///     |           |
    /// ,---´---.   ,---´---.
    /// +---+---+---+---+---+---+---+
    /// | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
    /// +---+---+---+---+---+---+---+
    /// ```
    /// Internally, this is implemented as the borrow `&data[0..5]`.
    /// Semantically, however, `slice` does not contain `data[2]` as
    /// an element, and attempting to access it eg. with `slice[0][2]`
    /// will panic, as expected.
    ///
    /// # Panics
    /// if `stride < width` or if the slice would overflow `data`.
    ///
    pub fn new(dims: Dims, stride: u32, data: &'a [T]) -> Self {
        Self(Inner::new(dims, stride, data))
    }
}

impl<'a, T> MutSlice2<'a, T> {
    /// Returns a new `MutSlice2` view to `data` with dimensions `w` and `h`
    /// and stride `stride`.
    ///
    /// See [`Slice2::new`] for more information.
    pub fn new(dims: Dims, stride: u32, data: &'a mut [T]) -> Self {
        Self(Inner::new(dims, stride, data))
    }
}

//
// Local trait impls
//

impl<T> AsSlice2<T> for Buf2<T> {
    #[inline]
    fn as_slice2(&self) -> Slice2<T> {
        self.0.as_slice2()
    }
}
impl<T> AsSlice2<T> for &Buf2<T> {
    #[inline]
    fn as_slice2(&self) -> Slice2<T> {
        self.0.as_slice2()
    }
}
impl<T> AsSlice2<T> for Slice2<'_, T> {
    #[inline]
    fn as_slice2(&self) -> Slice2<T> {
        self.0.as_slice2()
    }
}
impl<T> AsSlice2<T> for MutSlice2<'_, T> {
    #[inline]
    fn as_slice2(&self) -> Slice2<T> {
        self.0.as_slice2()
    }
}

impl<T> AsMutSlice2<T> for Buf2<T> {
    #[inline]
    fn as_mut_slice2(&mut self) -> MutSlice2<T> {
        self.0.as_mut_slice2()
    }
}
impl<T> AsMutSlice2<T> for &mut Buf2<T> {
    #[inline]
    fn as_mut_slice2(&mut self) -> MutSlice2<T> {
        self.0.as_mut_slice2()
    }
}
impl<T> AsMutSlice2<T> for MutSlice2<'_, T> {
    #[inline]
    fn as_mut_slice2(&mut self) -> MutSlice2<T> {
        self.0.as_mut_slice2()
    }
}

//
// Foreign trait impls
//

impl<T> Debug for Buf2<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        self.0.debug_fmt(f, "Buf2")
    }
}
impl<T> Debug for Slice2<'_, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        self.0.debug_fmt(f, "Slice2")
    }
}
impl<T> Debug for MutSlice2<'_, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        self.0.debug_fmt(f, "Slice2Mut")
    }
}

impl<T> Deref for Buf2<T> {
    type Target = Inner<T, Vec<T>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl<'a, T> Deref for Slice2<'a, T> {
    type Target = Inner<T, &'a [T]>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl<'a, T> Deref for MutSlice2<'a, T> {
    type Target = Inner<T, &'a mut [T]>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> DerefMut for Buf2<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}
impl<'a, T> DerefMut for MutSlice2<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

pub mod inner {
    use core::fmt::Formatter;
    use core::marker::PhantomData;
    use core::ops::{Deref, DerefMut, Index, IndexMut, Range};

    use crate::math::vec::Vec2u;
    use crate::util::rect::Rect;
    use crate::util::Dims;

    use super::{AsSlice2, MutSlice2, Slice2};

    /// A helper type that abstracts over owned and borrowed buffers.
    ///
    /// The types `Buf2`, `Slice2`, and `MutSlice2` deref to `Inner`.
    #[derive(Copy, Clone)]
    pub struct Inner<T, D> {
        dims: Dims,
        stride: u32,
        data: D,
        _pd: PhantomData<T>,
    }

    impl<T, D> Inner<T, D> {
        /// Returns the width of `self`.
        #[inline]
        pub fn width(&self) -> u32 {
            self.dims.0
        }
        /// Returns the height of `self`.
        #[inline]
        pub fn height(&self) -> u32 {
            self.dims.1
        }
        /// Returns the stride of `self`.
        #[inline]
        pub fn stride(&self) -> u32 {
            self.stride
        }
        /// Returns whether the rows of `self` are stored as one contiguous
        /// slice, without gaps between rows.
        ///
        /// `Buf2` instances are always contiguous. A `Slice2` or `MutSlice2`
        /// instance is contiguous if its width equals its stride, if its
        /// height is 1, or if it is empty.
        pub fn is_contiguous(&self) -> bool {
            let (w, h) = self.dims;
            self.stride == w || h <= 1 || w == 0
        }
        /// Returns whether `self` contains no elements.
        pub fn is_empty(&self) -> bool {
            self.dims.0 == 0 || self.dims.1 == 0
        }

        #[inline]
        fn to_index(&self, x: u32, y: u32) -> usize {
            (y * self.stride + x) as usize
        }
        #[inline]
        fn to_index_strict(&self, x: u32, y: u32) -> usize {
            self.to_index_checked(x, y).unwrap_or_else(|| {
                let (w, h) = self.dims;
                panic!(
                    "position (x={x}, y={y}) out of bounds (0..{w}, 0..{h})",
                )
            })
        }
        #[inline]
        fn to_index_checked(&self, x: u32, y: u32) -> Option<usize> {
            let (w, h) = self.dims;
            (x < w && y < h).then(|| self.to_index(x, y))
        }

        fn resolve_bounds(&self, rect: &Rect<u32>) -> (Dims, Range<usize>) {
            let (w, h) = self.dims;
            let l = rect.left.unwrap_or(0);
            let t = rect.top.unwrap_or(0);
            let r = rect.right.unwrap_or(w);
            let b = rect.bottom.unwrap_or(h);

            assert!(l <= r, "range left ({l}) > right ({r})");
            assert!(t <= b, "range top ({l}) > bottom ({r})");
            assert!(r <= w, "range right ({r}) > width ({w})");
            assert!(b <= h, "range bottom ({b}) > height ({h})");

            let start = self.to_index(l, t);
            // Slice end is the end of the last row
            let end = if b == t {
                self.to_index(r, t)
            } else {
                // b != 0 because b > t
                self.to_index(r, b - 1)
            };
            ((r - l, b - t), start..end)
        }

        /// A helper for implementing `Debug`.
        pub(super) fn debug_fmt(
            &self,
            f: &mut Formatter,
            name: &str,
        ) -> core::fmt::Result {
            f.debug_struct(name)
                .field("dims", &self.dims)
                .field("stride", &self.stride)
                .finish()
        }
    }

    impl<T, D: Deref<Target = [T]>> Inner<T, D> {
        /// # Panics
        /// if `stride < w` or if the slice would overflow `data`.
        #[rustfmt::skip]
        pub(super) fn new(dims: Dims, stride: u32, data: D) -> Self {
            let (w, h) = dims;
            let len = data.len() as u32;
            assert!(w <= stride, "width ({w}) > stride ({stride})");
            assert!(
                h <= 1 || stride <= len,
                "stride ({stride}) > data length ({len})"
            );
            assert!(h <= len, "height ({h}) > data length ({len})");
            if h > 0 {
                let size = (h - 1) * stride + w;
                assert!(
                    size <= len,
                    "required size ({size}) > data length ({len})"
                );
            }
            Self { dims, stride, data, _pd: PhantomData }
        }

        /// Returns the data of `self` as a linear slice.
        pub(super) fn data(&self) -> &[T] {
            &self.data
        }

        /// Borrows `self` as a `Slice2`.
        pub fn as_slice2(&self) -> Slice2<T> {
            Slice2::new(self.dims, self.stride, &self.data)
        }

        /// Returns a borrowed rectangular slice of `self`.
        ///
        /// # Panics
        /// If any part of `rect` is outside the bounds of `self`.
        pub fn slice(&self, rect: impl Into<Rect>) -> Slice2<T> {
            let (dims, rg) = self.resolve_bounds(&rect.into());
            Slice2::new(dims, self.stride, &self.data[rg])
        }

        /// Returns a reference to the element at `pos`,
        /// or `None` if `pos` is out of bounds.
        pub fn get(&self, pos: impl Into<Vec2u>) -> Option<&T> {
            let [x, y] = pos.into().0;
            self.to_index_checked(x, y).map(|i| &self.data[i])
        }

        /// Returns an iterator over the rows of `self` as `&[T]` slices.
        ///
        /// The length of each slice equals [`self.width()`](Self::width).
        pub fn rows(&self) -> impl Iterator<Item = &[T]> {
            self.data
                .chunks(self.stride as usize)
                .map(|row| &row[..self.dims.0 as usize])
        }

        /// Returns an iterator over all the elements of `self` in row-major order.
        ///
        /// First returns the elements on row 0 from left to right, followed by the elements
        /// on row 1, and so on.
        pub fn iter(&self) -> impl Iterator<Item = &'_ T> {
            self.rows().flatten()
        }
    }

    impl<T, D: DerefMut<Target = [T]>> Inner<T, D> {
        /// Returns a mutably borrowed rectangular slice of `self`.
        pub fn as_mut_slice2(&mut self) -> MutSlice2<T> {
            MutSlice2::new(self.dims, self.stride, &mut self.data)
        }

        /// Returns the data of `self` as a single mutable slice.
        pub(super) fn data_mut(&mut self) -> &mut [T] {
            &mut self.data
        }

        /// Returns an iterator over the rows of this buffer as `&mut [T]`.
        ///
        /// The length of each slice equals [`self.width()`](Self::width).
        pub fn rows_mut(&mut self) -> impl Iterator<Item = &mut [T]> {
            self.data
                .chunks_mut(self.stride as usize)
                .map(|row| &mut row[..self.dims.0 as usize])
        }

        /// Returns an iterator over all the elements of `self` in row-major
        /// order: first the elements on row 0 from left to right, followed
        /// by the elements on row 1, and so on.
        pub fn iter_mut(&mut self) -> impl Iterator<Item = &'_ mut T> {
            self.rows_mut().flatten()
        }

        /// Fills `self` with clones of `val`.
        pub fn fill(&mut self, val: T)
        where
            T: Clone,
        {
            if self.is_contiguous() {
                self.data.fill(val);
            } else {
                self.rows_mut()
                    .for_each(|row| row.fill(val.clone()));
            }
        }
        /// Fills `self` by invoking `f(x, y)` for every element, where
        /// `x` and `y` are the column and row of the element, respectively.
        pub fn fill_with<F>(&mut self, mut fill_fn: F)
        where
            F: Copy + FnMut(u32, u32) -> T,
        {
            let (w, h) = self.dims;
            let mut fill = (0..h).flat_map(move |y| {
                (0..w).map(move |x| fill_fn(x, y)) //
            });
            if self.is_contiguous() {
                self.data.fill_with(|| fill.next().unwrap());
            } else {
                self.rows_mut().for_each(|row| {
                    row.fill_with(|| fill.next().unwrap()); //
                })
            }
        }

        /// Copies each element in `src` to the same position in `self`.
        ///
        /// This operation is often called "blitting".
        ///
        /// # Panics
        /// if the dimensions of `self` and `src` don't match.
        #[doc(alias = "blit")]
        pub fn copy_from(&mut self, src: impl AsSlice2<T>)
        where
            T: Copy,
        {
            let src = src.as_slice2();
            let (sw, sh) = self.dims;
            let (ow, oh) = src.dims;
            assert_eq!(sw, ow, "width ({sw}) != source width ({ow})",);
            assert_eq!(sh, oh, "height ({sh}) != source height ({oh})",);
            for (dest, src) in self.rows_mut().zip(src.rows()) {
                dest.copy_from_slice(src);
            }
        }

        /// Returns a mutable reference to the element at `pos`,
        /// or `None` if `pos` is out of bounds.
        pub fn get_mut(&mut self, pos: impl Into<Vec2u>) -> Option<&mut T> {
            let [x, y] = pos.into().0;
            self.to_index_checked(x, y)
                .map(|i| &mut self.data[i])
        }

        /// Returns a mutably borrowed rectangular slice of `self`.
        ///
        /// # Panics
        /// If any part of `rect` is outside the bounds of `self`.
        pub fn slice_mut(&mut self, rect: impl Into<Rect>) -> MutSlice2<T> {
            let (dims, rg) = self.resolve_bounds(&rect.into());
            MutSlice2(Inner::new(dims, self.stride, &mut self.data[rg]))
        }
    }

    impl<T, D: Deref<Target = [T]>> Index<usize> for Inner<T, D> {
        type Output = [T];

        /// Returns a reference to the row of `self` at index `i`.
        /// The returned slice has length `self.width()`.
        #[inline]
        fn index(&self, i: usize) -> &[T] {
            let idx = self.to_index_strict(0, i as u32);
            let w = self.dims.0 as usize;
            &self.data[idx..][..w]
        }
    }

    impl<T, D> IndexMut<usize> for Inner<T, D>
    where
        Self: Index<usize, Output = [T]>,
        D: DerefMut<Target = [T]>,
    {
        /// Returns a mutable reference to the row of `self` at index `i`.
        /// The returned slice has length `self.width()`.
        #[inline]
        fn index_mut(&mut self, row: usize) -> &mut [T] {
            let idx = self.to_index_strict(0, row as u32);
            let w = self.dims.0 as usize;
            &mut self.data[idx..][..w]
        }
    }

    impl<T, D, Pos> Index<Pos> for Inner<T, D>
    where
        D: Deref<Target = [T]>,
        Pos: Into<Vec2u>,
    {
        type Output = T;

        /// Returns a reference to the element of `self` at position `pos`.
        /// # Panics
        /// If `pos` is out of bounds of `self`.
        #[inline]
        fn index(&self, pos: Pos) -> &T {
            let [x, y] = pos.into().0;
            &self.data[self.to_index_strict(x, y)]
        }
    }

    impl<T, D, Pos> IndexMut<Pos> for Inner<T, D>
    where
        D: DerefMut<Target = [T]>,
        Pos: Into<Vec2u>,
    {
        /// Returns a mutable reference to the element of `self`
        /// at position `pos`.
        /// # Panics
        /// If `pos` is out of bounds of `self`.
        #[inline]
        fn index_mut(&mut self, pos: Pos) -> &mut T {
            let [x, y] = pos.into().0;
            let idx = self.to_index_strict(x, y);
            &mut self.data[idx]
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::math::vec::vec2;

    use super::*;

    #[test]
    fn buf_new_from() {
        let buf = Buf2::new_from((3, 2), 1..);
        assert_eq!(buf.data(), &[1, 2, 3, 4, 5, 6]);
    }

    #[test]
    fn buf_new() {
        let buf: Buf2<i32> = Buf2::new((3, 2));
        assert_eq!(buf.data(), &[0, 0, 0, 0, 0, 0]);
    }

    #[test]
    fn buf_new_with() {
        let buf = Buf2::new_with((3, 2), |x, y| x + y);
        assert_eq!(buf.data(), &[0, 1, 2, 1, 2, 3]);
    }

    #[test]
    fn buf_extents() {
        let buf: Buf2<()> = Buf2::new((4, 5));
        assert_eq!(buf.width(), 4);
        assert_eq!(buf.height(), 5);
        assert_eq!(buf.stride(), 4);
    }

    #[test]
    fn buf_index_and_get() {
        let buf = Buf2::new_with((4, 5), |x, y| x * 10 + y);

        assert_eq!(buf[2usize], [2, 12, 22, 32]);

        assert_eq!(buf[[0, 0]], 0);
        assert_eq!(buf[vec2(1, 0)], 10);
        assert_eq!(buf[vec2(3, 4)], 34);

        assert_eq!(buf.get([2, 3]), Some(&23));
        assert_eq!(buf.get([4, 4]), None);
        assert_eq!(buf.get([3, 5]), None);
    }

    #[test]
    fn buf_index_mut_and_get_mut() {
        let mut buf = Buf2::new_with((4, 5), |x, y| x * 10 + y);

        buf[2usize][1] = 123;
        assert_eq!(buf[2usize], [2, 123, 22, 32]);

        buf[vec2(2, 3)] = 234;
        assert_eq!(buf[[2, 3]], 234);

        *buf.get_mut([3, 4]).unwrap() = 345;
        assert_eq!(buf.get_mut([3, 4]), Some(&mut 345));
        assert_eq!(buf.get_mut([4, 4]), None);
        assert_eq!(buf.get_mut([3, 5]), None);
    }

    #[test]
    #[should_panic = "position (x=4, y=0) out of bounds (0..4, 0..5)"]
    fn buf_index_x_out_of_bounds_should_panic() {
        let buf = Buf2::new((4, 5));
        let _: i32 = buf[[4, 0]];
    }

    #[test]
    #[should_panic = "position (x=0, y=4) out of bounds (0..5, 0..4)"]
    fn buf_index_y_out_of_bounds_should_panic() {
        let buf = Buf2::new((5, 4));
        let _: i32 = buf[[0, 4]];
    }

    #[test]
    #[should_panic = "position (x=0, y=5) out of bounds (0..4, 0..5)"]
    fn buf_index_row_out_of_bounds_should_panic() {
        let buf = Buf2::new((4, 5));
        let _: &[i32] = &buf[5usize];
    }

    #[test]
    fn buf_slice_range_full() {
        let buf: Buf2<()> = Buf2::new((4, 5));

        let slice = buf.slice(..);
        assert_eq!(slice.width(), 4);
        assert_eq!(slice.height(), 5);
        assert_eq!(slice.stride(), 4);

        let slice = buf.slice((.., ..));
        assert_eq!(slice.width(), 4);
        assert_eq!(slice.height(), 5);
        assert_eq!(slice.stride(), 4);
    }

    #[test]
    fn buf_slice_range_inclusive() {
        let buf: Buf2<()> = Buf2::new((4, 5));
        let slice = buf.slice((1..=3, 0..=3));
        assert_eq!(slice.width(), 3);
        assert_eq!(slice.height(), 4);
        assert_eq!(slice.stride(), 4);
    }

    #[test]
    fn buf_slice_range_to() {
        let buf: Buf2<()> = Buf2::new((4, 5));

        let slice = buf.slice((..2, ..4));
        assert_eq!(slice.width(), 2);
        assert_eq!(slice.height(), 4);
        assert_eq!(slice.stride(), 4);
    }

    #[test]
    fn buf_slice_range_from() {
        let buf: Buf2<()> = Buf2::new((4, 5));

        let slice = buf.slice((3.., 2..));
        assert_eq!(slice.width(), 1);
        assert_eq!(slice.height(), 3);
        assert_eq!(slice.stride(), 4);
    }

    #[test]
    fn buf_slice_empty_range() {
        let buf: Buf2<()> = Buf2::new((4, 5));

        let empty = buf.slice(vec2(1, 1)..vec2(1, 3));
        assert_eq!(empty.width(), 0);
        assert_eq!(empty.height(), 2);
        assert_eq!(empty.stride(), 4);

        let empty = buf.slice(vec2(1, 1)..vec2(3, 1));
        assert_eq!(empty.width(), 2);
        assert_eq!(empty.height(), 0);
        assert_eq!(empty.stride(), 4);
    }

    #[test]
    #[should_panic = "range right (5) > width (4)"]
    fn buf_slice_x_out_of_bounds_should_panic() {
        let buf: Buf2<()> = Buf2::new((4, 5));
        buf.slice((0..5, 1..3));
    }

    #[test]
    #[should_panic = "range bottom (6) > height (5)"]
    fn buf_slice_y_out_of_bounds_should_panic() {
        let buf: Buf2<()> = Buf2::new((4, 5));
        buf.slice((1..3, 0..6));
    }

    #[test]
    #[should_panic = "width (4) > stride (3)"]
    fn slice_stride_less_than_width_should_panic() {
        let _ = Slice2::new((4, 4), 3, &[0; 16]);
    }

    #[test]
    #[should_panic = "required size (19) > data length (16)"]
    fn slice_larger_than_data_should_panic() {
        let _ = Slice2::new((4, 4), 5, &[0; 16]);
    }

    #[test]
    fn slice_extents() {
        let buf: Buf2<()> = Buf2::new((10, 10));

        let slice = buf.slice((1..4, 2..8));
        assert_eq!(slice.width(), 3);
        assert_eq!(slice.height(), 6);
        assert_eq!(slice.stride(), 10);
        assert_eq!(slice.data().len(), 5 * 10 + 3);
    }

    #[test]
    fn slice_contiguity() {
        let buf: Buf2<()> = Buf2::new((10, 10));
        // Buf2 is always contiguous
        assert!(buf.is_contiguous());

        // Empty slice is contiguous
        assert!(buf.slice((2..2, 2..8)).is_contiguous());
        assert!(buf.slice((2..8, 2..2)).is_contiguous());
        // One-row slice is contiguous
        assert!(buf.slice((2..8, 2..3)).is_contiguous());
        // Slice spanning whole width of buf is contiguous
        assert!(buf.slice((0..10, 2..8)).is_contiguous());
        assert!(buf.slice((.., 2..8)).is_contiguous());

        // Slice not spanning the width of buf is not contiguous
        assert!(!buf.slice((2..=2, 1..9)).is_contiguous());
        assert!(!buf.slice((2..4, 0..9)).is_contiguous());
        assert!(!buf.slice((2..4, 1..10)).is_contiguous());
    }

    #[test]
    #[rustfmt::skip]
    fn slice_fill() {
        let mut buf = Buf2::new((5, 4));
        let mut slice = buf.slice_mut((2.., 1..3));

        slice.fill(1);

        assert_eq!(
            buf.data(),
            &[0, 0, 0, 0, 0,
              0, 0, 1, 1, 1,
              0, 0, 1, 1, 1,
              0, 0, 0, 0, 0]
        );
    }

    #[test]
    #[rustfmt::skip]
    fn slice_fill_with() {
        let mut buf = Buf2::new((5, 4));
        let mut slice = buf.slice_mut((2.., 1..3));

        slice.fill_with(|x, y| x + y);

        assert_eq!(
            buf.data(),
            &[0, 0, 0, 0, 0,
              0, 0, 0, 1, 2,
              0, 0, 1, 2, 3,
              0, 0, 0, 0, 0]
        );
    }

    #[test]
    #[rustfmt::skip]
    fn slice_copy_from() {
        let mut dest = Buf2::new((5, 4));
        let src = Buf2::new_with((3, 3), |x, y| x + y);

        dest.slice_mut((1..4, 1..)).copy_from(src);

        assert_eq!(
            dest.data(),
            &[0, 0, 0, 0, 0,
              0, 0, 1, 2, 0,
              0, 1, 2, 3, 0,
              0, 2, 3, 4, 0]
        );
    }

    #[test]
    fn slice_index() {
        let buf = Buf2::new_with((5, 4), |x, y| x * 10 + y);
        let slice = buf.slice((2.., 1..3));

        assert_eq!(slice[vec2(0, 0)], 21);
        assert_eq!(slice[vec2(1, 0)], 31);
        assert_eq!(slice[vec2(2, 1)], 42);

        assert_eq!(slice.get(vec2(2, 1)), Some(&42));
        assert_eq!(slice.get(vec2(2, 2)), None);
    }

    #[test]
    fn slice_index_mut() {
        let mut buf = Buf2::new_with((5, 5), |x, y| x * 10 + y);
        let mut slice = buf.slice_mut((2.., 1..3));

        slice[[2, 1]] = 123;
        assert_eq!(slice[vec2(2, 1)], 123);

        assert_eq!(slice.get_mut(vec2(2, 1)), Some(&mut 123));
        assert_eq!(slice.get(vec2(2, 2)), None);

        buf[[2, 2]] = 321;
        let slice = buf.slice((1.., 2..));
        assert_eq!(slice[[1, 0]], 321);
    }

    #[test]
    fn slice_rows() {
        let buf = Buf2::new_with((5, 4), |x, y| x * 10 + y);
        let slice = buf.slice((2..4, 1..));

        let mut rows = slice.rows();
        assert_eq!(rows.next(), Some(&[21, 31][..]));
        assert_eq!(rows.next(), Some(&[22, 32][..]));
        assert_eq!(rows.next(), Some(&[23, 33][..]));
        assert_eq!(rows.next(), None);
    }

    #[test]
    fn slice_rows_mut() {
        let mut buf = Buf2::new_with((5, 4), |x, y| x * 10 + y);
        let mut slice = buf.slice_mut((2..4, 1..));

        let mut rows = slice.rows_mut();
        assert_eq!(rows.next(), Some(&mut [21, 31][..]));
        assert_eq!(rows.next(), Some(&mut [22, 32][..]));
        assert_eq!(rows.next(), Some(&mut [23, 33][..]));
        assert_eq!(rows.next(), None);
    }

    #[test]
    fn buf_ref_as_slice() {
        fn foo<T: AsSlice2<u32>>(buf: T) -> u32 {
            buf.as_slice2().width()
        }
        let buf = Buf2::new((2, 2));
        let w = foo(&buf);
        assert_eq!(w, buf.width());
    }

    #[test]
    fn buf_ref_as_slice_mut() {
        fn foo<T: AsMutSlice2<u32>>(mut buf: T) {
            buf.as_mut_slice2()[[1, 1]] = 42;
        }
        let mut buf = Buf2::new((2, 2));
        foo(&mut buf);
        assert_eq!(buf[[1, 1]], 42);
    }
}