rustc_transmute/maybe_transmutable/
query_context.rs

1use crate::layout;
2
3/// Context necessary to answer the question "Are these types transmutable?".
4pub(crate) trait QueryContext {
5    type Def: layout::Def;
6    type Ref: layout::Ref;
7}
8
9#[cfg(test)]
10pub(crate) mod test {
11    use std::marker::PhantomData;
12
13    use super::QueryContext;
14
15    pub(crate) struct UltraMinimal<R = !>(PhantomData<R>);
16
17    impl<R> Default for UltraMinimal<R> {
18        fn default() -> Self {
19            Self(PhantomData)
20        }
21    }
22
23    #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
24    pub(crate) enum Def {
25        HasSafetyInvariants,
26        NoSafetyInvariants,
27    }
28
29    impl crate::layout::Def for Def {
30        fn has_safety_invariants(&self) -> bool {
31            self == &Self::HasSafetyInvariants
32        }
33    }
34
35    impl<R: crate::layout::Ref> QueryContext for UltraMinimal<R> {
36        type Def = Def;
37        type Ref = R;
38    }
39}
40
41#[cfg(feature = "rustc")]
42mod rustc {
43    use rustc_middle::ty::TyCtxt;
44
45    use super::*;
46
47    impl<'tcx> super::QueryContext for TyCtxt<'tcx> {
48        type Def = layout::rustc::Def<'tcx>;
49        type Ref = layout::rustc::Ref<'tcx>;
50    }
51}