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 Region: layout::Region;
7    type Type: layout::Type;
8}
9
10#[cfg(test)]
11pub(crate) mod test {
12    use std::marker::PhantomData;
13
14    use super::QueryContext;
15
16    pub(crate) struct UltraMinimal<R = !, T = !>(PhantomData<(R, T)>);
17
18    impl<R, T> Default for UltraMinimal<R, T> {
19        fn default() -> Self {
20            Self(PhantomData)
21        }
22    }
23
24    #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)]
25    pub(crate) enum Def {
26        HasSafetyInvariants,
27        NoSafetyInvariants,
28    }
29
30    impl crate::layout::Def for Def {
31        fn has_safety_invariants(&self) -> bool {
32            self == &Self::HasSafetyInvariants
33        }
34    }
35
36    impl<R, T> QueryContext for UltraMinimal<R, T>
37    where
38        R: crate::layout::Region,
39        T: crate::layout::Type,
40    {
41        type Def = Def;
42        type Region = R;
43        type Type = T;
44    }
45}
46
47#[cfg(feature = "rustc")]
48mod rustc {
49    use rustc_middle::ty::{Region, Ty, TyCtxt};
50
51    use super::*;
52
53    impl<'tcx> super::QueryContext for TyCtxt<'tcx> {
54        type Def = layout::rustc::Def<'tcx>;
55        type Region = Region<'tcx>;
56        type Type = Ty<'tcx>;
57    }
58}