Debian Codegraph @

third_party/rust/diplomat_core/src/hir/defs.rs

raw
1 //! Type definitions for structs, output structs, opaque structs, and enums.
2
3 use super::lifetimes::LifetimeEnv;
4 use super::{
5 Attrs, Everywhere, IdentBuf, Method, OutputOnly, SpecialMethodPresence, TyPosition, Type,
6 };
7 use crate::ast::Docs;
8
9 #[non_exhaustive]
10 pub enum ReturnableStructDef<'tcx> {
11 Struct(&'tcx StructDef),
12 OutStruct(&'tcx OutStructDef),
13 }
14
15 #[derive(Copy, Clone, Debug)]
16 #[non_exhaustive]
17 pub enum TypeDef<'tcx> {
18 Struct(&'tcx StructDef),
19 OutStruct(&'tcx OutStructDef),
20 Opaque(&'tcx OpaqueDef),
21 Enum(&'tcx EnumDef),
22 }
23
24 /// Structs that can only be returned from methods.
25 pub type OutStructDef = StructDef<OutputOnly>;
26
27 /// Structs that can be either inputs or outputs in methods.
28 #[derive(Debug)]
29 #[non_exhaustive]
30 pub struct StructDef<P: TyPosition = Everywhere> {
31 pub docs: Docs,
32 pub name: IdentBuf,
33 pub fields: Vec<StructField<P>>,
34 pub methods: Vec<Method>,
35 pub attrs: Attrs,
36 pub lifetimes: LifetimeEnv,
37 pub special_method_presence: SpecialMethodPresence,
38 }
39
40 /// A struct whose contents are opaque across the FFI boundary, and can only
41 /// cross when behind a pointer.
42 ///
43 /// All opaques can be inputs or outputs when behind a reference, but owned
44 /// opaques can only be returned since there isn't a general way for most languages
45 /// to give up ownership.
46 ///
47 /// A struct marked with `#[diplomat::opaque]`.
48 #[derive(Debug)]
49 #[non_exhaustive]
50 pub struct OpaqueDef {
51 pub docs: Docs,
52 pub name: IdentBuf,
53 pub methods: Vec<Method>,
54 pub attrs: Attrs,
55 pub lifetimes: LifetimeEnv,
56 pub special_method_presence: SpecialMethodPresence,
57 }
58
59 /// The enum type.
60 #[derive(Debug)]
61 #[non_exhaustive]
62 pub struct EnumDef {
63 pub docs: Docs,
64 pub name: IdentBuf,
65 pub variants: Vec<EnumVariant>,
66 pub methods: Vec<Method>,
67 pub attrs: Attrs,
68 pub special_method_presence: SpecialMethodPresence,
69 }
70
71 /// A field on a [`OutStruct`]s.
72 pub type OutStructField = StructField<OutputOnly>;
73
74 /// A field on a [`Struct`]s.
75 #[derive(Debug)]
76 #[non_exhaustive]
77 pub struct StructField<P: TyPosition = Everywhere> {
78 pub docs: Docs,
79 pub name: IdentBuf,
80 pub ty: Type<P>,
81 }
82
83 /// A variant of an [`Enum`].
84 #[derive(Debug)]
85 #[non_exhaustive]
86 pub struct EnumVariant {
87 pub docs: Docs,
88 pub name: IdentBuf,
89 pub discriminant: isize,
90 pub attrs: Attrs,
91 }
92
93 impl<P: TyPosition> StructDef<P> {
94 pub(super) fn new(
95 docs: Docs,
96 name: IdentBuf,
97 fields: Vec<StructField<P>>,
98 methods: Vec<Method>,
99 attrs: Attrs,
100 lifetimes: LifetimeEnv,
101 special_method_presence: SpecialMethodPresence,
102 ) -> Self {
103 Self {
104 docs,
105 name,
106 fields,
107 methods,
108 attrs,
109 lifetimes,
110 special_method_presence,
111 }
112 }
113 }
114
115 impl OpaqueDef {
116 pub(super) fn new(
117 docs: Docs,
118 name: IdentBuf,
119 methods: Vec<Method>,
120 attrs: Attrs,
121 lifetimes: LifetimeEnv,
122 special_method_presence: SpecialMethodPresence,
123 ) -> Self {
124 Self {
125 docs,
126 name,
127 methods,
128 attrs,
129 lifetimes,
130 special_method_presence,
131 }
132 }
133 }
134
135 impl EnumDef {
136 pub(super) fn new(
137 docs: Docs,
138 name: IdentBuf,
139 variants: Vec<EnumVariant>,
140 methods: Vec<Method>,
141 attrs: Attrs,
142 special_method_presence: SpecialMethodPresence,
143 ) -> Self {
144 Self {
145 docs,
146 name,
147 variants,
148 methods,
149 attrs,
150 special_method_presence,
151 }
152 }
153 }
154
155 impl<'a> From<&'a StructDef> for TypeDef<'a> {
156 fn from(x: &'a StructDef) -> Self {
157 TypeDef::Struct(x)
158 }
159 }
160
161 impl<'a> From<&'a OutStructDef> for TypeDef<'a> {
162 fn from(x: &'a OutStructDef) -> Self {
163 TypeDef::OutStruct(x)
164 }
165 }
166
167 impl<'a> From<&'a OpaqueDef> for TypeDef<'a> {
168 fn from(x: &'a OpaqueDef) -> Self {
169 TypeDef::Opaque(x)
170 }
171 }
172
173 impl<'a> From<&'a EnumDef> for TypeDef<'a> {
174 fn from(x: &'a EnumDef) -> Self {
175 TypeDef::Enum(x)
176 }
177 }
178
179 impl<'tcx> TypeDef<'tcx> {
180 pub fn name(&self) -> &'tcx IdentBuf {
181 match *self {
182 Self::Struct(ty) => &ty.name,
183 Self::OutStruct(ty) => &ty.name,
184 Self::Opaque(ty) => &ty.name,
185 Self::Enum(ty) => &ty.name,
186 }
187 }
188
189 pub fn docs(&self) -> &'tcx Docs {
190 match *self {
191 Self::Struct(ty) => &ty.docs,
192 Self::OutStruct(ty) => &ty.docs,
193 Self::Opaque(ty) => &ty.docs,
194 Self::Enum(ty) => &ty.docs,
195 }
196 }
197 pub fn methods(&self) -> &'tcx [Method] {
198 match *self {
199 Self::Struct(ty) => &ty.methods,
200 Self::OutStruct(ty) => &ty.methods,
201 Self::Opaque(ty) => &ty.methods,
202 Self::Enum(ty) => &ty.methods,
203 }
204 }
205
206 pub fn attrs(&self) -> &'tcx Attrs {
207 match *self {
208 Self::Struct(ty) => &ty.attrs,
209 Self::OutStruct(ty) => &ty.attrs,
210 Self::Opaque(ty) => &ty.attrs,
211 Self::Enum(ty) => &ty.attrs,
212 }
213 }
214
215 pub fn special_method_presence(&self) -> &'tcx SpecialMethodPresence {
216 match *self {
217 Self::Struct(ty) => &ty.special_method_presence,
218 Self::OutStruct(ty) => &ty.special_method_presence,
219 Self::Opaque(ty) => &ty.special_method_presence,
220 Self::Enum(ty) => &ty.special_method_presence,
221 }
222 }
223 }