use crate::{
Fields, Message, Pun, Variants,
analysis::{Analysis, analyze},
layout::{Field, Kind, Variant},
};
const LARGER: &str = "behind a pointer, the target is larger than what it is read from";
const UNINIT: &str = "behind a pointer, a byte that the target requires to be initialized is \
padding in what it is read from";
const UNPROVEN: &str = "behind a pointer, a pointer of the target is not a pointer of the source \
whose target it can read: only a pointer proves a pointer";
const UNCHECKED: &str = "behind a pointer, some values of the source are not valid for the \
target, or do not prove one of its rules: nothing is checked behind a \
pointer.\nGive the target a type that every value of the source is valid \
for";
const UNPAIRED: &str = "a pointer of the target is not, in the source, a pointer of the same \
scheme and offset that aims at the same kind of thing (a value, a \
sequence, text): only a pointer proves a pointer";
const FIELDS: &str = "a field of the target that holds a pointer is not paired with a field of \
the source at the same offset.\nFields are paired in the order of their \
declarations: give both structs the same fields, in the same order, up to \
the last one that holds a pointer";
const VARIANTS: &str = "a variant of the target that holds a pointer is not paired with the \
variant of the source of the same tag.\nVariants are paired in the order \
of their declarations: keep the variants of the source where they are, and \
add new ones after them";
const ELEMENTS: &str = "elements of the target that hold a pointer are not paired with elements \
of the source of the same size";
const LISTS: &str = "an implementation of `Pun` lists fields, variants, elements or a target of a \
pointer (`Fields`, `Variants`, `Elem`, `Pointee`) that are not those of its \
layout: pointers cannot be followed through it";
const fn require(why: Option<&'static str>) {
if let Some(why) = why {
Message::new(why).panic();
}
}
#[inline(always)]
pub fn proof<S: ?Sized + Pun, D: ?Sized + Pun>() {
const { require(local::<S, D>(0)) };
pair::<S, D>();
}
fn target<X: Pun, Y: Pun>() {
const { require(behind::<X, Y>(0)) };
pair::<X, Y>();
}
#[inline(always)]
fn pair<A: ?Sized + Pun, B: ?Sized + Pun>() {
let _: fn() = target::<A::Pointee, B::Pointee>;
if const { B::LAYOUT.has_pointers() } {
fields::<A::Fields, B::Fields>();
variants::<A::Variants, B::Variants>();
pair::<A::Elem, B::Elem>();
}
}
#[inline(always)]
fn fields<FA: Fields, FB: Fields>() {
if const { FB::FIELD.is_some() } {
pair::<FA::Type, FB::Type>();
fields::<FA::Next, FB::Next>();
}
}
#[inline(always)]
fn variants<VA: Variants, VB: Variants>() {
if const { VB::TAG.is_some() } {
fields::<VA::Fields, VB::Fields>();
variants::<VA::Next, VB::Next>();
}
}
pub const fn behind<X: Pun, Y: Pun>(fuel: usize) -> Option<&'static str> {
if X::LAYOUT.size() == 0 || Y::LAYOUT.size() == 0 {
return None;
}
let Analysis { fit, relation, .. } = analyze(&X::LAYOUT, &Y::LAYOUT);
if !fit.shrink {
return Some(LARGER);
}
if relation.uninit.is_some() {
return Some(UNINIT);
}
if relation.pointer.is_some() {
return Some(UNPROVEN);
}
if relation.disjoint.is_some() || relation.partial.is_some() {
return Some(UNCHECKED);
}
local::<X, Y>(fuel)
}
pub const fn local<A: ?Sized + Pun, B: ?Sized + Pun>(fuel: usize) -> Option<&'static str> {
let (a, b) = (A::LAYOUT, B::LAYOUT);
if !b.has_pointers() {
return None;
}
if !listed::<A>() || !listed::<B>() {
return Some(LISTS);
}
match b.kind {
Kind::Pointer(to) => match a.kind {
Kind::Pointer(from) if from.reads_as(&to).is_some() => {
if fuel == 0 {
None
} else {
behind::<A::Pointee, B::Pointee>(fuel - 1)
}
}
_ => Some(UNPAIRED),
},
Kind::Struct(..) => local_fields::<A::Fields, B::Fields>(fuel),
Kind::Enum(_) => local_variants::<A::Variants, A::Variants, B::Variants>(fuel),
Kind::Array(..) | Kind::Slice(_) => {
let elements = matches!(a.kind, Kind::Array(..) | Kind::Slice(_))
&& <A::Elem as Pun>::LAYOUT.size() == <B::Elem as Pun>::LAYOUT.size();
if elements { local::<A::Elem, B::Elem>(fuel) } else { Some(ELEMENTS) }
}
Kind::Scalar(_) | Kind::Pattern(_) => None,
}
}
const fn local_fields<FA: Fields, FB: Fields>(fuel: usize) -> Option<&'static str> {
let Some(b) = FB::FIELD else { return None };
if b.layout.has_pointers() {
match FA::FIELD {
Some(a) if a.offset() == b.offset() => {
if let Some(why) = local::<FA::Type, FB::Type>(fuel) {
return Some(why);
}
}
_ => return Some(FIELDS),
}
}
local_fields::<FA::Next, FB::Next>(fuel)
}
const fn local_variants<A0: Variants, VA: Variants, VB: Variants>(
fuel: usize,
) -> Option<&'static str> {
let Some(tag) = VB::TAG else { return None };
if has_pointers::<VB::Fields>() {
match VA::TAG {
Some(other) if other == tag => {
if let Some(why) = local_fields::<VA::Fields, VB::Fields>(fuel) {
return Some(why);
}
}
_ if !has_tag::<A0>(tag) && !has_pointers::<VA::Fields>() => {}
_ => return Some(VARIANTS),
}
}
local_variants::<A0, VA::Next, VB::Next>(fuel)
}
const fn listed<T: ?Sized + Pun>() -> bool {
match T::LAYOUT.kind {
Kind::Struct(fields, _) => listed_fields::<T::Fields>(fields, 0),
Kind::Enum(layout) => listed_variants::<T::Variants>(layout.variants, 0),
Kind::Array(elem, _) | Kind::Slice(elem) => <T::Elem as Pun>::LAYOUT.equals(elem),
Kind::Pointer(pointer) => {
let target = <T::Pointee as Pun>::LAYOUT;
target.size() == pointer.size && target.align() == pointer.align
}
Kind::Scalar(_) | Kind::Pattern(_) => true,
}
}
const fn listed_fields<F: Fields>(fields: &[Field], index: usize) -> bool {
match F::FIELD {
None => index == fields.len(),
Some(field) => {
index < fields.len()
&& field.index == index
&& (!field.layout.has_pointers() || <F::Type as Pun>::LAYOUT.equals(field.layout))
&& listed_fields::<F::Next>(fields, index + 1)
}
}
}
const fn listed_variants<V: Variants>(variants: &[Variant], index: usize) -> bool {
match V::TAG {
None => index == variants.len(),
Some(tag) => {
index < variants.len()
&& variants[index].tag == tag
&& listed_fields::<V::Fields>(variants[index].fields(), 0)
&& listed_variants::<V::Next>(variants, index + 1)
}
}
}
const fn has_pointers<F: Fields>() -> bool {
match F::FIELD {
Some(field) => field.layout.has_pointers() || has_pointers::<F::Next>(),
None => false,
}
}
const fn has_tag<V: Variants>(tag: u128) -> bool {
match V::TAG {
Some(other) => other == tag || has_tag::<V::Next>(tag),
None => false,
}
}