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
/*!
# Type wrappers

This module mostly defines type wrappers for primitive types. For example, tuple wrapper for arity equal to six named [T6].

## Tuple wrappers

These wrappers used to implement transformations using std trait [From].

[T3] Example implementations:

- [Splits one array to multiple arrays](struct.T3.html#impl-From<%5BT;+N%5D>-for-T3<%5BT;+AN%5D,+%5BT;+BN%5D,+%5BT;+CN%5D>)

  `[T;N]` -> `[T; AN], [T; BN], [T; CN]` where `AN + BN + CN = N`

  ```
  # use heterob::T3;
  let T3(a, b, c) = T3::from([0, 1, 1, 2, 2, 2]);
  assert_eq!([0], a);
  assert_eq!([1, 1], b);
  assert_eq!([2, 2, 2], c);
  ```

- [Transforms from native tuple](struct.T3.html#impl-From<(A0,+B0,+C0)>-for-T3<A1,+B1,+C1>)

  `(A0, B0, C0)` -> `T3(A1, B1, C1)` where A1, B1, C1 have [From] A0, B0, C0 traits respectively.
  It could be useful for muliple value-to-value conversion.

  ```
  # use heterob::T3;
  let T3(a, b, c) = T3::from(('a', 42u8, &"maybe"));
  assert_eq!('a', a);
  assert_eq!(42u32, b);
  assert_eq!(Some(&"maybe"), c);
  ```

- [Transforms into native tuple](struct.T3.html#impl-From<T3<A0,+B0,+C0>>-for-(A1,+B1,+C1))

  `T3(A0, B0, C0)` -> `(A1, B1, C1)` where A1, B1, C1 have [From] A0, B0, C0 traits respectively

  ```
  # use heterob::T3;
  let (a, b, c): (_, u32, _) = T3('a', 42u8, &"maybe").into();
  assert_eq!('a', a);
  assert_eq!(42, b);
  assert_eq!(Some(&"maybe"), c);
  ```

## Ready to partition value wrappers

These wrappers used to show how value could be partitioned

It is useful to reduce records length of same type tuple wrappers:
`T3<[u16; 1], [u16; 2], [u16; 3]>` equals to `P3<u16, 1, 2, 3>`

[P3] Example implementations:

- [Transforms array to tuple of types](struct.P3.html#impl-From<P3<%5BTY;+NU%5D,+AN,+BN,+CN>>-for-(A,+B,+C))

  `[T; N]` -> `(A, B, C)` where A, B, C have [From] `[T; AN]`, `[T; BN]`, `[T; CN]` traits respectively

  ```
  # use heterob::{P3, T1};
  let (a, b, c): ([_; 1], _, _) = P3::<_, 1, 2, 3>([0, 1, 1, 2, 2, 2]).into();
  assert_eq!([0], a);
  assert_eq!((1, 1), b);
  assert_eq!(T1([2, 2, 2]), c);
  ```

- [Try to transform slice to tuple of types](struct.P3.html#impl-TryFrom<P3<%26%5BT%5D,+AN,+BN,+CN>>-for-Seq<U,+%26%5BT%5D>)

  `&[N]` -> `(A, B, C)` where A, B, C have [From] `[T; AN]`, `[T; BN]`, `[T; CN]` traits respectively

  ```
  # use heterob::{Seq, T3, P3};
  let seq = P3([0, 1, 1, 2, 2, 2, 42].as_slice()).try_into().unwrap();
  assert_eq!(Seq { head: ([0], [1, 1], [2, 2, 2]), tail: [42].as_slice() }, seq);
  ```

## Byte or Bits sequences
- [Splits sequnce into a head and a tail](Seq)

  `[1, 2, 3, 4, 5]` -> `Seq { head: [1, 2], tail: [3, 4, 5] }`

  ```
  # use heterob::Seq;
  let seq = ['a', 'b', 'c'].as_slice().try_into().unwrap();
  assert_eq!(Seq { head: ['a', 'b'], tail: ['c'].as_slice() }, seq);
  ```

## Type coercion wrappers
- [Coerce arbitrary type to primitive](struct.U16.html)

  `U16<ArbitraryType>` -> `u16` where `ArbitraryType` has `From<u16>` implementation

  Instead of using `From::<u16>::from(value)` we could use just `U16(field)`

  ```
  # use heterob::{P2, U16, U32, U64, endianness::BeBytesInto};
  let P2((a, U16(b))): P2<_, 2, 2> = [0, 0, 1, 1].be_bytes_into();
  // The `a` should be exact 2 bytes long (u16)
  let _: u64 = From::<u16>::from(a);
  // Or U16 wrapper above
  let _: u64 = b;
  ```
*/

use core::array::TryFromSliceError;

use funty::Fundamental;
use paste::paste;

/// Trait derives primitive types.
///
/// 'Opposite' to [funty::Fundamental](https://docs.rs/funty/latest/funty/trait.Fundamental.html)
/// trait
pub trait AsPrimitive<T> {
    #[allow(clippy::wrong_self_convention)]
    fn as_primitive(self) -> T;
}
impl<T: Fundamental> AsPrimitive<Option<char>> for T {
    fn as_primitive(self) -> Option<char> {
        self.as_char()
    }
}

/// Reserved space unit type implementation
impl<T> AsPrimitive<()> for T {
    fn as_primitive(self) {}
}

macro_rules! main_impl_for {
    ( AsPrimitive => $($cl:ty),+ $(,)?) => {paste!{ $(
        impl<T: Fundamental> AsPrimitive<$cl> for T {
            fn as_primitive(self) -> $cl { self.[<as_ $cl>]() }
        }
    )+ }};
}

main_impl_for!(AsPrimitive => bool,u8,u16,u32,u64,u128,usize);

/// Sequence of elements with head and tail
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Seq<H, T> {
    pub head: H,
    pub tail: T,
}

/**
Fallible conversion from slice to array

In contrast with std
[`TryFrom<&'_ [T]> for [T; N]`](https://doc.rust-lang.org/std/primitive.array.html#impl-TryFrom%3C%26%27_%20%5BT%5D%3E)
this implementation return array and slice tail on any slice that longer than array
```rust
# use heterob::Seq;
let bytes = [1u8, 2, 2, 3, 3, 3, 3];
let seq: Seq<[_; 3], _> = bytes[..].try_into().unwrap();
assert_eq!(Seq { head: [1, 2, 2], tail: [3, 3, 3, 3].as_slice() }, seq);
```
*/

impl<'a, T, const N: usize> TryFrom<&'a [T]> for Seq<[T; N], &'a [T]>
where
    T: Copy,
{
    type Error = TryFromSliceError;

    fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
        let (head, tail) = slice.split_at(slice.len().min(N));
        Ok(Self {
            head: head.try_into()?,
            tail,
        })
    }
}

macro_rules! common_impl {
    ($len:expr; $_:ident $(,)? $($cl:ident),* $(,)?) => { paste!{
        // #[derive(Debug, Clone, PartialEq, Eq)]
        // pub struct T3<A, B, C>(pub A, pub B, pub C);
        #[doc=concat!($len, "-ary tuple wrapper")]
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub struct [<T $len>]<A, $($cl, )*>(pub A, $(pub $cl, )*);

        /*
        impl<A0, A1, B0, B1, C0, C1> From<T3<A0, B0, C0>> for (A1, B1, C1)
        where
            A1: From<A0>,
            B1: From<B0>,
            C1: From<C0>,
        {
            fn from(T3(a, b, c): T3<A0, B0, C0>) -> Self {
                (a.into(), b.into(), c.into())
            }
        }
        */
        impl<A0, A1, $([<$cl 0>], [<$cl 1>], )*> From<[<T $len>]<A0, $([<$cl 0>], )*>> for (A1, $([<$cl 1>], )*)
        where
            A1: From<A0>, $([<$cl 1>]: From<[<$cl 0>]>, )*
        {
            fn from([<T $len>](a, $([<$cl:lower>], )*): [<T $len>]<A0, $([<$cl 0>], )*>) -> Self {
                (a.into(), $([<$cl:lower>].into(), )*)
            }
        }

        /*
        impl<A0, A1, B0, B1, C0, C1> From<(A0, B0, C0)> for T3<A1, B1, C1>
        where
            A1: From<A0>,
            B1: From<B0>,
            C1: From<C0>,
        {
            fn from((a, b, c): (A0, B0, C0)) -> Self {
                T3(a.into(), b.into(), c.into())
            }
        }
        */
        impl<A0, A1, $([<$cl 0>], [<$cl 1>], )*> From<(A0, $([<$cl 0>], )*)> for [<T $len>]<A1, $([<$cl 1>], )*>
        where
            A1: From<A0>, $([<$cl 1>]: From<[<$cl 0>]>, )*
        {
            fn from((a, $([<$cl:lower>], )*): (A0, $([<$cl 0>], )*)) -> Self {
                Self(a.into(), $([<$cl:lower>].into(), )*)
            }
        }

        /*
        impl<T, const N: usize, const AN: usize, const BN: usize, const CN: usize>
            From<[T; N]> for T3<[T; AN], [T; BN], [T; CN]>
        where
            T: Default + Copy,
        {
            fn from(data: [T; N]) -> Self {
                const {
                    const MSG: &str =
                        concat!("The sum ", stringify!(AN $(+ [<$cl N>])*), " should be equal to N");
                     assert!(AN $(+ [<$cl N>])* == N, "{}", MSG);
                }

                let end = 0;

                let mut a = [Default::default(); AN];
                let (start, end) = (end, end + AN);
                a.copy_from_slice(&data[start..end]);

                let mut b = [Default::default(); BN];
                let (start, end) = (end, end + BN);
                b.copy_from_slice(&data[start..end]);

                let mut c = [Default::default(); CN];
                let (start, end) = (end, end + CN);
                c.copy_from_slice(&data[start..end]);

                T3(a, b, c)
            }
        }
        */
        impl<T, const N: usize, const AN: usize, $(const [<$cl N>]: usize, )*>
            From<[T; N]> for [<T $len>]<[T; AN], $([T; [<$cl N>]],)*>
        where
            T: Default + Copy,
        {
            fn from(data: [T; N]) -> Self {
                const {
                    const MSG: &str =
                        concat!("The sum ", stringify!(AN $(+ [<$cl N>])*), " should be equal to N");
                     assert!(AN $(+ [<$cl N>])* == N, "{}", MSG);
                }

                let end = 0;

                let mut a = [Default::default(); AN];
                let (start, end) = (end, end + AN);
                a.copy_from_slice(&data[start..end]);

                $(
                    let mut [<$cl:lower>] = [Default::default();[<$cl N>]];
                    let (start, end) = (end, end + [<$cl N>]);
                    [<$cl:lower>].copy_from_slice(&data[start..end]);
                )*
                Self(a, $([<$cl:lower>], )*)
            }
        }

        // #[derive(Debug, Clone, PartialEq, Eq)]
        // pub struct P3<TY, const A: usize, const B: usize, const C: usize>(pub TY);
        #[doc=concat!("Type wrapper with ", $len, " const generic parameters")]
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub struct [<P $len>]<TY, const A: usize, $(const $cl: usize, )*>(pub TY);
        // impl<TY, const AN: usize, const BN: usize, const CN: usize> P3<TY, AN, BN, CN> {
        //     pub const SUM: usize = AN + BN + CN;
        // }
        impl<TY, const AN: usize, $(const [<$cl N>]: usize, )*> [<P $len>]<TY, AN, $([<$cl N>], )*> {
            pub const SUM: usize = AN $(+ [<$cl N>])*;
        }

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

        /*
        impl<'a, T, const AN: usize, const BN: usize, const CN: usize> TryFrom<&'a [T]>
            for Seq<T3<[T; AN], [T; BN], [T; CN]>, &'a [T]>
        where
            T: Copy,
        {
            type Error = TryFromSliceError;

            fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
                let Seq { head: a, tail: slice }: Seq<_, &[T]> = slice.try_into()?;
                let Seq { head: b, tail: slice }: Seq<_, &[T]> = slice.try_into()?;
                let Seq { head: c, tail: slice }: Seq<_, &[T]> = slice.try_into()?;
                Ok(Self {
                    head: T3(a, b, c),
                    tail: slice,
                })
            }
        }
        */
        impl<'a, T, const AN: usize, $(const [<$cl N>]: usize, )*> TryFrom<&'a [T]>
            for Seq<[<T $len>]<[T; AN], $([T; [<$cl N>]], )*>, &'a [T]>
        where
            T: Copy,
        {
            type Error = TryFromSliceError;

            fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
                let Seq { head: a, tail: slice }: Seq<_, &[T]> = slice.try_into()?;
                $(
                    let Seq { head: [<$cl:lower>], tail: slice }: Seq<_, &[T]> =
                        slice.try_into()?;
                )*
                Ok(Self {
                    head: [<T $len>](a, $([<$cl:lower>], )*),
                    tail: slice,
                })
            }
        }

        /*
        impl<'a, T, U, const AN: usize, const BN: usize, const CN: usize>
            TryFrom<P3<&'a [T], AN, BN, CN>> for Seq<U, &'a [T]>
        where
            T: Copy,
            U: From<T3<[T; AN], [T; BN], [T; CN]>>,
        {
            type Error = TryFromSliceError;

            fn try_from(P3(data): P3<&'a [T], AN, BN, CN>) -> Result<Self, Self::Error> {
                data.try_into().map(|Seq { head, tail }| Seq {
                    head: From::<T3<[T; AN], [T; BN], [T; CN]>>::from(head),
                    tail,
                })
            }
        }
        */
        impl<'a, T, U, const AN: usize, $(const [<$cl N>]: usize, )*>
            TryFrom<[<P $len>]<&'a [T], AN, $([<$cl N>], )*>> for Seq<U, &'a [T]>
        where
            T: Copy,
            U: From<[<T $len>]<[T; AN], $([T;[<$cl N>]], )*>>
        {
            type Error = TryFromSliceError;

            fn try_from([<P $len>](data): [<P $len>]<&'a [T], AN, $([<$cl N>], )*>) ->
                 Result<Self, Self::Error>
            {
                data.try_into().map(|Seq { head,  tail }| Seq {
                    head: From::<[<T $len>]<[T; AN], $([T; [<$cl N>]], )*>>::from(head),
                    tail,
                })
            }
        }
    }};
}

// Monuple
common_impl!(1; A);
// Couple
common_impl!(2; A,B);
// Triple
common_impl!(3; A,B,C);
// Quadruple
common_impl!(4; A,B,C,D);
// Quintuple
common_impl!(5; A,B,C,D,E);
// Sextuple
common_impl!(6; A,B,C,D,E,F);
// Septuple
common_impl!(7; A,B,C,D,E,F,G);
// Octuple
common_impl!(8; A,B,C,D,E,F,G,H);
// Nonuple
common_impl!(9; A,B,C,D,E,F,G,H,I);
// Decuple
common_impl!(10; A,B,C,D,E,F,G,H,I,J);
// Undecuple
common_impl!(11; A,B,C,D,E,F,G,H,I,J,K);
// Duodecuple
common_impl!(12; A,B,C,D,E,F,G,H,I,J,K,L);
// Tredecuple
common_impl!(13; A,B,C,D,E,F,G,H,I,J,K,L,M);
// Quattuordecuple
common_impl!(14; A,B,C,D,E,F,G,H,I,J,K,L,M,N);
// Quindecuple
common_impl!(15; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O);
// Sexdecuple
common_impl!(16; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P);
// Septendecuple
common_impl!(17; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q);
// Octodecuple
common_impl!(18; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R);
// Novemdecuple
common_impl!(19; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S);
// Vigintuple
common_impl!(20; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T);
// Unvigintuple
common_impl!(21; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U);
// Duovigintuple
common_impl!(22; A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V);
// Trevigintuple
common_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);
// Quattuorvigintuple
common_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);
// Quinvigintuple
common_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);
// Sexvigintuple
common_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);

// #[derive(Debug, Clone, PartialEq, Eq)]
// pub struct U8<T>(T);

// impl<T: From<u8>, U: Fundamental> AsPrimitive<U8<T>> for U {
//     fn as_primitive(self) -> U8<T> {
//         U8(self.as_u8().into())
//     }
// }

macro_rules! impl_wrappers_as_primitive {
    ($($ty:ty),+ $(,)?) => {paste!{ $(
        #[doc=concat!("Wrapper around type that may be converted from [", stringify!([<$ty:lower>]), "]")]
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub struct $ty<T: From<[<$ty:lower>]>>(pub T);

        impl<T: From<[<$ty:lower>]>, U: Fundamental> AsPrimitive<$ty<T>> for U {
            fn as_primitive(self) -> $ty<T> {
                $ty(self.[<as_ $ty:lower>]().into())
            }
        }
    )+ }};
}

impl_wrappers_as_primitive!(Bool, U8, U16, U32, U64, U128, Usize);

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

    #[test]
    fn split_array() {
        let chars = ['a', 'b', 'b'];
        assert_eq!(T2(['a'], ['b', 'b']), T2::from(chars));

        let bytes = [1u8, 2, 2, 3, 3, 3, 3];
        assert_eq!(T3([1], [2, 2], [3, 3, 3, 3]), T3::from(bytes));
    }

    #[test]
    fn tuple_of_elements_from() {
        assert_eq!((97u128, 98usize), T2(97u8, 98u16).into());
        assert_eq!(('a', 'b', 'c'), T3(97, 98, 99).into());
    }

    #[test]
    fn tuple_of_elements_into() {
        assert_eq!(T2(97u32, 98i32), (97u8, 98u16).into());
        assert_eq!(T3('a', 'b', 'c'), (97, 98, 99).into());
    }

    #[test]
    fn slice_try_into_tuple_of_arrays() {
        let bytes = [1u8, 2, 2, 3, 3, 3, 3];
        let Seq {
            head: T3(a, b, c), ..
        } = bytes[..].try_into().unwrap();
        assert_eq!(([1], [2, 2], [3, 3, 3]), (a, b, c));
    }

    #[test]
    fn partition_ready_longer_slice_try_into() {
        let bytes = [1u8, 2, 2, 3, 3, 3, 3, 42];

        let result = bytes.as_slice().try_into().ok();
        let sample = Some(Seq {
            head: T3([1], [2, 2], [3, 3, 3, 3]),
            tail: &bytes[7..],
        });
        assert_eq!(sample, result, "tuple warpper");

        let result = P3(bytes.as_slice()).try_into().ok();
        let sample = Some(Seq {
            head: ([1], [2, 2], [3, 3, 3, 3]),
            tail: &bytes[7..],
        });
        assert_eq!(sample, result, "tuple of arrays");
    }

    #[test]
    fn partition_ready_exact_slice_try_into() {
        let bytes = [1u8, 2, 2, 3, 3, 3, 3];

        let result = bytes.as_slice().try_into().ok();
        let sample = Some(Seq {
            head: T3([1], [2, 2], [3, 3, 3, 3]),
            tail: [].as_slice(),
        });
        assert_eq!(sample, result, "tuple warpper");

        let result = P3(bytes.as_slice()).try_into().ok();
        let sample = Some(Seq {
            head: ([1], [2, 2], [3, 3, 3, 3]),
            tail: [].as_slice(),
        });
        assert_eq!(sample, result, "tuple of arrays");
    }

    #[test]
    fn partition_ready_shorter_slice_try_into() {
        let bytes = [1u8, 2, 2, 3, 3];

        #[allow(clippy::type_complexity)]
        let result: Option<Seq<T3<[_; 1], [_; 2], [_; 3]>, &[u8]>> =
            bytes.as_slice().try_into().ok();
        let sample = None;
        assert_eq!(sample, result, "slice is shorter");

        #[allow(clippy::type_complexity)]
        let result: Option<Seq<([_; 1], [_; 2], [_; 3]), &[u8]>> =
            P3(bytes.as_slice()).try_into().ok();
        let sample = None;
        assert_eq!(sample, result, "slice is shorter");
    }

    #[test]
    fn const_generic_params_sum() {
        assert_eq!(6, P3::<u8, 1, 2, 3>::SUM);
    }

    #[test]
    fn wrappers_as_primitives() {
        #[derive(Debug, Clone, PartialEq, Eq)]
        enum En {
            One,
        }
        impl From<u8> for En {
            fn from(_: u8) -> Self {
                Self::One
            }
        }
        assert_eq!(U8(En::One), 0u8.as_primitive());
        assert_eq!(U8(En::One), 0u16.as_primitive());
        assert_eq!(U8(En::One), 0u32.as_primitive());
        assert_eq!(U8(En::One), 0u64.as_primitive());
        assert_eq!(U8(En::One), 0u128.as_primitive());
        assert_eq!(U8(En::One), 0usize.as_primitive());
        assert_eq!(U8(En::One), false.as_primitive());
    }
}