wasmer_runtime_core_near/structures/
slice.rs1use super::{Iter, IterMut, TypedIndex};
2use std::{
3 marker::PhantomData,
4 ops::{Index, IndexMut},
5};
6
7#[derive(Debug)]
11pub struct SliceMap<K, V>
12where
13 K: TypedIndex,
14{
15 _marker: PhantomData<K>,
16 slice: [V],
17}
18
19impl<K, V> SliceMap<K, V>
20where
21 K: TypedIndex,
22{
23 pub fn get(&self, index: K) -> Option<&V> {
25 self.slice.get(index.index())
26 }
27
28 pub fn get_mut(&mut self, index: K) -> Option<&mut V> {
30 self.slice.get_mut(index.index())
31 }
32
33 pub fn len(&self) -> usize {
35 self.slice.len()
36 }
37
38 pub fn iter(&self) -> Iter<K, V> {
40 Iter::new(self.slice.iter())
41 }
42
43 pub fn iter_mut(&mut self) -> IterMut<K, V> {
45 IterMut::new(self.slice.iter_mut())
46 }
47
48 pub fn as_ptr(&self) -> *const V {
50 self as *const SliceMap<K, V> as *const V
51 }
52
53 pub fn as_mut_ptr(&mut self) -> *mut V {
55 self as *mut SliceMap<K, V> as *mut V
56 }
57}
58
59impl<K, V> Index<K> for SliceMap<K, V>
60where
61 K: TypedIndex,
62{
63 type Output = V;
64 fn index(&self, index: K) -> &V {
65 &self.slice[index.index()]
66 }
67}
68
69impl<K, V> IndexMut<K> for SliceMap<K, V>
70where
71 K: TypedIndex,
72{
73 fn index_mut(&mut self, index: K) -> &mut V {
74 &mut self.slice[index.index()]
75 }
76}