stable_mir/unstable/convert/
mod.rs

1//! This module holds the logic to convert rustc internal ADTs into stable mir ADTs.
2//!
3//! The conversion from stable to internal is not meant to be complete,
4//! and it should be added as when needed to be passed as input to rustc_smir functions.
5//!
6//! For contributors, please make sure to avoid calling rustc's internal functions and queries.
7//! These should be done via `rustc_smir` APIs, but it's possible to access ADT fields directly.
8
9use std::ops::RangeInclusive;
10
11use rustc_smir::Tables;
12use rustc_smir::context::SmirCtxt;
13
14use super::Stable;
15use crate::compiler_interface::BridgeTys;
16
17mod internal;
18mod stable;
19
20impl<'tcx, T> Stable<'tcx> for &T
21where
22    T: Stable<'tcx>,
23{
24    type T = T::T;
25
26    fn stable<'cx>(
27        &self,
28        tables: &mut Tables<'cx, BridgeTys>,
29        cx: &SmirCtxt<'cx, BridgeTys>,
30    ) -> Self::T {
31        (*self).stable(tables, cx)
32    }
33}
34
35impl<'tcx, T> Stable<'tcx> for Option<T>
36where
37    T: Stable<'tcx>,
38{
39    type T = Option<T::T>;
40
41    fn stable<'cx>(
42        &self,
43        tables: &mut Tables<'cx, BridgeTys>,
44        cx: &SmirCtxt<'cx, BridgeTys>,
45    ) -> Self::T {
46        self.as_ref().map(|value| value.stable(tables, cx))
47    }
48}
49
50impl<'tcx, T, E> Stable<'tcx> for Result<T, E>
51where
52    T: Stable<'tcx>,
53    E: Stable<'tcx>,
54{
55    type T = Result<T::T, E::T>;
56
57    fn stable<'cx>(
58        &self,
59        tables: &mut Tables<'cx, BridgeTys>,
60        cx: &SmirCtxt<'cx, BridgeTys>,
61    ) -> Self::T {
62        match self {
63            Ok(val) => Ok(val.stable(tables, cx)),
64            Err(error) => Err(error.stable(tables, cx)),
65        }
66    }
67}
68
69impl<'tcx, T> Stable<'tcx> for &[T]
70where
71    T: Stable<'tcx>,
72{
73    type T = Vec<T::T>;
74    fn stable<'cx>(
75        &self,
76        tables: &mut Tables<'cx, BridgeTys>,
77        cx: &SmirCtxt<'cx, BridgeTys>,
78    ) -> Self::T {
79        self.iter().map(|e| e.stable(tables, cx)).collect()
80    }
81}
82
83impl<'tcx, T, U> Stable<'tcx> for (T, U)
84where
85    T: Stable<'tcx>,
86    U: Stable<'tcx>,
87{
88    type T = (T::T, U::T);
89    fn stable<'cx>(
90        &self,
91        tables: &mut Tables<'cx, BridgeTys>,
92        cx: &SmirCtxt<'cx, BridgeTys>,
93    ) -> Self::T {
94        (self.0.stable(tables, cx), self.1.stable(tables, cx))
95    }
96}
97
98impl<'tcx, T> Stable<'tcx> for RangeInclusive<T>
99where
100    T: Stable<'tcx>,
101{
102    type T = RangeInclusive<T::T>;
103    fn stable<'cx>(
104        &self,
105        tables: &mut Tables<'cx, BridgeTys>,
106        cx: &SmirCtxt<'cx, BridgeTys>,
107    ) -> Self::T {
108        RangeInclusive::new(self.start().stable(tables, cx), self.end().stable(tables, cx))
109    }
110}