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