Skip to main content

rustc_public/unstable/convert/stable/
mod.rs

1//! Conversion of internal Rust compiler items to stable ones.
2
3use rustc_abi::FieldIdx;
4use rustc_public_bridge::Tables;
5use rustc_public_bridge::context::CompilerCtxt;
6
7use super::Stable;
8use crate::compiler_interface::BridgeTys;
9
10mod abi;
11mod mir;
12mod ty;
13
14impl<'tcx> Stable<'tcx> for rustc_hir::Safety {
15    type T = crate::mir::Safety;
16    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
17        match self {
18            rustc_hir::Safety::Unsafe => crate::mir::Safety::Unsafe,
19            rustc_hir::Safety::Safe => crate::mir::Safety::Safe,
20        }
21    }
22}
23
24impl<'tcx> Stable<'tcx> for rustc_hir::Constness {
25    type T = crate::ty::Constness;
26    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
27        match self {
28            rustc_hir::Constness::Const => crate::ty::Constness::Const,
29            rustc_hir::Constness::NotConst => crate::ty::Constness::NotConst,
30        }
31    }
32}
33
34impl<'tcx> Stable<'tcx> for rustc_middle::ty::Asyncness {
35    type T = crate::ty::Asyncness;
36    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
37        match self {
38            rustc_middle::ty::Asyncness::Yes => crate::ty::Asyncness::Async,
39            rustc_middle::ty::Asyncness::No => crate::ty::Asyncness::NotAsync,
40        }
41    }
42}
43
44impl<'tcx> Stable<'tcx> for FieldIdx {
45    type T = usize;
46    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
47        self.as_usize()
48    }
49}
50
51impl<'tcx> Stable<'tcx> for rustc_hir::CoroutineSource {
52    type T = crate::mir::CoroutineSource;
53    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
54        use rustc_hir::CoroutineSource;
55        match self {
56            CoroutineSource::Block => crate::mir::CoroutineSource::Block,
57            CoroutineSource::Closure => crate::mir::CoroutineSource::Closure,
58            CoroutineSource::Fn => crate::mir::CoroutineSource::Fn,
59        }
60    }
61}
62
63impl<'tcx> Stable<'tcx> for rustc_hir::CoroutineKind {
64    type T = crate::mir::CoroutineKind;
65    fn stable<'cx>(
66        &self,
67        tables: &mut Tables<'cx, BridgeTys>,
68        cx: &CompilerCtxt<'cx, BridgeTys>,
69    ) -> Self::T {
70        use rustc_hir::{CoroutineDesugaring, CoroutineKind};
71        match *self {
72            CoroutineKind::Desugared(CoroutineDesugaring::Async, source) => {
73                crate::mir::CoroutineKind::Desugared(
74                    crate::mir::CoroutineDesugaring::Async,
75                    source.stable(tables, cx),
76                )
77            }
78            CoroutineKind::Desugared(CoroutineDesugaring::Gen, source) => {
79                crate::mir::CoroutineKind::Desugared(
80                    crate::mir::CoroutineDesugaring::Gen,
81                    source.stable(tables, cx),
82                )
83            }
84            CoroutineKind::Coroutine(movability) => {
85                crate::mir::CoroutineKind::Coroutine(movability.stable(tables, cx))
86            }
87            CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, source) => {
88                crate::mir::CoroutineKind::Desugared(
89                    crate::mir::CoroutineDesugaring::AsyncGen,
90                    source.stable(tables, cx),
91                )
92            }
93        }
94    }
95}
96
97impl<'tcx> Stable<'tcx> for rustc_span::Symbol {
98    type T = crate::Symbol;
99
100    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
101        self.to_string()
102    }
103}
104
105impl<'tcx> Stable<'tcx> for rustc_span::def_id::DefId {
106    type T = crate::DefId;
107
108    fn stable<'cx>(
109        &self,
110        tables: &mut Tables<'cx, BridgeTys>,
111        _: &CompilerCtxt<'cx, BridgeTys>,
112    ) -> Self::T {
113        tables.create_def_id(*self)
114    }
115}
116
117impl<'tcx> Stable<'tcx> for rustc_span::Span {
118    type T = crate::ty::Span;
119
120    fn stable<'cx>(
121        &self,
122        tables: &mut Tables<'cx, BridgeTys>,
123        _: &CompilerCtxt<'cx, BridgeTys>,
124    ) -> Self::T {
125        tables.create_span(*self)
126    }
127}