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
use std::{
    any::{type_name, TypeId},
    ffi::c_void,
    fmt::Debug,
    iter::Iterator,
    marker::PhantomData,
    mem::MaybeUninit,
    sync::OnceLock,
};

use bones_utils::{default, HashMap};
use fxhash::FxHasher;
use parking_lot::RwLock;

use crate::{prelude::*, raw_fns::*};

use super::ResizableAlloc;

/// A type-erased [`Vec`]-like collection that for items with the same [`Schema`].
pub struct SchemaVec {
    /// The allocation for stored items.
    buffer: ResizableAlloc,
    /// The number of items actually stored in the vec.
    len: usize,
    /// The schema of the items stored in the vec.
    schema: &'static Schema,
}

impl std::fmt::Debug for SchemaVec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SchemaVec")
            .field("buffer", &"ResizableAlloc")
            .field("len", &self.len)
            .field("schema", &self.schema)
            .finish()
    }
}

// SOUND: the SchemaVec may only contain `HasSchema` types which are required to be `Sync + Send`.
unsafe impl Sync for SchemaVec {}
unsafe impl Send for SchemaVec {}

impl SchemaVec {
    /// Initialize an empty [`SchemaVec`] for items with the given schema.
    pub fn new(schema: &'static Schema) -> Self {
        Self {
            buffer: ResizableAlloc::new(schema.layout()),
            len: 0,
            schema,
        }
    }

    /// Grow the backing buffer to fit more elements.
    fn grow(&mut self) {
        let cap = self.buffer.capacity();
        if cap == 0 {
            self.buffer.resize(1).unwrap();
        } else {
            self.buffer.resize(cap * 2).unwrap();
        }
    }

    /// Push an item unsafely to the vector.
    /// # Safety
    /// - The item must be a pointer to data with the same schema.
    /// - You must ensure the `item` pointer is not used after pusing.
    unsafe fn push_raw(&mut self, item: *mut c_void) {
        // Make room for more elements if necessary
        if self.len == self.buffer.capacity() {
            self.grow();
        }

        // Copy the item into the vec
        unsafe {
            self.buffer
                .unchecked_idx(self.len)
                .copy_from_nonoverlapping(item, self.buffer.layout().size());
        }

        // Extend the length. This cannot overflow because we will run out of memory before we
        // exhaust `usize`.
        self.len += 1;
    }

    /// Push an item to the vec.
    /// # Errors
    /// Errors if the schema of `T` doesn't match the vec.
    pub fn try_push<T: HasSchema>(&mut self, mut item: T) -> Result<(), SchemaMismatchError> {
        // Ensure matching schema
        if self.schema != T::schema() {
            return Err(SchemaMismatchError);
        }

        unsafe {
            self.push_raw(&mut item as *mut T as *mut c_void);
            std::mem::forget(item);
        }

        Ok(())
    }

    /// Push an item to the vec.
    /// # Panics
    /// Panics if the schema of `T` doesn't match the vec.
    #[inline]
    #[track_caller]
    pub fn push<T: HasSchema>(&mut self, item: T) {
        self.try_push(item).unwrap()
    }

    /// Push the item into the end of the vector.
    pub fn try_push_box(&mut self, mut item: SchemaBox) -> Result<(), SchemaMismatchError> {
        // Ensure matching schema
        if self.schema != item.schema() {
            return Err(SchemaMismatchError);
        }

        // We validated matching schemas.
        unsafe {
            self.push_raw(item.as_mut().as_ptr());
        }

        // Don't run the item's destructor, it's the responsibility of the vec
        item.forget();

        Ok(())
    }

    /// Push the item into the end of the vector.
    #[track_caller]
    #[inline]
    pub fn push_box(&mut self, item: SchemaBox) {
        self.try_push_box(item).unwrap()
    }

    /// Pop the last item off of the end of the vector.
    pub fn pop_box(&mut self) -> Option<SchemaBox> {
        if self.len == 0 {
            None
        } else {
            unsafe { self.raw_pop() }.map(|ptr| unsafe {
                let mut b = SchemaBox::uninitialized(self.schema);
                b.as_mut()
                    .as_ptr()
                    .copy_from_nonoverlapping(ptr, self.buffer.layout().size());
                b
            })
        }
    }

    /// Pop an item off the vec.
    /// # Errors
    /// Errors if the schema of `T` doesn't match.
    pub fn try_pop<T: HasSchema>(&mut self) -> Result<Option<T>, SchemaMismatchError> {
        if self.schema != T::schema() {
            Err(SchemaMismatchError)
        } else {
            let ret = unsafe { self.raw_pop() }.map(|ptr| {
                let mut data = MaybeUninit::<T>::uninit();
                unsafe {
                    (data.as_mut_ptr() as *mut c_void)
                        .copy_from_nonoverlapping(ptr, self.buffer.layout().size());
                    data.assume_init()
                }
            });
            Ok(ret)
        }
    }

    /// # Safety
    /// The pointer may only be used immediately after calling raw_pop to read the data out of the
    /// popped item. Any further mutations to the vector may make the pointer invalid.
    unsafe fn raw_pop(&mut self) -> Option<*mut c_void> {
        if self.len == 0 {
            None
        } else {
            // Decrement our length
            self.len -= 1;

            // Return the pointer to the item that is being popped off.
            Some(unsafe { self.buffer.unchecked_idx(self.len) })
        }
    }

    /// Pop an item off the vec.
    /// # Panics
    /// Panics if the schema of `T` doesn't match.
    #[inline]
    #[track_caller]
    pub fn pop<T: HasSchema>(&mut self) -> Option<T> {
        self.try_pop().unwrap()
    }

    /// Get an item in the vec.
    /// # Errors
    /// Errors if the schema doesn't match.
    pub fn try_get<T: HasSchema>(&self, idx: usize) -> Result<Option<&T>, SchemaMismatchError> {
        self.get_ref(idx).map(|x| x.try_cast()).transpose()
    }

    /// Get an item in the vec.
    /// # Panics
    /// Panics if the schema doesn't match.
    #[inline]
    #[track_caller]
    pub fn get<T: HasSchema>(&self, idx: usize) -> Option<&T> {
        self.try_get(idx).unwrap()
    }

    /// Get the item with the given index.
    pub fn get_ref(&self, idx: usize) -> Option<SchemaRef<'_>> {
        if idx >= self.len {
            None
        } else {
            let ptr = unsafe { self.buffer.unchecked_idx(idx) };
            unsafe { Some(SchemaRef::from_ptr_schema(ptr, self.schema)) }
        }
    }

    /// Get an item in the vec.
    /// # Errors
    /// Errors if the schema doesn't match.
    pub fn try_get_mut<T: HasSchema>(
        &mut self,
        idx: usize,
    ) -> Result<Option<&mut T>, SchemaMismatchError> {
        self.get_ref_mut(idx)
            // SOUND: We are extending the lifetime of the cast to the lifetime of our borrow of
            // `&mut self`, which is valid.
            .map(|mut x| unsafe { x.try_cast_mut().map(|x| transmute_lifetime(x)) })
            .transpose()
    }

    /// Get an item in the vec.
    /// # Panics
    /// Panics if the schema doesn't match.
    #[inline]
    #[track_caller]
    pub fn get_mut<T: HasSchema>(&mut self, idx: usize) -> Option<&mut T> {
        self.try_get_mut(idx).unwrap()
    }

    /// Get an item with the given index.
    pub fn get_ref_mut(&mut self, idx: usize) -> Option<SchemaRefMut<'_>> {
        if idx >= self.len {
            None
        } else {
            let ptr = unsafe { self.buffer.unchecked_idx(idx) };
            unsafe { Some(SchemaRefMut::from_ptr_schema(ptr, self.schema)) }
        }
    }

    /// Get the number of items in the vector.
    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns `true` if the vector has zero items in it.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the capacity of the backing buffer.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.buffer.capacity()
    }

    /// Get the schema of items in this [`SchemaVec`].
    #[inline]
    pub fn schema(&self) -> &'static Schema {
        self.schema
    }

    /// Iterate over values in the vec
    pub fn iter(&self) -> SchemaVecIter {
        SchemaVecIter { vec: self, idx: 0 }
    }

    /// Iterate mutably over values in the vec
    pub fn iter_mut(&mut self) -> SchemaVecIterMut {
        SchemaVecIterMut { vec: self, idx: 0 }
    }

    /// Convert into a typed [`SVec`].
    /// # Panics
    /// Panics if the schema of `T` doesn't match this [`SchemaVec`]'s schema.
    #[track_caller]
    pub fn into_svec<T: HasSchema>(self) -> SVec<T> {
        self.try_into_svec().unwrap()
    }

    /// Try to convert into a typed [`SVec`].
    /// # Errors
    /// Errors if the schema of `T` doesn't match this [`SchemaVec`]'s schema.
    pub fn try_into_svec<T: HasSchema>(self) -> Result<SVec<T>, SchemaMismatchError> {
        if T::schema() == self.schema {
            Ok(SVec {
                vec: self,
                _phantom: PhantomData,
            })
        } else {
            Err(SchemaMismatchError)
        }
    }

    /// Get the hash of this [`SchemaVec`].
    /// # Panics
    /// Panics if the inner type doesn't implement hash.
    #[track_caller]
    pub fn hash(&self) -> u64 {
        use std::hash::{Hash, Hasher};
        let Some(hash_fn) = &self.schema.hash_fn else {
            panic!("Schema doesn't specify a hash_fn");
        };
        let mut hasher = FxHasher::default();
        for item_ptr in self.buffer.iter() {
            let item_hash = unsafe { (hash_fn.get())(item_ptr) };
            item_hash.hash(&mut hasher);
        }
        hasher.finish()
    }

    /// Raw version of the [`hash()`][Self::hash] function. Not meant for normal use.
    /// # Safety
    /// Pointer must be a valid pointer to a [`SchemaVec`].
    pub unsafe fn raw_hash(ptr: *const c_void) -> u64 {
        let this = unsafe { &*(ptr as *const Self) };
        this.hash()
    }

    /// Raw version of the [`eq()`][PartialEq::eq] function. Not meant for normal use.
    /// # Safety
    /// Pointers must be valid pointers to [`SchemaVec`]s.
    pub unsafe fn raw_eq(a: *const c_void, b: *const c_void) -> bool {
        let a = &*(a as *const Self);
        let b = &*(b as *const Self);
        a.eq(b)
    }

    /// Remove and return the element at position `index` within the vector,
    /// shifting all elements after it to the left.
    /// # Panics
    /// Panics if `index` is out of bounds.
    pub fn remove(&mut self, index: usize) -> SchemaBox {
        if index >= self.len {
            panic!("index out of bounds");
        }
        let item = unsafe {
            let ptr = self.buffer.unchecked_idx(index);
            let mut boxed = SchemaBox::uninitialized(self.schema);
            boxed
                .as_mut()
                .as_ptr()
                .copy_from_nonoverlapping(ptr, self.schema.layout().size());

            // Shift elements
            let to_move = self.len - index - 1;
            if to_move > 0 {
                std::ptr::copy(
                    self.buffer.unchecked_idx(index + 1),
                    self.buffer.unchecked_idx(index),
                    to_move * self.schema.layout().size(),
                );
            }

            self.len -= 1;
            boxed
        };
        item
    }

    /// Clears the vector, removing all values.
    pub fn clear(&mut self) {
        while self.pop_box().is_some() {}
    }

    /// Shortens the vector, keeping the first `len` elements and dropping the rest.
    ///
    /// If `len` is greater than the vector's current length, this has no effect.
    pub fn truncate(&mut self, len: usize) {
        while self.len > len {
            self.pop_box();
        }
    }
}

impl<T: HasSchema> FromIterator<T> for SVec<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut this = Self::default();
        for item in iter {
            this.push(item);
        }
        this
    }
}

impl<'a> IntoIterator for &'a SchemaVec {
    type Item = SchemaRef<'a>;
    type IntoIter = SchemaVecIter<'a>;
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}
impl<'a> IntoIterator for &'a mut SchemaVec {
    type Item = SchemaRefMut<'a>;
    type IntoIter = SchemaVecIterMut<'a>;
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

/// Iterator over [`SchemaVec`].
pub struct SchemaVecIter<'a> {
    vec: &'a SchemaVec,
    idx: usize,
}

impl<'a> Iterator for SchemaVecIter<'a> {
    type Item = SchemaRef<'a>;
    fn next(&mut self) -> Option<Self::Item> {
        let item = self.vec.get_ref(self.idx);
        if item.is_some() {
            self.idx += 1;
        }
        item
    }
}

/// Mutable iterator over [`SchemaVec`].
pub struct SchemaVecIterMut<'a> {
    vec: &'a mut SchemaVec,
    idx: usize,
}
impl<'a> Iterator for SchemaVecIterMut<'a> {
    type Item = SchemaRefMut<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let item = self
            .vec
            .get_ref_mut(self.idx)
            // SOUND: We are returning data with the lifetime of the SchemaVec, which is accurate
            // and sound as long as we don't return two mutable references to the same item.
            .map(|x| unsafe { SchemaRefMut::from_ptr_schema(x.as_ptr(), x.schema()) });
        if item.is_some() {
            self.idx += 1;
        }
        item
    }
}

impl Eq for SchemaVec {}
impl PartialEq for SchemaVec {
    #[track_caller]
    fn eq(&self, other: &Self) -> bool {
        if self.schema != other.schema {
            panic!("Cannot compare two `SchemaVec`s with different schemas.");
        }
        let Some(eq_fn) = &self.schema.eq_fn else {
            panic!("Schema doesn't have an eq_fn");
        };

        for i in 0..self.len {
            unsafe {
                let a = self.buffer.unchecked_idx(i);
                let b = self.buffer.unchecked_idx(i);
                if !(eq_fn.get())(a, b) {
                    return false;
                }
            }
        }
        true
    }
}

impl Clone for SchemaVec {
    fn clone(&self) -> Self {
        let Some(clone_fn) = &self.schema.clone_fn else {
            panic!("This type cannot be cloned");
        };
        let mut buffer_clone = ResizableAlloc::new(self.schema.layout());
        buffer_clone.resize(self.len).unwrap();

        // Clone each item in the vec
        for i in 0..self.len {
            // SOUND: we've check that the index is within bounds, and the schema asserts the
            // validity of the clone function.
            unsafe {
                let item = self.buffer.unchecked_idx(i);
                (clone_fn.get())(item, buffer_clone.unchecked_idx(i));
            }
        }

        SchemaVec {
            buffer: buffer_clone,
            len: self.len,
            schema: self.schema,
        }
    }
}

impl Drop for SchemaVec {
    fn drop(&mut self) {
        for _ in 0..self.len {
            drop(self.pop_box().unwrap());
        }
    }
}

/// A typed version of a [`SchemaVec`].
///
/// This type exists as an alternative to [`Vec`] that properly implements [`HasSchema`].
///
/// Additionally, accessing an [`SVec`] is more efficient than using a [`SchemaVec`] because it
/// avoids runtime schema checks after construction.
#[repr(transparent)]
#[derive(Eq, PartialEq)]
pub struct SVec<T: HasSchema> {
    vec: SchemaVec,
    _phantom: PhantomData<T>,
}

impl<T: HasSchema> SVec<T> {
    /// Create a new, empty [`SVec`].
    pub fn new() -> Self {
        Self {
            vec: SchemaVec::new(T::schema()),
            _phantom: PhantomData,
        }
    }

    /// Push an item onto the vector.
    pub fn push(&mut self, mut item: T) {
        // SOUND: We know that the schema matches, and we forget the item after pushing.
        unsafe {
            self.vec.push_raw(&mut item as *mut T as *mut c_void);
        }
        std::mem::forget(item);
    }

    /// Pop an item off of the vector.
    pub fn pop(&mut self) -> Option<T> {
        unsafe {
            self.vec.raw_pop().map(|ptr| {
                let mut ret = MaybeUninit::<T>::uninit();
                ret.as_mut_ptr().copy_from_nonoverlapping(ptr as *mut T, 1);
                ret.assume_init()
            })
        }
    }

    /// Get an item from the vec.
    pub fn get(&self, idx: usize) -> Option<&T> {
        // SOUND: We know that the pointer is to a type T
        self.vec
            .get_ref(idx)
            .map(|x| unsafe { x.cast_into_unchecked() })
    }

    /// Get an item from the vec.
    pub fn get_mut(&mut self, idx: usize) -> Option<&mut T> {
        // SOUND: We know that the pointer is to a type T
        self.vec
            .get_ref_mut(idx)
            .map(|x| unsafe { x.cast_into_mut_unchecked() })
    }

    /// Iterate over references to the items in the vec.
    pub fn iter(&self) -> SVecIter<T> {
        SVecIter {
            vec: self,
            idx: 0,
            end: self.len() as isize - 1,
        }
    }

    /// Iterate over mutable references to the items in the vec.
    pub fn iter_mut(&mut self) -> SVecIterMut<T> {
        SVecIterMut {
            idx: 0,
            end: self.len() as isize - 1,
            vec: self,
        }
    }

    /// Get the length of the vector.
    #[inline]
    pub fn len(&self) -> usize {
        self.vec.len()
    }

    /// Returns `true` if there are no items in the vector.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.vec.is_empty()
    }

    /// Get the capacity of the vec.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.vec.capacity()
    }

    /// Convert to an untyped [`SchemaVec`].
    #[inline]
    pub fn into_schema_vec(self) -> SchemaVec {
        self.vec
    }

    /// Get the hash of the [`SVec`].
    pub fn hash(&self) -> u64 {
        self.vec.hash()
    }

    /// Remove and return the element at position `index` within the vector,
    /// shifting all elements after it to the left.
    /// # Panics
    /// Panics if `index` is out of bounds.
    pub fn remove(&mut self, index: usize) -> T {
        let boxed = self.vec.remove(index);
        // SAFETY: We know that the SchemaBox contains a value of type T
        unsafe { boxed.cast_into_unchecked() }
    }

    /// Clears the vector, removing all values.
    pub fn clear(&mut self) {
        self.vec.clear();
    }

    /// Shortens the vector, keeping the first `len` elements and dropping the rest.
    ///
    /// If `len` is greater than the vector's current length, this has no effect.
    pub fn truncate(&mut self, len: usize) {
        self.vec.truncate(len);
    }

    /// Extends the vector with the contents of an iterator.
    pub fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        for item in iter {
            self.push(item);
        }
    }

    /// Retains only the elements specified by the predicate.
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&T) -> bool,
    {
        let mut i = 0;
        while i < self.len() {
            if !f(&self[i]) {
                self.remove(i);
            } else {
                i += 1;
            }
        }
    }

    /// Retains only the elements specified by the predicate, passing a mutable reference to it.
    pub fn retain_mut<F>(&mut self, mut f: F)
    where
        F: FnMut(&mut T) -> bool,
    {
        let mut i = 0;
        while i < self.len() {
            if !f(self.get_mut(i).unwrap()) {
                self.remove(i);
            } else {
                i += 1;
            }
        }
    }

    /// Removes and returns the last element of the vector if the predicate returns true.
    pub fn pop_if<F>(&mut self, f: F) -> Option<T>
    where
        F: FnOnce(&T) -> bool,
    {
        if let Some(last) = self.last() {
            if f(last) {
                self.pop()
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Returns a reference to the first element of the vector, or None if it is empty.
    pub fn first(&self) -> Option<&T> {
        self.get(0)
    }

    /// Returns a mutable reference to the first element of the vector, or None if it is empty.
    pub fn first_mut(&mut self) -> Option<&mut T> {
        self.get_mut(0)
    }

    /// Returns a reference to the last element of the vector, or None if it is empty.
    pub fn last(&self) -> Option<&T> {
        self.get(self.len().wrapping_sub(1))
    }

    /// Returns a mutable reference to the last element of the vector, or None if it is empty.
    pub fn last_mut(&mut self) -> Option<&mut T> {
        let len = self.len();
        self.get_mut(len.wrapping_sub(1))
    }

    /// Reverses the order of elements in the vector, in place.
    pub fn reverse(&mut self) {
        let mut i = 0;
        let mut j = self.len().saturating_sub(1);
        while i < j {
            // SAFETY: We know that i and j are within bounds and not equal.
            unsafe {
                let ptr_i = self.get_mut(i).unwrap() as *mut T;
                let ptr_j = self.get_mut(j).unwrap() as *mut T;
                std::ptr::swap(ptr_i, ptr_j);
            }
            i += 1;
            j -= 1;
        }
    }
}

/// Iterator over [`SVec`].
pub struct SVecIntoIter<T: HasSchema> {
    svec: SVec<T>,
    index: usize,
}

impl<T: HasSchema + Debug> std::fmt::Debug for SVec<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut l = f.debug_list();
        for item in self.iter() {
            l.entry(item);
        }
        l.finish()
    }
}

impl<T: HasSchema> From<SVec<T>> for Vec<T> {
    fn from(svec: SVec<T>) -> Self {
        let mut vec = Vec::with_capacity(svec.len());
        for item in svec {
            vec.push(item);
        }
        vec
    }
}

impl<T: HasSchema> From<Vec<T>> for SVec<T> {
    fn from(vec: Vec<T>) -> Self {
        let mut svec = SVec::new();
        for item in vec {
            svec.push(item);
        }
        svec
    }
}

// Implement IntoIterator for SVec<T>
impl<T: HasSchema> IntoIterator for SVec<T> {
    type Item = T;
    type IntoIter = SVecIntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        SVecIntoIter {
            svec: self,
            index: 0,
        }
    }
}

impl<T: HasSchema> Iterator for SVecIntoIter<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.svec.len() {
            let item = self.svec.remove(self.index);
            Some(item)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.svec.len() - self.index;
        (remaining, Some(remaining))
    }
}

impl<T: HasSchema> Drop for SVecIntoIter<T> {
    fn drop(&mut self) {
        // Ensure all remaining elements are properly dropped
        for _ in self.by_ref() {}
    }
}

impl<T: HasSchema, const N: usize> From<[T; N]> for SVec<T> {
    fn from(arr: [T; N]) -> Self {
        arr.into_iter().collect()
    }
}

impl<T: HasSchema> std::ops::Index<usize> for SVec<T> {
    type Output = T;

    #[track_caller]
    fn index(&self, idx: usize) -> &Self::Output {
        self.get(idx).unwrap()
    }
}

impl<T: HasSchema> std::ops::IndexMut<usize> for SVec<T> {
    #[track_caller]
    fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
        self.get_mut(idx).unwrap()
    }
}

impl<T: HasSchema> std::ops::Deref for SVec<T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        // SOUND: we know that the schema matches T, and the internal buffer of a SchemaVec stores
        // the types contiguously in memory.
        unsafe { std::slice::from_raw_parts(self.vec.buffer.as_ptr() as *const T, self.len()) }
    }
}
impl<T: HasSchema> std::ops::DerefMut for SVec<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // SOUND: we know that the schema matches T, and the internal buffer of a SchemaVec stores
        // the types contiguously in memory.
        unsafe { std::slice::from_raw_parts_mut(self.vec.buffer.as_ptr() as *mut T, self.len()) }
    }
}

unsafe impl<T: HasSchema> HasSchema for SVec<T> {
    fn schema() -> &'static Schema {
        static S: OnceLock<RwLock<HashMap<TypeId, &'static Schema>>> = OnceLock::new();
        let schema = {
            S.get_or_init(default)
                .read()
                .get(&TypeId::of::<Self>())
                .copied()
        };
        schema.unwrap_or_else(|| {
            let schema = SCHEMA_REGISTRY.register(SchemaData {
                name: type_name::<Self>().into(),
                full_name: format!("{}::{}", module_path!(), type_name::<Self>()).into(),
                kind: SchemaKind::Vec(T::schema()),
                type_id: Some(TypeId::of::<Self>()),
                clone_fn: Some(<Self as RawClone>::raw_clone_cb()),
                drop_fn: Some(<Self as RawDrop>::raw_drop_cb()),
                default_fn: Some(<Self as RawDefault>::raw_default_cb()),
                hash_fn: Some(unsafe {
                    Unsafe::new(Box::leak(Box::new(|a| SchemaVec::raw_hash(a))))
                }),
                eq_fn: Some(unsafe {
                    Unsafe::new(Box::leak(Box::new(|a, b| SchemaVec::raw_eq(a, b))))
                }),
                type_data: Default::default(),
            });

            S.get_or_init(default)
                .write()
                .insert(TypeId::of::<Self>(), schema);

            schema
        })
    }
}

impl<T: HasSchema> Default for SVec<T> {
    fn default() -> Self {
        Self {
            vec: SchemaVec::new(T::schema()),
            _phantom: Default::default(),
        }
    }
}
impl<T: HasSchema> Clone for SVec<T> {
    fn clone(&self) -> Self {
        Self {
            vec: self.vec.clone(),
            _phantom: self._phantom,
        }
    }
}

impl<'a, T: HasSchema> IntoIterator for &'a SVec<T> {
    type Item = &'a T;
    type IntoIter = SVecIter<'a, T>;
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}
impl<'a, T: HasSchema> IntoIterator for &'a mut SVec<T> {
    type Item = &'a mut T;
    type IntoIter = SVecIterMut<'a, T>;
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

/// Iterator over items in an [`SVec`].
pub struct SVecIter<'a, T: HasSchema> {
    vec: &'a SVec<T>,
    idx: usize,
    end: isize,
}
impl<'a, T: HasSchema> Iterator for SVecIter<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.end < 0 {
            return None;
        }
        let item = (self.idx <= self.end as usize).then(|| self.vec.get(self.idx).unwrap());
        if item.is_some() {
            self.idx += 1;
        }
        item
    }
}
impl<'a, T: HasSchema> DoubleEndedIterator for SVecIter<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.end < 0 {
            return None;
        }
        let item =
            (self.end as usize >= self.idx).then(|| self.vec.get(self.end as usize).unwrap());
        if item.is_some() {
            self.end -= 1;
        }
        item
    }
}

/// Iterator over items in an [`SVec`].
pub struct SVecIterMut<'a, T: HasSchema> {
    vec: &'a mut SVec<T>,
    idx: usize,
    end: isize,
}
impl<'a, T: HasSchema> Iterator for SVecIterMut<'a, T> {
    type Item = &'a mut T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.end < 0 {
            return None;
        }
        let item = (self.idx <= self.end as usize).then(|| self.vec.get_mut(self.idx).unwrap());
        if item.is_some() {
            self.idx += 1;
        }
        // SOUND: we are returning data with the lifetime of the vec which is valid and sound,
        // assuming we don't return two mutable references to the same item.
        item.map(|x| unsafe { transmute_lifetime(x) })
    }
}
impl<'a, T: HasSchema> DoubleEndedIterator for SVecIterMut<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.end < 0 {
            return None;
        }
        let item =
            (self.end as usize >= self.idx).then(|| self.vec.get_mut(self.end as usize).unwrap());
        if item.is_some() {
            self.end -= 1;
        }
        // SOUND: we are returning data with the lifetime of the vec which is valid and sound,
        // assuming we don't return two mutable references to the same item.
        item.map(|x| unsafe { transmute_lifetime(x) })
    }
}

/// Helper to transmute a lifetime unsafely.
///
/// This is safer than just calling [`transmute`][std::mem::transmute] because it can only transmut
/// the lifetime, not the type of the reference.
unsafe fn transmute_lifetime<'b, T>(v: &mut T) -> &'b mut T {
    std::mem::transmute(v)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn double_ended() {
        let mut v = [1, 2, 3, 4, 5, 6].into_iter().collect::<SVec<_>>();

        let mut iter = v.iter();
        assert_eq!(iter.next_back(), Some(&6));
        assert_eq!(iter.next_back(), Some(&5));
        assert_eq!(iter.next(), Some(&1));
        assert_eq!(iter.next(), Some(&2));
        assert_eq!(iter.next_back(), Some(&4));
        assert_eq!(iter.next(), Some(&3));
        assert_eq!(iter.next_back(), None);
        assert_eq!(iter.next(), None);

        let mut iter = v.iter_mut();
        assert_eq!(iter.next_back(), Some(&mut 6));
        assert_eq!(iter.next_back(), Some(&mut 5));
        assert_eq!(iter.next(), Some(&mut 1));
        assert_eq!(iter.next(), Some(&mut 2));
        assert_eq!(iter.next_back(), Some(&mut 4));
        assert_eq!(iter.next(), Some(&mut 3));
        assert_eq!(iter.next_back(), None);
        assert_eq!(iter.next(), None);

        let v = [].into_iter().collect::<SVec<u8>>();
        let mut iter = v.iter();
        assert_eq!(iter.next(), None);
        assert_eq!(iter.next(), None);
        assert_eq!(iter.next_back(), None);
        let mut iter = v.iter();
        assert_eq!(iter.next_back(), None);
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_vec_and_svec_conversions() {
        // Test Vec to SVec conversion
        let vec = vec![1, 2, 3, 4, 5];
        let svec: SVec<i32> = vec.clone().into();
        assert_eq!(svec.len(), 5);
        // Test SVec to Vec conversion
        let vec_from_svec: Vec<i32> = svec.into();
        assert_eq!(vec, vec_from_svec);
        // Test direct array conversion to SVec
        let svec_direct: SVec<i32> = [11, 12, 13].into();
        assert_eq!(svec_direct.len(), 3);
        // Test SVec to Vec conversion for array-created SVec
        let vec_from_array_svec: Vec<i32> = svec_direct.into();
        assert_eq!(vec_from_array_svec, vec![11, 12, 13]);
    }

    #[test]
    fn test_remove() {
        let mut svec: SVec<i32> = vec![10, 20, 30, 40, 50].into();

        // Remove from the middle
        let removed = svec.remove(2);
        assert_eq!(removed, 30);
        assert_eq!(svec.len(), 4);
        assert_eq!(svec[0], 10);

        // Remove from the beginning
        let removed = svec.remove(0);
        assert_eq!(removed, 10);
        assert_eq!(svec.len(), 3);
        assert_eq!(svec[0], 20);

        // Remove from the end
        let removed = svec.remove(2);
        assert_eq!(removed, 50);
        assert_eq!(svec.len(), 2);
        assert_eq!(svec[1], 40);

        // Test removing the last element
        let removed = svec.remove(1);
        assert_eq!(removed, 40);
        assert_eq!(svec.len(), 1);
        assert_eq!(svec[0], 20);

        // Test removing the very last element
        let removed = svec.remove(0);
        assert_eq!(removed, 20);
        assert_eq!(svec.len(), 0);
    }

    #[test]
    fn test_svec_operations() {
        let mut vec: SVec<i32> = SVec::new();

        // Test push and len
        vec.push(1);
        vec.push(2);
        vec.push(3);
        assert_eq!(vec.len(), 3);

        // Test get
        assert_eq!(vec.get(1), Some(&2));
        assert_eq!(vec.get(3), None);

        // Test remove
        let removed = vec.remove(2);
        assert_eq!(removed, 3);
        assert_eq!(vec.len(), 2);

        // Test iteration
        let sum: i32 = vec.iter().copied().sum();
        assert_eq!(sum, 3); // 1 + 2

        // Test extend
        vec.extend(vec![5, 6]);
        assert_eq!(vec.len(), 4);
        assert_eq!(vec.get(2), Some(&5));
        assert_eq!(vec.get(3), Some(&6));

        // Test retain
        vec.retain(|&x| x % 2 == 0);
        assert_eq!(vec.len(), 2);
        assert_eq!(vec[0], 2);
        assert_eq!(vec[1], 6);

        // Test retain_mut
        vec.retain_mut(|x| {
            *x *= 2;
            true
        });
        assert_eq!(vec.len(), 2);
        assert_eq!(vec[0], 4);
        assert_eq!(vec[1], 12);

        // Test truncate
        vec.truncate(1);
        assert_eq!(vec.len(), 1);
        assert_eq!(vec[0], 4);
        assert_eq!(vec.get(1), None);

        // Prepare for further tests
        vec.extend(vec![7, 9, 11]);

        // Test first() and first_mut()
        assert_eq!(vec.first(), Some(&4));
        if let Some(first) = vec.first_mut() {
            *first = 1;
        }
        assert_eq!(vec[0], 1);

        // Test last() and last_mut()
        assert_eq!(vec.last(), Some(&11));
        if let Some(last) = vec.last_mut() {
            *last = 15;
        }
        assert_eq!(vec[3], 15);

        // Test pop_if()
        assert_eq!(vec.pop_if(|&x| x > 10), Some(15));
        assert_eq!(vec.len(), 3);
        assert_eq!(vec.pop_if(|&x| x < 0), None);
        assert_eq!(vec.len(), 3);

        // Test clear
        vec.clear();
        assert_eq!(vec.len(), 0);
        assert!(vec.is_empty());

        // Test pop on empty vector
        assert_eq!(vec.pop(), None);

        // Test Vec and SVec conversions
        let original_vec = vec![1, 2, 3, 4, 5];
        let svec: SVec<i32> = original_vec.clone().into();
        let vec_from_svec: Vec<i32> = svec.into();
        assert_eq!(original_vec, vec_from_svec);

        // Test reverse
        let mut reversed_vec = original_vec.clone();
        reversed_vec.reverse();
        assert_eq!(reversed_vec, vec![5, 4, 3, 2, 1]);
    }

    #[test]
    fn miri_error_001() {
        let mut vec: SVec<i32> = SVec::new();
        vec.push(10);
        vec.pop();
    }
}