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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(feature = "std")]
extern crate core;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::{
    borrow::Borrow,
    ops::{
        Deref,
        DerefMut,
    },
};

use hashbrown::HashMap;

/// Trait for the key type of keyed registries.
pub trait Key = core::cmp::Eq + core::hash::Hash;

/// The internal container for a registry type, which associates zone-allocated data with a key
/// (which may or may not be generated by the caller).
pub(crate) trait RegistryContainer<K, V> {
    type Item;
    fn new() -> Self;
    fn with_capacity(size: usize) -> Self;
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;
    fn reserve(&mut self, additional: usize);
    fn get<'a, 'b>(&'a self, key: &K) -> Option<&'b V>
    where
        'a: 'b;
    fn get_mut<'a, 'b>(&'a mut self, key: &K) -> Option<&'b mut V>
    where
        'a: 'b;
}

/// The internal container for a keyed registry type, which associates zone-allocated data with a
/// caller-specified key.
pub(crate) trait KeyedRegistryContainer<K, V>:
    RegistryContainer<K, V, Item = (K, V)>
{
    fn contains_key<R>(&self, key: &R) -> bool
    where
        K: Borrow<R>,
        R: Key + ?Sized;
    fn get<'a, 'b, R>(&'a self, key: &R) -> Option<&'b V>
    where
        'a: 'b,
        K: Borrow<R>,
        R: Key + ?Sized;
    fn get_mut<'a, 'b, R>(&'a mut self, key: &R) -> Option<&'b mut V>
    where
        'a: 'b,
        K: Borrow<R>,
        R: Key + ?Sized;
}

impl<T> RegistryContainer<usize, T> for Vec<T> {
    type Item = T;

    fn new() -> Self {
        Vec::new()
    }

    fn with_capacity(size: usize) -> Self {
        Vec::with_capacity(size)
    }

    fn len(&self) -> usize {
        self.len()
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }

    fn reserve(&mut self, additional: usize) {
        self.reserve(additional)
    }

    fn get<'a, 'b>(&'a self, key: &usize) -> Option<&'b T>
    where
        'a: 'b,
    {
        self.deref().get(*key)
    }

    fn get_mut<'a, 'b>(&'a mut self, key: &usize) -> Option<&'b mut T>
    where
        'a: 'b,
    {
        self.deref_mut().get_mut(*key)
    }
}

impl<K, V> RegistryContainer<K, V> for HashMap<K, V>
where
    K: Key,
{
    type Item = (K, V);

    fn new() -> Self {
        HashMap::new()
    }

    fn with_capacity(size: usize) -> Self {
        HashMap::with_capacity(size)
    }

    fn len(&self) -> usize {
        self.len()
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }

    fn reserve(&mut self, additional: usize) {
        self.reserve(additional)
    }

    fn get<'a, 'b>(&'a self, key: &K) -> Option<&'b V>
    where
        'a: 'b,
    {
        self.get(key)
    }

    fn get_mut<'a, 'b>(&'a mut self, key: &K) -> Option<&'b mut V>
    where
        'a: 'b,
    {
        self.get_mut(key)
    }
}

impl<K, V> KeyedRegistryContainer<K, V> for HashMap<K, V>
where
    K: Key,
{
    fn contains_key<R>(&self, key: &R) -> bool
    where
        K: Borrow<R>,
        R: Key + ?Sized,
    {
        self.contains_key(key)
    }

    fn get<'a, 'b, R>(&'a self, key: &R) -> Option<&'b V>
    where
        'a: 'b,
        K: Borrow<R>,
        R: Key + ?Sized,
    {
        self.get(key)
    }

    fn get_mut<'a, 'b, R>(&'a mut self, key: &R) -> Option<&'b mut V>
    where
        'a: 'b,
        K: Borrow<R>,
        R: Key + ?Sized,
    {
        self.get_mut(key)
    }
}