heterob 0.4.0

Library for conversion between bytes/bits and heterogeneous lists (tuples)
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
/*!
Bytes conversions according to endianness

## Conversions implemented within library

- Integer conversion
```rust
# use heterob::endianness::*;
// Using LeBytesInto trait
let word: u16 = [0x11,0x22].le_bytes_into();
assert_eq!(0x2211, word);

// Using Le wrapper
let Le(word) = [0x11,0x22].into();
assert_eq!(0x2211u16, word);
```

- Tupled integers conversion
```rust
# use heterob::{P3, endianness::*};
// Using LeBytesInto trait
let P3((byte, word, dword)) = [0x00,0x11,0x22,0x33,0x44,0x55,0x66].le_bytes_into();
assert_eq!((0x00u8,0x2211u16,0x66554433u32), (byte, word, dword));

// Using Le wrapper for each value
let (Le(byte), Le(word), Le(dword)) = P3([0x00,0x11,0x22,0x33,0x44,0x55,0x66]).into();
assert_eq!((0x00u8,0x2211u16,0x66554433u32), (byte, word, dword));

// Using Le wrapper for all values at once
let Le((byte, word, dword)) = P3([0x00,0x11,0x22,0x33,0x44,0x55,0x66]).into();
assert_eq!((0x00u8,0x2211u16,0x66554433u32), (byte, word, dword));
```

- Array of integers
```rust
# use heterob::endianness::*;
// Using LeBytesInto trait
let array: [u16;3] = [0x00,0x11,0x22,0x33,0x44,0x55].le_bytes_into();
assert_eq!([0x1100,0x3322,0x5544], array);

// Using Le wrapper
let Le(array) = [0x00,0x11,0x22,0x33,0x44,0x55].into();
let _: [u16;3] = array; // let statements type coercioon
assert_eq!([0x1100,0x3322,0x5544], array);
```

## More than 26 entries conversion

Library limited max to 26 types list conversion. There are several workarounds

- Pre split large bytes array
```rust
# use heterob::{T2, P2, P3, endianness::*};
let data = [
    0x00, 0x11,0x11,0x11,0x11,
    0x22, 0x33,0x33, 0x44,0x44,
];

// Split data into 2 arrays
let T2(head, tail): T2<[u8;5], [u8;5]> = data.into();
// Convert first half
let P2((v0, v1)) = head.le_bytes_into();
// Convert second half
let Le((v2, v3, v4)) = P3(tail).into();

let sample = (0x00u8, 0x11111111u32, 0x22u8, 0x3333u16, 0x4444u16);

assert_eq!(sample, (v0, v1, v2, v3, v4));
```

- Complicated type annotation
```rust
# use heterob::{P3, endianness::*};
let data = [
    0x00, 0x11,0x11,0x11,0x11,
    0x22, 0x33,0x33, 0x44,0x44,
];

let P3((v0, v1, P3((v2, v3, v4)))):
    P3<(u8,  _, P3<(u8,  _,  _), 1, 2, 2>), 1, 4, 5>
    = data.le_bytes_into();

let sample = (0x00, 0x11111111u32, 0x22, 0x3333u16, 0x4444u16);

assert_eq!(sample, (v0, v1, v2, v3, v4));
```

- Using endianness independent bytes to bytes conversion feature
```rust
# use heterob::{T3, P3, endianness::*};
let data = [
    0x00, 0x11,0x11,0x11,0x11,
    0x22, 0x33,0x33, 0x44,0x44,
];

// Convert data to 2 values and tail bytes
let Le((v0, v1, tail)) = T3::from(data).into();
// Convert tail bytes to values
let Le((v2, v3, v4)) = P3::<[u8; 5], 1, 2, 2>(tail).into();

let sample = (0x00u8, 0x11111111u32, 0x22, 0x3333, 0x4444);

assert_eq!(sample, (v0, v1, v2, v3, v4));
```

*/
use core::array::TryFromSliceError;
use core::mem::size_of;
use paste::paste;

use super::*;

/// Little endian bytes to value conversion
///
/// It is the reciprocal of [LeBytesInto].
pub trait FromLeBytes<const N: usize>: Sized {
    fn from_le_bytes(bytes: [u8; N]) -> Self;
}

/// Little endian to value conversion that consumes the input bytes
///
/// The opposite of [FromLeBytes].
/// One should avoid implementing [LeBytesInto] and implement [FromLeBytes] instead.
pub trait LeBytesInto<T> {
    fn le_bytes_into(self) -> T;
}

/// Implementing [FromLeBytes] automatically provides one with an implementation of [LeBytesInto]
/// thanks to this blanket implementation.
impl<T, const N: usize> LeBytesInto<T> for [u8; N]
where
    T: FromLeBytes<N>,
{
    fn le_bytes_into(self) -> T {
        T::from_le_bytes(self)
    }
}

/// One byte array conversion
impl FromLeBytes<1> for u8 {
    fn from_le_bytes(bytes: [u8; 1]) -> Self {
        bytes[0]
    }
}

/// Bytes to bytes (no)conversion
impl<const N: usize> FromLeBytes<N> for [u8; N] {
    fn from_le_bytes(bytes: [u8; N]) -> Self {
        bytes
    }
}

/// Type wrapper for little endian bytes value
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Le<T>(pub T);

/// Any type that implemented [FromLeBytes] can be converted to [Le] wrapper
impl<T: FromLeBytes<N>, const N: usize> From<[u8; N]> for Le<T> {
    fn from(data: [u8; N]) -> Self {
        Le(data.le_bytes_into())
    }
}

/// Little endian bytes slice to value conversion that may fail
///
/// It is the reciprocal of [LeBytesTryInto].
pub trait TryFromLeBytes<const N: usize>: Sized + FromLeBytes<N> {
    fn try_from_le_bytes(slice: &[u8]) -> Result<Seq<Self, &[u8]>, TryFromSliceError>;
}

impl<T: FromLeBytes<N>, const N: usize> TryFromLeBytes<N> for T {
    fn try_from_le_bytes(slice: &[u8]) -> Result<Seq<Self, &[u8]>, TryFromSliceError> {
        let (head, tail) = slice.split_at(slice.len().min(size_of::<Self>()));
        let head = head.try_into().map(FromLeBytes::from_le_bytes)?;
        Ok(Seq { head, tail })
    }
}

/// Big endian bytes slice to value conversion that may fail
///
/// It is the reciprocal of [BeBytesTryInto].
pub trait TryFromBeBytes<const N: usize>: Sized + FromBeBytes<N> {
    fn try_from_be_bytes(slice: &[u8]) -> Result<Seq<Self, &[u8]>, TryFromSliceError>;
}

impl<T: FromBeBytes<N>, const N: usize> TryFromBeBytes<N> for T {
    fn try_from_be_bytes(slice: &[u8]) -> Result<Seq<Self, &[u8]>, TryFromSliceError> {
        let (head, tail) = slice.split_at(slice.len().min(size_of::<Self>()));
        let head = head.try_into().map(FromBeBytes::from_be_bytes)?;
        Ok(Seq { head, tail })
    }
}

/// Little endian bytes slice to value attempted conversion
///
/// The opposite of [TryFromLeBytes].
/// One should avoid implementing [LeBytesTryInto] and implement [TryFromLeBytes] instead.
pub trait LeBytesTryInto<'a, T, const N: usize> {
    /// Performs the conversion.
    fn le_bytes_try_into(self) -> Result<Seq<T, &'a [u8]>, TryFromSliceError>;
}

impl<'a, T, const N: usize> LeBytesTryInto<'a, T, N> for &'a [u8]
where
    T: TryFromLeBytes<N>,
{
    fn le_bytes_try_into(self) -> Result<Seq<T, &'a [u8]>, TryFromSliceError> {
        T::try_from_le_bytes(self)
    }
}

/// Big endian bytes slice to value attempted conversion
///
/// The opposite of [TryFromBeBytes].
/// One should avoid implementing [BeBytesTryInto] and implement [TryFromBeBytes] instead.
pub trait BeBytesTryInto<'a, T, const N: usize> {
    /// Performs the conversion.
    fn be_bytes_try_into(self) -> Result<Seq<T, &'a [u8]>, TryFromSliceError>;
}

impl<'a, T, const N: usize> BeBytesTryInto<'a, T, N> for &'a [u8]
where
    T: TryFromBeBytes<N>,
{
    fn be_bytes_try_into(self) -> Result<Seq<T, &'a [u8]>, TryFromSliceError> {
        T::try_from_be_bytes(self)
    }
}

/// Big endian bytes to value conversion
///
/// It is the reciprocal of [BeBytesInto].
pub trait FromBeBytes<const N: usize>: Sized {
    fn from_be_bytes(bytes: [u8; N]) -> Self;
}

/// Big endian to value conversion that consumes the input bytes
///
/// The opposite of [FromBeBytes].
/// One should avoid implementing [BeBytesInto] and implement [FromBeBytes] instead.
pub trait BeBytesInto<T> {
    fn be_bytes_into(self) -> T;
}

/// Implementing [FromBeBytes] automatically provides one with an implementation of [BeBytesInto]
/// thanks to this blanket implementation.
impl<T, const N: usize> BeBytesInto<T> for [u8; N]
where
    T: FromBeBytes<N>,
{
    fn be_bytes_into(self) -> T {
        T::from_be_bytes(self)
    }
}

/// One byte array conversion
impl FromBeBytes<1> for u8 {
    fn from_be_bytes(bytes: [u8; 1]) -> Self {
        bytes[0]
    }
}

/// Bytes to bytes (no)conversion
impl<const N: usize> FromBeBytes<N> for [u8; N] {
    fn from_be_bytes(bytes: [u8; N]) -> Self {
        bytes
    }
}

/// Type wrapper for big endian bytes value
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Be<T>(pub T);

/// Any type that implemented [FromBeBytes] can be converted to [Be] wrapper
impl<T: FromBeBytes<N>, const N: usize> From<[u8; N]> for Be<T> {
    fn from(data: [u8; N]) -> Self {
        Be(data.be_bytes_into())
    }
}

macro_rules! endianness_integer_impl {
    (Common: $e:ident => $($t:ty),+ $(,)?) => { paste!{ $(
        /*
        impl FromLeBytes<2> for u16 {
            fn from_le_bytes(bytes: [u8; 2]) -> Self {
                u16::from_le_bytes(bytes)
            }
        }
        */
        impl [<From $e Bytes>]<{ size_of::<Self>() }> for $t {
            fn [<from_ $e:lower _bytes>](bytes: [u8; size_of::<$t>()]) -> Self {
                $t::[<from_ $e:lower _bytes>](bytes)
            }
        }

        /*
        impl<const M: usize, const N: usize> FromLeBytes<M> for [u16; N] {
            fn from_le_bytes(bytes: [u8; M]) -> Self {
                const {
                    const MSG: &str =
                        concat!("The size of [", stringify!($t), "; M] and [u8; N] are different");
                    assert!(size_of::<Self>() == N, "{}", MSG);
                }

                let mut result = [0; N];
                for (n, data) in bytes.chunks_exact(size_of::<u16>()).enumerate() {
                    match <[u8;size_of::<u16>()]>::try_from(data) {
                        Ok(data) => result[n] = data.le_bytes_into(),
                        Err(_) => break,
                    }
                }
                result
            }
        }
        */
        impl<const N: usize, const M: usize> [<From $e Bytes>]<N> for [$t;M] {
            fn [<from_ $e:lower _bytes>](bytes: [u8;N]) -> Self {
                const {
                    const MSG: &str =
                        concat!("The size of [", stringify!($t), "; M] and [u8; N] are different");
                    assert!(size_of::<Self>() == N, "{}", MSG);
                }

                const SIZE: usize = size_of::<$t>();
                let mut result = [0;M];
                for (n, data) in bytes.chunks_exact(SIZE).enumerate() {
                    match <[u8;SIZE]>::try_from(data) {
                        Ok(data) => result[n] = data.[<$e:lower _bytes_into>](),
                        Err(_) => break,
                    }
                }
                result
            }
        }
    )+ }};
    (Le => $($t:ty),+ $(,)?) => { $(
    )+ };
    (Be => $($t:ty),+ $(,)?) => { $(
    )+ };
    ($($ty:ty),+ $(,)?) => {
        endianness_integer_impl!(Common: Le => $($ty,)+);
        // endianness_integers!(Le => $($ty,)+);
        endianness_integer_impl!(Common: Be => $($ty,)+);
        // endianness_integers!(Be => $($ty,)+);
    };
}

endianness_integer_impl!(u16, u32, u64, u128, usize);

macro_rules! endianness_tuple_impl {
    (Common: $e:ident => $len:literal: $($cl:ident),+ $(,)?) => { paste!{
        /*
        impl<A,B,C> From<(Le<A>,Le<B>,Le<C>)> for Le<(A,B,C)> {
            fn from((Le(a),Le(b),Le(c)): (Le<A>,Le<B>,Le<C>)) -> Self {
                Le((a,b,c))
            }
        }
        */
        impl<$($cl,)+> From<($($e<$cl>,)+)> for $e<($($cl,)+)> {
            fn from(($($e([<$cl:lower>]),)+): ($($e<$cl>,)+)) -> Self {
                $e(($([<$cl:lower>],)+))
            }
        }

        /*
        impl<TY,A,B,C, const AN: usize, const BN: usize, const CN: usize>
            From<T3<[TY;AN],[TY;BN],[TY;CN]>> for Le<(A,B,C)>
        where
            Le<A>: From<[TY;AN]>,
            Le<B>: From<[TY;BN]>,
            Le<C>: From<[TY;CN]>,
        {
            fn from(data: T3<[TY;AN],[TY;BN],[TY;CN]>) -> Self {
                <(Le<A>,Le<B>,Le<C>)>::from(data).into()
            }
        }
        */
        impl<TY, $($cl,)+ $(const [<$cl N>]: usize,)+>
            From<[<T $len>]<$([TY;[<$cl N>]],)+>> for $e<($($cl,)+)>
        where
            $($e<$cl>: From<[TY;[<$cl N>]]>,)+
        {
            fn from(data: [<T $len>]<$([TY;[<$cl N>]],)+>) -> Self {
                <($($e<$cl>,)+)>::from(data).into()
            }
        }

        /*
        impl<TY,A,B,C, const N: usize, const AN: usize, const BN: usize, const CN: usize>
            From<P3<[TY;N],AN,BN,CN>> for Le<(A,B,C)>
        where
            TY: Copy + Default,
            Le<A>: From<[TY;AN]>,
            Le<B>: From<[TY;BN]>,
            Le<C>: From<[TY;CN]>,
        {
            fn from(data: P3<[TY;N],AN,BN,CN>) -> Self {
                <(Le<A>,Le<B>,Le<C>)>::from(data).into()
            }
        }
        */
        impl<TY, $($cl,)+ const NU: usize, $(const [<$cl N>]: usize,)+>
            From<[<P $len>]<[TY;NU],$([<$cl N>],)+>> for $e<($($cl,)+)>
        where
            TY: Copy + Default,
            $($e<$cl>: From<[TY;[<$cl N>]]>,)+
        {
            fn from(data: [<P $len>]<[TY;NU],$([<$cl N>],)+>) -> Self {
                <($($e<$cl>,)+)>::from(data).into()
            }
        }

        /*
        impl<A,B,C, const N: usize, const AN: usize, const BN: usize, const CN: usize>
            FromLeBytes<N> for P3<(A,B,C), AN, BN, CN>
        where
            A: FromLeBytes<AN>,
            B: FromLeBytes<BN>,
            C: FromLeBytes<CN>,
        {
            fn from_le_bytes(bytes: [u8;N]) -> Self {
                let T3(a,b,c) = bytes.into();
                P3((a.le_bytes_into(),b.le_bytes_into(),c.le_bytes_into()))
            }
        }
        */
        impl<$($cl,)+ const NU: usize, $(const [<$cl N>]: usize,)+>
            [<From $e Bytes>]<NU> for [<P $len>]<($($cl,)+), $([<$cl N>],)+>
        where
            $( $cl: [<From $e Bytes>]<[<$cl N>]>, )+
        {
            fn [<from_ $e:lower _bytes>](bytes: [u8;NU]) -> Self {
                let [<T $len>]($([<$cl:lower>],)+) = bytes.into();
                [<P $len>](($([<$cl:lower>].[<$e:lower _bytes_into>](),)+))
            }
        }
    }};
    (Le => $len:literal: $($cl:ident),+ $(,)?) => { paste!{
    }};
    (Be => $len:literal: $($cl:ident),+ $(,)?) => { paste!{
    }};
    ($len:literal: $($cl:ident),+ $(,)?) => {
        endianness_tuple_impl!(Common: Le => $len: $($cl),+);
        // endianness_alphabet!(Le => $len: $($cl),+);
        endianness_tuple_impl!(Common: Be => $len: $($cl),+);
        // endianness_alphabet!(Be => $len: $($cl),+);
    };
}

endianness_tuple_impl!(1: A);
endianness_tuple_impl!(2: A,B);
endianness_tuple_impl!(3: A,B,C);
endianness_tuple_impl!(4: A,B,C,D);
endianness_tuple_impl!(5: A,B,C,D,E);
endianness_tuple_impl!(6: A,B,C,D,E,F);
endianness_tuple_impl!(7: A,B,C,D,E,F,G);
endianness_tuple_impl!(8: A,B,C,D,E,F,G,H);
endianness_tuple_impl!(9: A,B,C,D,E,F,G,H,I);
endianness_tuple_impl!(10: A,B,C,D,E,F,G,H,I,J);
endianness_tuple_impl!(11: A,B,C,D,E,F,G,H,I,J,K);
endianness_tuple_impl!(12: A,B,C,D,E,F,G,H,I,J,K,L);
endianness_tuple_impl!(13: A,B,C,D,E,F,G,H,I,J,K,L,M);
endianness_tuple_impl!(14: A,B,C,D,E,F,G,H,I,J,K,L,M,N);
endianness_tuple_impl!(15: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O);
endianness_tuple_impl!(16: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P);
endianness_tuple_impl!(17: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q);
endianness_tuple_impl!(18: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R);
endianness_tuple_impl!(19: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S);
endianness_tuple_impl!(20: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T);
endianness_tuple_impl!(21: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U);
endianness_tuple_impl!(22: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V);
endianness_tuple_impl!(23: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W);
endianness_tuple_impl!(24: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X);
endianness_tuple_impl!(25: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y);
endianness_tuple_impl!(26: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z);

macro_rules! endianness_for_wrappers_impl {
    ($e:ident @ $($ty:ty),+) => {paste!{ $(
        impl<T: From<[<$ty:lower>]>> [<From $e Bytes>]<{ size_of::<[<$ty:lower>]>() }> for $ty<T> {
            fn [<from_ $e:lower _bytes>](bytes: [u8; size_of::<[<$ty:lower>]>()]) -> Self {
                Self([<$ty:lower>]::[<from_ $e:lower _bytes>](bytes).into())
            }
        }
    )+ }};
    ($($ty:ty),+) => {
        endianness_for_wrappers_impl!(Le @ $($ty),+);
        endianness_for_wrappers_impl!(Be @ $($ty),+);
    };
}

endianness_for_wrappers_impl!(U8, U16, U32, U64, U128, Usize);

#[cfg(test)]
mod tests {
    use super::*;
    const DATA: [u8; 31] = [
        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE,
        0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD,
        0xEE,
    ];
    const RESULT_LE_U8: u8 = 0x00;
    const RESULT_LE_U16: u16 = 0x1100;
    const RESULT_LE_U32: u32 = 0x33221100;
    const RESULT_LE_U64: u64 = 0x7766554433221100;
    const RESULT_LE_U128: u128 = 0xFFEEDDCCBBAA99887766554433221100;
    const RESULT_BE_U8: u8 = 0x00;
    const RESULT_BE_U16: u16 = 0x0011;
    const RESULT_BE_U32: u32 = 0x00112233;
    const RESULT_BE_U64: u64 = 0x0011223344556677;
    const RESULT_BE_U128: u128 = 0x00112233445566778899AABBCCDDEEFF;

    #[test]
    fn le_bytes_into_integer_array() {
        let data: [u8; 16] = DATA[..16].try_into().unwrap();

        let result: [u8; 16] = data.le_bytes_into();
        assert_eq!(data, result, "[u8;16]");

        let result: [u16; 8] = data.le_bytes_into();
        let sample = [
            0x1100, 0x3322, 0x5544, 0x7766, 0x9988, 0xBBAA, 0xDDCC, 0xFFEE,
        ];
        assert_eq!(sample, result, "[u16;8]");

        let result: [u32; 4] = data.le_bytes_into();
        let sample = [0x33221100, 0x77665544, 0xBBAA9988, 0xFFEEDDCC];
        assert_eq!(sample, result, "[u32;4]");

        let result: [u64; 2] = data.le_bytes_into();
        let sample = [0x7766554433221100, 0xFFEEDDCCBBAA9988];
        assert_eq!(sample, result, "[u64;2]");

        let result: [u128; 1] = data.le_bytes_into();
        let sample = [0xFFEEDDCCBBAA99887766554433221100];
        assert_eq!(sample, result, "[u128;1]");
    }

    #[test]
    fn be_bytes_into_integer_array() {
        let data: [u8; 16] = DATA[..16].try_into().unwrap();

        let result: [u8; 16] = data.be_bytes_into();
        assert_eq!(data, result, "[u8;16]");

        let result: [u16; 8] = data.be_bytes_into();
        let sample = [
            0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xAABB, 0xCCDD, 0xEEFF,
        ];
        assert_eq!(sample, result, "[u16;8]");

        let result: [u32; 4] = data.be_bytes_into();
        let sample = [0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF];
        assert_eq!(sample, result, "[u32;4]");

        let result: [u64; 2] = data.be_bytes_into();
        let sample = [0x0011223344556677, 0x8899AABBCCDDEEFF];
        assert_eq!(sample, result, "[u64;2]");

        let result: [u128; 1] = data.be_bytes_into();
        let sample = [0x00112233445566778899AABBCCDDEEFF];
        assert_eq!(sample, result, "[u128;1]");
    }

    #[test]
    fn into_le_integer_array_wrapper() {
        let data: [u8; 16] = DATA[..16].try_into().unwrap();

        let result: Le<[u8; 16]> = data.into();
        assert_eq!(data, result.0, "Le<[u8;16]>");

        let result: Le<[u16; 8]> = data.into();
        let sample = [
            0x1100u16, 0x3322, 0x5544, 0x7766, 0x9988, 0xBBAA, 0xDDCC, 0xFFEE,
        ];
        assert_eq!(sample, result.0, "Le<[u16;8]>");

        let result: Le<[u32; 4]> = data.into();
        let sample = [0x33221100, 0x77665544, 0xBBAA9988, 0xFFEEDDCC];
        assert_eq!(sample, result.0, "Le<[u32;4]>");

        let result: Le<[u64; 2]> = data.into();
        let sample = [0x7766554433221100, 0xFFEEDDCCBBAA9988];
        assert_eq!(sample, result.0, "Le<[u64;2]>");

        let result: Le<[u128; 1]> = data.into();
        let sample = [0xFFEEDDCCBBAA99887766554433221100];
        assert_eq!(sample, result.0, "Le<[u128;1]>");
    }

    #[test]
    fn into_mixed_integers() {
        let data: [u8; 8] = DATA[..8].try_into().unwrap();
        let (Be(a), Le(b), c) = T3::from(data).into();
        let _: (u16, u32, [u8; 2]) = (a, b, c);

        let sample = (0x0011, 0x55443322, [0x66, 0x77]);
        assert_eq!(sample, (a, b, c), "mixed integers");
    }

    #[test]
    fn into_mixed_arrays() {
        let data: [u8; 8] = DATA[..8].try_into().unwrap();
        let (Be(a), Le(b)) = P2::<_, 4, 4>(data).into();
        let _: ([u16; 2], [u32; 1]) = (a, b);

        let sample = ([0x0011, 0x2233], [0x77665544]);
        assert_eq!(sample, (a, b), "mixed arrays");
    }

    #[test]
    fn into_tupled_integers() {
        let result: Le<(u8, u16, u32, u64, u128)> = T5::from(DATA).into();
        let sample = (
            0x00,
            0x2211,
            0x66554433,
            0xEEDDCCBBAA998877,
            0xEEDDCCBBAA99887766554433221100FF,
        );
        assert_eq!(sample, result.0, "u8 .. u128");

        let result: Le<(usize,)> = T1::from(0x1234usize.to_le_bytes()).into();
        assert_eq!(0x1234usize, result.0 .0, "usize");
    }

    #[test]
    fn destructuring() {
        if let Le((0x1100u16, data)) = T2::from(DATA).into() {
            let _: [u8; 29] = data;
        } else {
            panic!();
        }
    }

    #[test]
    fn le_bytes_into_mixed_integers() {
        let data: [u8; 8] = DATA[..8].try_into().unwrap();
        let P3((a, b, c)) = data.le_bytes_into();
        let _: (u16, u32, [u8; 2]) = (a, b, c);

        let sample = (0x1100, 0x55443322, [0x66, 0x77]);
        assert_eq!(sample, (a, b, c), "mixed integers");
    }

    macro_rules! integers {
        ($e:ident => $($ty:ty),+ $(,)?) => { paste!{ $(

            #[test]
            fn [<try_from_ $e:lower _bytes_ $ty>]() {
                // let Seq { head: result, .. } =
                //     TryFromLeBytes::try_from_le_bytes(&DATA[..2]).unwrap();
                let Seq { head: result, .. } =
                    [<TryFrom $e Bytes>]::[<try_from_ $e:lower _bytes>](&DATA[..size_of::<$ty>()])
                        .unwrap();
                assert_eq!([<RESULT_ $e:upper _ $ty:upper>], result);
            }

            #[test]
            fn [<$e:lower _bytes_into_ $ty>]() {
                // let data: [u8; 1] = DATA[..1].try_into().unwrap();
                // let result: u8 = data.le_bytes_into();
                let data: [u8; size_of::<$ty>()] =
                    DATA[..size_of::<$ty>()].try_into().unwrap();
                let result: $ty = data.[<$e:lower _bytes_into>]();
                assert_eq!([<RESULT_ $e:upper _ $ty:upper>], result);
            }

            #[test]
            fn [<into_ $e:lower _wrapper_ $ty>]() {
                // let data: [u8; 1] = DATA[..1].try_into().unwrap();
                // let result: Le<u8> = data.into();
                let data: [u8; size_of::<$ty>()] =
                    DATA[..size_of::<$ty>()].try_into().unwrap();
                let result: [<$e>]<$ty> = data.into();
                assert_eq!([<RESULT_ $e:upper _ $ty:upper>], result.0);
            }

            #[test]
            fn [<$e:lower _bytes_try_into_ $ty>]() {
                // let Seq { head: result, .. } = DATA[..2].le_bytes_try_into().unwrap();
                let Seq { head: result, .. } =
                    DATA[..size_of::<$ty>()].[<$e:lower _bytes_try_into>]().unwrap();
                assert_eq!([<RESULT_ $e:upper _ $ty:upper>], result);
            }

        )+ } }
    }
    integers!(Le => u8,u16,u32,u64,u128);
    integers!(Be => u8,u16,u32,u64,u128);
}