aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/arena.rs
blob: d4f9d9cb904231c34bb6b28865517def9690639b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! A simple id-based arena, similar to https://github.com/fitzgen/id-arena.
//! We use our own version for more compact id's and to allow inherent impls
//! on Ids.

use std::{
    fmt,
    hash::{Hash, Hasher},
    marker::PhantomData,
};

pub struct Id<T> {
    idx: u32,
    _ty: PhantomData<fn() -> T>,
}

impl<T> fmt::Debug for Id<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("Id").field(&self.idx).finish()
    }
}
impl<T> Copy for Id<T> {}
impl<T> Clone for Id<T> {
    fn clone(&self) -> Id<T> {
        *self
    }
}

impl<T> PartialEq for Id<T> {
    fn eq(&self, other: &Id<T>) -> bool {
        self.idx == other.idx
    }
}

impl<T> Eq for Id<T> {}

impl<T> Hash for Id<T> {
    fn hash<H: Hasher>(&self, h: &mut H) {
        self.idx.hash(h);
    }
}

#[derive(Debug, PartialEq, Eq)]
pub(crate) struct ArenaBehavior<T> {
    _ty: PhantomData<T>,
}

impl<T> id_arena::ArenaBehavior for ArenaBehavior<T> {
    type Id = Id<T>;
    fn new_arena_id() -> u32 {
        0
    }
    fn new_id(_arena_id: u32, index: usize) -> Id<T> {
        Id {
            idx: index as u32,
            _ty: PhantomData,
        }
    }
    fn index(id: Id<T>) -> usize {
        id.idx as usize
    }
    fn arena_id(_id: Id<T>) -> u32 {
        0
    }
}

pub(crate) type Arena<T> = id_arena::Arena<T, ArenaBehavior<T>>;