chive 0.1.0

A low-level type-safe binary serialization library with support for numbers, arrays, strings, and nested sub-archives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
#[cfg(test)]
mod test;

use std::{fs, io, marker::PhantomData, path::Path};

/// Trait for a user-defined type that can be serialized and deserialized
/// into a Chive.
pub trait Chivable: Sized {
    /// Serialize self into the ChiveIn by adding all
    /// relevant members in a stable order
    fn chive_in(&self, chive_in: &mut ChiveIn);

    /// Create a new instance of Self by deserializing
    /// all relevant members in the same order they were
    /// serialized by Self::chive_in()
    fn chive_out(chive_out: &mut ChiveOut) -> Result<Self, ()>;
}

/// Default implementation for the unit type
impl Chivable for () {
    fn chive_in(&self, _chive_in: &mut ChiveIn) {
        // Nothing to do
    }

    fn chive_out(_chive_out: &mut ChiveOut) -> Result<Self, ()> {
        Ok(())
    }
}

/// Enum for the set of primitive fixed-size types that are supported
#[derive(PartialEq, Eq, Debug)]
pub enum PrimitiveType {
    Bool,
    U8,
    I8,
    U16,
    I16,
    U32,
    I32,
    U64,
    I64,
    F32,
    F64,
}

/// Enum for set the of value types that are supported
#[derive(PartialEq, Eq, Debug)]
pub enum ValueType {
    /// A fixed-size primitive, e.g. boolean, integer, or floating point number
    Primitive(PrimitiveType),

    /// A list of values of a common primitive type whose number of elements can be queried
    Array(PrimitiveType),

    /// A utf-8 encoded string
    String,

    /// A chive within. Useful for encapsulating and separating sections of the chive
    /// for different purposes.
    Nest,
}

impl PrimitiveType {
    /// Returns an integer with value 0xF or less, used to uniquely tag each primitive type
    fn to_nibble(&self) -> u8 {
        match self {
            PrimitiveType::Bool => 0x01,
            PrimitiveType::U8 => 0x02,
            PrimitiveType::I8 => 0x03,
            PrimitiveType::U16 => 0x04,
            PrimitiveType::I16 => 0x05,
            PrimitiveType::U32 => 0x06,
            PrimitiveType::I32 => 0x07,
            PrimitiveType::U64 => 0x08,
            PrimitiveType::I64 => 0x09,
            PrimitiveType::F32 => 0x0A,
            PrimitiveType::F64 => 0x0B,
        }
    }

    /// Constructs a PrimitiveType from an integer value as returned by to_nibble()
    fn from_nibble(byte: u8) -> Result<PrimitiveType, ()> {
        match byte {
            0x01 => Ok(PrimitiveType::Bool),
            0x02 => Ok(PrimitiveType::U8),
            0x03 => Ok(PrimitiveType::I8),
            0x04 => Ok(PrimitiveType::U16),
            0x05 => Ok(PrimitiveType::I16),
            0x06 => Ok(PrimitiveType::U32),
            0x07 => Ok(PrimitiveType::I32),
            0x08 => Ok(PrimitiveType::U64),
            0x09 => Ok(PrimitiveType::I64),
            0x0A => Ok(PrimitiveType::F32),
            0x0B => Ok(PrimitiveType::F64),
            _ => Err(()),
        }
    }
}

impl ValueType {
    /// Returns an integer used to uniquely tag each value type
    fn to_byte(&self) -> u8 {
        match self {
            ValueType::Primitive(prim_type) => 0x00 | prim_type.to_nibble(),
            ValueType::Array(prim_type) => 0x10 | prim_type.to_nibble(),
            ValueType::String => 0x20,
            ValueType::Nest => 0x30,
        }
    }

    /// Constructs a ValueType from an integer value as returned by to_byte()
    fn from_byte(byte: u8) -> Result<ValueType, ()> {
        let hi_nibble = byte & 0xF0;
        let lo_nibble = byte & 0x0F;
        match hi_nibble {
            0x00 => Ok(ValueType::Primitive(PrimitiveType::from_nibble(lo_nibble)?)),
            0x10 => Ok(ValueType::Array(PrimitiveType::from_nibble(lo_nibble)?)),
            0x20 => Ok(ValueType::String),
            0x30 => Ok(ValueType::Nest),
            _ => Err(()),
        }
    }
}

/// Helper trait for serializing primitives directly
trait PrimitiveReadWrite {
    /// The number of bytes occupied by the value itself in memory
    const SIZE: usize;

    /// The PrimitiveType that this type corresponds to, e.g. PrimitiveType::I32 for i32
    const TYPE: PrimitiveType;

    /// Write self to the byte vector
    fn write_to(&self, data: &mut Vec<u8>);

    /// Read self from the byte vector.
    /// This method may panic if there are fewer than Self::SIZE bytes in the vector.
    fn read_from(data: &mut ChiveOut) -> Self;
}

/// Macro for implementing the PrimitiveReadWrite helper trait for a given
/// Rust type, given its size in bytes and its corresponding PrimitiveType.
/// The methods `to_be_bytes()` and `from_be_bytes` are used, which exist
/// for all primitive integer and floating point types
macro_rules! impl_primitive_read_write {
    ($primitive: ident, $size: literal, $typetag: expr) => {
        impl PrimitiveReadWrite for $primitive {
            const SIZE: usize = $size;
            const TYPE: PrimitiveType = $typetag;
            fn write_to(&self, data: &mut Vec<u8>) {
                for b in self.to_be_bytes() {
                    data.push(b);
                }
            }
            fn read_from(d: &mut ChiveOut) -> Self {
                let mut bytes = Self::default().to_be_bytes();
                for b in &mut bytes {
                    *b = d.read_byte().unwrap();
                }
                Self::from_be_bytes(bytes)
            }
        }
    };
}

impl_primitive_read_write!(u8, 1, PrimitiveType::U8);
impl_primitive_read_write!(i8, 1, PrimitiveType::I8);
impl_primitive_read_write!(u16, 2, PrimitiveType::U16);
impl_primitive_read_write!(i16, 2, PrimitiveType::I16);
impl_primitive_read_write!(u32, 4, PrimitiveType::U32);
impl_primitive_read_write!(i32, 4, PrimitiveType::I32);
impl_primitive_read_write!(u64, 8, PrimitiveType::U64);
impl_primitive_read_write!(i64, 8, PrimitiveType::I64);
impl_primitive_read_write!(f32, 4, PrimitiveType::F32);
impl_primitive_read_write!(f64, 8, PrimitiveType::F64);

/// Explicit implementation of PrimitiveReadWrite for bool,
/// which does not have from_be_bytes() / to_be_bytes()
impl PrimitiveReadWrite for bool {
    const SIZE: usize = 1;
    const TYPE: PrimitiveType = PrimitiveType::Bool;

    fn write_to(&self, data: &mut Vec<u8>) {
        data.push(if *self { 1 } else { 0 });
    }

    fn read_from(d: &mut ChiveOut) -> bool {
        d.read_byte().unwrap() != 0
    }
}

/// An in-memory archive of serialized data, which is simply a flat sequence
/// of bytes that can be saved, sent, copied, loaded, and deserialized again
/// at a different time and place.
///
/// To serialize data structures, use the [Chive::with_chive_in] method and
/// then use the given [ChiveIn] object to write individual values and objects.
///
/// To deserialize data structures, use the [Chive::chive_out] method and then
/// use the returned [ChiveOut] object to read data in the same order it was
/// written during serialization.
pub struct Chive {
    /// The serialized data. This may have been loaded from an arbitrary file
    /// or created from an arbitrary vector, and so may not have a valid structure.
    /// Validation is performed during deserialization using a Result<> return
    /// type on each deserialization method.
    data: Vec<u8>,
}

/// Public methods
impl Chive {
    /// Create a new Chive instance by serializing data a user-provided function
    /// that receives a [ChiveIn] instance. The body of the function needs to
    /// write all relevant data to the [ChiveIn] object. It is (currently) not
    /// possible to write additional data afterwards.
    ///
    /// Returns a Chive instance containing a flattened representation of all
    /// data that was given to the [ChiveIn] object.
    pub fn with_chive_in<F: Fn(ChiveIn)>(f: F) -> Chive {
        let mut data = Vec::<u8>::new();

        data.push(0);
        data.push(0);
        data.push(0);
        data.push(0);

        let chive_in = ChiveIn::new(&mut data);
        f(chive_in);

        debug_assert!(data.len() >= 4);
        let len_bytes = (data.len() - 4) as u32;
        let [b0, b1, b2, b3] = len_bytes.to_be_bytes();

        data[0] = b0;
        data[1] = b1;
        data[2] = b2;
        data[3] = b3;

        Chive { data }
    }

    /// Get a [ChiveOut] instance to deserialize and retrieve individual values
    /// out of the raw binary data.
    pub fn chive_out<'a>(&'a self) -> Result<ChiveOut<'a>, ()> {
        if self.data.len() < 4 {
            return Err(());
        }
        let len =
            u32::from_be_bytes([self.data[0], self.data[1], self.data[2], self.data[3]]) as usize;
        let slice = &self.data[4..];
        if len != slice.len() {
            return Err(());
        }
        Ok(ChiveOut {
            data: slice,
            position: 0,
        })
    }

    /// Write the binary contents the chive to a file at the given path
    pub fn dump_to_file(&self, path: &Path) -> Result<(), io::Error> {
        // TODO: magic number?
        fs::write(path, &self.data)?;
        Ok(())
    }

    /// Read the file at the given path and load its binary data into
    /// a new Chive instance. No validation of the contents is performed.
    pub fn load_from_file(path: &Path) -> Result<Chive, io::Error> {
        // TODO: magic number?
        let data = fs::read(path)?;
        Ok(Chive { data })
    }

    /// Take the underlying vector of bytes
    pub fn into_vec(self) -> Vec<u8> {
        self.data
    }

    /// Construct a new Chive instance from a vector of bytes.
    /// No validation of the contents is performed.
    pub fn from_vec(data: Vec<u8>) -> Chive {
        Chive { data }
    }
}

/// ChiveIn is used to serialize user-provided data in to a [Chive] instance.
pub struct ChiveIn<'a> {
    /// Mutable reference to a vector of bytes which all serialized data
    /// will be written to
    data: &'a mut Vec<u8>,
}

/// Private methods
impl<'a> ChiveIn<'a> {
    /// Create a new ChiveIn instance that will deserialize the given bytes
    fn new(data: &'a mut Vec<u8>) -> ChiveIn<'a> {
        ChiveIn { data }
    }

    /// Helper method to write a primitive
    fn write_primitive<T: PrimitiveReadWrite>(&mut self, x: T) {
        self.data.reserve(u8::SIZE + T::SIZE);
        self.data.push(ValueType::Primitive(T::TYPE).to_byte());
        x.write_to(self.data);
    }

    /// Helper method to write a slice of primitives
    fn write_primitive_array_slice<T: PrimitiveReadWrite>(&mut self, x: &[T]) {
        self.data
            .reserve(u8::SIZE + u32::SIZE + (x.len() * T::SIZE));
        self.data.push(ValueType::Array(T::TYPE).to_byte());
        let len = x.len() as u32;
        len.write_to(self.data);
        for xi in x {
            xi.write_to(self.data);
        }
    }

    /// Helper method to write an iterator of primitives
    fn write_primitive_array_iter<I: Iterator>(&mut self, mut it: I)
    where
        I::Item: PrimitiveReadWrite,
    {
        self.data.push(ValueType::Array(I::Item::TYPE).to_byte());
        let array_start_index = self.data.len();
        let mut n_items: u32 = 0;
        n_items.write_to(self.data);
        while let Some(x) = it.next() {
            x.write_to(self.data);
            n_items += 1;
        }
        for (i, b) in n_items.to_be_bytes().iter().enumerate() {
            self.data[array_start_index + i] = *b;
        }
    }
}

/// Public methods
impl<'a> ChiveIn<'a> {
    /// Write a single u8 value
    pub fn u8(&mut self, x: u8) {
        self.write_primitive::<u8>(x);
    }

    /// Write a single i8 value
    pub fn i8(&mut self, x: i8) {
        self.write_primitive::<i8>(x);
    }

    /// Write a single u16 value
    pub fn u16(&mut self, x: u16) {
        self.write_primitive::<u16>(x);
    }

    /// Write a single i16 value
    pub fn i16(&mut self, x: i16) {
        self.write_primitive::<i16>(x);
    }

    /// Write a single u32 value
    pub fn u32(&mut self, x: u32) {
        self.write_primitive::<u32>(x);
    }

    /// Write a single i32 value
    pub fn i32(&mut self, x: i32) {
        self.write_primitive::<i32>(x);
    }

    /// Write a single u64 value
    pub fn u64(&mut self, x: u64) {
        self.write_primitive::<u64>(x);
    }

    /// Write a single i64 value
    pub fn i64(&mut self, x: i64) {
        self.write_primitive::<i64>(x);
    }

    /// Write a single f32 value
    pub fn f32(&mut self, x: f32) {
        self.write_primitive::<f32>(x);
    }

    /// Write a single f64 value
    pub fn f64(&mut self, x: f64) {
        self.write_primitive::<f64>(x);
    }

    /// Write an array of u8 values from a slice
    pub fn array_slice_u8(&mut self, x: &[u8]) {
        self.write_primitive_array_slice::<u8>(x);
    }

    /// Write an array of i8 values from a slice
    pub fn array_slice_i8(&mut self, x: &[i8]) {
        self.write_primitive_array_slice::<i8>(x);
    }

    /// Write an array of u16 values from a slice
    pub fn array_slice_u16(&mut self, x: &[u16]) {
        self.write_primitive_array_slice::<u16>(x);
    }

    /// Write an array of i16 values from a slice
    pub fn array_slice_i16(&mut self, x: &[i16]) {
        self.write_primitive_array_slice::<i16>(x);
    }

    /// Write an array of u32 values from a slice
    pub fn array_slice_u32(&mut self, x: &[u32]) {
        self.write_primitive_array_slice::<u32>(x);
    }

    /// Write an array of i32 values from a slice
    pub fn array_slice_i32(&mut self, x: &[i32]) {
        self.write_primitive_array_slice::<i32>(x);
    }

    /// Write an array of u64 values from a slice
    pub fn array_slice_u64(&mut self, x: &[u64]) {
        self.write_primitive_array_slice::<u64>(x);
    }

    /// Write an array of i64 values from a slice
    pub fn array_slice_i64(&mut self, x: &[i64]) {
        self.write_primitive_array_slice::<i64>(x);
    }

    /// Write an array of f32 values from a slice
    pub fn array_slice_f32(&mut self, x: &[f32]) {
        self.write_primitive_array_slice::<f32>(x);
    }

    /// Write an array of f64 values from a slice
    pub fn array_slice_f64(&mut self, x: &[f64]) {
        self.write_primitive_array_slice::<f64>(x);
    }

    /// Write an array of u8 values from an iterator
    pub fn array_iter_u8<I: Iterator<Item = u8>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write an array of i8 values from an iterator
    pub fn array_iter_i8<I: Iterator<Item = i8>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write an array of u16 values from an iterator
    pub fn array_iter_u16<I: Iterator<Item = u16>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write an array of i16 values from an iterator
    pub fn array_iter_i16<I: Iterator<Item = i16>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write an array of u32 values from an iterator
    pub fn array_iter_u32<I: Iterator<Item = u32>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write an array of i32 values from an iterator
    pub fn array_iter_i32<I: Iterator<Item = i32>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write an array of u64 values from an iterator
    pub fn array_iter_u64<I: Iterator<Item = u64>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write an array of i64 values from an iterator
    pub fn array_iter_i64<I: Iterator<Item = i64>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write an array of f32 values from an iterator
    pub fn array_iter_f32<I: Iterator<Item = f32>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write an array of f64 values from an iterator
    pub fn array_iter_f64<I: Iterator<Item = f64>>(&mut self, it: I) {
        self.write_primitive_array_iter(it);
    }

    /// Write a string
    pub fn string(&mut self, x: &str) {
        let bytes = x.as_bytes();
        self.data.reserve(u8::SIZE + u32::SIZE + bytes.len());
        self.data.push(ValueType::String.to_byte());
        let len = bytes.len() as u32;
        len.write_to(self.data);
        for b in bytes {
            self.data.push(*b);
        }
    }

    /// Write a nested sub-chive. This creates a contiguous section of bytes
    /// which is cleanly separated from all data before and after the nested
    /// sub-chive. This is useful for separating concerns and (de)serializing
    /// separate complex data structures without fear that deserializing one
    ///  may read off the end and into the start of the next.
    pub fn nest<F: FnOnce(ChiveIn)>(&mut self, f: F) {
        self.data.push(ValueType::Nest.to_byte());

        let prefix_index = self.data.len();

        self.data.push(0);
        self.data.push(0);
        self.data.push(0);
        self.data.push(0);

        let len_before = self.data.len();

        f(ChiveIn::new(self.data));

        let len_after = self.data.len();
        debug_assert!(len_after >= len_before);
        let len = (len_after - len_before) as u32;
        let [b0, b1, b2, b3] = len.to_be_bytes();

        self.data[prefix_index + 0] = b0;
        self.data[prefix_index + 1] = b1;
        self.data[prefix_index + 2] = b2;
        self.data[prefix_index + 3] = b3;
    }

    /// Write a user-provided object which implements Chivable
    pub fn chivable<T: Chivable>(&mut self, chivable: &T) {
        chivable.chive_in(self);
    }
}

/// Iterator for reading values out of a serialized array in a [Chive] instance
/// one value at a time.
pub struct ChiveOutIterator<'a, T> {
    /// A ChiveOut instance pointing to a slice of serialized data containing an array
    chive_out: ChiveOut<'a>,

    /// PhantomData used to bake the type parameter T into the iterator
    _phantom_data: PhantomData<T>,
}

impl<'a, T> ChiveOutIterator<'a, T> {
    /// Construct a new ChiveOutIterator. The chive_out instance is expected
    /// to point to an homogenous array of serialized primitive values.
    fn new(chive_out: ChiveOut<'a>) -> ChiveOutIterator<'a, T>
    where
        T: PrimitiveReadWrite,
    {
        debug_assert_eq!(chive_out.remaining_len() % T::SIZE, 0);
        ChiveOutIterator {
            chive_out,
            _phantom_data: PhantomData,
        }
    }
}

/// Iterator implementation for ChiveOutIterator
impl<'a, T: PrimitiveReadWrite> Iterator for ChiveOutIterator<'a, T> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        if self.chive_out.is_empty() {
            return None;
        }
        debug_assert!(self.chive_out.remaining_len() >= T::SIZE);
        Some(T::read_from(&mut self.chive_out))
    }
}

/// ExactSizeIterator implementation for ChiveOutIterator
impl<'a, T: PrimitiveReadWrite> ExactSizeIterator for ChiveOutIterator<'a, T> {
    fn len(&self) -> usize {
        debug_assert_eq!(self.chive_out.remaining_len() % T::SIZE, 0);
        self.chive_out.remaining_len() / T::SIZE
    }
}

/// ChiveOut is used to deserialize data out of an existing [Chive] instance.
pub struct ChiveOut<'a> {
    data: &'a [u8],
    position: usize,
}

/// Private methods
impl<'a> ChiveOut<'a> {
    /// Create a new ChiveOut instance for deserializing the given slice of bytes
    fn new(data: &'a [u8]) -> ChiveOut<'a> {
        ChiveOut { data, position: 0 }
    }

    /// Get the number of bytes that have yet to be read
    fn remaining_len(&self) -> usize {
        let l = self.data.len();
        debug_assert!(self.position <= l);
        return l - self.position;
    }

    /// Read the next byte and advance past it
    fn read_byte(&mut self) -> Result<u8, ()> {
        if self.position >= self.data.len() {
            Err(())
        } else {
            let b = self.data[self.position];
            self.position += 1;
            Ok(b)
        }
    }

    /// Read the next byte without advancing past it
    fn peek_byte(&self, offset: usize) -> Result<u8, ()> {
        if (self.position + offset) >= self.data.len() {
            Err(())
        } else {
            Ok(self.data[self.position + offset])
        }
    }

    /// Try to perform an operation, get its result, and
    /// rollback the position in the underlying byte vector
    /// if it failed.
    fn reset_on_error<T: 'a, F: FnOnce(&mut ChiveOut<'a>) -> Result<T, ()>>(
        &mut self,
        f: F,
    ) -> Result<T, ()> {
        let original_position = self.position;
        let result = f(self);
        if result.is_err() {
            self.position = original_position;
        }
        result
    }

    /// Read a single primitive, checking for its type tag first and then
    /// reading its value
    fn read_primitive<T: PrimitiveReadWrite + 'static>(&mut self) -> Result<T, ()> {
        self.reset_on_error(|d| {
            if d.remaining_len() < (u8::SIZE + T::SIZE) {
                return Err(());
            }
            let the_type = ValueType::from_byte(d.read_byte()?)?;
            if the_type != ValueType::Primitive(T::TYPE) {
                return Err(());
            }
            Ok(T::read_from(d))
        })
    }

    /// Read an array of primitives to a vector, checking for its tag type and length
    /// first and then reading its values
    fn read_primitive_array_slice<T: PrimitiveReadWrite + 'static>(
        &mut self,
    ) -> Result<Vec<T>, ()> {
        self.reset_on_error(|d| {
            if d.remaining_len() < (u8::SIZE + u32::SIZE) {
                return Err(());
            }
            let the_type = ValueType::from_byte(d.read_byte()?)?;
            if the_type != ValueType::Array(T::TYPE) {
                return Err(());
            }
            let len = u32::read_from(d) as usize;
            if d.remaining_len() < (len * T::SIZE) {
                return Err(());
            }
            Ok((0..len).map(|_| T::read_from(d)).collect())
        })
    }

    /// Create an iterator that visits all primitives in an array, first checking
    /// the tag type and length.
    fn read_primitive_array_iter<'b, T: PrimitiveReadWrite + 'static>(
        &'b mut self,
    ) -> Result<ChiveOutIterator<'b, T>, ()> {
        self.reset_on_error(|d| {
            if d.remaining_len() < (u8::SIZE + u32::SIZE) {
                return Err(());
            }
            let the_type = ValueType::from_byte(d.read_byte()?)?;
            if the_type != ValueType::Array(T::TYPE) {
                return Err(());
            }
            let len = u32::read_from(d) as usize;
            let byte_len = len * T::SIZE;
            if d.remaining_len() < byte_len {
                return Err(());
            }
            let d2 = ChiveOut::new(&d.data[d.position..d.position + byte_len]);
            d.position += byte_len;
            Ok(ChiveOutIterator::new(d2))
        })
    }
}

/// Public methods
impl<'a> ChiveOut<'a> {
    /// Read a single u8 value
    pub fn u8(&mut self) -> Result<u8, ()> {
        self.read_primitive::<u8>()
    }

    /// Read a single i8 value
    pub fn i8(&mut self) -> Result<i8, ()> {
        self.read_primitive::<i8>()
    }

    /// Read a single u16 value
    pub fn u16(&mut self) -> Result<u16, ()> {
        self.read_primitive::<u16>()
    }

    /// Read a single i16 value
    pub fn i16(&mut self) -> Result<i16, ()> {
        self.read_primitive::<i16>()
    }

    /// Read a single u32 value
    pub fn u32(&mut self) -> Result<u32, ()> {
        self.read_primitive::<u32>()
    }

    /// Read a single i32 value
    pub fn i32(&mut self) -> Result<i32, ()> {
        self.read_primitive::<i32>()
    }

    /// Read a single u64 value
    pub fn u64(&mut self) -> Result<u64, ()> {
        self.read_primitive::<u64>()
    }

    /// Read a single i64 value
    pub fn i64(&mut self) -> Result<i64, ()> {
        self.read_primitive::<i64>()
    }

    /// Read a single f32 value
    pub fn f32(&mut self) -> Result<f32, ()> {
        self.read_primitive::<f32>()
    }

    /// Read a single f64 value
    pub fn f64(&mut self) -> Result<f64, ()> {
        self.read_primitive::<f64>()
    }

    /// Read an array of u8 values into a Vec
    pub fn array_slice_u8(&mut self) -> Result<Vec<u8>, ()> {
        self.read_primitive_array_slice::<u8>()
    }

    /// Read an array of i8 values into a Vec
    pub fn array_slice_i8(&mut self) -> Result<Vec<i8>, ()> {
        self.read_primitive_array_slice::<i8>()
    }

    /// Read an array of u16 values into a Vec
    pub fn array_slice_u16(&mut self) -> Result<Vec<u16>, ()> {
        self.read_primitive_array_slice::<u16>()
    }

    /// Read an array of i16 values into a Vec
    pub fn array_slice_i16(&mut self) -> Result<Vec<i16>, ()> {
        self.read_primitive_array_slice::<i16>()
    }

    /// Read an array of u32 values into a Vec
    pub fn array_slice_u32(&mut self) -> Result<Vec<u32>, ()> {
        self.read_primitive_array_slice::<u32>()
    }

    /// Read an array of i32 values into a Vec
    pub fn array_slice_i32(&mut self) -> Result<Vec<i32>, ()> {
        self.read_primitive_array_slice::<i32>()
    }

    /// Read an array of u64 values into a Vec
    pub fn array_slice_u64(&mut self) -> Result<Vec<u64>, ()> {
        self.read_primitive_array_slice::<u64>()
    }

    /// Read an array of i64 values into a Vec
    pub fn array_slice_i64(&mut self) -> Result<Vec<i64>, ()> {
        self.read_primitive_array_slice::<i64>()
    }

    /// Read an array of f32 values into a Vec
    pub fn array_slice_f32(&mut self) -> Result<Vec<f32>, ()> {
        self.read_primitive_array_slice::<f32>()
    }

    /// Read an array of f64 values into a Vec
    pub fn array_slice_f64(&mut self) -> Result<Vec<f64>, ()> {
        self.read_primitive_array_slice::<f64>()
    }

    /// Read an array of u8 values into an iterator
    pub fn array_iter_u8<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, u8>, ()> {
        self.read_primitive_array_iter::<u8>()
    }

    /// Read an array of i8 values into an iterator
    pub fn array_iter_i8<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, i8>, ()> {
        self.read_primitive_array_iter::<i8>()
    }

    /// Read an array of u16 values into an iterator
    pub fn array_iter_u16<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, u16>, ()> {
        self.read_primitive_array_iter::<u16>()
    }

    /// Read an array of i16 values into an iterator
    pub fn array_iter_i16<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, i16>, ()> {
        self.read_primitive_array_iter::<i16>()
    }

    /// Read an array of u32 values into an iterator
    pub fn array_iter_u32<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, u32>, ()> {
        self.read_primitive_array_iter::<u32>()
    }

    /// Read an array of i32 values into an iterator
    pub fn array_iter_i32<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, i32>, ()> {
        self.read_primitive_array_iter::<i32>()
    }

    /// Read an array of u64 values into an iterator
    pub fn array_iter_u64<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, u64>, ()> {
        self.read_primitive_array_iter::<u64>()
    }

    /// Read an array of i64 values into an iterator
    pub fn array_iter_i64<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, i64>, ()> {
        self.read_primitive_array_iter::<i64>()
    }

    /// Read an array of f32 values into an iterator
    pub fn array_iter_f32<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, f32>, ()> {
        self.read_primitive_array_iter::<f32>()
    }

    /// Read an array of f64 values into an iterator
    pub fn array_iter_f64<'b>(&'b mut self) -> Result<ChiveOutIterator<'b, f64>, ()> {
        self.read_primitive_array_iter::<f64>()
    }

    /// Read a string
    pub fn string(&mut self) -> Result<String, ()> {
        if self.remaining_len() < (u8::SIZE + u32::SIZE) {
            return Err(());
        }
        let the_type = ValueType::from_byte(self.read_byte()?)?;
        if the_type != ValueType::String {
            return Err(());
        }
        let len = u32::read_from(self) as usize;
        if self.remaining_len() < len {
            return Err(());
        }
        let slice = &self.data[self.position..(self.position + len)];
        self.position += len;
        let str_slice = std::str::from_utf8(slice).map_err(|_| ())?;
        Ok(str_slice.to_string())
    }

    /// Read a nested sub-chive
    pub fn nest<'b>(&'b mut self) -> Result<ChiveOut<'b>, ()> {
        if self.remaining_len() < (u8::SIZE + u32::SIZE) {
            return Err(());
        }
        let the_type = ValueType::from_byte(self.read_byte()?)?;
        if the_type != ValueType::Nest {
            return Err(());
        }
        let len = u32::read_from(self) as usize;
        if self.remaining_len() < len {
            return Err(());
        }
        let nest_slice: &[u8] = &self.data[self.position..(self.position + len)];
        self.position += len;
        Ok(ChiveOut::new(nest_slice))
    }

    /// Read a user-provided object which implements Chivable.
    /// The type of the object is not checked, only the types
    /// of individual values.
    pub fn chivable<T: Chivable>(&mut self) -> Result<T, ()> {
        T::chive_out(self)
    }

    /// Read the type of the next value
    pub fn peek_type(&self) -> Result<ValueType, ()> {
        ValueType::from_byte(self.peek_byte(0)?)
    }

    /// If the next type is an array, string, or nested chive,
    /// get its length, in bytes
    pub fn peek_length_bytes(&self) -> Result<usize, ()> {
        let the_type = ValueType::from_byte(self.peek_byte(0)?)?;
        if let ValueType::Primitive(_) = the_type {
            return Err(());
        }
        Ok(u32::from_be_bytes([
            self.peek_byte(1)?,
            self.peek_byte(2)?,
            self.peek_byte(3)?,
            self.peek_byte(4)?,
        ]) as usize)
    }

    /// Returns true iff the chive contains no more data to read
    pub fn is_empty(&self) -> bool {
        return self.position == self.data.len();
    }
}