rust_spec/layout.rs
1//! Representation stability and trap/robustness classification.
2//!
3//! Stability describes whether Rust guarantees a type's layout whereas robustness describes
4//! whether the represented value space has invalid bit patterns (i.e. trap values).
5//!
6//! This module provides the marker types used by [`crate::RustSpec::Layout`] and [`crate::RustSpec::Trap`].
7use core::ops::Add;
8
9use crate::{Stable, Unstable};
10
11/// Closed family of representation-stability classifications.
12#[sealed::sealed]
13pub trait LayoutKind {}
14
15/// Marker for a type whose bit patterns require no representation-validity conversion.
16///
17/// This marker also includes the validity of pointees, not just the immediate values.
18///
19/// This marker does not establish provenance, alignment, lifetimes, or aliasing requirements
20/// for pointers or references. For example, `Option<&u8>` is `Robust`, but a non-null
21/// representation must still be a valid reference before constructing the value.
22pub enum Robust {}
23
24/// Marker for a non-robust type that has a trap value.
25pub enum NonRobust {}
26
27#[sealed::sealed]
28impl LayoutKind for Stable {}
29
30#[sealed::sealed]
31impl LayoutKind for Unstable {}
32
33impl<K> Add<K> for NonRobust {
34 type Output = Self;
35
36 fn add(self, _: K) -> Self::Output {
37 unreachable!()
38 }
39}
40
41impl<K> Add<K> for Robust {
42 type Output = K;
43
44 fn add(self, _: K) -> Self::Output {
45 unreachable!()
46 }
47}
48
49impl<K> Add<K> for Unstable {
50 type Output = Self;
51
52 fn add(self, _: K) -> Self::Output {
53 unreachable!()
54 }
55}
56
57impl Add<Unstable> for Stable {
58 type Output = Unstable;
59
60 fn add(self, _: Unstable) -> Self::Output {
61 unreachable!()
62 }
63}
64
65impl Add for Stable {
66 type Output = Self;
67
68 fn add(self, _: Self) -> Self::Output {
69 unreachable!()
70 }
71}