binary_serde 1.0.24

simple binary serialization and deserialization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
//! this is a crate which allows serializing and deserializing rust structs into a packed binary format.
//!
//! the format does exactly what you expect it to do, it just serializes all fields in order,
//! according to their representation in memory, and according to the chosen endianness.
//!
//! this is very useful for parsing many common binary formats which often just represent fields in a packed binary representation,
//! just like the format used by this crate.
//!
//! additionally, this crate is very `no_std` friendly and allows writing highly performant code because it allows for knowing
//! the serialized size of a type as a compile time constant, which means that the type can be serialized into a buffer on
//! the stack whose size is known at compile time, requiring no heap allocations.
//!
//! please note that this means that dynamically sized types like `&[T]`, `Vec<T>` and `String` are not supported.
//!
//! ### bitfields
//!
//! this crate also supports defining bitfields since those seem to be quite common in a lot of binary formats.
//! the bitfield definition allows the user to specify the bit length of each field of the struct.
//! the bitfields are defined using the `binary_serde_bitfield` attribute.
//! the order of the fields in a bitfield is treated as lsb first.
//! an example of a bitfield can be seen in the example below.
//!
//! ### std support
//!
//! this crate provides a feature flag called `std` which enables a bunch of std related features:
//! - the error types implement `std::error::Error`
//! - adds the `binary_serialize_into` and the `binary_deserialize_from` functions to the `BinarySerde` trait which allow
//!   serializing/deserializing to/from data streams (`std::io::Read`/`std::io::Write`).
//! - adds a bunch of convenience functions and structs which require `std` support.
//!
//! # Example
//! a simple example of serializing and deserializing an elf 32 bit relocation entry:
//! ```
//! use binary_serde::{binary_serde_bitfield, BinarySerde, Endianness};
//!
//! #[derive(Debug, BinarySerde, Default, PartialEq, Eq)]
//! #[repr(u8)]
//! enum Elf32RelocationType {
//!     #[default]
//!     Direct = 1,
//!     PcRelative = 2,
//!     GotEntry = 3,
//!     // ...
//! }
//!
//! #[derive(Debug, Default, PartialEq, Eq)]
//! #[binary_serde_bitfield(order = BitfieldBitOrder::LsbFirst)]
//! struct Elf32RelocationInfo {
//!     #[bits(8)]
//!     ty: Elf32RelocationType,
//!
//!     #[bits(24)]
//!     symbol_index: u32,
//! }
//!
//! #[derive(Debug, BinarySerde, Default, PartialEq, Eq)]
//! struct Elf32RelocationWithAddend {
//!     offset: u32,
//!     info: Elf32RelocationInfo,
//!     addend: u32,
//! }
//!
//! fn main() {
//!     let rel = Elf32RelocationWithAddend::default();
//!
//!     // serialize the relocation to a statically allocated array on the stack
//!     let bytes = rel.binary_serialize_to_array(Endianness::Little);
//!
//!     // deserialize the relocation from its bytes to get back the original value
//!     let reconstructed_rel =
//!         Elf32RelocationWithAddend::binary_deserialize(bytes.as_ref(), Endianness::Little).unwrap();
//!
//!     assert_eq!(rel, reconstructed_rel)
//! }
//! ```

#![cfg_attr(not(feature = "std"), no_std)]

use core::marker::PhantomData;

pub use binary_serde_macros::{binary_serde_bitfield, BinarySerde};
use recursive_array::{
    recursive_array_type_of_size, EmptyRecursiveArray, RecursiveArray, RecursiveArrayMultiplier,
    RecursiveArraySingleItem,
};

pub use recursive_array;
use thiserror_no_std::Error;

/// endianness.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Endianness {
    Big,
    Little,
}
impl Endianness {
    #[cfg(target_endian = "little")]
    pub const NATIVE: Self = Endianness::Little;
    #[cfg(target_endian = "big")]
    pub const NATIVE: Self = Endianness::Big;
}

/// the bit order of a bitfield.
pub enum BitfieldBitOrder {
    /// the bitfield is msb first, which means that the bits of the first field will be placed at the msb.
    MsbFirst,

    /// the bitfield is lsb first, which means that the bits of the first field will be placed at the lsb.
    LsbFirst,
}

/// a trait for serializing and deserializing a type into a packed binary format.
pub trait BinarySerde: Sized {
    /// the size of this type when serialized to a packed binary format.
    const SERIALIZED_SIZE: usize;

    /// the fixed size recursive array type that is returned when serializing this type to an array.
    /// the length of this array is guaranteed to be equal to [`Self::SERIALIZED_SIZE`].
    type RecursiveArray: RecursiveArray<u8>;

    /// serialize this value into the given buffer using the given endianness.
    ///
    /// # Panics
    ///
    /// this function panics if the length of `buf` is not exactly equal to [`Self::SERIALIZED_SIZE`].
    fn binary_serialize(&self, buf: &mut [u8], endianness: Endianness);

    /// serialize this value to a fixed size array using the given endianness.
    fn binary_serialize_to_array(&self, endianness: Endianness) -> Self::RecursiveArray {
        let mut array: core::mem::MaybeUninit<Self::RecursiveArray> =
            core::mem::MaybeUninit::uninit();
        self.binary_serialize(
            unsafe {
                core::slice::from_raw_parts_mut(
                    array.as_mut_ptr().cast::<u8>(),
                    Self::SERIALIZED_SIZE,
                )
            },
            endianness,
        );
        unsafe { array.assume_init() }
    }

    #[cfg(feature = "std")]
    /// serialize this value into the given stream using the given endianness.
    fn binary_serialize_into<W: std::io::Write>(
        &self,
        stream: &mut W,
        endianness: Endianness,
    ) -> std::io::Result<()> {
        let serialized = self.binary_serialize_to_array(endianness);
        stream.write_all(serialized.as_slice())?;
        Ok(())
    }

    /// deserializes the given buffer using the given endianness into a value of this type.
    ///
    /// # Errors
    ///
    /// this function return an error if the given bytes do not represent a valid value of this type.
    /// this can only ever happen if during deserialization we got an enum value that does not match any of the enum's variants.
    ///
    /// # Panics
    ///
    /// this function panics if the length of `buf` is not exactly equal to [`Self::SERIALIZED_SIZE`].
    fn binary_deserialize(buf: &[u8], endianness: Endianness) -> Result<Self, DeserializeError>;

    #[cfg(feature = "std")]
    /// deserializes the data from the given stream using the given endianness into a value of this type.
    fn binary_deserialize_from<R: std::io::Read>(
        stream: &mut R,
        endianness: Endianness,
    ) -> Result<Self, DeserializeFromError> {
        let mut uninit_array: core::mem::MaybeUninit<Self::RecursiveArray> =
            core::mem::MaybeUninit::uninit();
        stream.read_exact(unsafe {
            core::slice::from_raw_parts_mut(
                uninit_array.as_mut_ptr().cast::<u8>(),
                Self::SERIALIZED_SIZE,
            )
        })?;
        let array = unsafe { uninit_array.assume_init() };
        Ok(Self::binary_deserialize(array.as_slice(), endianness)?)
    }
}

/// an error which can occur while deserializing.
#[derive(Debug, Error)]
pub enum DeserializeError {
    #[error("invalid value for enum {enum_name}")]
    InvalidEnumValue { enum_name: &'static str },
}

#[cfg(feature = "std")]
/// an error which can occur while deserializing from a data stream.
#[derive(Debug, Error)]
pub enum DeserializeFromError {
    /// an io error has occured while reading from the stream.
    #[error("io error while reading from stream")]
    IoError(
        #[from]
        #[source]
        std::io::Error,
    ),

    /// a deserialization error occured while trying to deserialize the bytes that were read from the stream.
    #[error("deserialization error")]
    DeserializeError(
        #[from]
        #[source]
        DeserializeError,
    ),
}

/// an error which can occur while serializing/deserializing to/from a safe buffer serializer/deserializer.
#[derive(Debug, Error)]
pub enum BinarySerdeBufSafeError {
    /// a deserialization error occured while trying to deserialize the bytes that were read from the buffer.
    #[error("deserialization error")]
    DeserializeError(
        #[from]
        #[source]
        DeserializeError,
    ),

    /// the index is out of the bounds of the buffer
    #[error("index {index} is out of bounds of buffer of len {buf_len}")]
    OutOfBounds { index: usize, buf_len: usize },
}
/// binary serializes the given value using the given endianness into the given vector.
#[cfg(feature = "std")]
pub fn binary_serialize_into_vec<T: BinarySerde>(
    value: &T,
    endianness: Endianness,
    vec: &mut Vec<u8>,
) {
    vec.reserve(T::SERIALIZED_SIZE);
    value.binary_serialize(
        unsafe {
            core::slice::from_raw_parts_mut(vec.as_mut_ptr().add(vec.len()), T::SERIALIZED_SIZE)
        },
        endianness,
    );
    unsafe { vec.set_len(vec.len() + T::SERIALIZED_SIZE) }
}

/// a serializer which serializes value into a vector of bytes.
#[cfg(feature = "std")]
#[derive(Clone)]
pub struct BinarySerializerToVec {
    buf: Vec<u8>,
    endianness: Endianness,
}
#[cfg(feature = "std")]
impl BinarySerializerToVec {
    /// creates a new serializer with an empty buffer and with the given endianness.
    pub fn new(endianness: Endianness) -> Self {
        Self {
            buf: Vec::new(),
            endianness,
        }
    }

    /// creates a new serializer with the given initial buffer and endianness.
    pub fn new_with_buffer(initial_buffer: Vec<u8>, endianness: Endianness) -> Self {
        Self {
            buf: initial_buffer,
            endianness,
        }
    }

    /// serializes the given value into the buffer.
    pub fn serialize<T: BinarySerde>(&mut self, value: &T) {
        binary_serialize_into_vec(value, self.endianness, &mut self.buf)
    }

    /// returns a reference to the serialized data in the buffer.
    pub fn data(&self) -> &[u8] {
        &self.buf
    }

    /// returns a refernece to the underlying buffer of the serializer.
    pub fn buffer(&self) -> &Vec<u8> {
        &self.buf
    }

    /// returns a mutable refernece to the underlying buffer of the serializer.
    pub fn buffer_mut(&mut self) -> &mut Vec<u8> {
        &mut self.buf
    }

    /// consumes this serializer and returns its internal buffer.
    pub fn into_buffer(self) -> Vec<u8> {
        self.buf
    }

    /// sets the endianness of this serializer
    pub fn set_endianness(&mut self, new_endianness: Endianness) {
        self.endianness = new_endianness
    }

    /// returns the endianness of this serializer.
    pub fn endianness(&self) -> Endianness {
        self.endianness
    }
}

/// a serializer which serializes value into a data stream.
#[cfg(feature = "std")]
#[derive(Clone)]
pub struct BinarySerializerToStream<W: std::io::Write> {
    stream: W,
    endianness: Endianness,
}
#[cfg(feature = "std")]
impl<W: std::io::Write> BinarySerializerToStream<W> {
    /// creates a new serializer which serializes into the given stream using the given endianness.
    pub fn new(stream: W, endianness: Endianness) -> Self {
        Self { stream, endianness }
    }

    /// serializes the given value into the stream.
    pub fn serialize<T: BinarySerde>(&mut self, value: &T) -> std::io::Result<()> {
        value.binary_serialize_into(&mut self.stream, self.endianness)
    }

    /// consumes this serializer and returns its internal stream.
    pub fn into_stream(self) -> W {
        self.stream
    }

    /// returns a refernece to the underlying stream of this serializer
    pub fn stream(&self) -> &W {
        &self.stream
    }

    /// returns a mutable refernece to the underlying stream of this serializer
    pub fn stream_mut(&mut self) -> &mut W {
        &mut self.stream
    }

    /// sets the endianness of this serializer
    pub fn set_endianness(&mut self, new_endianness: Endianness) {
        self.endianness = new_endianness
    }

    /// returns the endianness of this serializer.
    pub fn endianness(&self) -> Endianness {
        self.endianness
    }
}

/// a deserializer which deserializes values from a buffer.
#[derive(Clone)]
pub struct BinaryDeserializerFromBuf<'a> {
    buf: &'a [u8],
    endianness: Endianness,
    position: usize,
}
impl<'a> BinaryDeserializerFromBuf<'a> {
    /// creates a new deserializer which deserializes values from the given buffer using the given endianness.
    pub fn new(buf: &'a [u8], endianness: Endianness) -> Self {
        Self {
            buf,
            endianness,
            position: 0,
        }
    }

    /// deserializes a value of type `T` from the current position in the buffer, and advances the position accordingly.
    ///
    /// # Panics
    ///
    /// this function panics if the deserialization exceeds the bounds of the buffer.
    pub fn deserialize<T: BinarySerde>(&mut self) -> Result<T, DeserializeError> {
        let result = T::binary_deserialize(
            &self.buf[self.position..][..T::SERIALIZED_SIZE],
            self.endianness,
        )?;
        self.position += T::SERIALIZED_SIZE;
        Ok(result)
    }

    /// returns the current position of this deserializer in the buffer.
    pub fn position(&self) -> usize {
        self.position
    }

    /// sets the position of this deserializer in the buffer.
    pub fn set_position(&mut self, new_position: usize) {
        self.position = new_position;
    }

    /// moves this deserializer's position forwards according to the given amount.
    pub fn move_forwards(&mut self, amount: usize) {
        self.position += amount;
    }

    /// moves this deserializer's position backwards according to the given amount.
    pub fn move_backwards(&mut self, amount: usize) {
        self.position -= amount;
    }

    /// sets the endianness of this deserializer
    pub fn set_endianness(&mut self, new_endianness: Endianness) {
        self.endianness = new_endianness
    }

    /// returns the endianness of this deserializer.
    pub fn endianness(&self) -> Endianness {
        self.endianness
    }

    /// returns a reference to the underlying buffer of this deserializer
    pub fn buf(&self) -> &'a [u8] {
        self.buf
    }
}

/// a binary serializer and deserializes which serializes and deserializes values to or from a buffer.
pub struct BinarySerdeBuf<'a> {
    buf: &'a mut [u8],
    endianness: Endianness,
    position: usize,
}
impl<'a> BinarySerdeBuf<'a> {
    /// creates a new binary serializer/deserializer which serializes/deserializer values to/from the given buffer using
    /// the given endianness.
    pub fn new(buf: &'a mut [u8], endianness: Endianness) -> Self {
        Self {
            buf,
            endianness,
            position: 0,
        }
    }

    /// serializes a value of type `T` into the current position in the buffer, and advances the position accordingly.
    ///
    /// # Panics
    ///
    /// this function panics if the serialization exceeds the bounds of the buffer.
    pub fn serialize<T: BinarySerde>(&mut self, value: &T) {
        value.binary_serialize(
            &mut self.buf[self.position..][..T::SERIALIZED_SIZE],
            self.endianness,
        );
        self.position += T::SERIALIZED_SIZE;
    }

    /// deserializes a value of type `T` from the current position in the buffer, and advances the position accordingly.
    ///
    /// # Panics
    ///
    /// this function panics if the deserialization exceeds the bounds of the buffer.
    pub fn deserialize<T: BinarySerde>(&mut self) -> Result<T, DeserializeError> {
        let result = T::binary_deserialize(
            &self.buf[self.position..][..T::SERIALIZED_SIZE],
            self.endianness,
        )?;
        self.position += T::SERIALIZED_SIZE;
        Ok(result)
    }
    /// returns the current position of this deserializer in the buffer.
    pub fn position(&self) -> usize {
        self.position
    }

    /// sets the position of this deserializer in the buffer.
    pub fn set_position(&mut self, new_position: usize) {
        self.position = new_position;
    }

    /// moves this deserializer's position forwards according to the given amount.
    pub fn move_forwards(&mut self, amount: usize) {
        self.position += amount;
    }

    /// moves this deserializer's position backwards according to the given amount.
    pub fn move_backwards(&mut self, amount: usize) {
        self.position -= amount;
    }

    /// sets the endianness of this serializer/deserializer
    pub fn set_endianness(&mut self, new_endianness: Endianness) {
        self.endianness = new_endianness
    }

    /// returns the endianness of this serializer/deserializer.
    pub fn endianness(&self) -> Endianness {
        self.endianness
    }

    /// returns a reference to the underlying buffer of this serializer/deserializer.
    pub fn buf(&mut self) -> &mut [u8] {
        self.buf
    }
}

/// a binary serializer/deserializer which serializes/deserializer values to a buffer and performs bounds checks on the buffer
/// when serializing/deserializing to avoid panics.
pub struct BinarySerdeBufSafe<'a> {
    buf: &'a mut [u8],
    endianness: Endianness,
    position: usize,
}
impl<'a> BinarySerdeBufSafe<'a> {
    /// creates a new binary serializer/deserializer which serializes/deserializer values to/from the given buffer using
    /// the given endianness.
    pub fn new(buf: &'a mut [u8], endianness: Endianness) -> Self {
        Self {
            buf,
            endianness,
            position: 0,
        }
    }

    /// checks that the given index is in range of the buffer, and if it is not, returns an error.
    fn check_index(&self, index: usize) -> Result<(), BinarySerdeBufSafeError> {
        if index < self.buf.len() {
            Ok(())
        } else {
            Err(BinarySerdeBufSafeError::OutOfBounds {
                index,
                buf_len: self.buf.len(),
            })
        }
    }

    /// serializes a value of type `T` into the current position in the buffer, and advances the position accordingly.
    pub fn serialize<T: BinarySerde>(&mut self, value: &T) -> Result<(), BinarySerdeBufSafeError> {
        // make sure that the start index is in range
        self.check_index(self.position)?;

        // if the end index is not the same as the start index, make sure that it is also in range
        if T::SERIALIZED_SIZE > 1 {
            self.check_index(self.position + T::SERIALIZED_SIZE - 1)?;
        }

        value.binary_serialize(
            &mut self.buf[self.position..][..T::SERIALIZED_SIZE],
            self.endianness,
        );
        self.position += T::SERIALIZED_SIZE;

        Ok(())
    }

    /// deserializes a value of type `T` from the current position in the buffer, and advances the position accordingly.
    pub fn deserialize<T: BinarySerde>(&mut self) -> Result<T, BinarySerdeBufSafeError> {
        // make sure that the start index is in range
        self.check_index(self.position)?;

        // if the end index is not the same as the start index, make sure that it is also in range
        if T::SERIALIZED_SIZE > 1 {
            self.check_index(self.position + T::SERIALIZED_SIZE - 1)?;
        }

        let result = T::binary_deserialize(
            &self.buf[self.position..][..T::SERIALIZED_SIZE],
            self.endianness,
        )?;
        self.position += T::SERIALIZED_SIZE;
        Ok(result)
    }
    /// returns the current position of this deserializer in the buffer.
    pub fn position(&self) -> usize {
        self.position
    }

    /// sets the position of this deserializer in the buffer.
    pub fn set_position(&mut self, new_position: usize) {
        self.position = new_position;
    }

    /// moves this deserializer's position forwards according to the given amount.
    pub fn move_forwards(&mut self, amount: usize) {
        self.position += amount;
    }

    /// moves this deserializer's position backwards according to the given amount.
    pub fn move_backwards(&mut self, amount: usize) {
        self.position -= amount;
    }

    /// sets the endianness of this serializer/deserializer
    pub fn set_endianness(&mut self, new_endianness: Endianness) {
        self.endianness = new_endianness
    }

    /// returns the endianness of this serializer/deserializer.
    pub fn endianness(&self) -> Endianness {
        self.endianness
    }

    /// returns a reference to the underlying buffer of this serializer/deserializer.
    pub fn buf(&mut self) -> &mut [u8] {
        self.buf
    }
}

/// a deserializer which deserializes values from a buffer and performs bounds checks on the buffer when deserializing
/// to avoid panics.
#[derive(Clone)]
pub struct BinaryDeserializerFromBufSafe<'a> {
    buf: &'a [u8],
    endianness: Endianness,
    position: usize,
}
impl<'a> BinaryDeserializerFromBufSafe<'a> {
    /// creates a new deserializer which deserializes values from the given buffer using the given endianness.
    pub fn new(buf: &'a [u8], endianness: Endianness) -> Self {
        Self {
            buf,
            endianness,
            position: 0,
        }
    }

    /// checks that the given index is in range of the buffer, and if it is not, returns an error.
    fn check_index(&self, index: usize) -> Result<(), BinarySerdeBufSafeError> {
        if index < self.buf.len() {
            Ok(())
        } else {
            Err(BinarySerdeBufSafeError::OutOfBounds {
                index,
                buf_len: self.buf.len(),
            })
        }
    }

    /// deserializes a value of type `T` from the current position in the buffer, and advances the position accordingly.
    pub fn deserialize<T: BinarySerde>(&mut self) -> Result<T, BinarySerdeBufSafeError> {
        // make sure that the start index is in range
        self.check_index(self.position)?;

        // if the end index is not the same as the start index, make sure that it is also in range
        if T::SERIALIZED_SIZE > 1 {
            self.check_index(self.position + T::SERIALIZED_SIZE - 1)?;
        }

        let result = T::binary_deserialize(
            &self.buf[self.position..][..T::SERIALIZED_SIZE],
            self.endianness,
        )?;
        self.position += T::SERIALIZED_SIZE;
        Ok(result)
    }

    /// returns the current position of this deserializer in the buffer.
    pub fn position(&self) -> usize {
        self.position
    }

    /// sets the position of this deserializer in the buffer.
    pub fn set_position(&mut self, new_position: usize) {
        self.position = new_position;
    }

    /// moves this deserializer's position forwards according to the given amount.
    pub fn move_forwards(&mut self, amount: usize) {
        self.position += amount;
    }

    /// moves this deserializer's position backwards according to the given amount.
    pub fn move_backwards(&mut self, amount: usize) {
        self.position -= amount;
    }

    /// sets the endianness of this deserializer
    pub fn set_endianness(&mut self, new_endianness: Endianness) {
        self.endianness = new_endianness
    }

    /// returns the endianness of this deserializer.
    pub fn endianness(&self) -> Endianness {
        self.endianness
    }

    /// returns a reference to the underlying buffer of this deserializer.
    pub fn buf(&self) -> &'a [u8] {
        self.buf
    }
}

/// a deserializer which deserializes values from a data stream.
#[cfg(feature = "std")]
#[derive(Clone)]
pub struct BinaryDeserializerFromStream<R: std::io::Read> {
    stream: R,
    endianness: Endianness,
}
#[cfg(feature = "std")]
impl<R: std::io::Read> BinaryDeserializerFromStream<R> {
    /// creates a new deserializer which deserializes values from the given data stream using the given endianness.
    pub fn new(stream: R, endianness: Endianness) -> Self {
        Self { stream, endianness }
    }

    /// deserializes a value of type `T` from the data stream.
    pub fn deserialize<T: BinarySerde>(&mut self) -> Result<T, DeserializeFromError> {
        T::binary_deserialize_from(&mut self.stream, self.endianness)
    }

    /// consumes this deserializer and returns its internal stream.
    pub fn into_stream(self) -> R {
        self.stream
    }

    /// returns a refernece to the underlying stream of this deserializer
    pub fn stream(&self) -> &R {
        &self.stream
    }

    /// returns a mutable refernece to the underlying stream of this deserializer
    pub fn stream_mut(&mut self) -> &mut R {
        &mut self.stream
    }

    /// sets the endianness of this deserializer
    pub fn set_endianness(&mut self, new_endianness: Endianness) {
        self.endianness = new_endianness
    }

    /// returns the endianness of this deserializer.
    pub fn endianness(&self) -> Endianness {
        self.endianness
    }
}

/// extracts the bits at the given range from the given byte.
fn get_bits_of_byte(byte: u8, start_bit_index: usize, bits_amount: usize) -> u8 {
    let mask = if bits_amount == 8 {
        u8::MAX
    } else {
        (1 << bits_amount) - 1
    };
    (byte >> start_bit_index) & mask
}

/// returns a bitmask which extracts the bits at the given range from a byte.
fn get_bits_mask(start_bit_index: usize, bits_amount: usize) -> u8 {
    if bits_amount == 8 {
        u8::MAX
    } else {
        let unshifted = (1 << bits_amount) - 1;
        unshifted << start_bit_index
    }
}

/// a bit reader which allows reading a byte slice as a sequence of bits in an lsb first format.
#[doc(hidden)]
#[derive(Clone)]
pub struct LsbBitReader<'a> {
    bytes: &'a [u8],
    bit_index_in_cur_byte: usize,
    endianness: Endianness,
    endianness_neutral_byte_index: usize,
}
impl<'a> LsbBitReader<'a> {
    /// creates a new bit reader which reads from the given byte array using the given endianness.
    pub fn new(bytes: &'a [u8], endianness: Endianness) -> Self {
        Self {
            bytes,
            bit_index_in_cur_byte: 0,
            endianness,
            endianness_neutral_byte_index: 0,
        }
    }

    /// returns the current byte index while taking into account the endianness.
    pub fn cur_byte_index(&self) -> usize {
        match self.endianness {
            Endianness::Big => self.bytes.len() - 1 - self.endianness_neutral_byte_index,
            Endianness::Little => self.endianness_neutral_byte_index,
        }
    }

    /// returns the amount of bits left to read from the current byte. this is a value between 1-8 (including both ends).
    pub fn bits_left_in_cur_byte(&self) -> usize {
        8 - self.bit_index_in_cur_byte
    }

    /// reads the given amount of bits from this bit reader and advances the reader by the given amount.
    /// the provided amount must be lower than or equal to the amount of bits left to read from the current byte, such
    /// that the read doesn't require crossing a byte boundary.
    pub fn read_bits(&mut self, bits_amount: usize) -> u8 {
        assert!(bits_amount <= self.bits_left_in_cur_byte());
        let cur_byte_index = self.cur_byte_index();
        let result = get_bits_of_byte(
            self.bytes[cur_byte_index],
            self.bit_index_in_cur_byte,
            bits_amount,
        );

        self.bit_index_in_cur_byte += bits_amount;
        if self.bit_index_in_cur_byte == 8 {
            self.endianness_neutral_byte_index += 1;
            self.bit_index_in_cur_byte = 0;
        }

        result
    }
}

/// a bit writer which allows writing bit sequences to a byte slice in an lsb first format.
#[doc(hidden)]
pub struct LsbBitWriter<'a> {
    bytes: &'a mut [u8],
    bit_index_in_cur_byte: usize,
    endianness: Endianness,
    endianness_neutral_byte_index: usize,
}
impl<'a> LsbBitWriter<'a> {
    /// creates a new bit writer which writes to the given byte array using the given endianness, starting at the given bit offset.
    pub fn new(bytes: &'a mut [u8], endianness: Endianness) -> Self {
        Self {
            bit_index_in_cur_byte: 0,
            endianness,
            endianness_neutral_byte_index: 0,
            bytes,
        }
    }

    /// returns the current byte index while taking into account the endianness.
    pub fn cur_byte_index(&self) -> usize {
        match self.endianness {
            Endianness::Big => self.bytes.len() - 1 - self.endianness_neutral_byte_index,
            Endianness::Little => self.endianness_neutral_byte_index,
        }
    }

    /// returns the amount of bits left to write to the current byte. this is a value between 1-8 (including both ends).
    pub fn bits_left_in_cur_byte(&self) -> usize {
        8 - self.bit_index_in_cur_byte
    }

    /// writes the given amount of bits from the given bits and advances the bit writer by the given amount.
    /// the provided amount must be lower than or equal to the amount of bits left to write from the current byte, such
    /// that the write doesn't require crossing a byte boundary.
    pub fn write_bits(&mut self, bits: u8, bits_amount: usize) {
        let cur_byte_index = self.cur_byte_index();
        let mask = get_bits_mask(self.bit_index_in_cur_byte, bits_amount);
        self.bytes[cur_byte_index] =
            (self.bytes[cur_byte_index] & !mask) | (bits << self.bit_index_in_cur_byte);

        self.bit_index_in_cur_byte += bits_amount;
        if self.bit_index_in_cur_byte == 8 {
            self.endianness_neutral_byte_index += 1;
            self.bit_index_in_cur_byte = 0;
        }
    }
}

/// copies the given amount of bits from the `from` reader to the `to` writer.
#[doc(hidden)]
pub fn _copy_bits<'a, 'b>(
    from: &mut LsbBitReader<'a>,
    to: &mut LsbBitWriter<'b>,
    bits_amount: usize,
) {
    let mut bits_left = bits_amount;
    while bits_left > 0 {
        // calculate the amount of bits to copy in the current iteration such that we don't cross byte boundaries both in the
        // reader and writer, and also take into account the amount of bits left to copy.
        let cur_amount = core::cmp::min(
            core::cmp::min(from.bits_left_in_cur_byte(), to.bits_left_in_cur_byte()),
            bits_left,
        );

        to.write_bits(from.read_bits(cur_amount), cur_amount);
        bits_left -= cur_amount;
    }
}

/// a struct used for adding padding in the middle of your serializable structs.
/// when serializing, it will write the `PADDING_VALUE` to the buffer `PADDING_LENGTH` times.
/// deserializing, it will do nothing.
#[derive(Debug, Clone, Copy, Hash)]
pub struct BinarySerdePadding<const PADDING_LENGTH: usize, const PADDING_VALUE: u8>;
impl<const PADDING_LENGTH: usize, const PADDING_VALUE: u8> BinarySerde
    for BinarySerdePadding<PADDING_LENGTH, PADDING_VALUE>
{
    const SERIALIZED_SIZE: usize = PADDING_LENGTH;

    type RecursiveArray = recursive_array_type_of_size!(u8, PADDING_LENGTH);

    fn binary_serialize(&self, buf: &mut [u8], _endianness: Endianness) {
        buf[..PADDING_LENGTH].fill(PADDING_VALUE);
    }

    fn binary_deserialize(_buf: &[u8], _endianness: Endianness) -> Result<Self, DeserializeError> {
        Ok(Self)
    }
}

/// implements the [`BinarySerde`] trait for a type generated using the `bitflags!` macro from the `bitflags` crate.
#[macro_export]
macro_rules! impl_binary_serde_for_bitflags_ty {
    {$bitflags_ty: ty} => {
        impl ::binary_serde::BinarySerde for $bitflags_ty
        where
            <$bitflags_ty as ::bitflags::Flags>::Bits: ::binary_serde::BinarySerde,
        {
            const SERIALIZED_SIZE: usize =
                <<$bitflags_ty as ::bitflags::Flags>::Bits as ::binary_serde::BinarySerde>::SERIALIZED_SIZE;

            type RecursiveArray =
                <<$bitflags_ty as ::bitflags::Flags>::Bits as ::binary_serde::BinarySerde>::RecursiveArray;

            fn binary_serialize(&self, buf: &mut [u8], endianness: ::binary_serde::Endianness) {
                let bits = ::bitflags::Flags::bits(self);
                ::binary_serde::BinarySerde::binary_serialize(&bits, buf, endianness)
            }

            fn binary_deserialize(
                buf: &[u8],
                endianness: ::binary_serde::Endianness,
            ) -> Result<Self, ::binary_serde::DeserializeError> {
                let bits = ::binary_serde::BinarySerde::binary_deserialize(buf, endianness)?;
                Ok(::bitflags::Flags::from_bits_retain(bits))
            }

            fn binary_serialize_to_array(
                &self,
                endianness: ::binary_serde::Endianness,
            ) -> Self::RecursiveArray {
                let bits = ::bitflags::Flags::bits(self);
                ::binary_serde::BinarySerde::binary_serialize_to_array(&bits, endianness)
            }
        }
    };
}

impl BinarySerde for u8 {
    const SERIALIZED_SIZE: usize = 1;

    type RecursiveArray = RecursiveArraySingleItem<u8>;

    fn binary_serialize(&self, buf: &mut [u8], _endianness: Endianness) {
        buf[0] = *self;
    }

    fn binary_serialize_to_array(&self, _endianness: Endianness) -> Self::RecursiveArray {
        RecursiveArraySingleItem::new(*self)
    }

    #[cfg(feature = "std")]
    fn binary_serialize_into<W: std::io::Write>(
        &self,
        stream: &mut W,
        _endianness: Endianness,
    ) -> std::io::Result<()> {
        stream.write_all(&[*self])
    }

    fn binary_deserialize(buf: &[u8], _endianness: Endianness) -> Result<Self, DeserializeError> {
        Ok(buf[0])
    }

    #[cfg(feature = "std")]
    fn binary_deserialize_from<R: std::io::Read>(
        stream: &mut R,
        _endianness: Endianness,
    ) -> Result<Self, DeserializeFromError> {
        let mut result: core::mem::MaybeUninit<Self> = core::mem::MaybeUninit::uninit();
        stream.read_exact(unsafe { core::slice::from_raw_parts_mut(result.as_mut_ptr(), 1) })?;
        Ok(unsafe { result.assume_init() })
    }
}

impl BinarySerde for i8 {
    const SERIALIZED_SIZE: usize = 1;

    type RecursiveArray = RecursiveArraySingleItem<u8>;

    fn binary_serialize(&self, buf: &mut [u8], _endianness: Endianness) {
        buf[0] = *self as u8;
    }

    fn binary_serialize_to_array(&self, _endianness: Endianness) -> Self::RecursiveArray {
        RecursiveArraySingleItem::new(*self as u8)
    }

    #[cfg(feature = "std")]
    fn binary_serialize_into<W: std::io::Write>(
        &self,
        stream: &mut W,
        _endianness: Endianness,
    ) -> std::io::Result<()> {
        stream.write_all(&[*self as u8])
    }

    fn binary_deserialize(buf: &[u8], _endianness: Endianness) -> Result<Self, DeserializeError> {
        Ok(buf[0] as i8)
    }

    #[cfg(feature = "std")]
    fn binary_deserialize_from<R: std::io::Read>(
        stream: &mut R,
        _endianness: Endianness,
    ) -> Result<Self, DeserializeFromError> {
        let mut result: core::mem::MaybeUninit<u8> = core::mem::MaybeUninit::uninit();
        stream.read_exact(unsafe { core::slice::from_raw_parts_mut(result.as_mut_ptr(), 1) })?;
        let byte = unsafe { result.assume_init() };
        Ok(byte as i8)
    }
}

impl BinarySerde for bool {
    const SERIALIZED_SIZE: usize = 1;

    type RecursiveArray = RecursiveArraySingleItem<u8>;

    fn binary_serialize(&self, buf: &mut [u8], _endianness: Endianness) {
        buf[0] = *self as u8;
    }

    fn binary_serialize_to_array(&self, _endianness: Endianness) -> Self::RecursiveArray {
        RecursiveArraySingleItem::new(*self as u8)
    }

    #[cfg(feature = "std")]
    fn binary_serialize_into<W: std::io::Write>(
        &self,
        stream: &mut W,
        _endianness: Endianness,
    ) -> std::io::Result<()> {
        stream.write_all(&[*self as u8])
    }

    fn binary_deserialize(buf: &[u8], _endianness: Endianness) -> Result<Self, DeserializeError> {
        Ok(buf[0] != 0)
    }

    #[cfg(feature = "std")]
    fn binary_deserialize_from<R: std::io::Read>(
        stream: &mut R,
        _endianness: Endianness,
    ) -> Result<Self, DeserializeFromError> {
        let mut result: core::mem::MaybeUninit<u8> = core::mem::MaybeUninit::uninit();
        stream.read_exact(unsafe { core::slice::from_raw_parts_mut(result.as_mut_ptr(), 1) })?;
        let byte = unsafe { result.assume_init() };
        Ok(byte != 0)
    }
}

impl<T> BinarySerde for PhantomData<T> {
    const SERIALIZED_SIZE: usize = 0;

    type RecursiveArray = EmptyRecursiveArray;

    fn binary_serialize(&self, _buf: &mut [u8], _endianness: Endianness) {}

    fn binary_serialize_to_array(&self, _endianness: Endianness) -> Self::RecursiveArray {
        EmptyRecursiveArray
    }

    #[cfg(feature = "std")]
    fn binary_serialize_into<W: std::io::Write>(
        &self,
        _stream: &mut W,
        _endianness: Endianness,
    ) -> std::io::Result<()> {
        Ok(())
    }

    fn binary_deserialize(_buf: &[u8], _endianness: Endianness) -> Result<Self, DeserializeError> {
        Ok(Self)
    }

    #[cfg(feature = "std")]
    fn binary_deserialize_from<R: std::io::Read>(
        _stream: &mut R,
        _endianness: Endianness,
    ) -> Result<Self, DeserializeFromError> {
        Ok(Self)
    }
}

impl BinarySerde for () {
    const SERIALIZED_SIZE: usize = 0;

    type RecursiveArray = EmptyRecursiveArray;

    fn binary_serialize(&self, _buf: &mut [u8], _endianness: Endianness) {}

    fn binary_serialize_to_array(&self, _endianness: Endianness) -> Self::RecursiveArray {
        EmptyRecursiveArray
    }

    #[cfg(feature = "std")]
    fn binary_serialize_into<W: std::io::Write>(
        &self,
        _stream: &mut W,
        _endianness: Endianness,
    ) -> std::io::Result<()> {
        Ok(())
    }

    fn binary_deserialize(_buf: &[u8], _endianness: Endianness) -> Result<Self, DeserializeError> {
        Ok(())
    }

    #[cfg(feature = "std")]
    fn binary_deserialize_from<R: std::io::Read>(
        _stream: &mut R,
        _endianness: Endianness,
    ) -> Result<Self, DeserializeFromError> {
        Ok(())
    }
}

impl<const N: usize, T: BinarySerde> BinarySerde for [T; N] {
    const SERIALIZED_SIZE: usize = T::SERIALIZED_SIZE * N;

    type RecursiveArray = RecursiveArrayMultiplier<N, u8, T::RecursiveArray>;

    fn binary_serialize(&self, buf: &mut [u8], endianness: Endianness) {
        /// an iterator which zips 2 iterators and makes sure that they are of the same length, and panics if they are not.
        struct ZipExact<A: Iterator, B: Iterator> {
            a: A,
            b: B,
        }
        impl<A: Iterator, B: Iterator> Iterator for ZipExact<A, B> {
            type Item = (A::Item, B::Item);

            fn next(&mut self) -> Option<Self::Item> {
                match (self.a.next(), self.b.next()) {
                    (Some(a), Some(b)) => Some((a, b)),
                    (None, None) => None,
                    _ => panic!("zipped iterators are of different lengths"),
                }
            }
        }
        /// zip 2 iterators into an iterator which yields a single item at a time from both iterators, and panics if the iterators
        /// are not of the same length.
        fn zip_exact<A: Iterator, B: Iterator>(a: A, b: B) -> ZipExact<A, B> {
            ZipExact { a, b }
        }

        for (item, item_buf) in zip_exact(self.iter(), buf.chunks_mut(T::SERIALIZED_SIZE)) {
            item.binary_serialize(item_buf, endianness)
        }
    }

    fn binary_deserialize(buf: &[u8], endianness: Endianness) -> Result<Self, DeserializeError> {
        array_init::try_array_init(|i| {
            T::binary_deserialize(
                &buf[i * T::SERIALIZED_SIZE..][..T::SERIALIZED_SIZE],
                endianness,
            )
        })
    }
}

macro_rules! impl_for_primitive_types {
    {$($type: ty),+} => {
        $(
            impl BinarySerde for $type {
                const SERIALIZED_SIZE: usize = core::mem::size_of::<Self>();
                type RecursiveArray = recursive_array_type_of_size!(u8, core::mem::size_of::<Self>());

                fn binary_serialize(&self, buf: &mut [u8], endianness: Endianness) {
                    let bytes = match endianness {
                        Endianness::Big => self.to_be_bytes(),
                        Endianness::Little => self.to_le_bytes(),
                    };
                    buf.copy_from_slice(bytes.as_slice());
                }
                fn binary_deserialize(buf: &[u8], endianness: Endianness) -> Result<Self, DeserializeError> {
                    let array = buf.try_into().unwrap();
                    Ok(match endianness {
                        Endianness::Big => Self::from_be_bytes(array),
                        Endianness::Little => Self::from_le_bytes(array),
                    })
                }
            }
        )+
    };
}

impl_for_primitive_types! {u16,u32,u64,u128,i16,i32,i64,i128,f32,f64}