pasture-core 0.5.0

A framework for working with point cloud data
Documentation
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
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
use anyhow::Result;
use std::{collections::HashMap, iter::FromIterator, ops::Range};

use crate::layout::{
    PointAttributeDefinition, PointAttributeMember, PointLayout, PointType, PrimitiveType,
};

use super::{
    buffer_views::{AttributeView, AttributeViewMut, PointView, PointViewMut},
    AttributeViewConverting, BufferSliceColumnar, BufferSliceColumnarMut, BufferSliceInterleaved,
    BufferSliceInterleavedMut, RawAttributeView, RawAttributeViewMut, SliceBuffer, SliceBufferMut,
};

/// Base trait for all point buffers in pasture. The only assumption this trait makes is that the
/// underlying memory can be borrowed by the buffer. Provides point and attribute accessors by
/// untyped value (i.e. copying into a provided `&mut [u8]`)
pub trait BorrowedBuffer<'a> {
    /// Returns the length of this buffer, i.e. the number of points
    ///
    /// # Example
    ///
    /// ```
    /// use pasture_core::containers::*;
    /// use pasture_core::layout::*;
    ///
    /// let buffer = VectorBuffer::new_from_layout(PointLayout::default());
    /// assert_eq!(0, buffer.len());
    /// ```
    fn len(&self) -> usize;
    /// Returns `true` if this buffer does not store any points
    ///
    /// # Example
    ///
    /// ```
    /// use pasture_core::containers::*;
    /// use pasture_core::layout::*;
    ///
    /// let buffer = VectorBuffer::new_from_layout(PointLayout::default());
    /// assert!(buffer.is_empty());
    /// ```
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
    /// Returns the `PointLayout` of this buffer. The `PointLayout` describes the structure of a single
    /// point at runtime.
    ///
    /// # Example
    ///
    /// ```
    /// use pasture_core::containers::*;
    /// use pasture_core::layout::*;
    ///
    /// let layout = PointLayout::from_attributes(&[attributes::POSITION_3D, attributes::INTENSITY]);
    /// let buffer = VectorBuffer::new_from_layout(layout.clone());
    /// assert_eq!(layout, *buffer.point_layout());
    /// ```
    fn point_layout(&self) -> &PointLayout;
    /// Writes the data for the point at `index` from this buffer into `data`
    ///
    /// # Panics
    ///
    /// May panic if `index` is out of bounds.
    /// May panic if `data.len()` does not equal `self.point_layout().size_of_point_entry()`
    fn get_point(&self, index: usize, data: &mut [u8]);
    /// Writes the data for the given `range` of points from this buffer into `data`
    ///
    /// # Panics
    ///
    /// May panic if `range` is out of bounds.
    /// May panic if `data.len()` does not equal `range.len() * self.point_layout().size_of_point_entry()`
    fn get_point_range(&self, range: Range<usize>, data: &mut [u8]);
    /// Writes the data for the given `attribute` of the point at `index` into `data`
    ///
    /// # Panics
    ///
    /// May panic if `attribute` is not part of the `PointLayout` of this buffer. It is implementation-defined
    /// whether data type conversions are supported (i.e. the buffer stores positions as `Vector3<f64>`, but
    /// `get_attribute` is called with `Vector3<f32>` as the attribute data type), but generally assume that
    /// conversions are *not* possible!
    /// May panic if `data.len()` does not equal the size of a single value of the data type of `attribute`
    fn get_attribute(&self, attribute: &PointAttributeDefinition, index: usize, data: &mut [u8]) {
        let attribute_member = self
            .point_layout()
            .get_attribute(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        // Is safe because we get the `attribute_member` from this buffer's point layout
        unsafe {
            self.get_attribute_unchecked(attribute_member, index, data);
        }
    }
    /// Writes the data for the given `attribute` of the given range of points into `data`. The attribute data will
    /// be tightly packed in `data`, irregardless of the memory layout of this buffer. The attribute values might not
    /// be correctly aligned, if the alignment requirement of `attribute.datatype()` is larger than the size of the
    /// attribute. All of the built-in attributes in pasture have alignments that are less than or equal to their size,
    /// so for built-in attributes you can assume that the attributes in `data` are correctly aligned.
    ///
    /// # Panics
    ///
    /// May panic if `attribute` is not part of the `PointLayout` of this buffer.
    /// May panic if `point_range` is out of bounds.
    /// May panic if `data.len()` does not equal `attribute.size() * point_range.len()`
    fn get_attribute_range(
        &self,
        attribute: &PointAttributeDefinition,
        point_range: Range<usize>,
        data: &mut [u8],
    ) {
        let attribute_member = self
            .point_layout()
            .get_attribute(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        let attribute_size = attribute_member.size() as usize;
        let first_point = point_range.start;
        for point_index in point_range {
            let zero_based_index = point_index - first_point;
            let data_slice = &mut data
                [(zero_based_index * attribute_size)..((zero_based_index + 1) * attribute_size)];
            // Safe because we checked the attribute and size of the buffer slice
            unsafe {
                self.get_attribute_unchecked(attribute_member, point_index, data_slice);
            }
        }
    }

    /// Like `get_attribute`, but performs no check whether the attribute actually is part of this buffers `PointLayout`
    /// or not. Because of this, this function accepts a `PointAttributeMember` instead of a `PointAttributeDefinition`,
    /// and this `PointAttributeMember` must come from the `PointLayout` of this buffer! The benefit over `get_attribute`
    /// is that this function skips the include checks and thus will be faster if you repeatedly want to get data for a
    /// single attribute
    ///
    /// # Safety
    ///
    /// Requires `attribute_member` to be a part of this buffer's `PointLayout`
    unsafe fn get_attribute_unchecked(
        &self,
        attribute_member: &PointAttributeMember,
        index: usize,
        data: &mut [u8],
    );

    /// Try to get a reference to `self` as an `InterleavedBuffer`. Returns `None` if `self` does not
    /// implement `InterleavedBuffer`
    fn as_interleaved(&self) -> Option<&dyn InterleavedBuffer<'a>> {
        None
    }

    /// Try to get a reference to `self` as a `ColumnarBuffer`. Returns `None` if `self` does not
    /// implement `ColumnarBuffer`
    fn as_columnar(&self) -> Option<&dyn ColumnarBuffer<'a>> {
        None
    }
}

/// Trait for a point buffer that mutably borrows its memory. Compared to [`BorrowedBuffer`], buffers that implement
/// this trait support the following additional capabilities:
/// - Manipulating point and attribute data in-place through `set_point` and `set_attribute`
/// - Shuffling data through `swap`
/// - Mutable views to points and attributes through `view_mut` and `view_attribute_mut`
pub trait BorrowedMutBuffer<'a>: BorrowedBuffer<'a> {
    /// Sets the data for the point at the given `index`
    ///
    /// # Safety
    ///
    /// Requires that `point_data` contains memory for a single point with the same `PointLayout` as `self.point_layout()`.
    /// This property is not enforced at runtime, so this function is very unsafe!
    ///
    /// # Panics
    ///
    /// May panic if `index` is out of bounds.<br>
    /// May panic if `point_data.len()` does not equal `self.point_layout().size_of_point_record()`
    unsafe fn set_point(&mut self, index: usize, point_data: &[u8]);
    /// Sets the data for the given range of points. This function will generally be more efficient than calling [`set_point`]
    /// multiple times, as [`set_point`] performs size and index checks on every call, whereas this function can perform them
    /// only once. Assumes that the `point_data` is tightly packed, i.e. stored in an interleaved format with point alignment
    /// of `1`.
    ///
    /// # Safety
    ///
    /// `point_data` must contain correctly initialized data for `point_range.len()` points in interleaved layout. The data
    /// must be tightly packed (i.e. no padding bytes between adjacent points).
    ///
    /// # Panics
    ///
    /// May panic if `point_range` is out of bounds.<br>
    /// May panic if `point_data.len()` does not equal `point_range.len() * self.point_layout().size_of_point_record()`
    unsafe fn set_point_range(&mut self, point_range: Range<usize>, point_data: &[u8]);
    /// Sets the data for the given `attribute` of the point at `index`
    ///
    /// # Safety
    ///
    /// Requires that `attribute_data` contains memory for a single value of the data type of `attribute`. This property
    /// is not enforced at runtime, so this function is very unsafe!
    ///
    /// # Panics
    ///
    /// May panic if `attribute` is not part of the `PointLayout` of this buffer.<br>
    /// May panic if `index` is out of bounds.<br>
    /// May panic if `attribute_data.len()` does not match the size of the data type of `attribute`
    unsafe fn set_attribute(
        &mut self,
        attribute: &PointAttributeDefinition,
        index: usize,
        attribute_data: &[u8],
    );
    /// Sets the data for the given `attribute` for all points in `point_range`. This function will generally be more efficient
    /// than calling [`set_attribute`] multiple times, as [`set_attribute`] has to perform type and bounds checks on each call.
    ///
    /// # Safety
    ///
    /// Requires that `attribute_data` contains data for `point_range.len()` attribute values. The data must be tightly packed,
    /// i.e. there must be no padding bytes between adjacent values.
    ///
    /// # Panics
    ///
    /// May panic if `attribute` is not part of the `PointLayout` of this buffer.<br>
    /// May panic if `point_range` is out of bounds.<br>
    /// May panic if `attribute_data.len()` does not equal `point_range.len() * attribute.size()`
    unsafe fn set_attribute_range(
        &mut self,
        attribute: &PointAttributeDefinition,
        point_range: Range<usize>,
        attribute_data: &[u8],
    );
    /// Swaps the two points at `from_index` and `to_index`. Implementations should allow the case where `from_index == to_index`
    ///
    /// # Panics
    ///
    /// May panic if any of `from_index` or `to_index` is out of bounds
    fn swap(&mut self, from_index: usize, to_index: usize);

    /// Try to get a mutable reference to `self` as an `InterleavedBufferMut`. Returns `None` if `self` does not
    /// implement `InterleavedBufferMut`
    fn as_interleaved_mut(&mut self) -> Option<&mut dyn InterleavedBufferMut<'a>> {
        None
    }

    /// Try to get a mutable reference to `self` as a `ColumnarBufferMut`. Returns `None` if `self` does not
    /// implement `ColumnarBufferMut`
    fn as_columnar_mut(&mut self) -> Option<&mut dyn ColumnarBufferMut<'a>> {
        None
    }
}

/// Trait for point buffers that own their memory. Compared to [`BorrowedMutBuffer`], buffers that implement
/// this trait support the following additional capabilities:
/// - Pushing point data into the buffer using `push_points`
/// - Appending other buffers to the end of this buffer using `append`, `append_interleaved`, and `append_columnar`
/// - Resizing and clearing the contents of the buffer using `resize` and `clear`
pub trait OwningBuffer<'a>: BorrowedMutBuffer<'a> {
    /// Push the raw memory for a range of points into this buffer. Works similar to `Vec::push`
    ///
    /// # Safety
    ///
    /// `point_bytes` must contain the raw memory for a whole number of points in the `PointLayout` of this buffer.
    /// This property is not checked at runtime, so this function is very unsafe!
    ///
    /// # Panics
    ///
    /// May panic if `point_bytes.len()` is not a multiple of `self.point_layout().size_of_point_record()`
    unsafe fn push_points(&mut self, point_bytes: &[u8]);
    /// Resize this buffer to contain exactly `count` points. If `count` is less than `self.len()`, point data
    /// is removed, if `count` is greater than `self.len()` new points are default-constructed (i.e. zero-initialized).
    fn resize(&mut self, count: usize);
    /// Clears the contents of this buffer, removing all point data and setting the length to `0`
    fn clear(&mut self);
}

/// Extension trait for `BorrowedBuffer` that allows obtaining strongly-typed views over points and
/// attributes.
///
/// # Notes
///
/// The `view...` methods on this type are implemented in an extension trait and not the base trait
/// `BorrowedBuffer` so that we retain the option to create trait objects for types implementing
/// `BorrowedBuffer`, while also allowing both static types `T: BorrowedBuffer` and dynamic trait object
/// types (`dyn BorrowedBuffer`) to be used for views. I.e. this makes the following code possible:
///
/// ```ignore
/// let layout = ...;
/// let buffer = VectorBuffer::new_from_layout(layout);
/// let view_from_sized_type = buffer.view::<Vector3<f64>>(&POSITION_3D);
///
/// // In previous pasture version, this code was not possible because views required sized types:
/// let buffer_trait_object: &dyn InterleavedBuffer = buffer.as_interleaved().unwrap();
/// let view_from_trait_object = buffer_trait_object.view::<Vector3<f64>>(&POSITION_3D);
/// ```
pub trait BorrowedBufferExt<'a>: BorrowedBuffer<'a> {
    /// Get a strongly typed view of the point data of this buffer
    ///
    /// # Panics
    ///
    /// Panics if `T::layout()` does not match the `PointLayout` of this buffer
    fn view<'b, T: PointType>(&'b self) -> PointView<'a, 'b, Self, T>
    where
        'a: 'b,
    {
        PointView::new(self)
    }

    /// Gets a strongly typed view of the `attribute` of all points in this buffer
    ///
    /// # Panics
    ///
    /// If `attribute` is not part of the `PointLayout` of this buffer.
    /// If `T::data_type()` does not match the data type of the attribute within the buffer
    fn view_attribute<'b, T: PrimitiveType>(
        &'b self,
        attribute: &PointAttributeDefinition,
    ) -> AttributeView<'a, 'b, Self, T>
    where
        'a: 'b,
    {
        AttributeView::new(self, attribute)
    }

    /// Like `view_attribute`, but allows `T::data_type()` to be different from the data type of  
    /// the `attribute` within this buffer.
    ///
    /// # Panics
    ///
    /// If `T::data_type()` does not match the data type of `attribute`
    fn view_attribute_with_conversion<'b, T: PrimitiveType>(
        &'b self,
        attribute: &PointAttributeDefinition,
    ) -> Result<AttributeViewConverting<'a, 'b, Self, T>>
    where
        'a: 'b,
    {
        AttributeViewConverting::new(self, attribute)
    }
}

impl<'a, T: BorrowedBuffer<'a>> BorrowedBufferExt<'a> for T {}
impl<'a> BorrowedBufferExt<'a> for dyn BorrowedBuffer<'a> + 'a {}
impl<'a> BorrowedBufferExt<'a> for dyn BorrowedMutBuffer<'a> + 'a {}
// TODO Make OwningBuffer object safe, e.g. by moving the append functions to another extension trait
// Open question how to deal with append_interleaved / append_columnar
// impl<'a> BorrowedBufferExt<'a> for dyn OwningBuffer<'a> + 'a {}
impl<'a> BorrowedBufferExt<'a> for dyn InterleavedBuffer<'a> + 'a {}
impl<'a> BorrowedBufferExt<'a> for dyn InterleavedBufferMut<'a> + 'a {}
impl<'a> BorrowedBufferExt<'a> for dyn ColumnarBuffer<'a> + 'a {}
impl<'a> BorrowedBufferExt<'a> for dyn ColumnarBufferMut<'a> + 'a {}

/// Extension trait for `BorrowedMutBuffer` that allows obtaining strongly-typed views over points and
/// attributes.
pub trait BorrowedMutBufferExt<'a>: BorrowedMutBuffer<'a> {
    /// Get a strongly typed view of the point data of this buffer. This view allows mutating the point data!
    ///
    /// # Panics
    ///
    /// If `T::point_layout()` does not match `self.point_layout()`
    fn view_mut<'b, T: PointType>(&'b mut self) -> PointViewMut<'a, 'b, Self, T>
    where
        'a: 'b,
    {
        PointViewMut::new(self)
    }

    /// Get a strongly typed view of the `attribute` of all points in this buffer. This view allows mutating
    /// the attribute data!
    ///
    /// # Panics
    ///
    /// If `attribute` is not part of the `PointLayout` of this buffer.<br>
    /// If `T::data_type()` does not match `attribute.datatype()`
    fn view_attribute_mut<'b, T: PrimitiveType>(
        &'b mut self,
        attribute: &PointAttributeDefinition,
    ) -> AttributeViewMut<'a, 'b, Self, T>
    where
        'a: 'b,
    {
        AttributeViewMut::new(self, attribute)
    }

    /// Apply a transformation function to the given `attribute` of all points within this buffer. This function is
    /// helpful if you want to modify a single attribute of a buffer in-place and works for buffers of all memory
    /// layouts. For columnar buffers, prefer using `get_attribute_range_mut` to modify attribute data in-place.
    ///
    /// This function does not support attribute type conversion, so the type `T` must match the `PointAttributeDataType`
    /// of `attribute`!
    ///
    /// The conversion function takes the current value of the attribute as a strongly typed `T` and returns the new value
    /// for the attribute. It also takes the index of the point within the buffer, so that `func` can access additional
    /// data.
    ///
    /// # Panics
    ///
    /// If `attribute` is not part of the `PointLayout` of this buffer.<br>
    /// If `T::data_type()` does not equal `attribute.datatype()`
    fn transform_attribute<'b, T: PrimitiveType, F: Fn(usize, T) -> T>(
        &'b mut self,
        attribute: &PointAttributeDefinition,
        func: F,
    ) where
        'a: 'b,
    {
        let num_points = self.len();
        let mut attribute_view = self.view_attribute_mut(attribute);
        for point_index in 0..num_points {
            let attribute_value = attribute_view.at(point_index);
            attribute_view.set_at(point_index, func(point_index, attribute_value));
        }
    }
}

impl<'a, T: BorrowedMutBuffer<'a>> BorrowedMutBufferExt<'a> for T {}
impl<'a> BorrowedMutBufferExt<'a> for dyn BorrowedMutBuffer<'a> + 'a {}
// TODO impl for owning buffer
impl<'a> BorrowedMutBufferExt<'a> for dyn InterleavedBufferMut<'a> + 'a {}
impl<'a> BorrowedMutBufferExt<'a> for dyn ColumnarBufferMut<'a> + 'a {}

pub trait OwningBufferExt<'a>: OwningBuffer<'a> {
    /// Appends data from the given buffer to the end of this buffer
    ///
    /// # Panics
    ///
    /// If `self.point_layout()` does not equal `other.point_layout()`
    fn append<'b, B: BorrowedBuffer<'b> + ?Sized>(&mut self, other: &'_ B) {
        assert_eq!(self.point_layout(), other.point_layout());

        // There are a bunch of ways we can append data, depending on the memory layout. In general, if
        // the memory layout of this buffer and other match, we can get by with only a few copy operations
        // (one for interleaved layout, one per attribute for columnar layout)
        // If we know the specific layout of one buffer but not the other, we can get by without any allocations
        // If both buffers are neither interleaved nor columnar, we have to resort to the most general, but
        // slowest methods

        if let (Some(_), Some(other_interleaved)) =
            (self.as_interleaved_mut(), other.as_interleaved())
        {
            // The happy case where append is equal to Vec::append
            // Safe because both point layouts are equal
            unsafe {
                self.push_points(other_interleaved.get_point_range_ref(0..other.len()));
            }
            return;
        }

        let old_self_len = self.len();
        let new_self_len = old_self_len + other.len();
        self.resize(new_self_len);

        if let Some(self_interleaved) = self.as_interleaved_mut() {
            let point_size = self_interleaved.point_layout().size_of_point_entry() as usize;
            let new_points = self_interleaved.get_point_range_mut(old_self_len..new_self_len);
            for (index, new_point) in new_points.chunks_exact_mut(point_size).enumerate() {
                other.get_point(index, new_point);
            }
        } else if let Some(self_columnar) = self.as_columnar_mut() {
            if let Some(other_columnar) = other.as_columnar() {
                for attribute in other.point_layout().attributes() {
                    // Safe because point layouts are equal
                    unsafe {
                        self_columnar.set_attribute_range(
                            attribute.attribute_definition(),
                            old_self_len..new_self_len,
                            other_columnar.get_attribute_range_ref(
                                attribute.attribute_definition(),
                                0..other_columnar.len(),
                            ),
                        );
                    }
                }
            } else {
                for attribute in other.point_layout().attributes() {
                    let new_attributes = self_columnar.get_attribute_range_mut(
                        attribute.attribute_definition(),
                        old_self_len..new_self_len,
                    );
                    for (index, new_attribute) in new_attributes
                        .chunks_exact_mut(attribute.size() as usize)
                        .enumerate()
                    {
                        other.get_attribute(attribute.attribute_definition(), index, new_attribute);
                    }
                }
            }
        } else {
            let mut point_buffer = vec![0; self.point_layout().size_of_point_entry() as usize];
            for point_index in 0..other.len() {
                other.get_point(point_index, &mut point_buffer);
                // Is safe because we assert that the point layouts of self and other match
                unsafe {
                    self.set_point(old_self_len + point_index, &point_buffer);
                }
            }
        }
    }
}

impl<'a, T: OwningBuffer<'a>> OwningBufferExt<'a> for T {}
impl<'a> OwningBufferExt<'a> for dyn OwningBuffer<'a> + 'a {}

/// Trait for all buffers that can be default-constructed from a given `PointLayout`. This trait is helpful for generic
/// code that needs to construct an generic buffer type
pub trait MakeBufferFromLayout<'a>: BorrowedBuffer<'a> + Sized {
    /// Creates a new empty buffer from the given `PointLayout`
    fn new_from_layout(point_layout: PointLayout) -> Self;
}

/// Trait for point buffers that store their point data in interleaved memory layout. This allows accessing
/// point data by reference
pub trait InterleavedBuffer<'a>: BorrowedBuffer<'a> {
    /// Get an immutable slice of the point memory of the point at `index`
    ///
    /// # Lifetimes
    ///
    /// Has a more relaxed lifetime bound than the underlying buffer, since we should be able to borrow point
    /// data for a lifetime `'b` that is potentially shorter than the lifetime `'a` of the `BorrowedBuffer`
    ///
    /// # Panics
    ///
    /// Should panic if `index` is out of bounds
    fn get_point_ref<'b>(&'b self, index: usize) -> &'b [u8]
    where
        'a: 'b;
    /// Get an immutable slice of the memory for the given `range` of points. This is the range-version of [`get_point_ref`],
    /// see its documentation for more details
    ///
    /// # Panics
    ///
    /// If `range` is out of bounds
    fn get_point_range_ref<'b>(&'b self, range: Range<usize>) -> &'b [u8]
    where
        'a: 'b;

    /// Get a raw view over the given `attribute` from this point buffer. Unlike the typed view that `view_attribute`
    /// returns, this view dereferences to byte slices, but it is potentially more efficient to use than calling
    /// `get_attribute` repeatedly
    fn view_raw_attribute<'b>(&'b self, attribute: &PointAttributeMember) -> RawAttributeView<'b>
    where
        'a: 'b,
    {
        RawAttributeView::from_interleaved_buffer(self, attribute)
    }
}

/// Trait for buffers that store point data in interleaved memory layout and also borrow their memory mutably. Compared
/// to [`InterleavedBuffer`], this allows accessing point data by mutable reference!
pub trait InterleavedBufferMut<'a>: InterleavedBuffer<'a> + BorrowedMutBuffer<'a> {
    /// Get a mutable slice of the point memory of the point at `index`. This is the mutable version of [`InterleavedBuffer::get_point_ref`]
    ///
    /// # Panics
    ///
    /// Should panic if `index` is out of bounds
    fn get_point_mut<'b>(&'b mut self, index: usize) -> &'b mut [u8]
    where
        'a: 'b;
    /// Get a mutable slice of the memory for the given `range` of points. This is the mutable version of [`InterleavedBuffer::get_point_range_ref`]
    ///
    /// # Panics
    ///
    /// Should panic if `index` is out of bounds
    fn get_point_range_mut<'b>(&'b mut self, range: Range<usize>) -> &'b mut [u8]
    where
        'a: 'b;

    /// Like `view_raw_attribute`, but returns mutable byte slices of the attribute data
    fn view_raw_attribute_mut<'b>(
        &'b mut self,
        attribute: &PointAttributeMember,
    ) -> RawAttributeViewMut<'b>
    where
        'a: 'b,
    {
        RawAttributeViewMut::from_interleaved_buffer(self, attribute)
    }
}

/// Trait for point buffers that store their point data in columnar memory layout. This allows accessing point attributes
/// by reference
pub trait ColumnarBuffer<'a>: BorrowedBuffer<'a> {
    /// Get an immutable slice to the memory of the given `attribute` for the point at `index`. See [`InterleavedBuffer::get_point_ref`] for an explanation of the lifetime bounds.
    ///
    /// # Panics
    ///
    /// Should panic if `attribute` is not part of the `PointLayout` of this buffer.<br>
    /// Should panic if `index` is out of bounds.
    fn get_attribute_ref<'b>(
        &'b self,
        attribute: &PointAttributeDefinition,
        index: usize,
    ) -> &'b [u8]
    where
        'a: 'b;
    /// Get an immutable slice to the memory for the `attribute` of the given `range` of points
    ///
    /// # Panics
    ///
    /// Should panic if `attribute` is not part of the `PointLayout` of this buffer.<br>
    /// Should panic if `range` is out of bounds.
    fn get_attribute_range_ref<'b>(
        &'b self,
        attribute: &PointAttributeDefinition,
        range: Range<usize>,
    ) -> &'b [u8]
    where
        'a: 'b;

    /// Get a raw view over the given `attribute` from this point buffer. Unlike the typed view that `view_attribute`
    /// returns, this view dereferences to byte slices, but it is potentially more efficient to use than calling
    /// `get_attribute` repeatedly
    fn view_raw_attribute<'b>(&'b self, attribute: &PointAttributeMember) -> RawAttributeView<'b>
    where
        'a: 'b,
    {
        RawAttributeView::from_columnar_buffer(self, attribute.attribute_definition())
    }
}

/// Trait for buffers that store point data in columnar memory layout and also borrow their memory mutably. Compared
/// to [`ColumnarBuffer`], this allows accessing point attributes by mutable reference!
pub trait ColumnarBufferMut<'a>: ColumnarBuffer<'a> + BorrowedMutBuffer<'a> {
    /// Get a mutable slice to the memory of the given `attribute` for the point at `index`. This is the mutable
    /// version of [`ColumnarBuffer::get_attribute_ref`]
    ///
    /// # Panics
    ///
    /// Should panic if `attribute` is not part of the `PointLayout` of this buffer.<br>
    /// Should panic if `index` is out of bounds.
    fn get_attribute_mut<'b>(
        &'b mut self,
        attribute: &PointAttributeDefinition,
        index: usize,
    ) -> &'b mut [u8]
    where
        'a: 'b;
    /// Get a mutable slice to the memory for the `attribute` of the given `range` of points. This is the mutable
    /// version of [`ColumnarBuffer::get_attribute_range_ref`]
    ///
    /// # Panics
    ///
    /// Should panic if `attribute` is not part of the `PointLayout` of this buffer.<br>
    /// Should panic if `range` is out of bounds.
    fn get_attribute_range_mut<'b>(
        &'b mut self,
        attribute: &PointAttributeDefinition,
        range: Range<usize>,
    ) -> &'b mut [u8]
    where
        'a: 'b;

    /// Like `view_raw_attribute`, but returns mutable byte slices of the attribute data
    fn view_raw_attribute_mut<'b>(
        &'b mut self,
        attribute: &PointAttributeMember,
    ) -> RawAttributeViewMut<'b>
    where
        'a: 'b,
    {
        RawAttributeViewMut::from_columnar_buffer(self, attribute.attribute_definition())
    }
}

/// A point buffer that uses a `Vec<u8>` as its underlying storage. It stores point data in interleaved memory
/// layout and generally behaves like an untyped vector.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VectorBuffer {
    storage: Vec<u8>,
    point_layout: PointLayout,
    length: usize,
}

impl VectorBuffer {
    /// Creates a new `VectorBuffer` with the given `capacity` and `point_layout`. This preallocates enough memory
    /// to store at least `capacity` points
    pub fn with_capacity(capacity: usize, point_layout: PointLayout) -> Self {
        let required_bytes = capacity * point_layout.size_of_point_entry() as usize;
        Self {
            point_layout,
            storage: Vec::with_capacity(required_bytes),
            length: 0,
        }
    }

    fn get_byte_range_of_point(&self, point_index: usize) -> Range<usize> {
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        (point_index * size_of_point)..((point_index + 1) * size_of_point)
    }

    fn get_byte_range_of_points(&self, points_range: Range<usize>) -> Range<usize> {
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        (points_range.start * size_of_point)..(points_range.end * size_of_point)
    }

    fn get_byte_range_of_attribute(
        &self,
        point_index: usize,
        attribute: &PointAttributeMember,
    ) -> Range<usize> {
        let start_byte = (point_index * self.point_layout.size_of_point_entry() as usize)
            + attribute.offset() as usize;
        let end_byte = start_byte + attribute.size() as usize;
        start_byte..end_byte
    }
}

impl<'a> MakeBufferFromLayout<'a> for VectorBuffer {
    fn new_from_layout(point_layout: PointLayout) -> Self {
        Self {
            point_layout,
            storage: Default::default(),
            length: 0,
        }
    }
}

impl<'a> BorrowedBuffer<'a> for VectorBuffer
where
    VectorBuffer: 'a,
{
    fn len(&self) -> usize {
        self.length
    }

    fn point_layout(&self) -> &PointLayout {
        &self.point_layout
    }

    fn get_point(&self, index: usize, data: &mut [u8]) {
        let point_ref = self.get_point_ref(index);
        data.copy_from_slice(point_ref);
    }

    fn get_point_range(&self, range: Range<usize>, data: &mut [u8]) {
        let points_ref = self.get_point_range_ref(range);
        data.copy_from_slice(points_ref);
    }

    unsafe fn get_attribute_unchecked(
        &self,
        attribute_member: &PointAttributeMember,
        index: usize,
        data: &mut [u8],
    ) {
        let byte_range = self.get_byte_range_of_attribute(index, attribute_member);
        data.copy_from_slice(&self.storage[byte_range]);
    }

    fn as_interleaved(&self) -> Option<&dyn InterleavedBuffer<'a>> {
        Some(self)
    }
}

impl<'a> BorrowedMutBuffer<'a> for VectorBuffer
where
    VectorBuffer: 'a,
{
    unsafe fn set_point(&mut self, index: usize, point_data: &[u8]) {
        let point_bytes = self.get_point_mut(index);
        point_bytes.copy_from_slice(point_data);
    }

    unsafe fn set_attribute(
        &mut self,
        attribute: &PointAttributeDefinition,
        index: usize,
        attribute_data: &[u8],
    ) {
        let attribute_member = self
            .point_layout
            .get_attribute(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        let attribute_byte_range = self.get_byte_range_of_attribute(index, attribute_member);
        let attribute_bytes = &mut self.storage[attribute_byte_range];
        attribute_bytes.copy_from_slice(attribute_data);
    }

    fn swap(&mut self, from_index: usize, to_index: usize) {
        assert!(from_index < self.len());
        assert!(to_index < self.len());
        if from_index == to_index {
            return;
        }
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        // Is safe as long as 'from_index' and 'to_index' are not out of bounds, which is asserted
        unsafe {
            let from_ptr = self.storage.as_mut_ptr().add(from_index * size_of_point);
            let to_ptr = self.storage.as_mut_ptr().add(to_index * size_of_point);
            std::ptr::swap_nonoverlapping(from_ptr, to_ptr, size_of_point);
        }
    }

    unsafe fn set_point_range(&mut self, point_range: Range<usize>, point_data: &[u8]) {
        let point_bytes = self.get_point_range_mut(point_range);
        point_bytes.copy_from_slice(point_data);
    }

    unsafe fn set_attribute_range(
        &mut self,
        attribute: &PointAttributeDefinition,
        point_range: Range<usize>,
        attribute_data: &[u8],
    ) {
        let attribute_member = self
            .point_layout
            .get_attribute(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        let attribute_size = attribute_member.size() as usize;
        let first_point = point_range.start;
        for point_index in point_range {
            let zero_based_index = point_index - first_point;
            let src_slice = &attribute_data
                [(zero_based_index * attribute_size)..((zero_based_index + 1) * attribute_size)];
            let attribute_byte_range =
                self.get_byte_range_of_attribute(point_index, attribute_member);
            let attribute_bytes = &mut self.storage[attribute_byte_range];
            attribute_bytes.copy_from_slice(src_slice);
        }
    }

    fn as_interleaved_mut(&mut self) -> Option<&mut dyn InterleavedBufferMut<'a>> {
        Some(self)
    }
}

impl<'a> OwningBuffer<'a> for VectorBuffer
where
    VectorBuffer: 'a,
{
    unsafe fn push_points(&mut self, point_bytes: &[u8]) {
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        if size_of_point == 0 {
            assert_eq!(0, point_bytes.len());
        } else {
            assert_eq!(point_bytes.len() % size_of_point, 0);
            self.storage.extend_from_slice(point_bytes);
            self.length += point_bytes.len() / size_of_point;
        }
    }

    fn resize(&mut self, count: usize) {
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        self.storage.resize(count * size_of_point, 0);
        self.length = count;
    }

    fn clear(&mut self) {
        self.storage.clear();
        self.length = 0;
    }
}

impl<'a> InterleavedBuffer<'a> for VectorBuffer
where
    VectorBuffer: 'a,
{
    fn get_point_ref<'b>(&'b self, index: usize) -> &'b [u8]
    where
        'a: 'b,
    {
        &self.storage[self.get_byte_range_of_point(index)]
    }

    fn get_point_range_ref<'b>(&'b self, range: Range<usize>) -> &'b [u8]
    where
        'a: 'b,
    {
        &self.storage[self.get_byte_range_of_points(range)]
    }
}

impl<'a> InterleavedBufferMut<'a> for VectorBuffer
where
    VectorBuffer: 'a,
{
    fn get_point_mut<'b>(&'b mut self, index: usize) -> &'b mut [u8]
    where
        'a: 'b,
    {
        let byte_range = self.get_byte_range_of_point(index);
        &mut self.storage[byte_range]
    }

    fn get_point_range_mut<'b>(&'b mut self, range: Range<usize>) -> &'b mut [u8]
    where
        'a: 'b,
    {
        let byte_range = self.get_byte_range_of_points(range);
        &mut self.storage[byte_range]
    }
}

impl<'a> SliceBuffer<'a> for VectorBuffer
where
    Self: 'a,
{
    type SliceType = BufferSliceInterleaved<'a, Self>;

    fn slice(&'a self, range: Range<usize>) -> Self::SliceType {
        BufferSliceInterleaved::new(self, range)
    }
}

impl<'a> SliceBufferMut<'a> for VectorBuffer
where
    Self: 'a,
{
    type SliceTypeMut = BufferSliceInterleavedMut<'a, Self>;

    fn slice_mut(&'a mut self, range: Range<usize>) -> Self::SliceTypeMut {
        BufferSliceInterleavedMut::new(self, range)
    }
}

impl<T: PointType> FromIterator<T> for VectorBuffer {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let point_layout = T::layout();
        let iter = iter.into_iter();
        let (_, maybe_known_length) = iter.size_hint();
        if let Some(known_length) = maybe_known_length {
            let num_bytes = known_length * point_layout.size_of_point_entry() as usize;
            let storage = vec![0; num_bytes];
            let mut buffer = Self {
                point_layout,
                storage,
                length: known_length,
            };
            // Overwrite the preallocated memory of the buffer with the points in the iterator:
            iter.enumerate().for_each(|(index, point)| {
                let point_bytes = bytemuck::bytes_of(&point);
                // Safe because we created `buffer` from `T::layout()`, so we know the layouts match
                unsafe {
                    buffer.set_point(index, point_bytes);
                }
            });
            buffer
        } else {
            let mut buffer = Self {
                point_layout,
                storage: Default::default(),
                length: 0,
            };
            iter.for_each(|point| {
                let point_bytes = bytemuck::bytes_of(&point);
                // Safe because we know that `buffer` has the same layout as `T`
                unsafe {
                    buffer.push_points(point_bytes);
                }
            });
            buffer
        }
    }
}

/// Helper struct to push point data into a `HashMapBuffer` attribute by attribute. This allows constructing a point buffer
/// from multiple ranges of attribute data, since regular point buffers do not allow pushing just a single attribute into
/// the buffer, as buffers always have to store complete points (even with columnar memory layout)
pub struct HashMapBufferAttributePusher<'a> {
    attributes_storage: HashMap<PointAttributeDefinition, Vec<u8>>,
    num_new_points: Option<usize>,
    buffer: &'a mut HashMapBuffer,
}

impl<'a> HashMapBufferAttributePusher<'a> {
    pub(crate) fn new(buffer: &'a mut HashMapBuffer) -> Self {
        let attributes_storage = buffer
            .point_layout()
            .attributes()
            .map(|attribute| (attribute.attribute_definition().clone(), vec![]))
            .collect();
        Self {
            attributes_storage,
            num_new_points: None,
            buffer,
        }
    }

    /// Push a range of values for the given `attribute` into the underlying buffer. The first range of values that
    /// is pushed in this way determines the expected number of points that will be added to the buffer. Consecutive
    /// calls to `push_attribute_range` will assert that `data.len()` matches the expected count.
    ///
    /// # Panics
    ///
    /// If `attribute` is not part of the `PointLayout` of the underlying buffer.<br>
    /// If `T::data_type()` does not match `attribute.datatype()`.<br>
    /// If this is not the first call to `push_attribute_range`, and `data.len()` does not match the length of the
    /// data that was passed to the first invocation of `push_attribute_range`
    pub fn push_attribute_range<T: PrimitiveType>(
        &mut self,
        attribute: &PointAttributeDefinition,
        data: &[T],
    ) {
        assert_eq!(T::data_type(), attribute.datatype());
        let storage = self
            .attributes_storage
            .get_mut(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        if let Some(point_count) = self.num_new_points {
            assert_eq!(point_count, data.len());
        } else {
            self.num_new_points = Some(data.len());
        }
        storage.extend_from_slice(bytemuck::cast_slice(data));
    }

    /// Commit all pushed data into the underlying buffer. This function checks that there is the correct amount
    /// of data for all expected attributes in the `PointLayout` of the underlying buffer and will panic otherwise
    ///
    /// # Panics
    ///
    /// If there is missing data for at least one of the attributes in the `PointLayout` of the underlying buffer,
    /// i.e. if `push_attribute_range` was not called for at least one of these attributes.
    pub fn done(self) {
        let num_new_points = self.num_new_points.unwrap_or(0);
        if num_new_points == 0 {
            return;
        }

        // Check that all attributes are complete! We don't have to check the exact size of the vectors,
        // as this is checked in `push_attribute_range`, it is sufficient to verify that no vector is empty
        assert!(self
            .attributes_storage
            .values()
            .all(|vector| !vector.is_empty()));

        for (attribute, mut data) in self.attributes_storage {
            // Can safely unwrap, because self.attributes_storage was initialized from the `PointLayout` of the buffer!
            let buffer_storage = self.buffer.attributes_storage.get_mut(&attribute).unwrap();
            buffer_storage.append(&mut data);
        }

        self.buffer.length += num_new_points;
    }
}

/// A point buffer that stores point data in columnar memory layout, using a `HashMap<PointAttributeDefinition, Vec<u8>>` as
/// its underlying storage
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HashMapBuffer {
    attributes_storage: HashMap<PointAttributeDefinition, Vec<u8>>,
    point_layout: PointLayout,
    length: usize,
}

impl HashMapBuffer {
    /// Creates a new `HashMapBuffer` with the given `capacity` and `point_layout`. It preallocates enough memory to store
    /// at least `capacity` points
    pub fn with_capacity(capacity: usize, point_layout: PointLayout) -> Self {
        let attributes_storage = point_layout
            .attributes()
            .map(|attribute| {
                let bytes_for_attribute = capacity * attribute.size() as usize;
                (
                    attribute.attribute_definition().clone(),
                    Vec::with_capacity(bytes_for_attribute),
                )
            })
            .collect();
        Self {
            attributes_storage,
            point_layout,
            length: 0,
        }
    }

    /// Create a new helper object through which ranges of attribute data can be pushed into this buffer
    pub fn begin_push_attributes(&mut self) -> HashMapBufferAttributePusher<'_> {
        HashMapBufferAttributePusher::new(self)
    }

    /// Like `Iterator::filter`, but filters into a point buffer of type `B`
    pub fn filter<
        B: for<'a> OwningBuffer<'a> + for<'a> MakeBufferFromLayout<'a>,
        F: Fn(usize) -> bool,
    >(
        &self,
        predicate: F,
    ) -> B {
        let num_matches = (0..self.len()).filter(|idx| predicate(*idx)).count();
        let mut filtered_points = B::new_from_layout(self.point_layout.clone());
        filtered_points.resize(num_matches);
        self.filter_into(&mut filtered_points, predicate, Some(num_matches));
        filtered_points
    }

    /// Like `filter`, but writes the filtered points into the given `buffer`
    ///
    /// # panics
    ///
    /// If `buffer.len()` is less than the number of matching points according to `predicate`
    /// If the `PointLayout` of `buffer` does not match the `PointLayout` of `self`
    pub fn filter_into<B: for<'a> BorrowedMutBuffer<'a>, F: Fn(usize) -> bool>(
        &self,
        buffer: &mut B,
        predicate: F,
        num_matches_hint: Option<usize>,
    ) {
        if buffer.point_layout() != self.point_layout() {
            panic!("PointLayouts must match");
        }
        let num_matches = num_matches_hint
            .unwrap_or_else(|| (0..self.len()).filter(|idx| predicate(*idx)).count());
        if buffer.len() < num_matches {
            panic!("buffer.len() must be at least as large as the number of predicate matches");
        }
        if let Some(columnar_buffer) = buffer.as_columnar_mut() {
            for attribute in self.point_layout.attributes() {
                let src_attribute_data =
                    self.get_attribute_range_ref(attribute.attribute_definition(), 0..self.len());
                let dst_attribute_data = columnar_buffer
                    .get_attribute_range_mut(attribute.attribute_definition(), 0..num_matches);
                let stride = attribute.size() as usize;
                for (dst_index, src_index) in
                    (0..self.len()).filter(|idx| predicate(*idx)).enumerate()
                {
                    dst_attribute_data[(dst_index * stride)..((dst_index + 1) * stride)]
                        .copy_from_slice(
                            &src_attribute_data[(src_index * stride)..((src_index + 1) * stride)],
                        );
                }
            }
        } else if let Some(interleaved_buffer) = buffer.as_interleaved_mut() {
            let dst_data = interleaved_buffer.get_point_range_mut(0..num_matches);
            for attribute in self.point_layout.attributes() {
                let src_attribute_data =
                    self.get_attribute_range_ref(attribute.attribute_definition(), 0..self.len());
                let src_stride = attribute.size() as usize;
                let dst_offset = attribute.offset() as usize;
                let dst_stride = self.point_layout.size_of_point_entry() as usize;
                for (dst_index, src_index) in
                    (0..self.len()).filter(|idx| predicate(*idx)).enumerate()
                {
                    let dst_attribute_start = dst_offset + (dst_index * dst_stride);
                    let dst_point_range = dst_attribute_start..(dst_attribute_start + src_stride);
                    dst_data[dst_point_range].copy_from_slice(
                        &src_attribute_data
                            [(src_index * src_stride)..((src_index + 1) * src_stride)],
                    );
                }
            }
        } else {
            unimplemented!()
        }
    }

    fn get_byte_range_for_attribute(
        point_index: usize,
        attribute: &PointAttributeDefinition,
    ) -> Range<usize> {
        let attribute_size = attribute.size() as usize;
        (point_index * attribute_size)..((point_index + 1) * attribute_size)
    }

    fn get_byte_range_for_attributes(
        points_range: Range<usize>,
        attribute: &PointAttributeDefinition,
    ) -> Range<usize> {
        let attribute_size = attribute.size() as usize;
        (points_range.start * attribute_size)..(points_range.end * attribute_size)
    }
}

impl<'a> MakeBufferFromLayout<'a> for HashMapBuffer {
    fn new_from_layout(point_layout: PointLayout) -> Self {
        let attributes_storage = point_layout
            .attributes()
            .map(|attribute| (attribute.attribute_definition().clone(), Vec::default()))
            .collect();
        Self {
            attributes_storage,
            point_layout,
            length: 0,
        }
    }
}

impl<'a> BorrowedBuffer<'a> for HashMapBuffer
where
    HashMapBuffer: 'a,
{
    fn len(&self) -> usize {
        self.length
    }

    fn point_layout(&self) -> &PointLayout {
        &self.point_layout
    }

    fn get_point(&self, index: usize, data: &mut [u8]) {
        for attribute in self.point_layout.attributes() {
            let attribute_storage = self
                .attributes_storage
                .get(attribute.attribute_definition())
                .expect("Attribute not found within storage of this PointBuffer");
            let src_slice = &attribute_storage
                [Self::get_byte_range_for_attribute(index, attribute.attribute_definition())];
            let dst_slice = &mut data[attribute.byte_range_within_point()];
            dst_slice.copy_from_slice(src_slice);
        }
    }

    fn get_point_range(&self, range: Range<usize>, data: &mut [u8]) {
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        for attribute in self.point_layout.attributes() {
            let attribute_storage = self
                .attributes_storage
                .get(attribute.attribute_definition())
                .expect("Attribute not found within storage of this PointBuffer");
            for point_index in range.clone() {
                let src_slice = &attribute_storage[Self::get_byte_range_for_attribute(
                    point_index,
                    attribute.attribute_definition(),
                )];
                let dst_point_slice = &mut data[((point_index - range.start) * size_of_point)..];
                let dst_slice = &mut dst_point_slice[attribute.byte_range_within_point()];
                dst_slice.copy_from_slice(src_slice);
            }
        }
    }

    fn get_attribute(&self, attribute: &PointAttributeDefinition, index: usize, data: &mut [u8]) {
        let memory = self
            .attributes_storage
            .get(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        let attribute_byte_range = Self::get_byte_range_for_attribute(index, attribute);
        data.copy_from_slice(&memory[attribute_byte_range]);
    }

    unsafe fn get_attribute_unchecked(
        &self,
        attribute_member: &PointAttributeMember,
        index: usize,
        data: &mut [u8],
    ) {
        let memory = self
            .attributes_storage
            .get(attribute_member.attribute_definition())
            .expect("Attribute not found in PointLayout of this buffer");
        let attribute_byte_range =
            Self::get_byte_range_for_attribute(index, attribute_member.attribute_definition());
        data.copy_from_slice(&memory[attribute_byte_range]);
    }

    fn as_columnar(&self) -> Option<&dyn ColumnarBuffer<'a>> {
        Some(self)
    }
}

impl<'a> BorrowedMutBuffer<'a> for HashMapBuffer
where
    HashMapBuffer: 'a,
{
    unsafe fn set_point(&mut self, index: usize, point_data: &[u8]) {
        for attribute in self.point_layout.attributes() {
            let attribute_definition = attribute.attribute_definition();
            let attribute_byte_range =
                Self::get_byte_range_for_attribute(index, attribute_definition);
            let attribute_storage = self
                .attributes_storage
                .get_mut(attribute_definition)
                .expect("Attribute not found within storage of this PointBuffer");
            let dst_slice = &mut attribute_storage[attribute_byte_range];
            let src_slice = &point_data[attribute.byte_range_within_point()];
            dst_slice.copy_from_slice(src_slice);
        }
    }

    unsafe fn set_attribute(
        &mut self,
        attribute: &PointAttributeDefinition,
        index: usize,
        attribute_data: &[u8],
    ) {
        let attribute_byte_range = Self::get_byte_range_for_attribute(index, attribute);
        let attribute_storage = self
            .attributes_storage
            .get_mut(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        let attribute_bytes = &mut attribute_storage[attribute_byte_range];
        attribute_bytes.copy_from_slice(attribute_data);
    }

    fn swap(&mut self, from_index: usize, to_index: usize) {
        assert!(from_index < self.len());
        assert!(to_index < self.len());
        if from_index == to_index {
            return;
        }
        for (attribute, storage) in self.attributes_storage.iter_mut() {
            let src_byte_range = Self::get_byte_range_for_attribute(from_index, attribute);
            let dst_byte_range = Self::get_byte_range_for_attribute(to_index, attribute);
            // Is safe as long as 'from_index' and 'to_index' are not out of bounds, which is asserted
            unsafe {
                let src_ptr = storage.as_mut_ptr().add(src_byte_range.start);
                let dst_ptr = storage.as_mut_ptr().add(dst_byte_range.start);
                std::ptr::swap_nonoverlapping(src_ptr, dst_ptr, attribute.size() as usize);
            }
        }
    }

    unsafe fn set_point_range(&mut self, point_range: Range<usize>, point_data: &[u8]) {
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        let first_point = point_range.start;
        for attribute in self.point_layout.attributes() {
            let attribute_definition = attribute.attribute_definition();
            let attribute_storage = self
                .attributes_storage
                .get_mut(attribute_definition)
                .expect("Attribute not found within storage of this PointBuffer");
            for point_index in point_range.clone() {
                let zero_based_index = point_index - first_point;
                let attribute_byte_range =
                    Self::get_byte_range_for_attribute(point_index, attribute_definition);

                let dst_slice = &mut attribute_storage[attribute_byte_range];
                let src_point_slice = &point_data
                    [(zero_based_index * size_of_point)..((zero_based_index + 1) * size_of_point)];
                let src_slice = &src_point_slice[attribute.byte_range_within_point()];
                dst_slice.copy_from_slice(src_slice);
            }
        }
    }

    unsafe fn set_attribute_range(
        &mut self,
        attribute: &PointAttributeDefinition,
        point_range: Range<usize>,
        attribute_data: &[u8],
    ) {
        let attribute_range = self.get_attribute_range_mut(attribute, point_range);
        attribute_range.copy_from_slice(attribute_data);
    }

    fn as_columnar_mut(&mut self) -> Option<&mut dyn ColumnarBufferMut<'a>> {
        Some(self)
    }
}

impl<'a> OwningBuffer<'a> for HashMapBuffer
where
    HashMapBuffer: 'a,
{
    unsafe fn push_points(&mut self, point_bytes: &[u8]) {
        let point_size = self.point_layout.size_of_point_entry() as usize;
        assert_eq!(point_bytes.len() % point_size, 0);
        let num_points_added = point_bytes.len() / point_size;
        for attribute in self.point_layout.attributes() {
            let storage = self
                .attributes_storage
                .get_mut(attribute.attribute_definition())
                .expect("Attribute not found in storage of this buffer");
            for index in 0..num_points_added {
                let point_bytes = &point_bytes[(index * point_size)..((index + 1) * point_size)];
                let attribute_bytes = &point_bytes[attribute.byte_range_within_point()];
                storage.extend_from_slice(attribute_bytes);
            }
        }
        self.length += num_points_added;
    }

    fn resize(&mut self, count: usize) {
        for (attribute, storage) in self.attributes_storage.iter_mut() {
            let new_num_bytes = count * attribute.size() as usize;
            storage.resize(new_num_bytes, 0);
        }
        self.length = count;
    }

    fn clear(&mut self) {
        for storage in self.attributes_storage.values_mut() {
            storage.clear();
        }
        self.length = 0;
    }
}

impl<'a> ColumnarBuffer<'a> for HashMapBuffer
where
    HashMapBuffer: 'a,
{
    fn get_attribute_ref<'b>(
        &'b self,
        attribute: &PointAttributeDefinition,
        index: usize,
    ) -> &'b [u8]
    where
        'a: 'b,
    {
        let storage_of_attribute = self
            .attributes_storage
            .get(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        &storage_of_attribute[Self::get_byte_range_for_attribute(index, attribute)]
    }

    fn get_attribute_range_ref<'b>(
        &'b self,
        attribute: &PointAttributeDefinition,
        range: Range<usize>,
    ) -> &'b [u8]
    where
        'a: 'b,
    {
        let storage_of_attribute = self
            .attributes_storage
            .get(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        &storage_of_attribute[Self::get_byte_range_for_attributes(range, attribute)]
    }
}

impl<'a> ColumnarBufferMut<'a> for HashMapBuffer
where
    HashMapBuffer: 'a,
{
    fn get_attribute_mut<'b>(
        &'b mut self,
        attribute: &PointAttributeDefinition,
        index: usize,
    ) -> &'b mut [u8]
    where
        'a: 'b,
    {
        let byte_range = Self::get_byte_range_for_attribute(index, attribute);
        let storage_of_attribute = self
            .attributes_storage
            .get_mut(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        &mut storage_of_attribute[byte_range]
    }

    fn get_attribute_range_mut<'b>(
        &'b mut self,
        attribute: &PointAttributeDefinition,
        range: Range<usize>,
    ) -> &'b mut [u8]
    where
        'a: 'b,
    {
        let byte_range = Self::get_byte_range_for_attributes(range, attribute);
        let storage_of_attribute = self
            .attributes_storage
            .get_mut(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        &mut storage_of_attribute[byte_range]
    }
}

impl<'a> SliceBuffer<'a> for HashMapBuffer
where
    Self: 'a,
{
    type SliceType = BufferSliceColumnar<'a, Self>;

    fn slice(&'a self, range: Range<usize>) -> Self::SliceType {
        BufferSliceColumnar::new(self, range)
    }
}

impl<'a> SliceBufferMut<'a> for HashMapBuffer {
    type SliceTypeMut = BufferSliceColumnarMut<'a, Self>;

    fn slice_mut(&'a mut self, range: Range<usize>) -> Self::SliceTypeMut {
        BufferSliceColumnarMut::new(self, range)
    }
}

impl<T: PointType> FromIterator<T> for HashMapBuffer {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let point_layout = T::layout();
        let mut buffer = Self::new_from_layout(point_layout);
        iter.into_iter().for_each(|point| {
            let point_bytes = bytemuck::bytes_of(&point);
            // Safe because we know that `buffer` has the same `PointLayout` as `T`
            unsafe {
                buffer.push_points(point_bytes);
            }
        });
        buffer
    }
}

/// A point buffer that stores point data in interleaved memory layout in an externally borrowed memory resource.
/// This can be any type that is convertible to a `&[u8]`. If `T` also is convertible to a `&mut [u8]`, this buffer
/// also implements [`BorrowedMutBuffer`]
pub struct ExternalMemoryBuffer<T: AsRef<[u8]>> {
    external_memory: T,
    point_layout: PointLayout,
    length: usize,
}

impl<T: AsRef<[u8]>> ExternalMemoryBuffer<T> {
    /// Creates a new `ExternalMemoryBuffer` from the given `external_memory` resource and the given `PointLayout`
    pub fn new(external_memory: T, point_layout: PointLayout) -> Self {
        let length = match point_layout.size_of_point_entry() {
            0 => {
                assert_eq!(0, external_memory.as_ref().len());
                0
            }
            point_size => {
                assert!(external_memory.as_ref().len() % point_size as usize == 0);
                external_memory.as_ref().len() / point_size as usize
            }
        };
        Self {
            external_memory,
            point_layout,
            length,
        }
    }

    fn get_byte_range_for_point(&self, point_index: usize) -> Range<usize> {
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        (point_index * size_of_point)..((point_index + 1) * size_of_point)
    }

    fn get_byte_range_for_point_range(&self, point_range: Range<usize>) -> Range<usize> {
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        (point_range.start * size_of_point)..(point_range.end * size_of_point)
    }

    fn get_byte_range_of_attribute(
        &self,
        point_index: usize,
        attribute: &PointAttributeMember,
    ) -> Range<usize> {
        let start_byte = (point_index * self.point_layout.size_of_point_entry() as usize)
            + attribute.offset() as usize;
        let end_byte = start_byte + attribute.size() as usize;
        start_byte..end_byte
    }
}

impl<'a, T: AsRef<[u8]>> BorrowedBuffer<'a> for ExternalMemoryBuffer<T>
where
    ExternalMemoryBuffer<T>: 'a,
{
    fn len(&self) -> usize {
        self.length
    }

    fn point_layout(&self) -> &PointLayout {
        &self.point_layout
    }

    fn get_point(&self, index: usize, data: &mut [u8]) {
        let point_bytes = &self.external_memory.as_ref()[self.get_byte_range_for_point(index)];
        data.copy_from_slice(point_bytes);
    }

    fn get_point_range(&self, range: Range<usize>, data: &mut [u8]) {
        let point_bytes =
            &self.external_memory.as_ref()[self.get_byte_range_for_point_range(range)];
        data.copy_from_slice(point_bytes);
    }

    unsafe fn get_attribute_unchecked(
        &self,
        attribute_member: &PointAttributeMember,
        index: usize,
        data: &mut [u8],
    ) {
        let attribute_bytes_range = self.get_byte_range_of_attribute(index, attribute_member);
        let attribute_bytes = &self.external_memory.as_ref()[attribute_bytes_range];
        data.copy_from_slice(attribute_bytes);
    }

    fn as_interleaved(&self) -> Option<&dyn InterleavedBuffer<'a>> {
        Some(self)
    }
}

impl<'a, T: AsMut<[u8]> + AsRef<[u8]>> BorrowedMutBuffer<'a> for ExternalMemoryBuffer<T>
where
    ExternalMemoryBuffer<T>: 'a,
{
    unsafe fn set_point(&mut self, index: usize, point_data: &[u8]) {
        let point_byte_range = self.get_byte_range_for_point(index);
        let point_memory = &mut self.external_memory.as_mut()[point_byte_range];
        point_memory.copy_from_slice(point_data);
    }

    unsafe fn set_attribute(
        &mut self,
        attribute: &PointAttributeDefinition,
        index: usize,
        attribute_data: &[u8],
    ) {
        let attribute_member = self
            .point_layout
            .get_attribute(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        let attribute_byte_range = self.get_byte_range_of_attribute(index, attribute_member);
        let attribute_bytes = &mut self.external_memory.as_mut()[attribute_byte_range];
        attribute_bytes.copy_from_slice(attribute_data);
    }

    fn swap(&mut self, from_index: usize, to_index: usize) {
        assert!(from_index < self.len());
        assert!(to_index < self.len());
        if from_index == to_index {
            return;
        }
        let size_of_point = self.point_layout.size_of_point_entry() as usize;
        // Is safe if neither `from_index` nor `to_index` is out of bounds, which is asserted
        unsafe {
            let from_ptr = self
                .external_memory
                .as_mut()
                .as_mut_ptr()
                .add(from_index * size_of_point);
            let to_ptr = self
                .external_memory
                .as_mut()
                .as_mut_ptr()
                .add(to_index * size_of_point);
            std::ptr::swap_nonoverlapping(from_ptr, to_ptr, size_of_point);
        }
    }

    unsafe fn set_point_range(&mut self, point_range: Range<usize>, point_data: &[u8]) {
        let point_byte_range = self.get_byte_range_for_point_range(point_range);
        let point_memory = &mut self.external_memory.as_mut()[point_byte_range];
        point_memory.copy_from_slice(point_data);
    }

    unsafe fn set_attribute_range(
        &mut self,
        attribute: &PointAttributeDefinition,
        point_range: Range<usize>,
        attribute_data: &[u8],
    ) {
        let attribute_member = self
            .point_layout
            .get_attribute(attribute)
            .expect("Attribute not found in PointLayout of this buffer");
        let attribute_size = attribute_member.size() as usize;
        let first_point = point_range.start;
        for point_index in point_range {
            let zero_based_index = point_index - first_point;
            let src_slice = &attribute_data
                [(zero_based_index * attribute_size)..((zero_based_index + 1) * attribute_size)];
            let attribute_byte_range =
                self.get_byte_range_of_attribute(point_index, attribute_member);
            let attribute_bytes = &mut self.external_memory.as_mut()[attribute_byte_range];
            attribute_bytes.copy_from_slice(src_slice);
        }
    }

    fn as_interleaved_mut(&mut self) -> Option<&mut dyn InterleavedBufferMut<'a>> {
        Some(self)
    }
}

impl<'a, T: AsRef<[u8]>> InterleavedBuffer<'a> for ExternalMemoryBuffer<T>
where
    ExternalMemoryBuffer<T>: 'a,
{
    fn get_point_ref<'b>(&'b self, index: usize) -> &'b [u8]
    where
        'a: 'b,
    {
        let memory = self.external_memory.as_ref();
        &memory[self.get_byte_range_for_point(index)]
    }

    fn get_point_range_ref<'b>(&'b self, range: Range<usize>) -> &'b [u8]
    where
        'a: 'b,
    {
        let memory = self.external_memory.as_ref();
        &memory[self.get_byte_range_for_point_range(range)]
    }
}

impl<'a, T: AsRef<[u8]> + AsMut<[u8]>> InterleavedBufferMut<'a> for ExternalMemoryBuffer<T>
where
    ExternalMemoryBuffer<T>: 'a,
{
    fn get_point_mut<'b>(&'b mut self, index: usize) -> &'b mut [u8]
    where
        'a: 'b,
    {
        let byte_range = self.get_byte_range_for_point(index);
        let memory = self.external_memory.as_mut();
        &mut memory[byte_range]
    }

    fn get_point_range_mut<'b>(&'b mut self, range: Range<usize>) -> &'b mut [u8]
    where
        'a: 'b,
    {
        let byte_range = self.get_byte_range_for_point_range(range);
        let memory = self.external_memory.as_mut();
        &mut memory[byte_range]
    }
}

impl<'a, T: AsRef<[u8]> + 'a> SliceBuffer<'a> for ExternalMemoryBuffer<T>
where
    Self: 'a,
{
    type SliceType = BufferSliceInterleaved<'a, Self>;

    fn slice(&'a self, range: Range<usize>) -> Self::SliceType {
        BufferSliceInterleaved::new(self, range)
    }
}

impl<'a, T: AsRef<[u8]> + AsMut<[u8]> + 'a> SliceBufferMut<'a> for ExternalMemoryBuffer<T> {
    type SliceTypeMut = BufferSliceInterleavedMut<'a, Self>;
    fn slice_mut(&'a mut self, range: Range<usize>) -> Self::SliceTypeMut {
        BufferSliceInterleavedMut::new(self, range)
    }
}

#[cfg(test)]
mod tests {
    use itertools::Itertools;
    use nalgebra::Vector3;
    use rand::{prelude::Distribution, thread_rng, Rng};

    use crate::layout::{attributes::POSITION_3D, PointAttributeDataType};
    use crate::test_utils::*;

    use super::*;

    fn compare_attributes_typed<'a, U: PrimitiveType + std::fmt::Debug + PartialEq>(
        buffer: &'a impl BorrowedBuffer<'a>,
        attribute: &PointAttributeDefinition,
        expected_points: &'a impl BorrowedBuffer<'a>,
    ) {
        let collected_values = buffer
            .view_attribute::<U>(attribute)
            .into_iter()
            .collect::<Vec<_>>();
        let expected_values = expected_points
            .view_attribute::<U>(attribute)
            .into_iter()
            .collect::<Vec<_>>();
        assert_eq!(expected_values, collected_values);
    }

    /// Compare the given point attribute using the static type corresponding to the attribute's `PointAttributeDataType`
    fn compare_attributes<'a>(
        buffer: &'a impl BorrowedBuffer<'a>,
        attribute: &PointAttributeDefinition,
        expected_points: &'a impl BorrowedBuffer<'a>,
    ) {
        match attribute.datatype() {
            PointAttributeDataType::F32 => {
                compare_attributes_typed::<f32>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::F64 => {
                compare_attributes_typed::<f64>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::I16 => {
                compare_attributes_typed::<i16>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::I32 => {
                compare_attributes_typed::<i32>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::I64 => {
                compare_attributes_typed::<i64>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::I8 => {
                compare_attributes_typed::<i8>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::U16 => {
                compare_attributes_typed::<u16>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::U32 => {
                compare_attributes_typed::<u32>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::U64 => {
                compare_attributes_typed::<u64>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::U8 => {
                compare_attributes_typed::<u8>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::Vec3f32 => {
                compare_attributes_typed::<Vector3<f32>>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::Vec3f64 => {
                compare_attributes_typed::<Vector3<f64>>(buffer, attribute, expected_points);
            }
            PointAttributeDataType::Vec3i32 => {
                compare_attributes_typed::<Vector3<i32>>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::Vec3u16 => {
                compare_attributes_typed::<Vector3<u16>>(buffer, attribute, expected_points)
            }
            PointAttributeDataType::Vec3u8 => {
                compare_attributes_typed::<Vector3<u8>>(buffer, attribute, expected_points)
            }
            _ => unimplemented!(),
        }
    }

    fn test_vector_buffer_with_type<T: PointType + std::fmt::Debug + PartialEq + Copy + Clone>()
    where
        DefaultPointDistribution: Distribution<T>,
    {
        const COUNT: usize = 16;
        let test_data: Vec<T> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let overwrite_data: Vec<T> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();

        let test_data_as_buffer = test_data.iter().copied().collect::<VectorBuffer>();

        {
            let mut buffer = VectorBuffer::new_from_layout(T::layout());
            assert_eq!(0, buffer.len());
            assert_eq!(T::layout(), *buffer.point_layout());
            assert_eq!(0, buffer.view::<T>().into_iter().count());

            for (idx, point) in test_data.iter().enumerate() {
                buffer.view_mut().push_point(*point);
                assert_eq!(idx + 1, buffer.len());
                assert_eq!(*point, buffer.view().at(idx));
            }

            let mut collected_points = buffer.view().into_iter().collect::<Vec<_>>();
            assert_eq!(test_data, collected_points);

            let collected_points_by_ref = buffer.view().iter().copied().collect::<Vec<_>>();
            assert_eq!(test_data, collected_points_by_ref);

            for attribute in buffer.point_layout().attributes() {
                compare_attributes(
                    &buffer,
                    attribute.attribute_definition(),
                    &test_data_as_buffer,
                );
            }

            let slice = buffer.slice(1..2);
            assert_eq!(test_data[1], slice.view().at(0));

            for (idx, point) in overwrite_data.iter().enumerate() {
                *buffer.view_mut().at_mut(idx) = *point;
            }
            collected_points = buffer.view().iter().copied().collect();
            assert_eq!(overwrite_data, collected_points);
        }
    }

    fn test_hashmap_buffer_with_type<T: PointType + std::fmt::Debug + PartialEq + Copy + Clone>()
    where
        DefaultPointDistribution: Distribution<T>,
    {
        const COUNT: usize = 16;
        let test_data: Vec<T> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let overwrite_data: Vec<T> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();

        let test_data_as_buffer = test_data.iter().copied().collect::<HashMapBuffer>();

        {
            let mut buffer = HashMapBuffer::new_from_layout(T::layout());
            assert_eq!(0, buffer.len());
            assert_eq!(T::layout(), *buffer.point_layout());
            assert_eq!(0, buffer.view::<T>().into_iter().count());

            for (idx, point) in test_data.iter().enumerate() {
                buffer.view_mut().push_point(*point);
                assert_eq!(idx + 1, buffer.len());
                assert_eq!(*point, buffer.view().at(idx));
            }

            let mut collected_points = buffer.view().into_iter().collect::<Vec<_>>();
            assert_eq!(test_data, collected_points);

            for attribute in buffer.point_layout().attributes() {
                compare_attributes(
                    &buffer,
                    attribute.attribute_definition(),
                    &test_data_as_buffer,
                );
            }

            let slice = buffer.slice(1..2);
            assert_eq!(test_data[1], slice.view().at(0));

            for (idx, point) in overwrite_data.iter().enumerate() {
                buffer.view_mut().set_at(idx, *point);
            }
            collected_points = buffer.view().into_iter().collect();
            assert_eq!(overwrite_data, collected_points);
        }
    }

    fn test_external_memory_buffer_with_type<
        T: PointType + std::fmt::Debug + PartialEq + Copy + Clone,
    >()
    where
        DefaultPointDistribution: Distribution<T>,
    {
        const COUNT: usize = 16;
        let test_data: Vec<T> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let overwrite_data: Vec<T> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();

        let mut memory_of_buffer: Vec<u8> =
            vec![0; COUNT * T::layout().size_of_point_entry() as usize];
        let mut test_data_as_buffer = ExternalMemoryBuffer::new(&mut memory_of_buffer, T::layout());
        for (idx, point) in test_data.iter().copied().enumerate() {
            test_data_as_buffer.view_mut().set_at(idx, point);
        }

        {
            let mut buffer = VectorBuffer::new_from_layout(T::layout());
            assert_eq!(0, buffer.len());
            assert_eq!(T::layout(), *buffer.point_layout());
            assert_eq!(0, buffer.view::<T>().into_iter().count());

            for (idx, point) in test_data.iter().enumerate() {
                buffer.view_mut().push_point(*point);
                assert_eq!(idx + 1, buffer.len());
                assert_eq!(*point, buffer.view().at(idx));
            }

            let mut collected_points = buffer.view().into_iter().collect::<Vec<_>>();
            assert_eq!(test_data, collected_points);

            let collected_points_by_ref = buffer.view().iter().copied().collect::<Vec<_>>();
            assert_eq!(test_data, collected_points_by_ref);

            for attribute in buffer.point_layout().attributes() {
                compare_attributes(
                    &buffer,
                    attribute.attribute_definition(),
                    &test_data_as_buffer,
                );
            }

            let slice = buffer.slice(1..2);
            assert_eq!(test_data[1], slice.view().at(0));

            for (idx, point) in overwrite_data.iter().enumerate() {
                *buffer.view_mut().at_mut(idx) = *point;
            }
            collected_points = buffer.view().iter().copied().collect();
            assert_eq!(overwrite_data, collected_points);
        }
    }

    #[test]
    fn test_vector_buffer() {
        test_vector_buffer_with_type::<CustomPointTypeSmall>();
        test_vector_buffer_with_type::<CustomPointTypeBig>();
    }

    #[test]
    fn test_hash_map_buffer() {
        test_hashmap_buffer_with_type::<CustomPointTypeSmall>();
        test_hashmap_buffer_with_type::<CustomPointTypeBig>();
    }

    #[test]
    fn test_hash_map_buffer_mutate_attribute() {
        const COUNT: usize = 16;
        let test_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let overwrite_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();

        let mut buffer = test_data.iter().copied().collect::<HashMapBuffer>();

        for (idx, attribute) in buffer
            .view_attribute_mut::<Vector3<f64>>(&POSITION_3D)
            .iter_mut()
            .enumerate()
        {
            *attribute = overwrite_data[idx].position;
        }

        let expected_positions = overwrite_data
            .iter()
            .map(|point| point.position)
            .collect::<Vec<_>>();
        let actual_positions = buffer
            .view_attribute(&POSITION_3D)
            .into_iter()
            .collect::<Vec<_>>();
        assert_eq!(expected_positions, actual_positions);
    }

    #[test]
    fn test_external_memory_buffer() {
        test_external_memory_buffer_with_type::<CustomPointTypeSmall>();
        test_external_memory_buffer_with_type::<CustomPointTypeBig>();
    }

    fn test_transform_attribute_generic<
        'a,
        B: BorrowedMutBuffer<'a> + FromIterator<CustomPointTypeBig> + 'a,
    >() {
        const COUNT: usize = 16;
        let test_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let overwrite_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();

        let mut buffer = test_data.iter().copied().collect::<B>();
        // Overwrite the positions with the positions in `overwrite_data` using the `transform_attribute` function
        buffer.transform_attribute(&POSITION_3D, |index, _| -> Vector3<f64> {
            overwrite_data[index].position
        });

        let expected_positions = overwrite_data
            .iter()
            .map(|point| point.position)
            .collect::<Vec<_>>();
        let actual_positions = buffer
            .view_attribute(&POSITION_3D)
            .into_iter()
            .collect::<Vec<_>>();
        assert_eq!(expected_positions, actual_positions);
    }

    #[test]
    fn test_transform_attribute() {
        test_transform_attribute_generic::<VectorBuffer>();
        test_transform_attribute_generic::<HashMapBuffer>();
    }

    #[test]
    fn test_append() {
        const COUNT: usize = 16;
        let test_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();

        let expected_buffer_interleaved = test_data.iter().copied().collect::<VectorBuffer>();
        let expected_buffer_columnar = test_data.iter().copied().collect::<HashMapBuffer>();

        {
            let mut vector_buffer = VectorBuffer::new_from_layout(CustomPointTypeBig::layout());
            vector_buffer.append(&expected_buffer_interleaved);
            assert_eq!(expected_buffer_interleaved, vector_buffer);
        }
        {
            let mut vector_buffer = VectorBuffer::new_from_layout(CustomPointTypeBig::layout());
            vector_buffer.append(&expected_buffer_columnar);
            assert_eq!(expected_buffer_interleaved, vector_buffer);
        }
        {
            let mut hashmap_buffer = HashMapBuffer::new_from_layout(CustomPointTypeBig::layout());
            hashmap_buffer.append(&expected_buffer_columnar);
            assert_eq!(expected_buffer_columnar, hashmap_buffer);
        }
        {
            let mut hashmap_buffer = HashMapBuffer::new_from_layout(CustomPointTypeBig::layout());
            hashmap_buffer.append(&expected_buffer_interleaved);
            assert_eq!(expected_buffer_columnar, hashmap_buffer);
        }
    }

    #[test]
    fn test_buffers_from_empty_layout() {
        let empty_layout = PointLayout::default();

        {
            let buffer = VectorBuffer::new_from_layout(empty_layout.clone());
            assert_eq!(0, buffer.len());
        }
        {
            let buffer = HashMapBuffer::new_from_layout(empty_layout.clone());
            assert_eq!(0, buffer.len());
        }
        {
            let empty_memory = Vec::default();
            let buffer = ExternalMemoryBuffer::new(&empty_memory, empty_layout.clone());
            assert_eq!(0, buffer.len());
        }
    }

    fn test_buffer_set_point_range_generic<
        B: for<'a> BorrowedMutBuffer<'a>
            + FromIterator<CustomPointTypeBig>
            + for<'a> SliceBufferMut<'a>,
    >() {
        const COUNT: usize = 16;
        let test_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let overwrite_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let raw_overwrite_data: &[u8] = bytemuck::cast_slice(&overwrite_data);

        let mut buffer = test_data.iter().copied().collect::<B>();
        // Safe because we know the point layout of buffer is equal to that of CustomPointTypeBig
        unsafe {
            buffer.set_point_range(0..COUNT, raw_overwrite_data);
        }

        let actual_data = buffer
            .view::<CustomPointTypeBig>()
            .into_iter()
            .collect_vec();
        assert_eq!(overwrite_data, actual_data);

        // Do the same thing, but with a slice
        buffer = test_data.iter().copied().collect::<B>();
        let mut buffer_slice = buffer.slice_mut(0..COUNT);
        unsafe {
            buffer_slice.set_point_range(0..COUNT, raw_overwrite_data);
        }
        drop(buffer_slice);

        let actual_data = buffer
            .view::<CustomPointTypeBig>()
            .into_iter()
            .collect_vec();
        assert_eq!(overwrite_data, actual_data);
    }

    #[test]
    fn test_buffers_set_point_range() {
        test_buffer_set_point_range_generic::<VectorBuffer>();
        test_buffer_set_point_range_generic::<HashMapBuffer>();
    }

    fn test_buffer_get_point_range_generic<
        B: for<'a> BorrowedMutBuffer<'a>
            + FromIterator<CustomPointTypeBig>
            + for<'a> SliceBufferMut<'a>,
    >() {
        const COUNT: usize = 16;
        let test_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let raw_test_data: &[u8] = bytemuck::cast_slice(test_data.as_slice());
        let size_of_single_point = std::mem::size_of::<CustomPointTypeBig>();

        let buffer = test_data.iter().copied().collect::<B>();

        let mut actual_point_data = vec![0; raw_test_data.len()];
        buffer.get_point_range(0..COUNT, &mut actual_point_data);

        assert_eq!(raw_test_data, actual_point_data);

        // Check that subset ranges work correctly as well
        let subset_slice = &mut actual_point_data[..(6 * size_of_single_point)];
        buffer.get_point_range(2..8, subset_slice);
        assert_eq!(
            &raw_test_data[(2 * size_of_single_point)..(8 * size_of_single_point)],
            subset_slice
        );
    }

    #[test]
    fn test_buffer_get_point_range() {
        test_buffer_get_point_range_generic::<VectorBuffer>();
        test_buffer_get_point_range_generic::<HashMapBuffer>();
    }

    fn test_buffer_set_attribute_range_generic<
        B: for<'a> BorrowedMutBuffer<'a>
            + FromIterator<CustomPointTypeBig>
            + for<'a> SliceBufferMut<'a>,
    >() {
        const COUNT: usize = 16;
        let test_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let overwrite_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let overwrite_positions = overwrite_data
            .iter()
            .map(|point| point.position)
            .collect_vec();
        let overwrite_positions_raw_data: &[u8] = bytemuck::cast_slice(&overwrite_positions);

        let mut buffer = test_data.iter().copied().collect::<B>();
        // Safe because we know the point layout of buffer
        unsafe {
            buffer.set_attribute_range(&POSITION_3D, 0..COUNT, overwrite_positions_raw_data);
        }

        let actual_positions = buffer
            .view_attribute(&POSITION_3D)
            .into_iter()
            .collect::<Vec<_>>();
        assert_eq!(overwrite_positions, actual_positions);

        // Do the same test, but for a slice
        buffer = test_data.iter().copied().collect::<B>();
        let mut buffer_slice = buffer.slice_mut(0..test_data.len());
        unsafe {
            buffer_slice.set_attribute_range(&POSITION_3D, 0..COUNT, overwrite_positions_raw_data);
        }
        drop(buffer_slice);

        let actual_positions = buffer
            .view_attribute(&POSITION_3D)
            .into_iter()
            .collect::<Vec<_>>();
        assert_eq!(overwrite_positions, actual_positions);
    }

    #[test]
    fn test_buffers_set_attribute_range() {
        test_buffer_set_attribute_range_generic::<VectorBuffer>();
        test_buffer_set_attribute_range_generic::<HashMapBuffer>();
    }

    #[test]
    fn test_buffers_as_memory_layout_accessors() {
        let mut vector_buffer = VectorBuffer::new_from_layout(CustomPointTypeSmall::layout());
        let mut hashmap_buffer = HashMapBuffer::new_from_layout(CustomPointTypeSmall::layout());

        let mut memory: Vec<u8> = Vec::default();
        let mut external_memory_buffer =
            ExternalMemoryBuffer::new(memory.as_mut_slice(), CustomPointTypeSmall::layout());

        assert!(vector_buffer.as_interleaved().is_some());
        assert!(vector_buffer.slice(0..0).as_interleaved().is_some());
        assert!(vector_buffer.slice_mut(0..0).as_interleaved().is_some());
        assert!(hashmap_buffer.as_interleaved().is_none());
        assert!(hashmap_buffer.slice(0..0).as_interleaved().is_none());
        assert!(hashmap_buffer.slice_mut(0..0).as_interleaved().is_none());
        assert!(external_memory_buffer.as_interleaved().is_some());
        assert!(external_memory_buffer
            .slice(0..0)
            .as_interleaved()
            .is_some());
        assert!(external_memory_buffer
            .slice_mut(0..0)
            .as_interleaved()
            .is_some());

        assert!(vector_buffer.as_interleaved_mut().is_some());
        assert!(vector_buffer.slice_mut(0..0).as_interleaved_mut().is_some());
        assert!(hashmap_buffer.as_interleaved_mut().is_none());
        assert!(hashmap_buffer
            .slice_mut(0..0)
            .as_interleaved_mut()
            .is_none());
        assert!(external_memory_buffer.as_interleaved_mut().is_some());
        assert!(external_memory_buffer
            .slice_mut(0..0)
            .as_interleaved_mut()
            .is_some());

        assert!(vector_buffer.as_columnar().is_none());
        assert!(vector_buffer.slice(0..0).as_columnar().is_none());
        assert!(vector_buffer.slice_mut(0..0).as_columnar().is_none());
        assert!(hashmap_buffer.as_columnar().is_some());
        assert!(hashmap_buffer.slice(0..0).as_columnar().is_some());
        assert!(hashmap_buffer.slice_mut(0..0).as_columnar().is_some());
        assert!(external_memory_buffer.as_columnar().is_none());
        assert!(external_memory_buffer.slice(0..0).as_columnar().is_none());
        assert!(external_memory_buffer
            .slice_mut(0..0)
            .as_columnar()
            .is_none());

        assert!(vector_buffer.as_columnar_mut().is_none());
        assert!(vector_buffer.slice_mut(0..0).as_columnar_mut().is_none());
        assert!(hashmap_buffer.as_columnar_mut().is_some());
        assert!(hashmap_buffer.slice_mut(0..0).as_columnar_mut().is_some());
        assert!(external_memory_buffer.as_columnar_mut().is_none());
        assert!(external_memory_buffer
            .slice_mut(0..0)
            .as_columnar_mut()
            .is_none());
    }

    #[test]
    fn test_hash_map_buffer_filter() {
        const COUNT: usize = 16;
        let test_data: Vec<CustomPointTypeBig> = thread_rng()
            .sample_iter(DefaultPointDistribution)
            .take(COUNT)
            .collect();
        let even_points = test_data
            .iter()
            .enumerate()
            .filter_map(
                |(idx, point)| {
                    if idx % 2 == 0 {
                        Some(*point)
                    } else {
                        None
                    }
                },
            )
            .collect_vec();

        let src_buffer = test_data.iter().copied().collect::<HashMapBuffer>();

        let even_points_columnar = src_buffer.filter::<HashMapBuffer, _>(|idx| idx % 2 == 0);
        assert_eq!(
            even_points_columnar,
            even_points.iter().copied().collect::<HashMapBuffer>()
        );

        let even_points_interleaved = src_buffer.filter::<VectorBuffer, _>(|idx| idx % 2 == 0);
        assert_eq!(
            even_points_interleaved,
            even_points.iter().copied().collect::<VectorBuffer>()
        );
    }
}