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
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::{
    collections::VecDeque,
    io::{Error, IoSlice},
};

use crate::interfaces::{Reader, Writer};

pub const ERR_EOB: &str = "No more bytes left to be read in buffer";
pub const ERR_EOM: &str = "Buffer is full, cannot write more bytes";
pub const ERR_VARINT_TOO_LONG: &str = "Varint is too long to be written to buffer";

macro_rules! can_read {
    ($self: ident, $size: expr) => {
        $self.buf.remaining() >= $size
    };
}

macro_rules! can_write {
    ($self: ident, $size: expr) => {
        $self.buf.remaining_mut() >= $size
    };
}

macro_rules! read_fn {
    ($name: ident, $typ: ident, $fn_name: ident, $byte_size: literal) => {
        #[inline]
        pub fn $name(&mut self) -> Result<$typ, std::io::Error> {
            if can_read!(self, $byte_size) {
                return Ok(self.buf.$fn_name());
            } else {
                return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
            }
        }
    };
}

macro_rules! write_fn {
    ($name: ident, $typ: ident, $fn_name: ident, $byte_size: literal) => {
        #[inline]
        pub fn $name(&mut self, num: $typ) -> Result<(), std::io::Error> {
            if can_write!(self, $byte_size) {
                self.buf.$fn_name(num);
                return Ok(());
            } else {
                return Err(Error::new(std::io::ErrorKind::OutOfMemory, ERR_EOM));
            }
        }
    };
}

/// ByteReader is a panic-free way to read bytes from the `byte::Buf` trait.
///
/// ## Example
/// ```rust
/// use binary_util::io::ByteReader;
///
/// fn main() {
///    let mut buf = ByteReader::from(&[0, 253, 255, 255, 255, 15][..]);
///    assert_eq!(buf.read_u8().unwrap(), 0);
///    assert_eq!(buf.read_var_i32().unwrap(), -2147483647);
/// }
/// ```
///
/// ## Peek Ahead
/// `ByteReader` also provides a utility `peek_ahead` function that allows you to
/// "peek ahead" at the next byte in the stream without advancing the stream.
///
/// Do not confuse this with any sort of "peek" function. This function does not
/// increment the read position of the stream, but rather copies the byte at the
/// specified position.
/// ```rust
/// use binary_util::io::ByteReader;
///
/// fn main() {
///    let mut buf = ByteReader::from(&[253, 255, 14, 255, 255, 15][..]);
///    if buf.peek_ahead(3).unwrap() != 255 {
///        // buffer is corrupted!
///    } else {
///        // read the varint
///        let num = buf.read_var_i32().unwrap();
///    }
/// }
/// ```
///
/// ## Reading a struct without `BinaryDecoder`
/// This is useful if you are trying to read a struct or optional type and validate the type before
/// reading the rest of the struct.
/// ```rust
/// use binary_util::io::ByteReader;
///
/// struct PingPacket {
///    pub id: u8,
///    pub time: u64,
///    pub ack_id: Option<i32>
/// }
///
/// fn main() {
///     let mut buf = ByteReader::from(&[0, 253, 255, 255, 255, 255, 255, 255, 255, 0][..]);
///
///     // Read the id
///     let id = buf.read_u8().unwrap();
///
///     if id == 0 {
///         // Read the time
///        let time = buf.read_u64().unwrap();
///        // read ack
///        if buf.read_bool().unwrap() {
///            let ack_id = buf.read_var_i32().unwrap();
///            let packet = PingPacket { id, time, ack_id: Some(ack_id) };
///        } else {
///            let packet = PingPacket { id, time, ack_id: None };
///        }
///    }
/// }
/// ```
#[derive(Debug, Clone)]
pub struct ByteReader {
    pub(crate) buf: Bytes,
}

impl From<ByteWriter> for ByteReader {
    fn from(writer: ByteWriter) -> Self {
        Self {
            buf: writer.buf.freeze(),
        }
    }
}

impl Into<Bytes> for ByteReader {
    fn into(self) -> Bytes {
        self.buf
    }
}

impl Into<Vec<u8>> for ByteReader {
    fn into(self) -> Vec<u8> {
        self.buf.to_vec()
    }
}

impl Into<VecDeque<u8>> for ByteReader {
    fn into(self) -> VecDeque<u8> {
        self.buf.to_vec().into()
    }
}

impl From<Bytes> for ByteReader {
    fn from(buf: Bytes) -> Self {
        Self { buf }
    }
}

impl From<Vec<u8>> for ByteReader {
    fn from(buf: Vec<u8>) -> Self {
        Self { buf: buf.into() }
    }
}

impl From<&[u8]> for ByteReader {
    fn from(buf: &[u8]) -> Self {
        Self {
            buf: Bytes::from(buf.to_vec()),
        }
    }
}

impl ByteReader {
    /// `ByteReader` also provides a utility `peek_ahead` function that allows you to
    /// "peek ahead" at the next byte in the stream without advancing the stream.
    ///
    /// Do not confuse this with any sort of "peek" function. This function does not
    /// increment the read position of the stream, but rather copies the byte at the
    /// specified position.
    /// ```rust
    /// use binary_util::io::ByteReader;
    ///
    /// fn main() {
    ///    let mut buf = ByteReader::from(&[253, 255, 14, 255, 255, 15][..]);
    ///    if buf.peek_ahead(3).unwrap() != 255 {
    ///        // buffer is corrupted, varints can never have a leading byte less than 255 if
    ///        // Their are bytes remaining!
    ///    } else {
    ///        // read the varint
    ///        let num = buf.read_var_i32().unwrap();
    ///    }
    /// }
    /// ```
    pub fn peek_ahead(&mut self, pos: usize) -> Result<u8, std::io::Error> {
        if can_read!(self, pos) {
            return Ok(self.buf.chunk()[pos]);
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    read_fn!(read_u8, u8, get_u8, 1);
    read_fn!(read_i8, i8, get_i8, 1);
    read_fn!(read_u16, u16, get_u16, 2);
    read_fn!(read_u16_le, u16, get_u16_le, 2);
    read_fn!(read_i16, i16, get_i16, 2);
    read_fn!(read_i16_le, i16, get_i16_le, 2);

    /// Reads a 3-byte unsigned integer from the stream.
    pub fn read_u24(&mut self) -> Result<u32, std::io::Error> {
        if can_read!(self, 3) {
            if let Ok(num) = self.read_uint(3) {
                return Ok(num as u32);
            } else {
                return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
            }
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    /// Reads a 3-byte unsigned integer from the stream in little endian.
    /// This is the same as `read_u24` but in little endian.
    pub fn read_u24_le(&mut self) -> Result<u32, std::io::Error> {
        if can_read!(self, 3) {
            if let Ok(num) = self.read_uint_le(3) {
                return Ok(num as u32);
            } else {
                return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
            }
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    pub fn read_i24(&mut self) -> Result<i32, std::io::Error> {
        if can_read!(self, 3) {
            if let Ok(num) = self.read_int(3) {
                return Ok(num as i32);
            } else {
                return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
            }
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    read_fn!(read_u32, u32, get_u32, 4);
    read_fn!(read_u32_le, u32, get_u32_le, 4);
    read_fn!(read_f32, f32, get_f32, 4);
    read_fn!(read_f32_le, f32, get_f32_le, 4);

    /// Reads a var-int 32-bit unsigned integer from the stream.
    /// This is a variable length integer that can be 1, 2, 3, or 4 bytes long.
    ///
    /// This function is recoverable, meaning that if the stream ends before the
    /// var-int is fully read, it will return an error, and will not consume the
    /// bytes that were read.
    #[inline]
    pub fn read_var_u32(&mut self) -> Result<u32, std::io::Error> {
        let mut num = 0u32;
        let mut interval = 0_usize;
        for i in (0..35).step_by(7) {
            let byte = self.peek_ahead(interval)?;

            num |= ((byte & 0x7F) as u32) << i;
            interval += 1;

            if byte & 0x80 == 0 {
                self.buf.advance(interval);
                return Ok(num);
            }
        }
        return Err(Error::new(
            std::io::ErrorKind::Other,
            "Varint overflow's 32-bit integer",
        ));
    }

    read_fn!(read_i32, i32, get_i32, 4);
    read_fn!(read_i32_le, i32, get_i32_le, 4);

    /// Reads a var-int 32-bit signed integer from the stream.
    /// This method is the same as `read_var_u32` but it will return a signed integer.
    pub fn read_var_i32(&mut self) -> Result<i32, std::io::Error> {
        let num = self.read_var_u32()?;

        Ok((num >> 1) as i32 ^ -((num & 1) as i32))
    }

    read_fn!(read_u64, u64, get_u64, 8);
    read_fn!(read_u64_le, u64, get_u64_le, 8);
    read_fn!(read_i64, i64, get_i64, 8);
    read_fn!(read_i64_le, i64, get_i64_le, 8);
    read_fn!(read_f64, f64, get_f64, 8);
    read_fn!(read_f64_le, f64, get_f64_le, 8);

    /// Reads a var-int 64-bit unsigned integer from the stream.
    /// This is a variable length integer that can be 1, 2, 3, 4, 5, 6, 7, or 8 bytes long.
    #[inline]
    pub fn read_var_u64(&mut self) -> Result<u64, std::io::Error> {
        let mut num = 0u64;
        let mut interval = 0_usize;
        for i in (0..70).step_by(7) {
            let byte = self.peek_ahead(interval)?;

            num |= ((byte & 0x7F) as u64) << i;
            interval += 1;

            if byte & 0x80 == 0 {
                self.buf.advance(interval);
                return Ok(num);
            }
        }
        return Err(Error::new(
            std::io::ErrorKind::Other,
            "Varint overflow's 64-bit integer",
        ));
    }

    /// Reads a var-int 64-bit signed integer from the stream.
    /// This method is the same as `read_var_u64` but it will return a signed integer.
    ///
    /// For more information on how this works, see `read_var_i32`.
    #[inline]
    pub fn read_var_i64(&mut self) -> Result<i64, std::io::Error> {
        let num = self.read_var_u64()?;
        Ok((num >> 1) as i64 ^ -((num & 1) as i64))
    }

    read_fn!(read_u128, u128, get_u128, 16);
    read_fn!(read_u128_le, u128, get_u128_le, 16);
    read_fn!(read_i128, i128, get_i128, 16);
    read_fn!(read_i128_le, i128, get_i128_le, 16);

    /// Reads an unsigned integer from the stream with a varying size
    /// indicated by the `size` parameter.
    pub fn read_uint(&mut self, size: usize) -> Result<u64, std::io::Error> {
        // todo: Check whether we should use `copy_nonoverlapping` or `self.get_uint`
        if can_read!(self, size) {
            let mut num = 0u64;
            let ptr_to = &mut num as *mut u64 as *mut u8;
            // we're not using for loops because they're slower
            unsafe {
                core::ptr::copy_nonoverlapping(self.buf.chunk().as_ptr(), ptr_to, size);
            }
            // increment the position
            self.buf.advance(size);
            return Ok(num.to_be());
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    /// Reads an unsigned integer from the stream with a varying size in little endian
    /// indicated by the `size` parameter.
    pub fn read_uint_le(&mut self, size: usize) -> Result<u64, std::io::Error> {
        if can_read!(self, size) {
            let mut num = 0u64;
            let ptr_to = &mut num as *mut u64 as *mut u8;
            unsafe {
                core::ptr::copy_nonoverlapping(self.buf.chunk().as_ptr(), ptr_to, size);
            }
            // increment the position
            self.buf.advance(size);
            return Ok(num.to_le());
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    pub fn read_int(&mut self, size: usize) -> Result<i64, std::io::Error> {
        if can_read!(self, size) {
            return Ok(self.buf.get_int(size));
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    pub fn read_int_le(&mut self, size: usize) -> Result<i64, std::io::Error> {
        if can_read!(self, size) {
            return Ok(self.buf.get_int_le(size));
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    pub fn read_char(&mut self) -> Result<char, std::io::Error> {
        let c = self.read_u32()?;

        if let Some(c) = char::from_u32(c) {
            return Ok(c);
        } else {
            return Err(Error::new(std::io::ErrorKind::InvalidData, "Invalid char"));
        }
    }

    pub fn read_bool(&mut self) -> Result<bool, std::io::Error> {
        if can_read!(self, 1) {
            return Ok(self.buf.get_u8() != 0);
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    /// Reads a string from the stream.
    /// This is a reversable operation, meaning if it fails,
    /// the stream will be in the same state as before.
    pub fn read_string(&mut self) -> Result<String, std::io::Error> {
        // todo: Make this reversable
        let len = self.read_var_u64()?;
        if can_read!(self, len as usize) {
            let mut string = String::with_capacity(len as usize);
            unsafe {
                let v = string.as_mut_vec();
                v.set_len(len as usize);
                self.buf.copy_to_slice(&mut v[..]);
            }
            return Ok(string);
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    /// Reads an `Option` of `T` from the stream.
    /// `T` must implement the `Reader` trait and be sized.
    ///
    /// This operation is not recoverable and will corrupt the stream if it fails.
    /// If this behavior is desired, you should use `peek_ahead` when implementing
    /// the `Reader` trait.
    ///
    /// # Example
    /// ```rust
    /// use binary_util::io::ByteReader;
    /// use binary_util::interfaces::Reader;
    ///
    /// pub struct HelloWorld {
    ///     pub magic: u32
    /// }
    ///
    /// impl Reader<HelloWorld> for HelloWorld {
    ///     fn read(reader: &mut ByteReader) -> Result<HelloWorld, std::io::Error> {
    ///         Ok(HelloWorld {
    ///             magic: reader.read_u32()?
    ///         })
    ///     }
    /// }
    ///
    /// fn main() {
    ///     // Nothing is here!
    ///     let mut reader = ByteReader::from(&[0x00][..]);
    ///     let hello_world = reader.read_option::<HelloWorld>().unwrap();
    ///     assert_eq!(hello_world.is_some(), false);
    /// }
    /// ```
    pub fn read_option<T: Reader<T>>(&mut self) -> Result<Option<T>, std::io::Error> {
        if self.read_bool()? {
            return Ok(Some(T::read(self)?));
        } else {
            return Ok(None);
        }
    }

    /// Reads a varu32 sized slice from the stream.
    /// For reading a slice of raw bytes, use `read` instead.
    pub fn read_sized_slice(&mut self) -> Result<Bytes, std::io::Error> {
        let len = self.read_var_u32()?;

        if can_read!(self, len as usize) {
            let b = self.buf.slice(..len as usize);
            self.buf.advance(len as usize);
            return Ok(b);
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    /// Reads a slice from the stream into the slice passed by the caller.
    /// For reading a prefixed sized slice, use `read_sized_slice` instead.
    pub fn read(&mut self, buffer: &mut [u8]) -> Result<(), std::io::Error> {
        if can_read!(self, buffer.len()) {
            self.buf.copy_to_slice(buffer);
            return Ok(());
        } else {
            return Err(Error::new(std::io::ErrorKind::UnexpectedEof, ERR_EOB));
        }
    }

    /// Returns the remaining bytes in the stream.
    pub fn as_slice(&self) -> &[u8] {
        self.buf.chunk()
    }
}

/// ByteWriter is a panic-free way to write bytes to a `BufMut` trait.
///
/// ## Example
/// A generic example of how to use the `ByteWriter` struct.
/// ```rust
/// use binary_util::io::ByteWriter;
/// use binary_util::io::ByteReader;
///
/// fn main() {
///    let mut writer = ByteWriter::new();
///    writer.write_string("Hello World!").unwrap();
///    writer.write_var_u32(65536).unwrap();
///    writer.write_u8(0).unwrap();
///
///    println!("Bytes: {:?}", writer.as_slice());
/// }
/// ```
///
/// `ByteWriter` also implements the `Into` trait to convert the `ByteWriter` into a `BytesMut` or `Bytes` structs.
/// ```rust
/// use binary_util::io::ByteWriter;
/// use binary_util::io::ByteReader;
///
/// fn main() {
///     let mut writer = ByteWriter::new();
///     writer.write_u8(1);
///     writer.write_u8(2);
///     writer.write_u8(3);
///
///     let mut reader: ByteReader = writer.into();
///     assert_eq!(reader.read_u8().unwrap(), 1);
///     assert_eq!(reader.read_u8().unwrap(), 2);
///     assert_eq!(reader.read_u8().unwrap(), 3);
/// }
/// ```
///
/// #### ByteWriter Implementation Notice
/// While most of the methods are reversable, some are not.
/// Meaning there is a chance that if you call a method in a edge case, it will corrupt the stream.
///
/// For example, `write_var_u32` is not reversable because we currently do not
/// allocate a buffer to store the bytes before writing them to the buffer.
/// While you should never encounter this issue, it is possible when you run out of memory.
/// This issue is marked as a todo, but is low priority.
#[derive(Debug, Clone)]
pub struct ByteWriter {
    pub(crate) buf: BytesMut,
}

impl Into<BytesMut> for ByteWriter {
    fn into(self) -> BytesMut {
        self.buf
    }
}

impl Into<Bytes> for ByteWriter {
    fn into(self) -> Bytes {
        self.buf.freeze()
    }
}

impl Into<Vec<u8>> for ByteWriter {
    fn into(self) -> Vec<u8> {
        self.buf.to_vec()
    }
}

impl Into<VecDeque<u8>> for ByteWriter {
    fn into(self) -> VecDeque<u8> {
        self.buf.to_vec().into()
    }
}

impl From<IoSlice<'_>> for ByteWriter {
    fn from(slice: IoSlice) -> Self {
        let mut buf = BytesMut::with_capacity(slice.len());
        buf.put_slice(&slice);
        return Self { buf };
    }
}

impl From<&[u8]> for ByteWriter {
    fn from(slice: &[u8]) -> Self {
        let mut buf = BytesMut::with_capacity(slice.len());
        buf.put_slice(slice);
        return Self { buf };
    }
}

impl From<ByteReader> for ByteWriter {
    fn from(reader: ByteReader) -> Self {
        Self {
            buf: reader.buf.chunk().into(),
        }
    }
}

impl ByteWriter {
    pub fn new() -> Self {
        return Self {
            buf: BytesMut::new(),
        };
    }

    write_fn!(write_u8, u8, put_u8, 1);
    write_fn!(write_i8, i8, put_i8, 1);
    write_fn!(write_u16, u16, put_u16, 2);
    write_fn!(write_u16_le, u16, put_u16_le, 2);
    write_fn!(write_i16, i16, put_i16, 2);
    write_fn!(write_i16_le, i16, put_i16_le, 2);

    pub fn write_u24(&mut self, num: u32) -> Result<(), std::io::Error> {
        return self.write_uint(num.into(), 3);
    }

    pub fn write_u24_le(&mut self, num: u32) -> Result<(), std::io::Error> {
        return self.write_uint_le(num.into(), 3);
    }

    pub fn write_i24(&mut self, num: i32) -> Result<(), std::io::Error> {
        return self.write_int(num.into(), 3);
    }

    pub fn write_i24_le(&mut self, num: i32) -> Result<(), std::io::Error> {
        return self.write_int_le(num.into(), 3);
    }

    write_fn!(write_u32, u32, put_u32, 4);
    write_fn!(write_u32_le, u32, put_u32_le, 4);
    write_fn!(write_i32, i32, put_i32, 4);
    write_fn!(write_i32_le, i32, put_i32_le, 4);
    write_fn!(write_f32, f32, put_f32, 4);
    write_fn!(write_f32_le, f32, put_f32_le, 4);

    // todo: write_var_u32, write_var_i32 should be reversable and should not corrupt the stream on failure
    pub fn write_var_u32(&mut self, num: u32) -> Result<(), std::io::Error> {
        let mut x = num;
        while x >= 0x80 {
            self.write_u8((x as u8) | 0x80)?;
            x >>= 7;
        }
        self.write_u8(x as u8)?;
        return Ok(());
    }

    pub fn write_var_i32(&mut self, num: i32) -> Result<(), std::io::Error> {
        return if num < 0 {
            let num = num as u32;
            self.write_var_u32(!(num << 1))
        } else {
            let num = num as u32;
            self.write_var_u32(num << 1)
        };
    }

    write_fn!(write_u64, u64, put_u64, 8);
    write_fn!(write_u64_le, u64, put_u64_le, 8);
    write_fn!(write_i64, i64, put_i64, 8);
    write_fn!(write_i64_le, i64, put_i64_le, 8);
    write_fn!(write_f64, f64, put_f64, 8);
    write_fn!(write_f64_le, f64, put_f64_le, 8);

    pub fn write_var_u64(&mut self, num: u64) -> Result<(), std::io::Error> {
        let mut x = (num as u64) & u64::MAX;
        for _ in (0..70).step_by(7) {
            if x >> 7 == 0 {
                self.write_u8(x as u8)?;
                return Ok(());
            } else {
                self.write_u8(((x & 0x7F) | 0x80) as u8)?;
                x >>= 7;
            }
        }

        return Err(Error::new(
            std::io::ErrorKind::InvalidData,
            ERR_VARINT_TOO_LONG,
        ));
    }

    pub fn write_var_i64(&mut self, num: i64) -> Result<(), std::io::Error> {
        return if num < 0 {
            let num = num as u64;
            self.write_var_u64(!(num << 1))
        } else {
            let num = num as u64;
            self.write_var_u64(num << 1)
        };
    }

    write_fn!(write_u128, u128, put_u128, 16);
    write_fn!(write_u128_le, u128, put_u128_le, 16);
    write_fn!(write_i128, i128, put_i128, 16);
    write_fn!(write_i128_le, i128, put_i128_le, 16);

    pub fn write_uint(&mut self, num: u64, size: usize) -> Result<(), std::io::Error> {
        if can_write!(self, size) {
            self.buf.put_uint(num, size);
            return Ok(());
        } else {
            return Err(Error::new(std::io::ErrorKind::OutOfMemory, ERR_EOM));
        }
    }

    pub fn write_uint_le(&mut self, num: u64, size: usize) -> Result<(), std::io::Error> {
        if can_write!(self, size) {
            self.buf.put_uint_le(num, size);
            return Ok(());
        } else {
            return Err(Error::new(std::io::ErrorKind::OutOfMemory, ERR_EOM));
        }
    }

    pub fn write_int(&mut self, num: i64, size: usize) -> Result<(), std::io::Error> {
        if can_write!(self, size) {
            self.buf.put_int(num, size);
            return Ok(());
        } else {
            return Err(Error::new(std::io::ErrorKind::OutOfMemory, ERR_EOM));
        }
    }

    pub fn write_int_le(&mut self, num: i64, size: usize) -> Result<(), std::io::Error> {
        if can_write!(self, size) {
            self.buf.put_int_le(num, size);
            return Ok(());
        } else {
            return Err(Error::new(std::io::ErrorKind::OutOfMemory, ERR_EOM));
        }
    }

    pub fn write_char(&mut self, c: char) -> Result<(), std::io::Error> {
        self.write_u32(c as u32)
    }

    pub fn write_bool(&mut self, b: bool) -> Result<(), std::io::Error> {
        if can_write!(self, 1) {
            self.buf.put_u8(b as u8);
            return Ok(());
        } else {
            return Err(Error::new(std::io::ErrorKind::OutOfMemory, ERR_EOM));
        }
    }

    /// Write a string to the buffer
    /// The string is written as a var_u32 length followed by the bytes of the string.
    /// Uses <https://protobuf.dev/programming-guides/encoding/#length-types> for length encoding
    pub fn write_string(&mut self, string: &str) -> Result<(), std::io::Error> {
        // https://protobuf.dev/programming-guides/encoding/#length-types
        if can_write!(self, string.len()) {
            self.write_var_u32(string.len() as u32)?;
            self.buf.put_slice(string.as_bytes());
            return Ok(());
        } else {
            return Err(Error::new(std::io::ErrorKind::OutOfMemory, ERR_EOM));
        }
    }

    /// Writes an `Option` to the buffer. The option must implement the `Writer` trait.
    ///
    /// ## Example
    /// ```rust
    /// use binary_util::io::ByteWriter;
    /// use binary_util::interfaces::Writer;
    ///
    /// pub struct HelloWorld {
    ///     pub magic: u32
    /// }
    ///
    /// impl Writer for HelloWorld {
    ///     fn write(&self, buf: &mut ByteWriter) -> Result<(), std::io::Error> {
    ///         buf.write_u32(self.magic)?;
    ///         return Ok(());
    ///     }
    /// }
    ///
    /// fn main() {
    ///     let hello = HelloWorld { magic: 0xCAFEBABE };
    ///     let mut buf = hello.write_to_bytes().unwrap();
    ///
    ///     println!("Hello World: {:?}", buf);
    /// }
    /// ```
    pub fn write_option(&mut self, option: &Option<impl Writer>) -> Result<(), std::io::Error> {
        if let Some(option) = option {
            self.write_bool(true)?;
            option.write(self)?;
        } else {
            self.write_bool(false)?;
        }
        return Ok(());
    }

    /// Writes a size-prefixed slice of bytes to the buffer. The slice is prefixed with a var_u32 length.
    pub fn write_slice(&mut self, slice: &[u8]) -> Result<(), std::io::Error> {
        if can_write!(self, slice.len()) {
            self.write_var_u32(slice.len() as u32)?;
            self.buf.put_slice(slice);
            return Ok(());
        } else {
            return Err(Error::new(std::io::ErrorKind::OutOfMemory, ERR_EOM));
        }
    }

    /// Writes a slice of bytes to the buffer
    /// This is not the same as a size-prefixed slice, this is just a raw slice of bytes.
    ///
    /// For automatically size-prefixed slices, use `write_slice`.
    pub fn write(&mut self, buf: &[u8]) -> Result<(), std::io::Error> {
        if can_write!(self, buf.len()) {
            self.buf.put_slice(buf);
            return Ok(());
        } else {
            return Err(Error::new(std::io::ErrorKind::OutOfMemory, ERR_EOM));
        }
    }

    pub fn as_slice(&self) -> &[u8] {
        self.buf.chunk()
    }

    pub fn clear(&mut self) {
        self.buf.clear();
    }
}