hax_frontend_exporter/
index_vec.rs

1use crate::prelude::*;
2
3#[derive_group(Serializers)]
4#[derive(Clone, Debug, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord)]
5pub struct IndexVec<I: 'static, T: 'static> {
6    pub raw: Vec<T>,
7    _marker: std::marker::PhantomData<fn(_: &I)>,
8}
9
10impl<I, T: Sized> IndexVec<I, T> {
11    pub fn into_iter(self) -> impl DoubleEndedIterator<Item = T> + ExactSizeIterator {
12        self.raw.into_iter()
13    }
14}
15
16#[cfg(feature = "rustc")]
17impl<I: rustc_index::Idx, T: Sized> IndexVec<I, T> {
18    pub fn into_iter_enumerated(
19        self,
20    ) -> impl DoubleEndedIterator<Item = (I, T)> + ExactSizeIterator {
21        rustc_index::IndexVec::from_raw(self.raw).into_iter_enumerated()
22    }
23}
24
25#[cfg(feature = "rustc")]
26impl<I: rustc_index::Idx, T: Sized> std::ops::Deref for IndexVec<I, T> {
27    type Target = rustc_index::IndexSlice<I, T>;
28    fn deref(&self) -> &Self::Target {
29        Self::Target::from_raw(&self.raw)
30    }
31}
32
33#[cfg(feature = "rustc")]
34impl<I: rustc_index::Idx, T: Sized> std::ops::DerefMut for IndexVec<I, T> {
35    fn deref_mut(&mut self) -> &mut Self::Target {
36        Self::Target::from_raw_mut(&mut self.raw)
37    }
38}
39
40#[cfg(feature = "rustc")]
41impl<I: rustc_index::Idx, T> From<rustc_index::IndexVec<I, T>> for IndexVec<I, T> {
42    fn from(val: rustc_index::IndexVec<I, T>) -> Self {
43        IndexVec {
44            raw: val.raw,
45            _marker: std::marker::PhantomData,
46        }
47    }
48}
49
50#[cfg(feature = "rustc")]
51impl<S, J: rustc_index::Idx, I: rustc_index::Idx + SInto<S, J>, U: Clone, T: SInto<S, U>>
52    SInto<S, IndexVec<J, U>> for rustc_index::IndexSlice<I, T>
53{
54    fn sinto(&self, s: &S) -> IndexVec<J, U> {
55        IndexVec {
56            raw: self.raw.sinto(s),
57            _marker: std::marker::PhantomData,
58        }
59    }
60}
61
62#[cfg(feature = "rustc")]
63impl<I, T> FromIterator<T> for IndexVec<I, T>
64where
65    I: rustc_index::Idx,
66{
67    #[inline]
68    fn from_iter<It: IntoIterator<Item = T>>(iter: It) -> Self {
69        Self {
70            raw: Vec::from_iter(iter),
71            _marker: std::marker::PhantomData,
72        }
73    }
74}
75
76macro_rules! make_idx_wrapper {
77    ($($mod:ident)::+, $type:ident) => {
78        #[derive_group(Serializers)]#[derive(Copy, Clone, Eq, Debug, Hash, PartialEq, PartialOrd, Ord, JsonSchema)]
79        #[serde(untagged)]
80        pub enum $type {
81            $type(usize),
82        }
83        #[cfg(feature = "rustc")]
84        const _: () = {
85            use rustc_index::Idx;
86            type OriginalType = $($mod::)+$type;
87            impl Idx for $type {
88                fn new(idx: usize) -> Self {
89                    $type::$type(idx)
90                }
91                fn index(self) -> usize {
92                    let $type::$type(x) = self;
93                    x.index()
94                }
95            }
96            impl<S> SInto<S, $type> for OriginalType {
97                fn sinto(&self, _s: &S) -> $type {
98                    $type::new(self.index())
99                }
100            }
101        };
102    };
103}
104pub(crate) use make_idx_wrapper;