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
/*!
# Deku: Declarative binary reading and writing

Deriving a struct or enum with `DekuRead` and `DekuWrite` provides bit-level, symmetric, serialization/deserialization implementations.

This allows the developer to focus on building and maintaining how the data is represented and manipulated and not on redundant, error-prone, parsing/writing code.

This approach is especially useful when dealing with binary structures such as TLVs or network protocols.

Under the hood, it makes use of the [bitvec](https://crates.io/crates/bitvec) crate as the "Reader" and “Writer”

For documentation and examples on available `#deku[()]` attributes and features, see [attributes list](attributes/index.html)

For more examples, see the [examples folder](https://github.com/sharksforarms/deku/tree/master/examples)!

## no_std

For use in `no_std` environments, `alloc` is the single feature which is required on deku.

# Simple Example

Let's read big-endian data into a struct, with fields containing different sizes, modify a value, and write it back

```rust
use deku::prelude::*;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "big")]
struct DekuTest {
    #[deku(bits = "4")]
    field_a: u8,
    #[deku(bits = "4")]
    field_b: u8,
    field_c: u16,
}

let data: Vec<u8> = vec![0b0110_1001, 0xBE, 0xEF];
let (_rest, mut val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap();
assert_eq!(DekuTest {
    field_a: 0b0110,
    field_b: 0b1001,
    field_c: 0xBEEF,
}, val);

val.field_c = 0xC0FE;

let data_out = val.to_bytes().unwrap();
assert_eq!(vec![0b0110_1001, 0xC0, 0xFE], data_out);
```

# Composing

Deku structs/enums can be composed as long as they implement BitsReader / BitsWriter! (Which DekuRead/DekuWrite implement)

```rust
use deku::prelude::*;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuTest {
    header: DekuHeader,
    data: DekuData,
}

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuHeader(u8);

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuData(u16);

let data: Vec<u8> = vec![0xAA, 0xEF, 0xBE];
let (_rest, mut val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap();
assert_eq!(DekuTest {
    header: DekuHeader(0xAA),
    data: DekuData(0xBEEF),
}, val);

let data_out = val.to_bytes().unwrap();
assert_eq!(data, data_out);
```

# Vec

Vec<T> can be used in combination with the [count](attributes/index.html#count) attribute (T must implement BitsReader/BitsWriter)

If the length of Vec changes, the original field specified in `count` will not get updated.
Calling `.update()` can be used to "update" the field!

```rust
use deku::prelude::*;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct DekuTest {
    count: u8,
    #[deku(count = "count")]
    data: Vec<u8>,
}

let data: Vec<u8> = vec![0x02, 0xBE, 0xEF, 0xFF, 0xFF];
let (_rest, mut val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap();
assert_eq!(DekuTest {
    count: 0x02,
    data: vec![0xBE, 0xEF]
}, val);

let data_out = val.to_bytes().unwrap();
assert_eq!(vec![0x02, 0xBE, 0xEF], data_out);

// Pushing an element to data
val.data.push(0xAA);

assert_eq!(DekuTest {
    count: 0x02, // Note: this value has not changed
    data: vec![0xBE, 0xEF, 0xAA]
}, val);

let data_out = val.to_bytes().unwrap();
// Note: `count` is still 0x02 while 3 bytes got written
assert_eq!(vec![0x02, 0xBE, 0xEF, 0xAA], data_out);

// Use `update` to update `count`
val.update().unwrap();

assert_eq!(DekuTest {
    count: 0x03,
    data: vec![0xBE, 0xEF, 0xAA]
}, val);

```

# Enums

As enums can have multiple variants, each variant must have a way to match on the incoming data.

First the "type" is read using the `id_type`, then is matched against the variants given `id`. What happens after is the same as structs!

This is implemented with the [id](/attributes/index.html#id) and [id_type](attributes/index.html#id_type) attributes.

Example:

```rust
use deku::prelude::*;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(id_type = "u8")]
enum DekuTest {
    #[deku(id = "0x01")]
    VariantA,
    #[deku(id = "0x02")]
    VariantB(u16),
}

let data: Vec<u8> = vec![0x01, 0x02, 0xEF, 0xBE];

let (rest, val) = DekuTest::from_bytes((data.as_ref(), 0)).unwrap();
assert_eq!(DekuTest::VariantA , val);

let (rest, val) = DekuTest::from_bytes(rest).unwrap();
assert_eq!(DekuTest::VariantB(0xBEEF) , val);
```

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

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::{format, vec::Vec};

#[cfg(feature = "std")]
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use bitvec::prelude::*;
use core::convert::TryInto;
pub use deku_derive::*;
pub mod attributes;
pub mod error;
pub mod prelude;
mod slice_impls;
use crate::error::DekuError;

/// "Reader" trait: read bits and construct type
pub trait BitsReader {
    /// Read bits and construct type
    /// * **input** - Input as bits
    /// * **input_is_le** - `true` if input is to be interpreted as little endian,
    /// false otherwise (controlled via `endian` deku attribute)
    /// * **bit_size** - `Some` if `bits` or `bytes` deku attributes provided,
    /// `None` otherwise
    /// * **count** - Number of elements to read for container, Some if `count` attribute
    /// is provided, else None
    fn read(
        input: &BitSlice<Msb0, u8>,
        input_is_le: bool,
        bit_size: Option<usize>,
        count: Option<usize>,
    ) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
    where
        Self: Sized;
}

/// "Writer" trait: write from type to bits
pub trait BitsWriter {
    /// Write type to bits
    /// * **output_is_le** - `true` if output is to be interpreted as little endian,
    /// false otherwise (controlled via `endian` deku attribute)
    /// * **bit_size** - `Some` if `bits` or `bytes` deku attributes provided,
    /// `None` otherwise
    fn write(
        &self,
        output_is_le: bool,
        bit_size: Option<usize>,
    ) -> Result<BitVec<Msb0, u8>, DekuError>;
}

macro_rules! ImplDekuTraits {
    ($typ:ty) => {
        impl BitsReader for $typ {
            fn read(
                input: &BitSlice<Msb0, u8>,
                input_is_le: bool,
                bit_size: Option<usize>,
                count: Option<usize>,
            ) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError> {
                assert!(count.is_none(), "Dev error: `count` should always be None");

                let max_type_bits: usize = core::mem::size_of::<$typ>() * 8;

                let bit_size = match bit_size {
                    None => max_type_bits,
                    Some(s) if s > max_type_bits => {
                        return Err(DekuError::Parse(format!(
                            "too much data: container of {} cannot hold {}",
                            max_type_bits, s
                        )))
                    }
                    Some(s) => s,
                };
                if input.len() < bit_size {
                    return Err(DekuError::Parse(format!(
                        "not enough data: expected {} got {}",
                        bit_size,
                        input.len()
                    )));
                }

                let (bit_slice, rest) = input.split_at(bit_size);

                // Create a new BitVec from the slice and pad un-aligned chunks
                // i.e. [10010110, 1110] -> [10010110, 00001110]
                let bits: BitVec<Msb0, u8> = {
                    let mut bits = BitVec::with_capacity(bit_slice.len());
                    for chunk in bit_slice.chunks(8) {
                        if chunk.len() != 8 {
                            let pad = 8 * ((chunk.len() + 7) / 8) - chunk.len();
                            for _ in 0..pad {
                                bits.push(false);
                            }
                        }

                        for b in chunk {
                            bits.push(*b);
                        }
                    }

                    // Pad up-to size of type
                    for _ in 0..(max_type_bits - bits.len()) {
                        if input_is_le {
                            bits.push(false);
                        } else {
                            bits.insert(0, false);
                        }
                    }

                    bits
                };

                let bytes = bits.into_vec();
                let bytes: &[u8] = bytes.as_ref();

                // Read value
                let value = if input_is_le {
                    <$typ>::from_le_bytes(bytes.try_into()?)
                } else {
                    <$typ>::from_be_bytes(bytes.try_into()?)
                };

                Ok((rest, value))
            }
        }

        impl BitsWriter for $typ {
            fn write(
                &self,
                output_is_le: bool,
                bit_size: Option<usize>,
            ) -> Result<BitVec<Msb0, u8>, DekuError> {
                let input = if output_is_le {
                    self.to_le_bytes()
                } else {
                    self.to_be_bytes()
                };

                let input_bits: BitVec<Msb0, u8> = input.to_vec().into();

                let res_bits: BitVec<Msb0, u8> = {
                    if let Some(bit_size) = bit_size {
                        if bit_size > input_bits.len() {
                            return Err(DekuError::InvalidParam(format!(
                                "bit size {} is larger then input {}",
                                bit_size,
                                input_bits.len()
                            )));
                        }

                        if output_is_le {
                            // Example read 10 bits u32 [0xAB, 0b11_000000]
                            // => [10101011, 00000011, 00000000, 00000000]
                            let mut res_bits = BitVec::<Msb0, u8>::with_capacity(bit_size);
                            let mut remaining_bits = bit_size;
                            for chunk in input_bits.chunks(8) {
                                if chunk.len() > remaining_bits {
                                    let bits = &chunk[chunk.len() - remaining_bits..];
                                    for b in bits {
                                        res_bits.push(*b);
                                    }
                                    // https://github.com/myrrlyn/bitvec/issues/62
                                    // res_bits.extend_from_slice(chunk[chunk.len() - remaining_bits..]);
                                    break;
                                } else {
                                    for b in chunk {
                                        res_bits.push(*b);
                                    }
                                    // https://github.com/myrrlyn/bitvec/issues/62
                                    // res_bits.extend_from_slice(chunk)
                                }
                                remaining_bits -= chunk.len();
                            }

                            res_bits
                        } else {
                            // Example read 10 bits u32 [0xAB, 0b11_000000]
                            // => [00000000, 00000000, 00000010, 10101111]
                            input_bits[input_bits.len() - bit_size..].into()
                        }
                    } else {
                        input_bits
                    }
                };

                Ok(res_bits)
            }
        }
    };
}

impl<T: BitsReader> BitsReader for Vec<T> {
    fn read(
        input: &BitSlice<Msb0, u8>,
        input_is_le: bool,
        bit_size: Option<usize>,
        count: Option<usize>,
    ) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
    where
        Self: Sized,
    {
        let count = count.expect("Dev error: `count` should always be Some");

        let mut res = Vec::with_capacity(count);
        let mut rest = input;
        for _i in 0..count {
            let (new_rest, val) = <T>::read(rest, input_is_le, bit_size, None)?;
            res.push(val);
            rest = new_rest;
        }

        Ok((rest, res))
    }
}

impl<T: BitsWriter> BitsWriter for Vec<T> {
    fn write(
        &self,
        output_is_le: bool,
        bit_size: Option<usize>,
    ) -> Result<BitVec<Msb0, u8>, DekuError> {
        let mut acc = BitVec::new();

        for v in self {
            let r = v.write(output_is_le, bit_size)?;
            acc.extend(r);
        }

        Ok(acc)
    }
}

ImplDekuTraits!(u8);
ImplDekuTraits!(u16);
ImplDekuTraits!(u32);
ImplDekuTraits!(u64);
ImplDekuTraits!(u128);
ImplDekuTraits!(usize);
ImplDekuTraits!(i8);
ImplDekuTraits!(i16);
ImplDekuTraits!(i32);
ImplDekuTraits!(i64);
ImplDekuTraits!(i128);
ImplDekuTraits!(isize);
ImplDekuTraits!(f32);
ImplDekuTraits!(f64);

#[cfg(feature = "std")]
impl BitsReader for Ipv4Addr {
    fn read(
        input: &BitSlice<Msb0, u8>,
        input_is_le: bool,
        bit_size: Option<usize>,
        count: Option<usize>,
    ) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
    where
        Self: Sized,
    {
        let (rest, ip) = u32::read(input, input_is_le, bit_size, count)?;
        Ok((rest, ip.into()))
    }
}

#[cfg(feature = "std")]
impl BitsWriter for Ipv4Addr {
    fn write(
        &self,
        output_is_le: bool,
        bit_size: Option<usize>,
    ) -> Result<BitVec<Msb0, u8>, DekuError> {
        let ip: u32 = (*self).into();
        ip.write(output_is_le, bit_size)
    }
}

#[cfg(feature = "std")]
impl BitsReader for Ipv6Addr {
    fn read(
        input: &BitSlice<Msb0, u8>,
        input_is_le: bool,
        bit_size: Option<usize>,
        count: Option<usize>,
    ) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
    where
        Self: Sized,
    {
        let (rest, ip) = u128::read(input, input_is_le, bit_size, count)?;
        Ok((rest, ip.into()))
    }
}

#[cfg(feature = "std")]
impl BitsWriter for Ipv6Addr {
    fn write(
        &self,
        output_is_le: bool,
        bit_size: Option<usize>,
    ) -> Result<BitVec<Msb0, u8>, DekuError> {
        let ip: u128 = (*self).into();
        ip.write(output_is_le, bit_size)
    }
}

#[cfg(feature = "std")]
impl BitsWriter for IpAddr {
    fn write(
        &self,
        output_is_le: bool,
        bit_size: Option<usize>,
    ) -> Result<BitVec<Msb0, u8>, DekuError> {
        match self {
            IpAddr::V4(ipv4) => ipv4.write(output_is_le, bit_size),
            IpAddr::V6(ipv6) => ipv6.write(output_is_le, bit_size),
        }
    }
}

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

    use rstest::rstest;

    #[cfg(target_endian = "little")]
    static IS_LE: bool = true;

    #[cfg(target_endian = "big")]
    static IS_LE: bool = false;

    macro_rules! TestPrimitive {
        ($test_name:ident, $typ:ty, $input:expr, $expected:expr) => {
            #[test]
            fn $test_name() {
                let input = $input;
                let bit_slice = input.bits::<Msb0>();
                let (_rest, res_read) = <$typ>::read(bit_slice, IS_LE, None, None).unwrap();
                assert_eq!($expected, res_read);

                let res_write = res_read.write(IS_LE, None).unwrap().into_vec();
                assert_eq!(input, res_write);
            }
        };
    }

    TestPrimitive!(test_u8, u8, vec![0xAAu8], 0xAAu8);
    TestPrimitive!(test_u16, u16, vec![0xABu8, 0xCD], 0xCDAB);
    TestPrimitive!(test_u32, u32, vec![0xABu8, 0xCD, 0xEF, 0xBE], 0xBEEFCDAB);
    TestPrimitive!(
        test_u64,
        u64,
        vec![0xABu8, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD, 0xFE, 0xC0],
        0xC0FECDABBEEFCDAB
    );
    TestPrimitive!(
        test_u128,
        u128,
        vec![
            0xABu8, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD, 0xFE, 0xC0, 0xAB, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD,
            0xFE, 0xC0
        ],
        0xC0FECDABBEEFCDABC0FECDABBEEFCDAB
    );
    TestPrimitive!(
        test_usize,
        usize,
        vec![0xABu8, 0xCD, 0xEF, 0xBE, 0xAB, 0xCD, 0xFE, 0xC0],
        if core::mem::size_of::<usize>() == 8 {
            0xC0FECDABBEEFCDAB
        } else {
            0xBEEFCDAB
        }
    );
    TestPrimitive!(test_i8, i8, vec![0xFBu8], -5);
    TestPrimitive!(test_i16, i16, vec![0xFDu8, 0xFE], -259);
    TestPrimitive!(test_i32, i32, vec![0x02u8, 0x3F, 0x01, 0xEF], -0x10FEC0FE);
    TestPrimitive!(
        test_i64,
        i64,
        vec![0x02u8, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF],
        -0x10FEC0FE10FEC0FE
    );
    TestPrimitive!(
        test_i128,
        i128,
        vec![
            0x02u8, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF, 0x01, 0x3F,
            0x01, 0xEF
        ],
        -0x10FEC0FE10FEC0FE10FEC0FE10FEC0FE
    );
    TestPrimitive!(
        test_isize,
        isize,
        vec![0x02u8, 0x3F, 0x01, 0xEF, 0x01, 0x3F, 0x01, 0xEF],
        if core::mem::size_of::<isize>() == 8 {
            -0x10FEC0FE10FEC0FE
        } else {
            -0x10FEC0FE
        }
    );
    TestPrimitive!(test_f32, f32, vec![0xA6u8, 0x9B, 0xC4, 0xBB], -0.006);
    TestPrimitive!(
        test_f64,
        f64,
        vec![0xFAu8, 0x7E, 0x6A, 0xBC, 0x74, 0x93, 0x78, 0xBF],
        -0.006
    );

    #[rstest(input,input_is_le,bit_size,count,expected,expected_rest,
        case::normal([0xDD, 0xCC, 0xBB, 0xAA].as_ref(), IS_LE, Some(32), None, 0xAABB_CCDD, bits![Msb0, u8;]),
        case::normal_offset([0b1001_0110, 0b1110_0000, 0xCC, 0xDD ].as_ref(), IS_LE, Some(12), None, 0b1110_1001_0110, bits![Msb0, u8; 0,0,0,0, 1,1,0,0,1,1,0,0, 1,1,0,1,1,1,0,1]),

        #[should_panic(expected="Parse(\"not enough data: expected 32 got 0\")")]
        case::not_enough_data([].as_ref(), IS_LE, Some(32), None, 0xFF, bits![Msb0, u8;]),
        #[should_panic(expected="Parse(\"not enough data: expected 32 got 16\")")]
        case::not_enough_data([0xAA, 0xBB].as_ref(), IS_LE, Some(32), None, 0xFF, bits![Msb0, u8;]),
        #[should_panic(expected="Parse(\"too much data: container of 32 cannot hold 64\")")]
        case::too_much_data([0xAA, 0xBB, 0xCC, 0xDD, 0xAA, 0xBB, 0xCC, 0xDD].as_ref(), IS_LE, Some(64), None, 0xFF, bits![Msb0, u8;]),
        #[should_panic(expected="Dev error: `count` should always be None")]
        case::dev_err_count_some([].as_ref(), IS_LE, Some(64), Some(1), 0xFF, bits![Msb0, u8;]),
    )]
    fn test_bit_read(
        input: &[u8],
        input_is_le: bool,
        bit_size: Option<usize>,
        count: Option<usize>,
        expected: u32,
        expected_rest: &BitSlice<Msb0, u8>,
    ) {
        let bit_slice = input.bits::<Msb0>();

        let (rest, res_read) = u32::read(bit_slice, input_is_le, bit_size, count).unwrap();
        assert_eq!(expected, res_read);
        assert_eq!(expected_rest, rest);
    }

    #[rstest(input,output_is_le,bit_size,expected,
        case::normal_le(0xDDCC_BBAA, IS_LE, None, vec![0xAA, 0xBB, 0xCC, 0xDD]),
        case::normal_be(0xDDCC_BBAA, !IS_LE, None, vec![0xDD, 0xCC, 0xBB, 0xAA]),
        case::bit_size_le_smaller(0x03AB, IS_LE, Some(10), vec![0xAB, 0b11_000000]),
        case::bit_size_be_smaller(0x03AB, !IS_LE, Some(10), vec![0b11, 0xAB]),
        #[should_panic(expected = "InvalidParam(\"bit size 100 is larger then input 32\")")]
        case::bit_size_le_bigger(0x03AB, IS_LE, Some(100), vec![0xAB, 0b11_000000]),
    )]
    fn test_bit_write(input: u32, output_is_le: bool, bit_size: Option<usize>, expected: Vec<u8>) {
        let res_write = input.write(output_is_le, bit_size).unwrap().into_vec();
        assert_eq!(expected, res_write);
    }

    #[rstest(input,is_le,bit_size,expected,expected_rest,expected_write,
        case::normal([0xDD, 0xCC, 0xBB, 0xAA].as_ref(), IS_LE, Some(32), 0xAABB_CCDD, bits![Msb0, u8;], vec![0xDD, 0xCC, 0xBB, 0xAA]),
    )]
    fn test_bit_read_write(
        input: &[u8],
        is_le: bool,
        bit_size: Option<usize>,
        expected: u32,
        expected_rest: &BitSlice<Msb0, u8>,
        expected_write: Vec<u8>,
    ) {
        let bit_slice = input.bits::<Msb0>();

        let (rest, res_read) = u32::read(bit_slice, is_le, bit_size, None).unwrap();
        assert_eq!(expected, res_read);
        assert_eq!(expected_rest, rest);

        let res_write = res_read.write(is_le, bit_size).unwrap().into_vec();
        assert_eq!(expected_write, res_write);
    }

    #[rstest(input,input_is_le,bit_size,count,expected,expected_rest,
        case::count_0([0xAA].as_ref(), IS_LE, Some(8), Some(0), vec![], bits![Msb0, u8; 1,0,1,0,1,0,1,0]),
        case::count_1([0xAA, 0xBB].as_ref(), IS_LE, Some(8), Some(1), vec![0xAA], bits![Msb0, u8; 1,0,1,1,1,0,1,1]),
        case::count_2([0xAA, 0xBB, 0xCC].as_ref(), IS_LE, Some(8), Some(2), vec![0xAA, 0xBB], bits![Msb0, u8; 1,1,0,0,1,1,0,0]),

        case::bits_6([0b0110_1001, 0b1110_1001].as_ref(), IS_LE, Some(6), Some(2), vec![0b00_011010, 0b00_011110], bits![Msb0, u8; 1,0,0,1]),

        #[should_panic(expected="Parse(\"too much data: container of 8 cannot hold 9\")")]
        case::not_enough_data([].as_ref(), IS_LE, Some(9), Some(1), vec![], bits![Msb0, u8;]),
        #[should_panic(expected="Parse(\"too much data: container of 8 cannot hold 9\")")]
        case::not_enough_data([0xAA].as_ref(), IS_LE, Some(9), Some(1), vec![], bits![Msb0, u8;]),
        #[should_panic(expected="Parse(\"not enough data: expected 8 got 0\")")]
        case::not_enough_data([0xAA].as_ref(), IS_LE, Some(8), Some(2), vec![], bits![Msb0, u8;]),
        #[should_panic(expected="Parse(\"too much data: container of 8 cannot hold 9\")")]
        case::too_much_data([0xAA, 0xBB].as_ref(), IS_LE, Some(9), Some(1), vec![], bits![Msb0, u8;]),
        #[should_panic(expected="Dev error: `count` should always be Some")]
        case::dev_err_count_none([].as_ref(), IS_LE, Some(0), None, vec![], bits![Msb0, u8;]),
    )]
    fn test_vec_read(
        input: &[u8],
        input_is_le: bool,
        bit_size: Option<usize>,
        count: Option<usize>,
        expected: Vec<u8>,
        expected_rest: &BitSlice<Msb0, u8>,
    ) {
        let bit_slice = input.bits::<Msb0>();

        let (rest, res_read) = Vec::<u8>::read(bit_slice, input_is_le, bit_size, count).unwrap();
        assert_eq!(expected, res_read);
        assert_eq!(expected_rest, rest);
    }

    #[rstest(input,output_is_le,bit_size,expected,
        case::normal(vec![0xAABB, 0xCCDD], IS_LE, None, vec![0xBB, 0xAA, 0xDD, 0xCC]),
    )]
    fn test_vec_write(
        input: Vec<u16>,
        output_is_le: bool,
        bit_size: Option<usize>,
        expected: Vec<u8>,
    ) {
        let res_write = input.write(output_is_le, bit_size).unwrap().into_vec();
        assert_eq!(expected, res_write);
    }

    #[rstest(input,is_le,bit_size,count,expected,expected_rest,expected_write,
        case::normal_le([0xAA, 0xBB, 0xCC, 0xDD].as_ref(), IS_LE, Some(16), Some(2), vec![0xBBAA, 0xDDCC], bits![Msb0, u8;], vec![0xAA, 0xBB, 0xCC, 0xDD]),
        case::normal_be([0xAA, 0xBB, 0xCC, 0xDD].as_ref(), !IS_LE, Some(16), Some(2), vec![0xAABB, 0xCCDD], bits![Msb0, u8;], vec![0xAA, 0xBB, 0xCC, 0xDD]),
    )]
    fn test_vec_read_write(
        input: &[u8],
        is_le: bool,
        bit_size: Option<usize>,
        count: Option<usize>,
        expected: Vec<u16>,
        expected_rest: &BitSlice<Msb0, u8>,
        expected_write: Vec<u8>,
    ) {
        let bit_slice = input.bits::<Msb0>();

        let (rest, res_read) = Vec::<u16>::read(bit_slice, is_le, bit_size, count).unwrap();
        assert_eq!(expected, res_read);
        assert_eq!(expected_rest, rest);

        let res_write: Vec<u8> = res_read.write(is_le, bit_size).unwrap().into_vec();
        assert_eq!(expected_write, res_write);

        assert_eq!(input[..expected_write.len()].to_vec(), expected_write);
    }

    #[rstest(input,is_le,bit_size,count,expected,expected_rest,
        case::normal_le([237,160,254,145].as_ref(), IS_LE, None, None, Ipv4Addr::new(145,254,160,237), bits![Msb0, u8;]),
        case::normal_be([145,254,160,237].as_ref(), !IS_LE, None, None, Ipv4Addr::new(145,254,160,237), bits![Msb0, u8;]),
    )]
    fn test_ipv4(
        input: &[u8],
        is_le: bool,
        bit_size: Option<usize>,
        count: Option<usize>,
        expected: Ipv4Addr,
        expected_rest: &BitSlice<Msb0, u8>,
    ) {
        let bit_slice = input.bits::<Msb0>();

        let (rest, res_read) = Ipv4Addr::read(bit_slice, is_le, bit_size, count).unwrap();
        assert_eq!(expected, res_read);
        assert_eq!(expected_rest, rest);

        let res_write: Vec<u8> = res_read.write(is_le, bit_size).unwrap().into_vec();
        assert_eq!(input.to_vec(), res_write);
    }

    #[rstest(input,is_le,bit_size,count,expected,expected_rest,
        case::normal_le([0xFF, 0x02, 0x0A, 0xC0, 0xFF, 0xFF,
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00].as_ref(),
                        IS_LE, None, None, Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff), bits![Msb0, u8;]),
        case::normal_be([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                        0xFF, 0xFF, 0xC0, 0x0A, 0x02, 0xFF].as_ref(),
                        !IS_LE, None, None, Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff), bits![Msb0, u8;]),
    )]
    fn test_ipv6(
        input: &[u8],
        is_le: bool,
        bit_size: Option<usize>,
        count: Option<usize>,
        expected: Ipv6Addr,
        expected_rest: &BitSlice<Msb0, u8>,
    ) {
        let bit_slice = input.bits::<Msb0>();

        let (rest, res_read) = Ipv6Addr::read(bit_slice, is_le, bit_size, count).unwrap();
        assert_eq!(expected, res_read);
        assert_eq!(expected_rest, rest);

        let res_write: Vec<u8> = res_read.write(is_le, bit_size).unwrap().into_vec();
        assert_eq!(input.to_vec(), res_write);
    }

    #[test]
    fn test_ip_addr_write() {
        let ip_addr = IpAddr::V4(Ipv4Addr::new(145, 254, 160, 237));
        let ret_write = ip_addr.write(true, None).unwrap().into_vec();
        assert_eq!(vec![237, 160, 254, 145], ret_write);

        let ip_addr = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x02ff));
        let ret_write = ip_addr.write(true, None).unwrap().into_vec();
        assert_eq!(
            vec![
                0xFF, 0x02, 0x0A, 0xC0, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00
            ],
            ret_write
        );
    }
}