puns 0.1.5

Type punning proven at compile time: a safe alternative to transmute and to hand-written parsing of bytes
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
//! Enums whose layout `rustc` chose (no `repr`), found by experiments in const evaluation, and
//! types without values.

// Validation recurses over the variants of an enum: `Many` has 300.
#![recursion_limit = "1024"]

use core::{convert::Infallible, num::NonZero};

use puns::{Error, Pun};

/// A variant that holds a type without values is one that no value is.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
#[expect(dead_code, reason = "no value is `B`, which is the point")]
enum Never {
  A(u8),
  B(Infallible),
}

#[derive(Pun, Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
enum Unit {
  A(u8),
  B(()),
}

/// An enum without variants: no value.
#[derive(Pun, Clone, Copy)]
#[expect(clippy::empty_enums, reason = "a type without values, which is the point")]
enum Void {}

#[test]
fn uninhabited() {
  assert_eq!(puns::try_pun::<[u8; 2], Never>([0, 5]), Ok(Never::A(5)));
  // The tag of `B`, whose field has no value.
  assert_eq!(puns::try_pun::<[u8; 2], Never>([1, 5]), Err(Error::Validity));
  // Variant by variant: `A` is proven, `B` is never a `Never`.
  assert_eq!(puns::try_pun::<Unit, Never>(Unit::A(3)), Ok(Never::A(3)));
  assert_eq!(puns::try_pun::<Unit, Never>(Unit::B(())), Err(Error::Validity));
  // A `Never` is always a `Unit`: its `B` is never there.
  assert_eq!(puns::pun::<Never, Unit>(Never::A(3)), Unit::A(3));
  // A source without values proves anything, as nothing is ever made of one.
  let _ = puns::pun::<(u8, Void), (u8, Void)>;
}

// --- Enums without `repr` -------------------------------------------------------------------------

/// A direct tag, of one byte: the first field of `A` follows it.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Direct {
  A(u8),
  B(u16),
}

/// A direct tag that `rustc` widened to two bytes: no field starts before 2.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Widened {
  A(u16),
  B(u32),
}

/// A niche: the values of the `bool` that are not one.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum MaybeFlag {
  Nothing,
  Flag(bool),
}

/// A niche in a `char`, for two variants.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Letter {
  Char(char),
  End,
  Error,
}

/// A niche in the field of one variant, and another variant with a field beside it.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Mixed {
  Both(u8, bool),
  Byte(u8),
  Nothing,
}

/// One variant: a struct.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Single {
  Only { byte: u8, flag: bool },
}

/// Generic, as `Option` is.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Maybe<T> {
  No,
  Yes(T),
}

/// A niche in the tag of another enum, which `rustc` widened.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Outer {
  Inner(Widened),
  Other,
}

/// A variant without values, which has a field with a size: `rustc` gives it a tag all the same.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum OneOfTwo {
  A(u8),
  B(u32, Infallible),
}

/// The bytes of the integer `value` of `N` bytes, in native byte order.
fn bytes<const N: usize>(value: u128) -> [u8; N] {
  let mut out = [0; N];
  for (i, byte) in out.iter_mut().enumerate() {
    let digit = if cfg!(target_endian = "little") { i } else { N - 1 - i };
    *byte = (value >> (8 * digit)) as u8;
  }
  out
}

#[test]
fn direct() {
  // The tag, then the field of the variant; the rest is padding, which bytes may hold.
  assert_eq!(puns::try_pun::<[u8; 4], Direct>([0, 7, 0xaa, 0xbb]), Ok(Direct::A(7)));
  let [lo, hi] = bytes::<2>(0x0102);
  assert_eq!(puns::try_pun::<[u8; 4], Direct>([1, 0xaa, lo, hi]), Ok(Direct::B(0x0102)));
  assert_eq!(puns::try_pun::<[u8; 4], Direct>([2, 0, 0, 0]), Err(Error::Validity));
  // Proven between enums of the same layout.
  assert_eq!(puns::pun::<Direct, Direct>(Direct::B(3)), Direct::B(3));
  // The widened tag is two bytes, both read.
  let tag = bytes::<2>(1);
  let [a, b, c, d] = bytes::<4>(9);
  let value = [tag[0], tag[1], 0, 0, a, b, c, d];
  assert_eq!(puns::try_pun::<[u8; 8], Widened>(value), Ok(Widened::B(9)));
  let tag = bytes::<2>(0x0101);
  let value = [tag[0], tag[1], 0, 0, a, b, c, d];
  assert!(puns::try_pun::<[u8; 8], Widened>(value).is_err());
  assert_eq!(puns::pun::<Widened, Widened>(Widened::A(5)), Widened::A(5));
}

#[test]
fn niche() {
  // The values of the `bool`, then the one of `Nothing`.
  assert_eq!(puns::pun::<MaybeFlag, u8>(MaybeFlag::Flag(true)), 1);
  assert_eq!(puns::try_pun::<u8, MaybeFlag>(0), Ok(MaybeFlag::Flag(false)));
  let nothing = puns::pun::<MaybeFlag, u8>(MaybeFlag::Nothing);
  assert!(nothing > 1);
  assert_eq!(puns::try_pun::<u8, MaybeFlag>(nothing), Ok(MaybeFlag::Nothing));
  for byte in 2..=255u8 {
    if byte != nothing {
      assert_eq!(puns::try_pun::<u8, MaybeFlag>(byte), Err(Error::Validity));
    }
  }
  // A `char` and two more values.
  let end: u32 = puns::pun(Letter::End);
  assert_eq!(puns::try_pun::<u32, Letter>(end), Ok(Letter::End));
  assert_eq!(puns::try_pun::<u32, Letter>(0x41), Ok(Letter::Char('A')));
  assert_eq!(puns::try_pun::<u32, Letter>(0xd800), Err(Error::Validity));
  assert_eq!(puns::pun::<Letter, u32>(Letter::Char('A')), 0x41);
  // Proven: each variant of the source is the variant of the target of the same tags.
  assert_eq!(puns::pun::<Mixed, Mixed>(Mixed::Byte(4)), Mixed::Byte(4));
}

#[test]
fn single_and_generic() {
  // Its fields where `rustc` put them, which is not where they are declared.
  let only = Single::Only { byte: 3, flag: true };
  let pair: [u8; 2] = puns::pun(only);
  assert!(pair == [3, 1] || pair == [1, 3]);
  assert_eq!(puns::try_pun::<[u8; 2], Single>(pair), Ok(only));
  assert!(puns::try_pun::<[u8; 2], Single>([2, 2]).is_err());
  // A niche in `NonZero`, a direct tag beside a `u8`.
  assert_eq!(puns::pun::<Maybe<NonZero<u32>>, u32>(Maybe::No), 0);
  let five = NonZero::new(5).unwrap();
  assert_eq!(puns::try_pun::<u32, Maybe<NonZero<u32>>>(5), Ok(Maybe::Yes(five)));
  assert_eq!(puns::try_pun::<[u8; 2], Maybe<u8>>([1, 9]), Ok(Maybe::Yes(9)));
  // A niche in the widened tag of another enum.
  assert_eq!(puns::pun::<Outer, Outer>(Outer::Other), Outer::Other);
  // One variant that has values, and a tag for the other.
  assert_eq!(puns::try_pun::<[u8; 8], OneOfTwo>([0, 3, 0, 0, 0, 0, 0, 0]), Ok(OneOfTwo::A(3)));
  assert!(puns::try_pun::<[u8; 8], OneOfTwo>([1, 3, 0, 0, 0, 0, 0, 0]).is_err());
}

/// More than 256 variants: a tag of two bytes.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Many {
  First(u16),
  V0,
  V1,
  V2,
  V3,
  V4,
  V5,
  V6,
  V7,
  V8,
  V9,
  V10,
  V11,
  V12,
  V13,
  V14,
  V15,
  V16,
  V17,
  V18,
  V19,
  V20,
  V21,
  V22,
  V23,
  V24,
  V25,
  V26,
  V27,
  V28,
  V29,
  V30,
  V31,
  V32,
  V33,
  V34,
  V35,
  V36,
  V37,
  V38,
  V39,
  V40,
  V41,
  V42,
  V43,
  V44,
  V45,
  V46,
  V47,
  V48,
  V49,
  V50,
  V51,
  V52,
  V53,
  V54,
  V55,
  V56,
  V57,
  V58,
  V59,
  V60,
  V61,
  V62,
  V63,
  V64,
  V65,
  V66,
  V67,
  V68,
  V69,
  V70,
  V71,
  V72,
  V73,
  V74,
  V75,
  V76,
  V77,
  V78,
  V79,
  V80,
  V81,
  V82,
  V83,
  V84,
  V85,
  V86,
  V87,
  V88,
  V89,
  V90,
  V91,
  V92,
  V93,
  V94,
  V95,
  V96,
  V97,
  V98,
  V99,
  V100,
  V101,
  V102,
  V103,
  V104,
  V105,
  V106,
  V107,
  V108,
  V109,
  V110,
  V111,
  V112,
  V113,
  V114,
  V115,
  V116,
  V117,
  V118,
  V119,
  V120,
  V121,
  V122,
  V123,
  V124,
  V125,
  V126,
  V127,
  V128,
  V129,
  V130,
  V131,
  V132,
  V133,
  V134,
  V135,
  V136,
  V137,
  V138,
  V139,
  V140,
  V141,
  V142,
  V143,
  V144,
  V145,
  V146,
  V147,
  V148,
  V149,
  V150,
  V151,
  V152,
  V153,
  V154,
  V155,
  V156,
  V157,
  V158,
  V159,
  V160,
  V161,
  V162,
  V163,
  V164,
  V165,
  V166,
  V167,
  V168,
  V169,
  V170,
  V171,
  V172,
  V173,
  V174,
  V175,
  V176,
  V177,
  V178,
  V179,
  V180,
  V181,
  V182,
  V183,
  V184,
  V185,
  V186,
  V187,
  V188,
  V189,
  V190,
  V191,
  V192,
  V193,
  V194,
  V195,
  V196,
  V197,
  V198,
  V199,
  V200,
  V201,
  V202,
  V203,
  V204,
  V205,
  V206,
  V207,
  V208,
  V209,
  V210,
  V211,
  V212,
  V213,
  V214,
  V215,
  V216,
  V217,
  V218,
  V219,
  V220,
  V221,
  V222,
  V223,
  V224,
  V225,
  V226,
  V227,
  V228,
  V229,
  V230,
  V231,
  V232,
  V233,
  V234,
  V235,
  V236,
  V237,
  V238,
  V239,
  V240,
  V241,
  V242,
  V243,
  V244,
  V245,
  V246,
  V247,
  V248,
  V249,
  V250,
  V251,
  V252,
  V253,
  V254,
  V255,
  V256,
  V257,
  V258,
  V259,
  V260,
  V261,
  V262,
  V263,
  V264,
  V265,
  V266,
  V267,
  V268,
  V269,
  V270,
  V271,
  V272,
  V273,
  V274,
  V275,
  V276,
  V277,
  V278,
  V279,
  V280,
  V281,
  V282,
  V283,
  V284,
  V285,
  V286,
  V287,
  V288,
  V289,
  V290,
  V291,
  V292,
  V293,
  V294,
  V295,
  V296,
  V297,
  V298,
}

/// As `Result` is.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Either<T, E> {
  Left(T),
  Right(E),
}

/// A field that only some values of its type are valid for: not a niche, but checked.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
enum Restricted {
  Small(#[pun(valid(0..=9))] u8),
  Other(u8),
}

#[test]
fn many_variants() {
  // The tag is the first two bytes, which every value has initialized: read as such (proven),
  // without the check of 300 variants, which is the one of any enum and costs seconds to build.
  assert_eq!(puns::head::<Many, [u8; 2]>(Many::V297), bytes::<2>(298));
  assert_eq!(puns::head::<Many, [u8; 2]>(Many::First(7)), bytes::<2>(0));
  assert_eq!(puns::pun::<Many, Many>(Many::V3), Many::V3);
}

#[test]
fn generic_and_restricted() {
  // `Left` alone has values: a struct of a `u32`.
  assert_eq!(puns::pun::<Either<u32, Infallible>, u32>(Either::Left(7)), 7);
  assert_eq!(puns::try_pun::<u32, Either<u32, Infallible>>(7), Ok(Either::Left(7)));
  // A niche in a niche.
  assert_eq!(puns::try_pun::<u8, Maybe<MaybeFlag>>(0), Ok(Maybe::Yes(MaybeFlag::Flag(false))));
  let no: u8 = puns::pun(Maybe::<MaybeFlag>::No);
  assert_eq!(puns::try_pun::<u8, Maybe<MaybeFlag>>(no), Ok(Maybe::No));
  // `valid(..)` is checked, and is not a niche: the tag is before the byte.
  assert_eq!(puns::try_pun::<[u8; 2], Restricted>([0, 9]), Ok(Restricted::Small(9)));
  assert!(puns::try_pun::<[u8; 2], Restricted>([0, 10]).is_err());
  assert_eq!(puns::try_pun::<[u8; 2], Restricted>([1, 10]), Ok(Restricted::Other(10)));
}

// --- Enums made of what is not an enum ------------------------------------------------------------

/// An enum of a specified layout: a tag of one byte, then the byte of `Some`.
#[derive(Pun, Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
enum Opt {
  None,
  Some(u8),
}

/// Two bytes, the first of which is always 1.
#[derive(Pun, Clone, Copy)]
#[repr(C)]
struct Tagged {
  tag: puns::num::u8c<1>,
  value: u8,
}

#[test]
fn from_other_than_enums() {
  // Every byte is a `Maybe<NonZero<u8>>`: proven, nothing checked.
  assert_eq!(puns::pun::<u8, Maybe<NonZero<u8>>>(0), Maybe::No);
  assert_eq!(puns::pun::<u32, Maybe<NonZero<u32>>>(9), Maybe::Yes(NonZero::new(9).unwrap()));
  // A `bool` is a `MaybeFlag`, which only some bytes are.
  assert_eq!(puns::pun::<bool, MaybeFlag>(true), MaybeFlag::Flag(true));
  assert_eq!(puns::try_pun::<u8, MaybeFlag>(1), Ok(MaybeFlag::Flag(true)));
  // A tag that is always the one of `Some`, and what `Some` holds.
  let some: Opt = puns::pun(Tagged { tag: puns::num::u8c::new(), value: 7 });
  assert_eq!(some, Opt::Some(7));
  // A tag of `None` or of `Some`, and a byte that `None` does not look at.
  assert_eq!(puns::pun::<(bool, u8), Opt>((false, 7)), Opt::None);
  assert_eq!(puns::pun::<(bool, u8), Opt>((true, 7)), Opt::Some(7));
  // Not every byte is a tag: checked.
  assert_eq!(puns::try_pun::<[u8; 2], Opt>([2, 0]), Err(Error::Validity));
}

// --- `Option` and `Result` ------------------------------------------------------------------------

#[test]
fn option_and_result() {
  // A niche in a `NonZero`: every `u32` is an `Option<NonZero<u32>>`, both ways proven.
  assert_eq!(puns::pun::<Option<NonZero<u32>>, u32>(None), 0);
  assert_eq!(puns::pun::<u32, Option<NonZero<u32>>>(5), NonZero::new(5));
  let none: &[Option<NonZero<u16>>] = puns::pun_ref(&[0u16, 3][..]);
  assert_eq!(none, &[None, NonZero::new(3)]);
  // A niche in a `bool`: `None` is 2, checked from a byte, proven from a `bool`.
  assert_eq!(puns::pun::<Option<bool>, u8>(None), 2);
  assert_eq!(puns::try_pun::<u8, Option<bool>>(2), Ok(None));
  assert_eq!(puns::try_pun::<u8, Option<bool>>(1), Ok(Some(true)));
  assert_eq!(puns::try_pun::<u8, Option<bool>>(3), Err(Error::Validity));
  assert_eq!(puns::pun::<bool, Option<bool>>(false), Some(false));
  // Two niches in one byte: `Some(None)` is 2, `None` where `rustc` put it (255 today, below the
  // valid values rather than after them), which is found, not assumed.
  let none: u8 = puns::pun(None::<Option<bool>>);
  assert!(none > 2);
  assert_eq!(puns::try_pun::<u8, Option<Option<bool>>>(none), Ok(None));
  assert_eq!(puns::try_pun::<u8, Option<Option<bool>>>(2), Ok(Some(None)));
  assert_eq!(puns::try_pun::<u8, Option<Option<bool>>>(none ^ 1), Err(Error::Validity));
  // A niche in a `char`.
  let none: u32 = puns::pun(None::<char>);
  assert_eq!(puns::try_pun::<u32, Option<char>>(none), Ok(None));
  assert_eq!(puns::try_pun::<u32, Option<char>>(0x41), Ok(Some('A')));
  // A direct tag of two bytes (widened to the alignment of the `u16`): no niche in a `u16`.
  assert_eq!(puns::try_pun::<[u8; 4], Option<u16>>([0, 0, 9, 9]), Ok(None));
  assert!(puns::try_pun::<[u8; 4], Option<u16>>([0, 9, 9, 9]).is_err());
  assert!(puns::try_pun::<[u8; 4], Option<u16>>([2, 0, 0, 0]).is_err());
  // `Ok` alone has values: a struct of its field.
  assert_eq!(puns::pun::<Result<u32, Infallible>, u32>(Ok(7)), 7);
  assert_eq!(puns::pun::<u32, Result<u32, Infallible>>(7), Ok(7));
  // Both have values: a tag, then the field.
  let err = puns::pun::<Result<u8, bool>, [u8; 2]>(Err(true));
  assert_eq!(puns::try_pun::<[u8; 2], Result<u8, bool>>(err), Ok(Err(true)));
  // Proven between them, variant by variant.
  assert_eq!(puns::pun::<Result<NonZero<u8>, bool>, Result<u8, u8>>(Err(true)), Err(1));
}

/// A field restricted to a value, in a struct whose rule no value meets: safe code makes values of
/// it all the same (`valid(..)` and rules are requirements, not what the type system enforces).
#[derive(Pun, Clone, Copy)]
#[repr(C)]
#[pun(assert(x == 1 && x == 2))]
struct Impossible {
  #[pun(valid(3))]
  x: u8,
}

#[derive(Pun, Clone, Copy)]
enum HoldsImpossible {
  A(Impossible),
  B(u16),
}

/// "No value" is only said of what the type system makes so (a field of a type without values),
/// never of what a requirement excludes: a variant believed to have no value would be left out of
/// what a source can be.
#[test]
fn no_value_is_what_the_type_system_says() {
  assert!(<Impossible as puns::Pun>::LAYOUT.is_inhabited());
  let layout = <HoldsImpossible as puns::Pun>::LAYOUT;
  assert!(layout.is_inhabited());
  // Both variants have tags: `A` is one that a value can be.
  assert_ne!(layout.variant_ranges(0), Some(&[][..]));
  assert_ne!(layout.variant_ranges(1), Some(&[][..]));
  // And a type without values is said to have none, as is what holds one.
  assert!(!<Infallible as puns::Pun>::LAYOUT.is_inhabited());
  assert!(!<(u8, Infallible) as puns::Pun>::LAYOUT.is_inhabited());
  assert!(<[Infallible; 0] as puns::Pun>::LAYOUT.is_inhabited());
  assert!(<Never as puns::Pun>::LAYOUT.is_inhabited());
  assert_eq!(<Never as puns::Pun>::LAYOUT.variant_ranges(1), None);
}