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
638
639
//! `validate_in` and `validate_elems_in`: a value in a buffer, everything but what its pointers
//! aim at, each pointer handed to a visitor. What each kind of layout answers (scalars, structs
//! and their rules, enums and niches, unions, `MaybeUninit`, `Option` and `Result`, pointers),
//! where an error is, and that no input makes them read out of the bytes or panic (under Miri on
//! every target of CI). And `pointers_in` and `pointers_elems_in`: on what `validate_in` accepts,
//! the same pointers, in the same order. And `copy_in`: the bytes that a value holds initialized,
//! and no other, from a source whose other bytes are uninitialized. Neither needs the rules of the
//! value to hold, which safe code can break: they read no rule.

#[path = "support/relative.rs"]
#[expect(dead_code, reason = "what the model check uses, and these tests do not")]
mod relative;

use core::{mem::MaybeUninit, num::NonZero};

use puns::{Invalid, Pun, Visit, num::u8in};
use relative::{Builder, Fix, One, Opt, Region, Seq, Text, addr, field};

/// Records every pointer handed over: its position, and what it is.
#[derive(Default)]
struct Record {
  pointers: Vec<Handed>,
  /// Refuse the pointer at this position.
  refuse: Option<usize>,
}

#[derive(Debug, PartialEq, Eq)]
enum Stop {
  Invalid(usize),
  Refused(usize),
}

impl From<Invalid> for Stop {
  fn from(invalid: Invalid) -> Self {
    Self::Invalid(invalid.at)
  }
}

impl Visit for Record {
  type Error = Stop;

  fn pointer<P: ?Sized + Pun>(&mut self, at: usize) -> Result<(), Stop> {
    let pointer = const { P::LAYOUT.as_pointer() }.expect("only pointers are handed over");
    if self.refuse == Some(at) {
      return Err(Stop::Refused(at));
    }
    self.pointers.push((at, pointer.aim(), pointer.is_nullable()));
    Ok(())
  }
}

/// `validate_in::<T>` of `bytes` at `at`, and the pointers handed over.
/// A pointer handed over: its position, what it aims at, whatever it may be null.
type Handed = (usize, puns::layout::Aim, bool);

fn check<T: Pun>(bytes: &[u8], at: usize) -> (Result<(), Stop>, Vec<Handed>) {
  let mut record = Record::default();
  let result = puns::validate_in::<T, Record>(bytes, at, &mut record);
  (result, record.pointers)
}

fn ok<T: Pun>(bytes: &[u8], at: usize) -> Result<(), Stop> {
  check::<T>(bytes, at).0
}

/// `check::<T>`, and if `validate_in` accepts the bytes, the pointers that `pointers_in` hands over
/// on them, which must be the same.
fn walk<T: Pun>(bytes: &[u8], at: usize) -> (Result<(), Stop>, Vec<Handed>) {
  let (result, handed) = check::<T>(bytes, at);
  if result.is_ok() {
    let mut record = Record::default();
    // SAFETY: `validate_in` accepted the bytes as a `T` at `at`, in bounds.
    let walked = unsafe { puns::pointers_in::<T, Record>(bytes.as_ptr(), at, &mut record) };
    assert_eq!(walked, Ok(()), "{}: at {at}", core::any::type_name::<T>());
    assert_eq!(record.pointers, handed, "{}: at {at}", core::any::type_name::<T>());
  }
  (result, handed)
}

// -------------------------------------------------------------------------------------------------
// Values without pointers

#[derive(Pun)]
#[repr(C)]
#[pun(assert(lo <= hi))]
struct Span {
  lo: u8,
  #[pun(valid(..=9))]
  hi: u8,
  on: bool,
}

#[test]
fn scalars_structs_and_rules() {
  assert_eq!(ok::<u32>(&[1, 2, 3, 4], 0), Ok(()));
  assert_eq!(ok::<bool>(&[0, 1, 2], 1), Ok(()));
  assert_eq!(ok::<bool>(&[0, 1, 2], 2), Err(Stop::Invalid(2)));
  assert_eq!(ok::<char>(&0xd800u32.to_ne_bytes(), 0), Err(Stop::Invalid(0)));
  assert_eq!(ok::<NonZero<i16>>(&[0, 0, 0xff, 0xff], 2), Ok(()));
  assert_eq!(ok::<NonZero<i16>>(&[0, 0, 0xff, 0xff], 0), Err(Stop::Invalid(0)));
  // The first thing that is not valid, in the order of the positions; the rule after the fields.
  let bytes = [7, 1, 2, 1, 3, 4, 2];
  assert_eq!(ok::<Span>(&bytes, 1), Ok(()));
  assert_eq!(ok::<Span>(&bytes, 4), Err(Stop::Invalid(6))); // `on` is 2
  assert_eq!(ok::<Span>(&[3, 2, 1], 0), Err(Stop::Invalid(0))); // `lo <= hi`
  assert_eq!(ok::<Span>(&[3, 10, 0], 0), Err(Stop::Invalid(1))); // `valid(..=9)`
  assert_eq!(ok::<[Span; 2]>(&[1, 2, 0, 1, 2, 3], 0), Err(Stop::Invalid(5)));
  // A `u8in`, which guarantees its values.
  assert_eq!(ok::<u8in<1, 3>>(&[0, 4], 1), Err(Stop::Invalid(1)));
}

#[derive(Pun, Clone, Copy)]
#[repr(u8)]
#[expect(dead_code, reason = "made of bytes")]
enum Shape {
  Dot,
  Line(bool),
  Box(u8, NonZero<u8>),
}

/// An enum without `repr`, with a niche: `None` is 2 in the `bool`.
#[test]
fn enums_and_niches() {
  assert_eq!(ok::<Shape>(&[2, 7, 1], 0), Ok(()));
  assert_eq!(ok::<Shape>(&[1, 2, 9], 0), Err(Stop::Invalid(1))); // the `bool`
  assert_eq!(ok::<Shape>(&[3, 0, 0], 0), Err(Stop::Invalid(0))); // no such tag
  assert_eq!(ok::<Shape>(&[0, 9, 9], 0), Ok(())); // a `Dot`: the rest is padding
  assert_eq!(ok::<Option<bool>>(&[2], 0), Ok(()));
  assert_eq!(ok::<Option<bool>>(&[3], 0), Err(Stop::Invalid(0)));
  assert_eq!(ok::<Option<NonZero<u16>>>(&[0, 0], 0), Ok(()));
  assert_eq!(ok::<Result<u8, bool>>(puns::as_bytes(&Ok::<u8, bool>(9)), 0), Ok(()));
  let err = puns::as_bytes(&Err::<bool, bool>(true)).to_vec();
  assert_eq!(ok::<Result<bool, bool>>(&err, 0), Ok(()));
  // The byte of the `bool` of `Err`, made 2: where it is, whatever the layout `rustc` chose.
  let mut bad = err;
  let at = bad.iter().position(|byte| *byte == 1).expect("the `true`");
  bad[at] = 2;
  assert!(ok::<Result<bool, bool>>(&bad, 0).is_err());
}

#[derive(Pun, Clone, Copy)]
#[repr(C)]
union Choice {
  flag: bool,
  small: u8in<0, 3>,
}

/// A union is one of its fields; `MaybeUninit` is anything.
#[test]
fn unions_and_maybe_uninit() {
  assert_eq!(ok::<Choice>(&[3], 0), Ok(()));
  assert_eq!(ok::<Choice>(&[4], 0), Err(Stop::Invalid(0)));
  assert_eq!(ok::<MaybeUninit<bool>>(&[9], 0), Ok(()));
  // A `MaybeUninit` of a pointer: no value is required of it, and no pointer handed over.
  assert_eq!(check::<MaybeUninit<One<u8>>>(&[0; 4], 0), (Ok(()), vec![]));
}

// -------------------------------------------------------------------------------------------------
// Values with pointers

#[derive(Pun)]
#[repr(C)]
struct Node {
  id: u8,
  name: Text,
  kids: Seq<Node>,
  next: Opt<Node>,
  flag: bool,
}

#[derive(Pun)]
#[repr(u8)]
#[expect(dead_code, reason = "made of bytes")]
enum Tree {
  Leaf(bool),
  Node(One<Tree>, One<Tree>),
}

/// A pointer whose offset cannot be zero.
#[derive(Pun)]
#[repr(C)]
struct Strict {
  to: One<u8, relative::Nz32le>,
}

/// Each pointer handed over once, in order, with what it is; a null pointer that may be is not;
/// the error of the visitor ends the walk.
#[test]
fn pointers_are_handed_over() {
  use puns::layout::Aim;
  // `id`, then three offsets of 4 bytes, `flag`.
  let mut bytes = vec![5, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1];
  let all =
    vec![(1, Aim::Text, false), (5, Aim::Sequence { align: 4 }, false), (9, Aim::One, true)];
  assert_eq!(walk::<Node>(&bytes, 0), (Ok(()), all.clone()));
  // `next` null: not handed over.
  bytes[9] = 0;
  assert_eq!(walk::<Node>(&bytes, 0), (Ok(()), all[..2].to_vec()));
  // `flag` is 2: the pointers before it were handed over.
  bytes[13] = 2;
  assert_eq!(check::<Node>(&bytes, 0), (Err(Stop::Invalid(13)), all[..2].to_vec()));
  // Refused by the visitor, before `flag` is looked at.
  let mut record = Record { refuse: Some(5), ..Record::default() };
  assert_eq!(puns::validate_in::<Node, Record>(&bytes, 0, &mut record), Err(Stop::Refused(5)));
  // A pointer of offset zero: one whose offset may be zero is one, handed over (its scheme
  // says what it aims at); one whose offset is a `NonZero` is not valid there.
  let here = [1u8, 0, 0, 0, 0, 1, 0, 0, 0];
  assert_eq!(walk::<Tree>(&here, 0), (Ok(()), vec![(1, Aim::One, false), (5, Aim::One, false)]));
  assert_eq!(check::<Strict>(&[0; 4], 0), (Err(Stop::Invalid(0)), vec![]));
  assert_eq!(walk::<Strict>(&[4, 0, 0, 0], 0), (Ok(()), vec![(0, Aim::One, false)]));
  // The pointers of the variant that the tag tells, and only those.
  let node = [1u8, 4, 0, 0, 0, 8, 0, 0, 0];
  assert_eq!(walk::<Tree>(&node, 0), (Ok(()), vec![(1, Aim::One, false), (5, Aim::One, false)]));
  let leaf = [0u8, 1, 0, 0, 0, 1, 0, 0, 0];
  assert_eq!(walk::<Tree>(&leaf, 0), (Ok(()), vec![]));
}

/// `pointers_in` checks nothing, and hands over what `validate_in` does: the pointers of the
/// variant that the tag tells, not a null one that may be, in order, and an error of the visitor
/// ends the walk. It reads only what `validate_in` reads: the bytes that the variant does not
/// hold are uninitialized here.
#[test]
fn pointers_in_hands_over_the_same() {
  use puns::layout::Aim;
  let mut node = MaybeUninit::<[u8; 9]>::uninit();
  let mut leaf = MaybeUninit::<[u8; 9]>::uninit();
  // SAFETY: the bytes of a `Tree::Node` that are written, then those of a `Tree::Leaf`: the tag
  // and the `bool`; the rest stays uninitialized.
  unsafe {
    node.as_mut_ptr().cast::<[u8; 9]>().write([1, 4, 0, 0, 0, 8, 0, 0, 0]);
    leaf.as_mut_ptr().cast::<[u8; 2]>().write([0, 1]);
  }
  let mut record = Record::default();
  // SAFETY: the bytes of a `Tree` that `validate_in` accepts (above, `node` and `leaf`); the
  // uninitialized ones are those it does not read.
  unsafe {
    puns::pointers_in::<Tree, _>(node.as_ptr().cast(), 0, &mut record).expect("no refusal");
    assert_eq!(record.pointers, vec![(1, Aim::One, false), (5, Aim::One, false)]);
    puns::pointers_in::<Tree, _>(leaf.as_ptr().cast(), 0, &mut record).expect("no refusal");
    assert_eq!(record.pointers.len(), 2);
  }
  // The error of the visitor ends the walk: the second pointer is not handed over.
  let bytes = [5, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1];
  let mut record = Record { refuse: Some(5), ..Record::default() };
  // SAFETY: a `Node` that `validate_in` accepts (`pointers_are_handed_over`).
  let walked = unsafe { puns::pointers_in::<Node, _>(bytes.as_ptr(), 0, &mut record) };
  assert_eq!((walked, record.pointers), (Err(Stop::Refused(5)), vec![(1, Aim::Text, false)]));
  // At any alignment: the same `Node` one byte further.
  let mut later = vec![0];
  later.extend_from_slice(&bytes);
  let (result, handed) = walk::<Node>(&later, 1);
  assert_eq!(result, Ok(()));
  assert_eq!(handed.iter().map(|p| p.0).collect::<Vec<_>>(), [2, 6, 10]);
  // An array of values with pointers, and their elements.
  let pair = [1u8, 4, 0, 0, 0, 8, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0];
  let (_, handed) = walk::<[Tree; 2]>(&pair, 0);
  assert_eq!(handed.iter().map(|p| p.0).collect::<Vec<_>>(), [1, 5]);
  let mut record = Record::default();
  // SAFETY: two `Tree`s, which `validate_elems_in` accepts.
  unsafe {
    assert_eq!(puns::validate_elems_in::<Tree, _>(&pair, 0, 2, &mut Record::default()), Ok(()));
    puns::pointers_elems_in::<Tree, _>(pair.as_ptr(), 0, 2, &mut record).expect("no refusal");
  }
  assert_eq!(record.pointers.iter().map(|p| p.0).collect::<Vec<_>>(), [1, 5]);
  // Elements without pointers are not looked at, whatever their count.
  // SAFETY: no byte is read of them.
  unsafe {
    puns::pointers_elems_in::<bool, _>(core::ptr::null(), 0, usize::MAX, &mut record)
      .expect("nothing");
  }
}

/// The elements one after the other: by blocks without pointers, one by one with.
#[test]
fn elements() {
  let mut record = Record::default();
  let bytes = [1u8, 0, 1, 1, 0, 2, 1];
  assert_eq!(puns::validate_elems_in::<bool, _>(&bytes, 0, 5, &mut record), Ok(()));
  assert_eq!(puns::validate_elems_in::<bool, _>(&bytes, 0, 6, &mut record), Err(Stop::Invalid(5)));
  assert_eq!(puns::validate_elems_in::<bool, _>(&bytes, 3, 5, &mut record), Err(Stop::Invalid(3)));
  assert_eq!(puns::validate_elems_in::<u16, _>(&bytes, 1, 3, &mut record), Ok(()));
  assert_eq!(puns::validate_elems_in::<u16, _>(&bytes, 1, 4, &mut record), Err(Stop::Invalid(1)));
  // Many, by blocks: the first invalid one.
  let mut many = vec![1u8; 1000];
  many[777] = 9;
  assert_eq!(
    puns::validate_elems_in::<bool, _>(&many, 0, 1000, &mut record),
    Err(Stop::Invalid(777))
  );
  assert_eq!(puns::validate_elems_in::<bool, _>(&many, 0, 777, &mut record), Ok(()));
  // With pointers: each, in order.
  let ones = [4u8, 0, 0, 0, 4, 0, 0, 0];
  assert_eq!(puns::validate_elems_in::<One<u8>, _>(&ones, 0, 2, &mut record), Ok(()));
  assert_eq!(record.pointers.iter().map(|p| p.0).collect::<Vec<_>>(), [0, 4]);
  // A count whose bytes overflow, or that is past the end.
  assert_eq!(
    puns::validate_elems_in::<u16, _>(&bytes, 1, usize::MAX, &mut record),
    Err(Stop::Invalid(1))
  );
  assert_eq!(
    puns::validate_elems_in::<u8, _>(&bytes, usize::MAX, 1, &mut record),
    Err(Stop::Invalid(usize::MAX))
  );
  // No bytes, and none needed.
  assert_eq!(puns::validate_elems_in::<(), _>(&[], 0, usize::MAX, &mut record), Ok(()));
}

// -------------------------------------------------------------------------------------------------
// Any input

/// A small deterministic generator.
struct Rng(u64);

impl Rng {
  const fn next(&mut self) -> u64 {
    self.0 ^= self.0 >> 12;
    self.0 ^= self.0 << 25;
    self.0 ^= self.0 >> 27;
    self.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
  }
}

/// Random bytes, random positions (past the end too), random counts (some that overflow): no
/// panic and no read out of the bytes (which Miri would report), and what is accepted is what
/// the same check of each element accepts.
#[test]
fn any_input() {
  fn all<T: Pun>(rng: &mut Rng, bytes: &[u8]) {
    let len = bytes.len();
    let at = match rng.next() % 4 {
      0 => usize::MAX - (rng.next() % 8) as usize,
      _ => (rng.next() % (len as u64 + 4)) as usize,
    };
    let count = match rng.next() % 4 {
      0 => usize::MAX / (1 + (rng.next() % 3) as usize),
      _ => (rng.next() % 8) as usize,
    };
    let one = walk::<T>(bytes, at).0;
    let mut record = Record::default();
    let many = puns::validate_elems_in::<T, _>(bytes, at, count, &mut record);
    if many.is_ok() && count > 0 {
      assert_eq!(one, Ok(()), "{}: at {at}, {count}", core::any::type_name::<T>());
      // The same pointers, from `pointers_elems_in`.
      let mut walked = Record::default();
      // SAFETY: `validate_elems_in` accepted the `count` values, in bounds.
      unsafe { puns::pointers_elems_in::<T, _>(bytes.as_ptr(), at, count, &mut walked) }
        .expect("no refusal");
      assert_eq!(walked.pointers, record.pointers, "{}: at {at}", core::any::type_name::<T>());
    }
  }
  let mut rng = Rng(0x1234_5678_9abc_def1);
  let rounds = if cfg!(miri) { 40 } else { 4000 };
  for _ in 0..rounds {
    let len = (rng.next() % 24) as usize;
    let bytes: Vec<u8> = (0..len).map(|_| rng.next() as u8 & 0x3).collect();
    all::<bool>(&mut rng, &bytes);
    all::<Span>(&mut rng, &bytes);
    all::<Shape>(&mut rng, &bytes);
    all::<Choice>(&mut rng, &bytes);
    all::<Option<bool>>(&mut rng, &bytes);
    all::<Node>(&mut rng, &bytes);
    all::<Tree>(&mut rng, &bytes);
    all::<[NonZero<u16>; 3]>(&mut rng, &bytes);
    all::<()>(&mut rng, &bytes);
    // `copy_in` of what is accepted (not of a union, which it refuses).
    let at = (rng.next() % (len as u64 + 1)) as usize;
    copied::<bool>(&bytes, at);
    copied::<Span>(&bytes, at);
    copied::<Shape>(&bytes, at);
    copied::<Option<bool>>(&bytes, at);
    copied::<Node>(&bytes, at);
    copied::<Tree>(&bytes, at);
    copied::<[NonZero<u16>; 3]>(&bytes, at);
    copied::<()>(&bytes, at);
  }
}

/// If `validate_in` accepts a `T` at `at`: `copy_in` of it into bytes all `0xee` makes a `T` that
/// `validate_in` accepts too, with the same pointers (at the same places in the value), every byte
/// either the source's or `0xee`.
fn copied<T: Pun>(bytes: &[u8], at: usize) {
  let (result, handed) = check::<T>(bytes, at);
  if result.is_err() {
    return;
  }
  let size = size_of::<T>();
  let mut dst = vec![0xee; size];
  // SAFETY: `validate_in` accepted a `T` at `at`, in bounds; `dst` holds `size` bytes.
  unsafe { puns::copy_in::<T>(bytes[at..].as_ptr(), dst.as_mut_ptr()) };
  let name = core::any::type_name::<T>();
  for (i, byte) in dst.iter().enumerate() {
    assert!(*byte == bytes[at + i] || *byte == 0xee, "{name}: at {at}, byte {i}");
  }
  // The bytes of the pointers handed over are left to the caller.
  for &(p, ..) in &handed {
    assert_eq!(dst[p - at], 0xee, "{name}: at {at}, the pointer at {p}");
  }
  let (again, moved) = check::<T>(&dst, 0);
  assert_eq!(again, Ok(()), "{name}: at {at}");
  let shifted: Vec<Handed> = handed.into_iter().map(|(p, aim, null)| (p - at, aim, null)).collect();
  assert_eq!(moved, shifted, "{name}: at {at}");
}

/// No value.
#[derive(Pun)]
#[expect(clippy::empty_enums, reason = "a type without values is what is tested")]
enum Never {}

/// A struct that holds a type without values: none either.
#[derive(Pun)]
#[repr(C)]
struct Holds {
  id: u8,
  never: Never,
}

/// A type that has no value is never valid, whatever the bytes: it has something to check (its
/// layout is not free of constraints, `is_free` is `leaves == 0`), so `validate_in` does not
/// return early, and what it checks fails.
#[test]
fn types_without_values() {
  use core::convert::Infallible;
  fn never<T: Pun>(bytes: &[u8]) {
    assert!(!const { T::LAYOUT.is_free() }, "{}", core::any::type_name::<T>());
    assert!(!const { T::LAYOUT.is_inhabited() }, "{}", core::any::type_name::<T>());
    for at in 0..=bytes.len() {
      assert!(ok::<T>(bytes, at).is_err(), "{} at {at}", core::any::type_name::<T>());
    }
    let mut record = Record::default();
    for count in 1..3 {
      assert!(puns::validate_elems_in::<T, _>(bytes, 0, count, &mut record).is_err());
    }
  }
  let bytes = [0u8, 1, 2, 0xff];
  never::<Never>(&bytes);
  never::<Infallible>(&bytes);
  never::<Holds>(&bytes);
  never::<[Never; 2]>(&bytes);
  never::<(u8, Infallible)>(&bytes);
  never::<Result<Infallible, Never>>(&bytes);
  // No element of a type without values: one value, of no byte.
  assert_eq!(ok::<[Never; 0]>(&bytes, 4), Ok(()));
  // `Option<Infallible>`: `None` only, whatever its bytes (it has none).
  assert_eq!(size_of::<Option<Infallible>>(), 0);
  assert_eq!(ok::<Option<Infallible>>(&bytes, 0), Ok(()));
  // `Result<u8, Infallible>`: an `Ok` only.
  assert_eq!(ok::<Result<u8, Infallible>>(&[7], 0), Ok(()));
}

/// Bytes at an address aligned for anything here.
#[repr(C, align(16))]
struct Aligned([u8; 32]);

/// For types without pointers, `validate_in` says what `try_pun_ref` says, on a value and on
/// every copy of it with one byte changed (by every mask of one bit and all bits); and
/// `validate_elems_in` of two what `try_pun_ref` to a slice of two says.
#[test]
fn the_verdict_is_the_one_of_try_pun_ref() {
  /// From the bytes of a value of `T`.
  fn agree<T: Pun>(value: &[u8]) {
    let size = size_of::<T>();
    assert_eq!(value.len(), size);
    let mut buffer = Aligned([0; 32]);
    buffer.0[..size].copy_from_slice(value);
    buffer.0[size..2 * size].copy_from_slice(value);
    let masks: &[u8] =
      if cfg!(miri) { &[0x01, 0xff] } else { &[1, 2, 4, 8, 16, 32, 64, 128, 0xff] };
    for at in 0..2 * size {
      for &mask in masks {
        buffer.0[at] ^= mask;
        let bytes = &buffer.0[..2 * size];
        let mut record = Record::default();
        let ours = puns::validate_in::<T, _>(bytes, 0, &mut record).is_ok();
        assert_eq!(ours, puns::try_pun_ref::<[u8], T>(&bytes[..size]).is_ok(), "{at} {mask:#x}");
        let ours = puns::validate_elems_in::<T, _>(bytes, 0, 2, &mut record).is_ok();
        assert_eq!(ours, puns::try_pun_ref::<[u8], [T]>(bytes).is_ok(), "{at} {mask:#x}, two");
        buffer.0[at] ^= mask;
      }
    }
  }
  agree::<bool>(&[1]);
  agree::<char>(&('é' as u32).to_ne_bytes());
  agree::<NonZero<u16>>(&258u16.to_ne_bytes());
  agree::<Span>(&[1, 2, 1]);
  agree::<Shape>(&[2, 3, 4]);
  agree::<Shape>(&[1, 1, 0]);
  agree::<Choice>(&[1]);
  agree::<Option<bool>>(&[0]);
  agree::<[NonZero<u16>; 3]>(&[1, 1, 1, 1, 1, 1]);
  agree::<Result<u8, bool>>(puns::as_bytes(&Ok::<u8, bool>(7)));
  agree::<Result<u8, bool>>(puns::as_bytes(&Err::<u8, bool>(true)));
  agree::<u8in<1, 3>>(&[2]);
  agree::<Option<NonZero<u32>>>(&9u32.to_ne_bytes());
}

// -------------------------------------------------------------------------------------------------
// `copy_in`

#[derive(Pun, Clone, Copy)]
#[repr(C)]
struct Padded {
  a: u8,
  b: u32,
}

#[derive(Pun, Clone, Copy)]
#[repr(u8)]
#[expect(dead_code, reason = "read as bytes")]
enum Two {
  Small(u8),
  Big(u32),
}

#[derive(Pun, Clone, Copy)]
#[repr(C)]
struct Mixed {
  two: Two,
  padded: [Padded; 2],
  maybe: Option<bool>,
  any: MaybeUninit<u16>,
}

/// The bytes of `value` that `copy_in` copies into memory of `fill` bytes: read whole, as a
/// `&[u8]`, which Miri refuses if one of them is uninitialized.
fn copy_of<T: Pun>(value: &T, fill: u8) -> Vec<u8> {
  let mut dst = vec![fill; size_of::<T>()];
  // SAFETY: a valid `T` (padding and the other variants uninitialized); `dst` is its size.
  unsafe { puns::copy_in::<T>(core::ptr::from_ref(value).cast(), dst.as_mut_ptr()) };
  dst
}

/// A value on the stack, whose padding and other variants are uninitialized: `copy_in` copies
/// its scalars, tags and the fields of its variants, and leaves every other byte of the
/// destination as it was (here, read whole).
#[test]
fn copy_in_copies_the_value_and_nothing_else() {
  let b = 0x0102_0304_u32.to_ne_bytes();
  let padded = Padded { a: 9, b: 0x0102_0304 };
  assert_eq!(copy_of(&padded, 0), [&[9, 0, 0, 0][..], &b].concat());
  assert_eq!(copy_of(&padded, 0xaa), [&[9, 0xaa, 0xaa, 0xaa][..], &b].concat());
  // The other variant is uninitialized: only the tag and the field of `Small`.
  assert_eq!(copy_of(&Two::Small(7), 0), [0, 7, 0, 0, 0, 0, 0, 0]);
  assert_eq!(copy_of(&Two::Big(0x0102_0304), 0xaa), [&[1, 0xaa, 0xaa, 0xaa][..], &b].concat());
  // Nested: an enum, an array of padded structs, a niche, bytes that may be uninitialized.
  let mixed = Mixed {
    two: Two::Small(5),
    padded: [padded, Padded { a: 8, b: 0x0102_0304 }],
    maybe: None,
    any: MaybeUninit::uninit(),
  };
  let copy = copy_of(&mixed, 0xaa);
  let at = |offset: usize| copy[offset];
  assert_eq!((at(0), at(1), at(2)), (0, 5, 0xaa));
  let p = core::mem::offset_of!(Mixed, padded);
  assert_eq!(&copy[p..p + 8], [&[9, 0xaa, 0xaa, 0xaa][..], &b].concat());
  assert_eq!(&copy[p + 8..p + 16], [&[8, 0xaa, 0xaa, 0xaa][..], &b].concat());
  assert_eq!(at(core::mem::offset_of!(Mixed, maybe)), 2);
  let any = core::mem::offset_of!(Mixed, any);
  assert_eq!(&copy[any..any + 2], [0xaa, 0xaa]);
  // What is copied is the value.
  assert!(puns::validate_in::<Mixed, Record>(&copy, 0, &mut Record::default()).is_ok());
  // Pointers are left to the caller (bytes that `validate_in` accepted), but a null one, whose
  // zeros are copied.
  let mut node = [5u8, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1];
  let a = 0xaa;
  for (next, copied) in [(3, [a, a, a, a]), (0, [0, 0, 0, 0])] {
    node[9] = next;
    let mut dst = [a; 14];
    // SAFETY: a `Node` that `validate_in` accepts (`pointers_are_handed_over`), `dst` its size.
    unsafe { puns::copy_in::<Node>(node.as_ptr(), dst.as_mut_ptr()) };
    assert_eq!(dst, [&[5, a, a, a, a, a, a, a, a][..], &copied, &[1]].concat()[..]);
  }
}

/// A struct with pointers and rules: `lo <= hi`, and `level` up to 9.
#[derive(Pun)]
#[repr(C)]
#[pun(assert(lo <= hi))]
struct Ruled {
  lo: u8,
  hi: u8,
  #[pun(valid(..=9))]
  level: u8,
  first: One<u8>,
  next: Opt<u8>,
}

// SAFETY: every pointer, at its position.
unsafe impl Fix for Ruled {
  fn fix(&mut self, at: usize) {
    let whole = addr(self);
    field(whole, at, &mut self.first);
    field(whole, at, &mut self.next);
  }
}

/// `pointers_in` and `copy_in` need a value whose scalars, tags and pointers are what its type
/// says, not its rules: a `Ruled` that breaks both of its rules, as safe code can make one, in a
/// buffer, with a `next` and without. The pointers handed over are those of the value, the bytes
/// copied its scalars (the restricted one too) and its null pointer; `validate_in` refuses it.
#[test]
fn rules_need_not_hold_for_pointers_in_and_copy_in() {
  use puns::layout::Aim;
  let (first, next) = (core::mem::offset_of!(Ruled, first), core::mem::offset_of!(Ruled, next));
  for some in [true, false] {
    let mut b = Builder::new();
    let (one, opt) = (b.one(7u8), b.opt(some.then_some(9u8)));
    let broken = Ruled { lo: 2, hi: 1, level: 12, first: one, next: opt };
    // SAFETY: every pointer made by `b`, with what it aims at, in one value.
    let region = unsafe { Region::new(b, broken) };
    assert_eq!(*region.get(&region.first), 7);
    assert_eq!(region.opt(&region.next).copied(), some.then_some(9));
    let (base, root) = (region.bytes().as_ptr().cast::<u8>(), region.root());

    let mut record = Record::default();
    // SAFETY: a `Ruled` at `root` whose scalars and pointers are valid for their types, which is
    // all that `pointers_in` requires: its rules need not hold.
    unsafe { puns::pointers_in::<Ruled, _>(base, root, &mut record) }.expect("no refusal");
    let mut handed = vec![(root + first, Aim::One, false)];
    if some {
      handed.push((root + next, Aim::One, true));
    }
    assert_eq!(record.pointers, handed);

    let a = 0xaa;
    let mut dst = [a; size_of::<Ruled>()];
    // SAFETY: as above, all that `copy_in` requires; `dst` is the size of a `Ruled`.
    unsafe { puns::copy_in::<Ruled>(base.add(root), dst.as_mut_ptr()) };
    assert_eq!(dst[..3], [2, 1, 12]);
    // A pointer is left to the caller; a null one is copied.
    assert_eq!(dst[first..first + 4], [a; 4]);
    assert_eq!(dst[next..next + 4], if some { [a; 4] } else { [0; 4] });

    // What `validate_in` refuses: `level` (12), then `lo <= hi`, after the pointers.
    assert_eq!(check::<Ruled>(&dst, 0), (Err(Stop::Invalid(2)), vec![]));
    dst[2] = 3;
    assert_eq!(check::<Ruled>(&dst, 0).0, Err(Stop::Invalid(0)));
  }
}