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
#![allow(missing_docs)]

//! Parse binary data
//!
//! The is module provides the basis for all font parsing in Allsorts. The parsing approach
//! is inspired by the paper,
//! [The next 700 data description languages](https://collaborate.princeton.edu/en/publications/the-next-700-data-description-languages) by Kathleen Fisher, Yitzhak Mandelbaum, David P. Walker.

use crate::binary::{I16Be, I32Be, I64Be, U16Be, U24Be, U32Be, I8, U8};
use crate::error::ParseError;
use crate::layout::{LayoutCache, LayoutTableType};
use crate::size;
use alloc::borrow::Cow;
use alloc::vec::Vec;
use alloc::boxed::Box;
use core::cmp;
use alloc::collections::btree_map::Entry;
use alloc::collections::BTreeMap;
use core::fmt;
use core::marker::PhantomData;
use alloc::rc::Rc;

#[derive(Debug, Copy, Clone)]
pub struct ReadEof {}

pub struct ReadBuf<'a> {
    data: Cow<'a, [u8]>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ReadScope<'a> {
    base: usize,
    data: &'a [u8],
}

pub struct ReadScopeOwned {
    base: usize,
    data: Box<[u8]>,
}

impl ReadScopeOwned {
    pub fn new<'a>(scope: ReadScope<'a>) -> ReadScopeOwned {
        ReadScopeOwned {
            base: scope.base,
            data: Box::from(scope.data),
        }
    }

    pub fn scope<'a>(&'a self) -> ReadScope<'a> {
        ReadScope {
            base: self.base,
            data: &self.data,
        }
    }
}

#[derive(Clone)]
pub struct ReadCtxt<'a> {
    scope: ReadScope<'a>,
    offset: usize,
}

pub struct ReadCache<T> {
    map: BTreeMap<usize, Rc<T>>,
}

pub trait ReadBinary<'a> {
    type HostType: Sized; // default = Self

    fn read(ctxt: &mut ReadCtxt<'a>) -> Result<Self::HostType, ParseError>;
}

pub trait ReadBinaryDep<'a> {
    type Args: Clone;
    type HostType: Sized; // default = Self

    fn read_dep(ctxt: &mut ReadCtxt<'a>, args: Self::Args) -> Result<Self::HostType, ParseError>;
}

pub trait ReadFixedSizeDep<'a>: ReadBinaryDep<'a> {
    /// The number of bytes consumed by `ReadBinaryDep::read`.
    fn size(args: Self::Args) -> usize;
}

/// Read will always succeed if sufficient bytes are available.
pub trait ReadUnchecked<'a> {
    type HostType: Sized; // default = Self

    /// The number of bytes consumed by `read_unchecked`.
    const SIZE: usize;

    /// Must read exactly `SIZE` bytes.
    /// Unsafe as it avoids prohibitively expensive per-byte bounds checking.
    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> Self::HostType;
}

pub trait ReadFrom<'a> {
    type ReadType: ReadUnchecked<'a>;
    fn from(value: <Self::ReadType as ReadUnchecked<'a>>::HostType) -> Self;
}

impl<'a, T> ReadUnchecked<'a> for T
where
    T: ReadFrom<'a>,
{
    type HostType = T;

    const SIZE: usize = T::ReadType::SIZE;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> Self::HostType {
        let t = T::ReadType::read_unchecked(ctxt);
        T::from(t)
    }
}

impl<'a, T> ReadBinary<'a> for T
where
    T: ReadUnchecked<'a>,
{
    type HostType = T::HostType;

    fn read(ctxt: &mut ReadCtxt<'a>) -> Result<Self::HostType, ParseError> {
        ctxt.check_avail(T::SIZE)?;
        Ok(unsafe { T::read_unchecked(ctxt) })
        // Safe because we have `SIZE` bytes available.
    }
}

impl<'a, T> ReadBinaryDep<'a> for T
where
    T: ReadBinary<'a>,
{
    type Args = ();
    type HostType = T::HostType;

    fn read_dep(ctxt: &mut ReadCtxt<'a>, (): Self::Args) -> Result<Self::HostType, ParseError> {
        T::read(ctxt)
    }
}

impl<'a, T> ReadFixedSizeDep<'a> for T
where
    T: ReadUnchecked<'a>,
{
    fn size((): ()) -> usize {
        T::SIZE
    }
}

pub trait CheckIndex {
    fn check_index(&self, index: usize) -> Result<(), ParseError>;
}

#[derive(Clone)]
pub struct ReadArray<'a, T: ReadFixedSizeDep<'a>> {
    scope: ReadScope<'a>,
    length: usize,
    args: T::Args,
}

pub struct ReadArrayIter<'a, T: ReadUnchecked<'a>> {
    ctxt: ReadCtxt<'a>,
    length: usize,
    phantom: PhantomData<T>,
}

pub struct ReadArrayDepIter<'a, 'b, T: ReadFixedSizeDep<'a>> {
    array: &'b ReadArray<'a, T>,
    index: usize,
}

#[derive(Clone)]
pub enum ReadArrayCow<'a, T>
where
    T: ReadUnchecked<'a>,
{
    Owned(Vec<T::HostType>),
    Borrowed(ReadArray<'a, T>),
}

pub struct ReadArrayCowIter<'a, 'b, T: ReadUnchecked<'a>> {
    array: &'b ReadArrayCow<'a, T>,
    index: usize,
}

impl<'a, T: ReadUnchecked<'a>> ReadArrayCow<'a, T> {
    pub fn len(&self) -> usize {
        match self {
            ReadArrayCow::Borrowed(array) => array.len(),
            ReadArrayCow::Owned(vec) => vec.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        match self {
            ReadArrayCow::Borrowed(array) => array.is_empty(),
            ReadArrayCow::Owned(vec) => vec.is_empty(),
        }
    }

    pub fn read_item(&self, index: usize) -> Result<T::HostType, ParseError>
    where
        T::HostType: Copy,
    {
        match self {
            ReadArrayCow::Borrowed(array) => array.read_item(index),
            ReadArrayCow::Owned(vec) => Ok(vec[index]),
        }
    }

    pub fn get_item(&self, index: usize) -> <T as ReadUnchecked<'a>>::HostType
    where
        T: ReadUnchecked<'a>,
        <T as ReadUnchecked<'a>>::HostType: Copy,
    {
        match self {
            ReadArrayCow::Borrowed(array) => array.get_item(index),
            ReadArrayCow::Owned(vec) => vec[index],
        }
    }

    // subarray and iter_res are not yet implemented

    pub fn iter<'b>(&'b self) -> ReadArrayCowIter<'a, 'b, T> {
        ReadArrayCowIter {
            array: &self,
            index: 0,
        }
    }
}

impl<'a, T: ReadUnchecked<'a>> CheckIndex for ReadArrayCow<'a, T> {
    fn check_index(&self, index: usize) -> Result<(), ParseError> {
        if index < self.len() {
            Ok(())
        } else {
            Err(ParseError::BadIndex)
        }
    }
}

impl<'a> ReadScope<'a> {
    pub fn new(data: &'a [u8]) -> ReadScope<'a> {
        let base = 0;
        ReadScope { base, data }
    }

    pub fn data(&self) -> &'a [u8] {
        self.data
    }

    pub fn offset(&self, offset: usize) -> ReadScope<'a> {
        let base = self.base + offset;
        let data = self.data.get(offset..).unwrap_or(&[]);
        ReadScope { base, data }
    }

    pub fn offset_length(&self, offset: usize, length: usize) -> Result<ReadScope<'a>, ParseError> {
        if offset < self.data.len() || length == 0 {
            let data = &self.data[offset..];
            if length <= data.len() {
                let base = self.base + offset;
                let data = &data[0..length];
                Ok(ReadScope { base, data })
            } else {
                Err(ParseError::BadEof)
            }
        } else {
            Err(ParseError::BadOffset)
        }
    }

    pub fn ctxt(&self) -> ReadCtxt<'a> {
        ReadCtxt::new(self.clone())
    }

    pub fn read<T: ReadBinaryDep<'a, Args = ()>>(&self) -> Result<T::HostType, ParseError> {
        self.ctxt().read::<T>()
    }

    pub fn read_dep<T: ReadBinaryDep<'a>>(&self, args: T::Args) -> Result<T::HostType, ParseError> {
        self.ctxt().read_dep::<T>(args)
    }

    pub fn read_cache<'b, T>(
        &self,
        cache: &mut ReadCache<T::HostType>,
    ) -> Result<Rc<T::HostType>, ParseError>
    where
        T: 'static + ReadBinaryDep<'a, Args = ()>,
    {
        match cache.map.entry(self.base) {
            Entry::Vacant(entry) => {
                let t = Rc::new(self.read::<T>()?);
                Ok(Rc::clone(entry.insert(t)))
            }
            Entry::Occupied(entry) => Ok(Rc::clone(entry.get())),
        }
    }

    pub fn read_cache_state<'b, T, Table>(
        &self,
        cache: &mut ReadCache<T::HostType>,
        state: LayoutCache<Table>,
    ) -> Result<Rc<T::HostType>, ParseError>
    where
        T: 'static + ReadBinaryDep<'a, Args = LayoutCache<Table>>,
        Table: LayoutTableType,
    {
        match cache.map.entry(self.base) {
            Entry::Vacant(entry) => {
                let t = Rc::new(self.read_dep::<T>(state)?);
                Ok(Rc::clone(entry.insert(t)))
            }
            Entry::Occupied(entry) => Ok(Rc::clone(entry.get())),
        }
    }
}

impl<T> ReadCache<T> {
    pub fn new() -> Self {
        let map = BTreeMap::new();
        ReadCache { map }
    }
}

impl<'a> ReadCtxt<'a> {
    /// ReadCtxt is constructed by calling `ReadScope::ctxt`.
    fn new(scope: ReadScope<'a>) -> ReadCtxt<'a> {
        ReadCtxt { scope, offset: 0 }
    }

    pub fn check(&self, cond: bool) -> Result<(), ParseError> {
        match cond {
            true => Ok(()),
            false => Err(ParseError::BadValue),
        }
    }

    /// Check a condition, returning `ParseError::BadVersion` if `false`.
    ///
    /// Intended for use in checking versions read from data. Example:
    ///
    /// ```
    /// use allsorts::binary::read::ReadScope;
    /// use allsorts::error::ParseError;
    ///
    /// let scope = ReadScope::new(&[0, 2]);
    /// let mut ctxt = scope.ctxt();
    /// let major_version = ctxt.read_u16be().expect("unable to read version");
    ///
    /// assert!(ctxt.check_version(major_version == 2).is_ok());
    /// assert_eq!(ctxt.check_version(major_version == 1), Err(ParseError::BadVersion));
    /// ```
    pub fn check_version(&self, cond: bool) -> Result<(), ParseError> {
        match cond {
            true => Ok(()),
            false => Err(ParseError::BadVersion),
        }
    }

    pub fn scope(&self) -> ReadScope<'a> {
        self.scope.offset(self.offset)
    }

    pub fn read<T: ReadBinaryDep<'a, Args = ()>>(&mut self) -> Result<T::HostType, ParseError> {
        T::read_dep(self, ())
    }

    pub fn read_dep<T: ReadBinaryDep<'a>>(
        &mut self,
        args: T::Args,
    ) -> Result<T::HostType, ParseError> {
        T::read_dep(self, args)
    }

    pub fn bytes_available(&self) -> bool {
        self.offset < self.scope.data.len()
    }

    fn check_avail(&self, length: usize) -> Result<(), ReadEof> {
        match self.offset.checked_add(length) {
            Some(endpos) if endpos <= self.scope.data.len() => Ok(()),
            _ => Err(ReadEof {}),
        }
    }

    unsafe fn read_unchecked_u8(&mut self) -> u8 {
        let byte = *self.scope.data.get_unchecked(self.offset);
        self.offset += 1;
        byte
    }

    unsafe fn read_unchecked_i8(&mut self) -> i8 {
        self.read_unchecked_u8() as i8
    }

    unsafe fn read_unchecked_u16be(&mut self) -> u16 {
        let hi = u16::from(*self.scope.data.get_unchecked(self.offset));
        let lo = u16::from(*self.scope.data.get_unchecked(self.offset + 1));
        self.offset += 2;
        (hi << 8) | lo
    }

    unsafe fn read_unchecked_i16be(&mut self) -> i16 {
        self.read_unchecked_u16be() as i16
    }

    unsafe fn read_unchecked_u24be(&mut self) -> u32 {
        let b0 = u32::from(*self.scope.data.get_unchecked(self.offset));
        let b1 = u32::from(*self.scope.data.get_unchecked(self.offset + 1));
        let b2 = u32::from(*self.scope.data.get_unchecked(self.offset + 2));
        self.offset += 3;
        (b0 << 16) | (b1 << 8) | b2
    }

    unsafe fn read_unchecked_u32be(&mut self) -> u32 {
        let b0 = u32::from(*self.scope.data.get_unchecked(self.offset));
        let b1 = u32::from(*self.scope.data.get_unchecked(self.offset + 1));
        let b2 = u32::from(*self.scope.data.get_unchecked(self.offset + 2));
        let b3 = u32::from(*self.scope.data.get_unchecked(self.offset + 3));
        self.offset += 4;
        (b0 << 24) | (b1 << 16) | (b2 << 8) | b3
    }

    unsafe fn read_unchecked_i32be(&mut self) -> i32 {
        self.read_unchecked_u32be() as i32
    }

    unsafe fn read_unchecked_u64be(&mut self) -> u64 {
        let hi = u64::from(self.read_unchecked_u32be());
        let lo = u64::from(self.read_unchecked_u32be());
        (hi << 32) | lo
    }

    unsafe fn read_unchecked_i64be(&mut self) -> i64 {
        self.read_unchecked_u64be() as i64
    }

    pub fn read_u8(&mut self) -> Result<u8, ReadEof> {
        self.check_avail(1)?;
        Ok(unsafe { self.read_unchecked_u8() })
        // Safe because we have 1 byte available.
    }

    pub fn read_i8(&mut self) -> Result<i8, ReadEof> {
        self.check_avail(1)?;
        Ok(unsafe { self.read_unchecked_i8() })
        // Safe because we have 1 byte available.
    }

    pub fn read_u16be(&mut self) -> Result<u16, ReadEof> {
        self.check_avail(2)?;
        Ok(unsafe { self.read_unchecked_u16be() })
        // Safe because we have 2 bytes available.
    }

    pub fn read_i16be(&mut self) -> Result<i16, ReadEof> {
        self.check_avail(2)?;
        Ok(unsafe { self.read_unchecked_i16be() })
        // Safe because we have 2 bytes available.
    }

    pub fn read_u32be(&mut self) -> Result<u32, ReadEof> {
        self.check_avail(4)?;
        Ok(unsafe { self.read_unchecked_u32be() })
        // Safe because we have 4 bytes available.
    }

    pub fn read_i32be(&mut self) -> Result<i32, ReadEof> {
        self.check_avail(4)?;
        Ok(unsafe { self.read_unchecked_i32be() })
        // Safe because we have 4 bytes available.
    }

    pub fn read_u64be(&mut self) -> Result<u64, ReadEof> {
        self.check_avail(8)?;
        Ok(unsafe { self.read_unchecked_u64be() })
        // Safe because we have 8 bytes available.
    }

    pub fn read_i64be(&mut self) -> Result<i64, ReadEof> {
        self.check_avail(8)?;
        Ok(unsafe { self.read_unchecked_i64be() })
        // Safe because we have 8 bytes available.
    }

    pub fn read_array<T: ReadUnchecked<'a>>(
        &mut self,
        length: usize,
    ) -> Result<ReadArray<'a, T>, ParseError> {
        let scope = self.read_scope(length * T::SIZE)?;
        let args = ();
        Ok(ReadArray {
            scope,
            length,
            args,
        })
    }

    pub fn read_array_upto_hack<T: ReadUnchecked<'a>>(
        &mut self,
        length: usize,
    ) -> Result<ReadArray<'a, T>, ParseError> {
        let start_pos = self.offset;
        let buf_size = self.scope.data.len();
        let avail_bytes = cmp::max(0, buf_size - start_pos);
        let max_length = avail_bytes / T::SIZE;
        let length = cmp::min(length, max_length);
        self.read_array(length)
    }

    /// Read up to and including the supplied nibble.
    pub fn read_until_nibble(&mut self, nibble: u8) -> Result<&'a [u8], ReadEof> {
        let end = self.scope.data[self.offset..]
            .iter()
            .position(|&b| (b >> 4) == nibble || (b & 0xF) == nibble)
            .ok_or(ReadEof {})?;
        self.read_slice(end + 1)
    }

    pub fn read_array_dep<T: ReadFixedSizeDep<'a>>(
        &mut self,
        length: usize,
        args: T::Args,
    ) -> Result<ReadArray<'a, T>, ParseError> {
        let scope = self.read_scope(length * T::size(args.clone()))?;
        Ok(ReadArray {
            scope,
            length,
            args: args.clone(),
        })
    }

    pub fn read_scope(&mut self, length: usize) -> Result<ReadScope<'a>, ReadEof> {
        if let Ok(scope) = self.scope.offset_length(self.offset, length) {
            self.offset += length;
            Ok(scope)
        } else {
            Err(ReadEof {})
        }
    }

    pub fn read_slice(&mut self, length: usize) -> Result<&'a [u8], ReadEof> {
        let scope = self.read_scope(length)?;
        Ok(scope.data)
    }
}

impl<'a> ReadBuf<'a> {
    pub fn scope(&'a self) -> ReadScope<'a> {
        ReadScope::new(&*self.data)
    }

    pub fn into_data(self) -> Cow<'a, [u8]> {
        self.data
    }
}

impl<'a> From<&'a [u8]> for ReadBuf<'a> {
    fn from(data: &'a [u8]) -> ReadBuf<'a> {
        ReadBuf {
            data: Cow::Borrowed(data),
        }
    }
}

impl<'a> From<Vec<u8>> for ReadBuf<'a> {
    fn from(data: Vec<u8>) -> ReadBuf<'a> {
        ReadBuf {
            data: Cow::Owned(data),
        }
    }
}

impl<'a, T: ReadFixedSizeDep<'a>> ReadArray<'a, T> {
    pub fn len(&self) -> usize {
        self.length
    }

    pub fn is_empty(&self) -> bool {
        self.length == 0
    }

    pub fn read_item(&self, index: usize) -> Result<T::HostType, ParseError> {
        if index < self.length {
            let size = T::size(self.args.clone());
            let offset = index * size;
            let scope = self.scope.offset_length(offset, size).unwrap();
            let mut ctxt = scope.ctxt();
            T::read_dep(&mut ctxt, self.args.clone())
        } else {
            panic!("ReadArray::read_item: index out of bounds");
        }
    }

    pub fn get_item(&self, index: usize) -> <T as ReadUnchecked<'a>>::HostType
    where
        T: ReadUnchecked<'a>,
    {
        if index < self.length {
            let offset = index * T::SIZE;
            let scope = self.scope.offset_length(offset, T::SIZE).unwrap();
            let mut ctxt = scope.ctxt();
            unsafe { T::read_unchecked(&mut ctxt) } // Safe because we have `SIZE` bytes available.
        } else {
            panic!("ReadArray::get_item: index out of bounds");
        }
    }

    pub fn subarray(&self, index: usize) -> Self {
        if index < self.length {
            let offset = index * T::size(self.args.clone());
            ReadArray {
                scope: self.scope.offset(offset),
                length: self.length - index,
                args: self.args.clone(),
            }
        } else {
            ReadArray {
                scope: ReadScope::new(&[]),
                length: 0,
                args: self.args.clone(),
            }
        }
    }

    pub fn to_vec(&self) -> Vec<<T as ReadUnchecked<'a>>::HostType>
    where
        T: ReadUnchecked<'a>,
    {
        let mut vec = Vec::with_capacity(self.length);
        for t in self.iter() {
            vec.push(t);
        }
        vec
    }

    pub fn read_to_vec(&self) -> Result<Vec<T::HostType>, ParseError> {
        let mut vec = Vec::with_capacity(self.length);
        for res in self.iter_res() {
            let t = res?;
            vec.push(t);
        }
        Ok(vec)
    }

    pub fn iter(&self) -> ReadArrayIter<'a, T>
    where
        T: ReadUnchecked<'a>,
    {
        ReadArrayIter {
            ctxt: self.scope.ctxt(),
            length: self.length,
            phantom: PhantomData,
        }
    }

    pub fn iter_res<'b>(&'b self) -> ReadArrayDepIter<'a, 'b, T> {
        ReadArrayDepIter {
            array: &self,
            index: 0,
        }
    }
}

impl<'a, T: ReadFixedSizeDep<'a>> CheckIndex for ReadArray<'a, T> {
    fn check_index(&self, index: usize) -> Result<(), ParseError> {
        if index < self.len() {
            Ok(())
        } else {
            Err(ParseError::BadIndex)
        }
    }
}

impl<T> CheckIndex for Vec<T> {
    fn check_index(&self, index: usize) -> Result<(), ParseError> {
        if index < self.len() {
            Ok(())
        } else {
            Err(ParseError::BadIndex)
        }
    }
}

impl<'a, 'b, T: ReadUnchecked<'a>> IntoIterator for &'b ReadArray<'a, T> {
    type Item = T::HostType;
    type IntoIter = ReadArrayIter<'a, T>;
    fn into_iter(self) -> ReadArrayIter<'a, T> {
        self.iter()
    }
}

impl<'a, T: ReadUnchecked<'a>> Iterator for ReadArrayIter<'a, T> {
    type Item = T::HostType;

    fn next(&mut self) -> Option<T::HostType> {
        if self.length > 0 {
            self.length -= 1;
            Some(unsafe { T::read_unchecked(&mut self.ctxt) })
        // Safe because we have (at least) `SIZE` bytes available.
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.length, Some(self.length))
    }
}

impl<'a, 'b, T: ReadUnchecked<'a>> IntoIterator for &'b ReadArrayCow<'a, T>
where
    T::HostType: Copy,
{
    type Item = T::HostType;
    type IntoIter = ReadArrayCowIter<'a, 'b, T>;

    fn into_iter(self) -> ReadArrayCowIter<'a, 'b, T> {
        self.iter()
    }
}

impl<'a, 'b, T: ReadUnchecked<'a>> Iterator for ReadArrayCowIter<'a, 'b, T>
where
    T::HostType: Copy,
{
    type Item = T::HostType;

    fn next(&mut self) -> Option<T::HostType> {
        if self.index < self.array.len() {
            let item = self.array.get_item(self.index);
            self.index += 1;
            Some(item)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.index < self.array.len() {
            let length = self.array.len() - self.index;
            (length, Some(length))
        } else {
            (0, Some(0))
        }
    }
}

impl<'a, 'b, T: ReadFixedSizeDep<'a>> Iterator for ReadArrayDepIter<'a, 'b, T> {
    type Item = Result<T::HostType, ParseError>;

    fn next(&mut self) -> Option<Result<T::HostType, ParseError>> {
        if self.index < self.array.len() {
            let result = self.array.read_item(self.index);
            self.index += 1;
            Some(result)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.index < self.array.len() {
            let length = self.array.len() - self.index;
            (length, Some(length))
        } else {
            (0, Some(0))
        }
    }
}

impl<'a, T: ReadUnchecked<'a>> ReadArray<'a, T> {
    pub fn empty() -> ReadArray<'a, T> {
        ReadArray {
            scope: ReadScope::new(&[]),
            length: 0,
            args: (),
        }
    }
}

impl<'a> ReadUnchecked<'a> for U8 {
    type HostType = u8;

    const SIZE: usize = size::U8;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> u8 {
        ctxt.read_unchecked_u8()
    }
}

impl<'a> ReadUnchecked<'a> for I8 {
    type HostType = i8;

    const SIZE: usize = size::I8;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> i8 {
        ctxt.read_unchecked_i8()
    }
}

impl<'a> ReadUnchecked<'a> for U16Be {
    type HostType = u16;

    const SIZE: usize = size::U16;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> u16 {
        ctxt.read_unchecked_u16be()
    }
}

impl<'a> ReadUnchecked<'a> for I16Be {
    type HostType = i16;

    const SIZE: usize = size::I16;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> i16 {
        ctxt.read_unchecked_i16be()
    }
}

impl<'a> ReadUnchecked<'a> for U24Be {
    type HostType = u32;

    const SIZE: usize = size::U24;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> u32 {
        ctxt.read_unchecked_u24be()
    }
}

impl<'a> ReadUnchecked<'a> for U32Be {
    type HostType = u32;

    const SIZE: usize = size::U32;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> u32 {
        ctxt.read_unchecked_u32be()
    }
}

impl<'a> ReadUnchecked<'a> for I32Be {
    type HostType = i32;

    const SIZE: usize = size::I32;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> i32 {
        ctxt.read_unchecked_i32be()
    }
}

impl<'a> ReadUnchecked<'a> for I64Be {
    type HostType = i64;

    const SIZE: usize = size::I64;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> i64 {
        ctxt.read_unchecked_i64be()
    }
}

impl<'a, T1, T2> ReadUnchecked<'a> for (T1, T2)
where
    T1: ReadUnchecked<'a>,
    T2: ReadUnchecked<'a>,
{
    type HostType = (T1::HostType, T2::HostType);

    const SIZE: usize = T1::SIZE + T2::SIZE;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> Self::HostType {
        let t1 = T1::read_unchecked(ctxt);
        let t2 = T2::read_unchecked(ctxt);
        (t1, t2)
    }
}

impl<'a, T1, T2, T3> ReadUnchecked<'a> for (T1, T2, T3)
where
    T1: ReadUnchecked<'a>,
    T2: ReadUnchecked<'a>,
    T3: ReadUnchecked<'a>,
{
    type HostType = (T1::HostType, T2::HostType, T3::HostType);

    const SIZE: usize = T1::SIZE + T2::SIZE + T3::SIZE;

    unsafe fn read_unchecked(ctxt: &mut ReadCtxt<'a>) -> Self::HostType {
        let t1 = T1::read_unchecked(ctxt);
        let t2 = T2::read_unchecked(ctxt);
        let t3 = T3::read_unchecked(ctxt);
        (t1, t2, t3)
    }
}

impl<'a, T> fmt::Debug for ReadArrayCow<'a, T>
where
    T: ReadUnchecked<'a>,
    <T as ReadUnchecked<'a>>::HostType: Copy + fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        f.debug_list().entries(self.iter()).finish()
    }
}

impl<'a, T> fmt::Debug for ReadArray<'a, T>
where
    T: ReadUnchecked<'a>,
    <T as ReadUnchecked<'a>>::HostType: Copy + fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        f.debug_list().entries(self.iter()).finish()
    }
}

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

    #[test]
    fn test_read_u24be() {
        let scope = ReadScope::new(&[1, 2, 3]);
        assert_eq!(scope.read::<U24Be>().unwrap(), 0x10203);
    }
}