simdnbt 0.10.0

an unnecessarily fast nbt decoder
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
use std::{
    fmt::{self, Debug},
    marker::PhantomData,
    mem,
};

use super::{
    NbtCompound, Tapes,
    compound::{ParsingStack, ParsingStackElement},
    extra_tapes::{ExtraTapeElement, ExtraTapes},
    tape::{TapeElement, TapeTagKind, UnalignedU32},
};
use crate::{
    Mutf8Str,
    common::{
        BYTE_ARRAY_ID, BYTE_ID, COMPOUND_ID, DOUBLE_ID, END_ID, FLOAT_ID, INT_ARRAY_ID, INT_ID,
        LIST_ID, LONG_ARRAY_ID, LONG_ID, SHORT_ID, STRING_ID, read_i8_array, read_int_array,
        read_long_array, read_string, read_u8_array, read_with_u32_length, slice_i8_into_u8,
        write_string, write_u32, write_with_u32_length,
    },
    error::NonRootError,
    fastvec::{FastVec, FastVecFromVec},
    raw_list::RawList,
    reader::Reader,
    swap_endianness::SwappableNumber,
};

/// A list of NBT tags of a single type.
#[derive(Clone, Copy)]
pub struct NbtList<'a: 'tape, 'tape> {
    pub(crate) element: *const TapeElement, // the initial list element
    pub(crate) extra_tapes: &'tape ExtraTapes<'a>,
}
impl<'a, 'tape> NbtList<'a, 'tape> {
    pub(crate) fn read(
        data: &mut Reader<'a>,
        tapes: &mut Tapes<'a>,
        stack: &mut ParsingStack,
    ) -> Result<(), NonRootError> {
        let tag_type = data.read_u8()?;

        let pushing_element = match tag_type {
            END_ID => {
                // the length is unused for this type of lists
                data.skip(4)?;
                TapeElement::new_with_0(TapeTagKind::EmptyList)
            }
            BYTE_ID => {
                let byte_list_ptr = data.cur;
                let _ = read_i8_array(data)?;
                TapeElement::new_with_ptr(TapeTagKind::ByteList, byte_list_ptr)
            }
            SHORT_ID => {
                let short_list_ptr = data.cur;
                read_with_u32_length(data, 2)?;
                TapeElement::new_with_ptr(TapeTagKind::ShortList, short_list_ptr)
            }
            INT_ID => {
                let int_list_ptr = data.cur;
                read_with_u32_length(data, 4)?;
                TapeElement::new_with_ptr(TapeTagKind::IntList, int_list_ptr)
            }
            LONG_ID => {
                let long_list_ptr = data.cur;
                read_with_u32_length(data, 8)?;
                TapeElement::new_with_ptr(TapeTagKind::LongList, long_list_ptr)
            }
            FLOAT_ID => {
                let float_list_ptr = data.cur;
                read_with_u32_length(data, 4)?;
                TapeElement::new_with_ptr(TapeTagKind::FloatList, float_list_ptr)
            }
            DOUBLE_ID => {
                let double_list_ptr = data.cur;
                read_with_u32_length(data, 8)?;
                TapeElement::new_with_ptr(TapeTagKind::DoubleList, double_list_ptr)
            }
            BYTE_ARRAY_ID => {
                let index_of_element = tapes.extra.elements.len() as u32;

                let length = data.read_u32()?;
                tapes.extra.elements.push(ExtraTapeElement { length });
                for _ in 0..length {
                    let byte_array = read_u8_array(data)?;
                    tapes.extra.elements.push(ExtraTapeElement { byte_array });
                }

                TapeElement::new_with_u32(TapeTagKind::ByteArrayList, index_of_element)
            }
            STRING_ID => {
                let index_of_element = tapes.extra.elements.len() as u32;

                let length = data.read_u32()?;
                tapes.extra.elements.push(ExtraTapeElement { length });
                for _ in 0..length {
                    let string = read_string(data)?;
                    tapes.extra.elements.push(ExtraTapeElement { string });
                }

                TapeElement::new_with_u32(TapeTagKind::StringList, index_of_element)
            }
            LIST_ID => {
                let length = data.read_u32()?;
                // length estimate + tape index offset to the end of the list
                let index_of_list_element = tapes.main.len();

                stack.push(ParsingStackElement::list_of_lists(
                    index_of_list_element as u32,
                ))?;
                stack.set_list_length(length);
                TapeElement::new_with_approx_len_and_offset(
                    TapeTagKind::ListList,
                    length,
                    // can't know the offset until after
                    0,
                )
            }
            COMPOUND_ID => {
                let length = data.read_u32()?;
                // length estimate + tape index offset to the end of the compound
                let index_of_list_element = tapes.main.len();

                stack.push(ParsingStackElement::list_of_compounds(
                    index_of_list_element as u32,
                ))?;
                stack.set_list_length(length);
                TapeElement::new_with_approx_len_and_offset(
                    TapeTagKind::CompoundList,
                    length,
                    // this gets overwritten after the list is fully read
                    0,
                )
            }
            INT_ARRAY_ID => {
                let index_of_element = tapes.extra.elements.len() as u32;
                let length = data.read_u32()?;
                tapes.extra.elements.push(ExtraTapeElement { length });
                for _ in 0..length {
                    let int_array = read_int_array(data)?;
                    tapes.extra.elements.push(ExtraTapeElement { int_array });
                }

                TapeElement::new_with_u32(TapeTagKind::IntArrayList, index_of_element)
            }
            LONG_ARRAY_ID => {
                let index_of_element = tapes.extra.elements.len() as u32;
                let length = data.read_u32()?;
                tapes.extra.elements.push(ExtraTapeElement { length });
                for _ in 0..length {
                    let long_array = read_long_array(data)?;
                    tapes.extra.elements.push(ExtraTapeElement { long_array });
                }

                TapeElement::new_with_u32(TapeTagKind::LongArrayList, index_of_element)
            }
            _ => return Err(NonRootError::unknown_tag_id(tag_type)),
        };

        tapes.main.push(pushing_element);

        Ok(())
    }

    pub fn write(self, data: &mut Vec<u8>) {
        self.write_fastvec(&mut FastVecFromVec::new(data));
    }

    pub(crate) fn write_fastvec(&self, data: &mut FastVec<u8>) {
        let el = self.element();

        data.push(self.id());

        match el.kind() {
            TapeTagKind::EmptyList => {
                data.extend_from_slice(&0u32.to_be_bytes());
            }
            TapeTagKind::ByteList => {
                write_with_u32_length(data, 1, slice_i8_into_u8(self.bytes().unwrap()));
            }
            TapeTagKind::ShortList => {
                write_with_u32_length(
                    data,
                    2,
                    u32_prefixed_list_to_rawlist::<i16>(TapeTagKind::ShortList, self.element)
                        .unwrap()
                        .as_big_endian(),
                );
            }
            TapeTagKind::IntList => {
                write_with_u32_length(
                    data,
                    4,
                    u32_prefixed_list_to_rawlist::<i32>(TapeTagKind::IntList, self.element)
                        .unwrap()
                        .as_big_endian(),
                );
            }
            TapeTagKind::LongList => {
                write_with_u32_length(
                    data,
                    8,
                    u32_prefixed_list_to_rawlist::<i64>(TapeTagKind::LongList, self.element)
                        .unwrap()
                        .as_big_endian(),
                );
            }
            TapeTagKind::FloatList => {
                write_with_u32_length(
                    data,
                    4,
                    u32_prefixed_list_to_rawlist::<f32>(TapeTagKind::FloatList, self.element)
                        .unwrap()
                        .as_big_endian(),
                );
            }
            TapeTagKind::DoubleList => {
                write_with_u32_length(
                    data,
                    8,
                    u32_prefixed_list_to_rawlist::<f64>(TapeTagKind::DoubleList, self.element)
                        .unwrap()
                        .as_big_endian(),
                );
            }
            TapeTagKind::ByteArrayList => {
                let byte_arrays = self.byte_arrays().unwrap();
                for array in byte_arrays.iter() {
                    write_with_u32_length(data, 1, array);
                }
            }
            TapeTagKind::StringList => {
                let strings = self.strings().unwrap();
                for string in strings.iter() {
                    write_string(data, string);
                }
            }
            TapeTagKind::ListList => {
                let lists = self.lists().unwrap();
                for list in lists {
                    list.write_fastvec(data);
                }
            }
            TapeTagKind::CompoundList => {
                let compounds = self.compounds().unwrap();
                write_u32(data, compounds.clone().len() as u32);
                for compound in compounds {
                    compound.write_fastvec(data);
                }
            }
            TapeTagKind::IntArrayList => {
                let int_arrays = self.int_arrays().unwrap();
                for array in int_arrays.iter() {
                    write_with_u32_length(data, 4, array.as_big_endian());
                }
            }
            TapeTagKind::LongArrayList => {
                let long_arrays = self.long_arrays().unwrap();
                for array in long_arrays.iter() {
                    write_with_u32_length(data, 8, array.as_big_endian());
                }
            }
            _ => unreachable!(),
        }
    }

    /// Get the tape element kind and value for this list.
    fn element(&self) -> TapeElement {
        unsafe { *self.element }
    }

    /// Get the numerical ID of the tag type.
    #[inline]
    pub fn id(&self) -> u8 {
        match self.element().kind() {
            TapeTagKind::EmptyList => END_ID,
            TapeTagKind::ByteList => BYTE_ID,
            TapeTagKind::ShortList => SHORT_ID,
            TapeTagKind::IntList => INT_ID,
            TapeTagKind::LongList => LONG_ID,
            TapeTagKind::FloatList => FLOAT_ID,
            TapeTagKind::DoubleList => DOUBLE_ID,
            TapeTagKind::ByteArrayList => BYTE_ARRAY_ID,
            TapeTagKind::StringList => STRING_ID,
            TapeTagKind::ListList => LIST_ID,
            TapeTagKind::CompoundList => COMPOUND_ID,
            TapeTagKind::IntArrayList => INT_ARRAY_ID,
            TapeTagKind::LongArrayList => LONG_ARRAY_ID,
            _ => unreachable!(),
        }
    }

    /// Returns whether the list is specifically a list with the `empty` tag
    /// type. This will return false if the list is any other type (even it
    /// has a length of zero).
    pub fn empty(&self) -> bool {
        self.element().kind() == TapeTagKind::EmptyList
    }

    pub fn bytes(&self) -> Option<&[i8]> {
        let el = self.element();
        if el.kind() != TapeTagKind::ByteList {
            return None;
        }
        let length_ptr = el.ptr::<UnalignedU32>();
        let length = unsafe { u32::from(*length_ptr) };
        #[cfg(target_endian = "little")]
        let length = length.swap_bytes();
        let byte_array =
            unsafe { std::slice::from_raw_parts(length_ptr.add(1) as *const i8, length as usize) };
        Some(byte_array)
    }
    pub fn shorts(&self) -> Option<Vec<i16>> {
        u32_prefixed_list_to_vec(TapeTagKind::ShortList, self.element)
    }
    pub fn ints(&self) -> Option<Vec<i32>> {
        u32_prefixed_list_to_vec(TapeTagKind::IntList, self.element)
    }
    pub fn longs(&self) -> Option<Vec<i64>> {
        u32_prefixed_list_to_vec(TapeTagKind::LongList, self.element)
    }
    pub fn floats(&self) -> Option<Vec<f32>> {
        u32_prefixed_list_to_vec(TapeTagKind::FloatList, self.element)
    }
    pub fn doubles(&self) -> Option<Vec<f64>> {
        u32_prefixed_list_to_vec(TapeTagKind::DoubleList, self.element)
    }
    pub fn byte_arrays(&self) -> Option<&'a [&'a [u8]]> {
        let el = self.element();
        if el.kind() != TapeTagKind::ByteArrayList {
            return None;
        }
        let index_to_extra_tapes = el.u32() as usize;
        let length_ref = &self.extra_tapes.elements[index_to_extra_tapes];
        let length = unsafe { length_ref.length as usize };
        let slice = unsafe {
            std::slice::from_raw_parts(
                self.extra_tapes
                    .elements
                    .as_ptr()
                    .add(index_to_extra_tapes + 1)
                    .cast(),
                length,
            )
        };
        Some(slice)
    }
    pub fn strings(&self) -> Option<&'a [&'a Mutf8Str]> {
        let el = self.element();
        if el.kind() != TapeTagKind::StringList {
            return None;
        }
        let index_to_extra_tapes = el.u32() as usize;
        let length_ref = &self.extra_tapes.elements[index_to_extra_tapes];
        let length = unsafe { length_ref.length as usize };
        let slice = unsafe {
            std::slice::from_raw_parts(
                self.extra_tapes
                    .elements
                    .as_ptr()
                    .add(index_to_extra_tapes + 1)
                    .cast(),
                length,
            )
        };
        Some(slice)
    }
    pub fn lists(&self) -> Option<NbtListList<'a, 'tape>> {
        let el = self.element();
        if el.kind() != TapeTagKind::ListList {
            return None;
        }

        let (approx_length, max_tape_offset) = el.approx_len_and_offset();

        Some(NbtListList {
            iter: NbtListListIter {
                // it's an iterator, it starts at 0
                current_tape_offset: 0,
                max_tape_offset: max_tape_offset as usize,
                approx_length,
                // the first element is the listlist element so we don't include it
                tape: unsafe { self.element.add(1) },
                extra_tapes: self.extra_tapes,
                _phantom: PhantomData,
            },
        })
    }

    pub fn compounds(&self) -> Option<NbtCompoundList<'a, 'tape>> {
        let el = self.element();
        if el.kind() != TapeTagKind::CompoundList {
            return None;
        }

        let (approx_length, max_tape_offset) = el.approx_len_and_offset();
        let max_tape_offset = max_tape_offset as usize;

        let tape_slice =
            unsafe { std::slice::from_raw_parts(self.element.add(1), max_tape_offset) };

        Some(NbtCompoundList {
            iter: NbtCompoundListIter {
                current_tape_offset: 0,
                max_tape_offset,
                approx_length,
                tape: tape_slice,
                extra_tapes: self.extra_tapes,
            },
        })
    }
    pub fn int_arrays(&self) -> Option<&[RawList<'_, i32>]> {
        let el = self.element();
        if el.kind() != TapeTagKind::IntArrayList {
            return None;
        }
        let index_to_extra_tapes = el.u32() as usize;
        let length_ref = &self.extra_tapes.elements[index_to_extra_tapes];
        let length = unsafe { length_ref.length as usize };
        let slice = unsafe {
            std::slice::from_raw_parts(
                self.extra_tapes
                    .elements
                    .as_ptr()
                    .add(index_to_extra_tapes + 1)
                    .cast(),
                length,
            )
        };
        Some(slice)
    }
    pub fn long_arrays(&self) -> Option<&[RawList<'_, i64>]> {
        let el = self.element();
        if el.kind() != TapeTagKind::LongArrayList {
            return None;
        }
        let index_to_extra_tapes = el.u32() as usize;
        let length_ref = &self.extra_tapes.elements[index_to_extra_tapes];
        let length = unsafe { length_ref.length as usize };
        let slice = unsafe {
            std::slice::from_raw_parts(
                self.extra_tapes
                    .elements
                    .as_ptr()
                    .add(index_to_extra_tapes + 1)
                    .cast(),
                length,
            )
        };
        Some(slice)
    }

    pub fn to_owned(&self) -> crate::owned::NbtList {
        let el = self.element();

        match el.kind() {
            TapeTagKind::EmptyList => crate::owned::NbtList::Empty,
            TapeTagKind::ByteList => crate::owned::NbtList::Byte(self.bytes().unwrap().to_vec()),
            TapeTagKind::ShortList => crate::owned::NbtList::Short(self.shorts().unwrap().to_vec()),
            TapeTagKind::IntList => crate::owned::NbtList::Int(self.ints().unwrap().to_vec()),
            TapeTagKind::LongList => crate::owned::NbtList::Long(self.longs().unwrap().to_vec()),
            TapeTagKind::FloatList => crate::owned::NbtList::Float(self.floats().unwrap().to_vec()),
            TapeTagKind::DoubleList => {
                crate::owned::NbtList::Double(self.doubles().unwrap().to_vec())
            }
            TapeTagKind::ByteArrayList => crate::owned::NbtList::ByteArray(
                self.byte_arrays()
                    .unwrap()
                    .iter()
                    .map(|array| array.to_vec())
                    .collect(),
            ),
            TapeTagKind::StringList => crate::owned::NbtList::String(
                self.strings()
                    .unwrap()
                    .iter()
                    .map(|&string| string.to_owned())
                    .collect(),
            ),
            TapeTagKind::ListList => crate::owned::NbtList::List(
                self.lists()
                    .unwrap()
                    .into_iter()
                    .map(|list| list.to_owned())
                    .collect(),
            ),
            TapeTagKind::CompoundList => crate::owned::NbtList::Compound(
                self.compounds()
                    .unwrap()
                    .into_iter()
                    .map(|compound| compound.to_owned())
                    .collect(),
            ),
            TapeTagKind::IntArrayList => crate::owned::NbtList::IntArray(
                self.int_arrays()
                    .unwrap()
                    .iter()
                    .map(|array| array.to_vec())
                    .collect::<Vec<_>>(),
            ),
            TapeTagKind::LongArrayList => crate::owned::NbtList::LongArray(
                self.long_arrays()
                    .unwrap()
                    .iter()
                    .map(|array| array.to_vec())
                    .collect::<Vec<_>>(),
            ),
            _ => unreachable!("this is an NbtList, no other kinds should be possible"),
        }
    }
}

impl PartialEq for NbtList<'_, '_> {
    fn eq(&self, other: &Self) -> bool {
        let self_el = self.element();
        let other_el = other.element();
        if self_el.kind() != other_el.kind() {
            return false;
        }
        match self_el.kind() {
            TapeTagKind::EmptyList => true,
            TapeTagKind::ByteList => self.bytes().unwrap() == other.bytes().unwrap(),
            TapeTagKind::ShortList => self.shorts().unwrap() == other.shorts().unwrap(),
            TapeTagKind::IntList => self.ints().unwrap() == other.ints().unwrap(),
            TapeTagKind::LongList => self.longs().unwrap() == other.longs().unwrap(),
            TapeTagKind::FloatList => self.floats().unwrap() == other.floats().unwrap(),
            TapeTagKind::DoubleList => self.doubles().unwrap() == other.doubles().unwrap(),
            TapeTagKind::ByteArrayList => {
                self.byte_arrays().unwrap() == other.byte_arrays().unwrap()
            }
            TapeTagKind::StringList => self.strings().unwrap() == other.strings().unwrap(),
            TapeTagKind::ListList => self.lists().unwrap() == other.lists().unwrap(),
            TapeTagKind::CompoundList => self.compounds().unwrap() == other.compounds().unwrap(),
            TapeTagKind::IntArrayList => self.int_arrays().unwrap() == other.int_arrays().unwrap(),
            TapeTagKind::LongArrayList => {
                self.long_arrays().unwrap() == other.long_arrays().unwrap()
            }
            _ => unreachable!("this is an NbtList, no other kinds should be possible"),
        }
    }
}
impl Debug for NbtList<'_, '_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.to_owned().fmt(f)
    }
}

/// A wrapper over [`NbtListListIter`] that acts more like a Vec.
#[derive(Clone, Default)]
pub struct NbtListList<'a, 'tape> {
    iter: NbtListListIter<'a, 'tape>,
}
impl<'a, 'tape> NbtListList<'a, 'tape> {
    /// Returns the number of tags directly in this list.
    ///
    /// Note that due to an internal optimization, this function runs at `O(n)`
    /// if the list has at least 2^24 items. Use [`Self::approx_len`] if you
    /// want to avoid that.
    pub fn len(&self) -> usize {
        self.iter.len()
    }
    /// A version of [`Self::len`] that saturates at 2^24.
    pub fn approx_len(&self) -> u32 {
        self.iter.approx_len()
    }
    /// Get the element at the given index. This is O(n) where n is index, so if
    /// you'll be calling this more than once you should probably just use
    /// the iterator.
    pub fn get(&self, index: usize) -> Option<NbtList<'a, 'tape>> {
        self.iter.clone().nth(index)
    }
    pub fn first(&self) -> Option<NbtList<'a, 'tape>> {
        self.iter.clone().next()
    }
    pub fn last(&self) -> Option<NbtList<'a, 'tape>> {
        self.iter.clone().last()
    }

    pub fn is_empty(&self) -> bool {
        self.approx_len() == 0
    }
}
impl<'a: 'tape, 'tape> IntoIterator for NbtListList<'a, 'tape> {
    type Item = NbtList<'a, 'tape>;
    type IntoIter = NbtListListIter<'a, 'tape>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter
    }
}
impl PartialEq for NbtListList<'_, '_> {
    fn eq(&self, other: &Self) -> bool {
        if self.iter.clone().approx_len() != other.iter.clone().approx_len() {
            return false;
        }
        if self.iter.clone().len() != other.iter.clone().len() {
            return false;
        }
        self.iter
            .clone()
            .zip(other.iter.clone())
            .all(|(a, b)| a == b)
    }
}
/// An iterator over a list of lists.
#[derive(Clone)]
pub struct NbtListListIter<'a, 'tape> {
    current_tape_offset: usize,
    max_tape_offset: usize,
    approx_length: u32,
    tape: *const TapeElement,
    extra_tapes: *const ExtraTapes<'a>,
    _phantom: PhantomData<&'tape ()>,
}
impl<'a: 'tape, 'tape> NbtListListIter<'a, 'tape> {
    /// Returns the number of tags directly in this list.
    ///
    /// Note that due to an internal optimization, this function runs at `O(n)`
    /// if the list has at least 2^24 items. Use [`Self::approx_len`] if you
    /// want to avoid that.
    pub fn len(&self) -> usize {
        let len = self.approx_len();
        if len < 2u32.pow(24) {
            len as usize
        } else {
            self.clone().count()
        }
    }

    /// A version of [`Self::len`] that saturates at 2^24.
    pub fn approx_len(&self) -> u32 {
        self.approx_length
    }

    pub fn is_empty(&self) -> bool {
        self.approx_len() == 0
    }
}
impl<'a: 'tape, 'tape> Iterator for NbtListListIter<'a, 'tape> {
    type Item = NbtList<'a, 'tape>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_tape_offset + 1 >= self.max_tape_offset {
            return None;
        }

        let el_ptr = unsafe { self.tape.add(self.current_tape_offset) };
        let el = unsafe { *el_ptr };
        debug_assert!(el.kind().is_list());

        let offset = if matches!(el.kind(), TapeTagKind::CompoundList | TapeTagKind::ListList) {
            el.u32() as usize
        } else {
            1
        };

        let nbt_list = NbtList {
            element: el_ptr,
            extra_tapes: unsafe { &*self.extra_tapes },
        };

        self.current_tape_offset += offset;
        Some(nbt_list)
    }
}
impl Default for NbtListListIter<'_, '_> {
    fn default() -> Self {
        NbtListListIter {
            current_tape_offset: 0,
            max_tape_offset: 0,
            approx_length: 0,
            tape: std::ptr::null(),
            // this won't ever get dereferenced because .next() will return immediately
            extra_tapes: std::ptr::null(),
            _phantom: PhantomData,
        }
    }
}

/// A wrapper over [`NbtCompoundListIter`] that acts more like a Vec.
#[derive(Clone, Default)]
pub struct NbtCompoundList<'a, 'tape> {
    iter: NbtCompoundListIter<'a, 'tape>,
}
impl<'a, 'tape> NbtCompoundList<'a, 'tape> {
    /// Returns the number of tags directly in this list.
    ///
    /// Note that due to an internal optimization, this function runs at `O(n)`
    /// if the list has at least 2^24 items. Use [`Self::approx_len`] if you
    /// want to avoid that.
    pub fn len(&self) -> usize {
        self.iter.len()
    }
    /// A version of [`Self::len`] that saturates at 2^24.
    pub fn approx_len(&self) -> u32 {
        self.iter.approx_len()
    }
    /// Get the element at the given index. This is `O(n)` where n is index, so
    /// if you'll be calling this more than once you should probably just use
    /// the iterator.
    pub fn get(&self, index: usize) -> Option<NbtCompound<'a, 'tape>> {
        self.iter.clone().nth(index)
    }
    pub fn first(&self) -> Option<NbtCompound<'a, 'tape>> {
        self.iter.clone().next()
    }
    pub fn last(&self) -> Option<NbtCompound<'a, 'tape>> {
        self.iter.clone().last()
    }

    pub fn is_empty(&self) -> bool {
        self.approx_len() == 0
    }
}
impl<'a: 'tape, 'tape> IntoIterator for NbtCompoundList<'a, 'tape> {
    type Item = NbtCompound<'a, 'tape>;
    type IntoIter = NbtCompoundListIter<'a, 'tape>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter
    }
}
impl PartialEq for NbtCompoundList<'_, '_> {
    fn eq(&self, other: &Self) -> bool {
        if self.iter.clone().approx_len() != other.iter.clone().approx_len() {
            return false;
        }
        if self.iter.clone().len() != other.iter.clone().len() {
            return false;
        }
        self.iter
            .clone()
            .zip(other.iter.clone())
            .all(|(a, b)| a == b)
    }
}

#[derive(Clone)]
pub struct NbtCompoundListIter<'a, 'tape> {
    current_tape_offset: usize,
    max_tape_offset: usize,
    approx_length: u32,
    tape: &'tape [TapeElement],
    extra_tapes: *const ExtraTapes<'a>,
}
impl<'a: 'tape, 'tape> NbtCompoundListIter<'a, 'tape> {
    /// Returns the number of tags directly in this list.
    ///
    /// Note that due to an internal optimization, this function runs at `O(n)`
    /// if the list has at least 2^24 items. Use [`Self::approx_len`] if you
    /// want to avoid that.
    pub fn len(&self) -> usize {
        let len = self.approx_len();
        if len < 2u32.pow(24) {
            len as usize
        } else {
            self.clone().count()
        }
    }

    /// A version of [`Self::len`] that saturates at 2^24.
    pub fn approx_len(&self) -> u32 {
        self.approx_length
    }

    pub fn is_empty(&self) -> bool {
        self.approx_len() == 0
    }
}
impl<'a: 'tape, 'tape> Iterator for NbtCompoundListIter<'a, 'tape> {
    type Item = NbtCompound<'a, 'tape>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_tape_offset + 1 >= self.max_tape_offset {
            return None;
        }

        let el_ptr = unsafe { self.tape.as_ptr().add(self.current_tape_offset) };
        let el = unsafe { *el_ptr };
        debug_assert_eq!(el.kind(), TapeTagKind::Compound);

        let offset = el.u32() as usize;

        let compound = NbtCompound {
            element: el_ptr,
            extra_tapes: unsafe { &*self.extra_tapes },
        };

        self.current_tape_offset += offset;
        Some(compound)
    }
}
impl Default for NbtCompoundListIter<'_, '_> {
    fn default() -> Self {
        NbtCompoundListIter {
            current_tape_offset: 0,
            max_tape_offset: 0,
            approx_length: 0,
            tape: &[],
            // this won't ever get dereferenced because .next() will return immediately
            extra_tapes: std::ptr::null(),
        }
    }
}

pub(crate) fn u32_prefixed_list_to_rawlist<'a, T>(
    expected_kind: TapeTagKind,
    el_ptr: *const TapeElement,
) -> Option<RawList<'a, T>>
where
    T: Copy + SwappableNumber,
{
    let el = unsafe { *el_ptr };
    if el.kind() != expected_kind {
        return None;
    }

    unsafe { u32_prefixed_list_to_rawlist_unchecked(el.ptr()) }
}

#[inline]
pub(crate) unsafe fn u32_prefixed_list_to_rawlist_unchecked<'a, T>(
    ptr: *const UnalignedU32,
) -> Option<RawList<'a, T>>
where
    T: Copy + SwappableNumber,
{
    // length is always a u32
    let length = unsafe { u32::from(*ptr) };
    #[cfg(target_endian = "little")]
    let length = length.swap_bytes();
    let length_in_bytes = length as usize * mem::size_of::<T>();
    let array_be = unsafe { std::slice::from_raw_parts(ptr.add(1) as *const u8, length_in_bytes) };
    Some(RawList::new(array_be))
}

pub(crate) fn u32_prefixed_list_to_vec<T>(
    expected_kind: TapeTagKind,
    element: *const TapeElement,
) -> Option<Vec<T>>
where
    T: Copy + SwappableNumber,
{
    u32_prefixed_list_to_rawlist(expected_kind, element).map(|rawlist| rawlist.to_vec())
}

#[inline]
pub fn read_list_in_list<'a>(
    data: &mut Reader<'a>,
    tapes: &mut Tapes<'a>,
    stack: &mut ParsingStack,
) -> Result<(), NonRootError> {
    let index_of_list_element = stack.peek().index;

    let remaining = stack.remaining_elements_in_list();

    if remaining == 0 {
        stack.pop();

        let index_after_end_element = tapes.main.len();
        unsafe {
            tapes
                .main
                .get_unchecked_mut(index_of_list_element as usize)
                .set_offset(index_after_end_element as u32 - index_of_list_element);
        };
        return Ok(());
    }

    stack.decrement_list_length();

    NbtList::read(data, tapes, stack)
}

#[inline]
pub(crate) fn read_compound_in_list<'a>(
    data: &mut Reader<'a>,
    tapes: &mut Tapes<'a>,
    stack: &mut ParsingStack,
) -> Result<(), NonRootError> {
    let index_of_list_element = stack.peek().index;

    let remaining = stack.remaining_elements_in_list();

    if remaining == 0 {
        stack.pop();

        let index_after_end_element = tapes.main.len();
        unsafe {
            tapes
                .main
                .get_unchecked_mut(index_of_list_element as usize)
                .set_offset(index_after_end_element as u32 - index_of_list_element);
        };
        return Ok(());
    }

    stack.decrement_list_length();

    NbtCompound::read(data, tapes, stack)
}