Skip to main content

hir_ty/next_solver/
allocation.rs

1use std::{fmt, hash::Hash};
2
3use intern::{Interned, InternedRef, impl_internable};
4use macros::GenericTypeVisitable;
5use rustc_type_ir::GenericTypeVisitable;
6
7use crate::{
8    MemoryMap,
9    next_solver::{Ty, impl_stored_interned},
10};
11
12#[derive(Clone, Copy, PartialEq, Eq, Hash)]
13pub struct Allocation<'db> {
14    interned: InternedRef<'db, AllocationInterned>,
15}
16
17impl<'db> Allocation<'db> {
18    pub fn new(data: AllocationData<'db>) -> Self {
19        let data =
20            unsafe { std::mem::transmute::<AllocationData<'db>, AllocationData<'static>>(data) };
21        Self { interned: Interned::new_gc(AllocationInterned(data)) }
22    }
23}
24
25impl<'db> std::ops::Deref for Allocation<'db> {
26    type Target = AllocationData<'db>;
27
28    #[inline]
29    fn deref(&self) -> &Self::Target {
30        let inner = &self.interned.0;
31        unsafe { std::mem::transmute::<&AllocationData<'static>, &AllocationData<'db>>(inner) }
32    }
33}
34
35impl<'db, V: super::WorldExposer> GenericTypeVisitable<V> for Allocation<'db> {
36    fn generic_visit_with(&self, visitor: &mut V) {
37        if visitor.on_interned(self.interned).is_continue() {
38            (**self).generic_visit_with(visitor);
39        }
40    }
41}
42
43impl fmt::Debug for Allocation<'_> {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        let AllocationData { ty, memory, memory_map } = &**self;
46        f.debug_struct("Allocation")
47            .field("ty", ty)
48            .field("memory", memory)
49            .field("memory_map", memory_map)
50            .finish()
51    }
52}
53
54#[derive(PartialEq, Eq, Hash, GenericTypeVisitable)]
55pub(super) struct AllocationInterned(AllocationData<'static>);
56
57#[derive(Debug, PartialEq, Eq, GenericTypeVisitable)]
58pub struct AllocationData<'db> {
59    pub ty: Ty<'db>,
60    pub memory: Box<[u8]>,
61    pub memory_map: MemoryMap<'db>,
62}
63
64impl<'db> Hash for AllocationData<'db> {
65    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
66        let Self { ty, memory, memory_map: _ } = self;
67        ty.hash(state);
68        memory.hash(state);
69    }
70}
71
72impl_internable!(gc; AllocationInterned);
73impl_stored_interned!(AllocationInterned, Allocation, StoredAllocation);