1#[macro_export]
3macro_rules! setup_input_struct {
4 (
5 attrs: [$(#[$attr:meta]),*],
7
8 vis: $vis:vis,
10
11 Struct: $Struct:ident,
13
14 new_fn: $new_fn:ident,
16
17 field_options: [$($field_option:tt),*],
19
20 field_ids: [$($field_id:ident),*],
22
23 field_getters: [$($field_getter_vis:vis $field_getter_id:ident),*],
25
26 field_setters: [$($field_setter_vis:vis $field_setter_id:ident),*],
28
29 field_tys: [$($field_ty:ty),*],
31
32 field_indices: [$($field_index:tt),*],
34
35 required_fields: [$($required_field_id:ident $required_field_ty:ty),*],
37
38 field_durability_ids: [$($field_durability_id:ident),*],
40
41 num_fields: $N:literal,
43
44 is_singleton: $is_singleton:tt,
46
47 generate_debug_impl: $generate_debug_impl:tt,
49
50 unused_names: [
54 $zalsa:ident,
55 $zalsa_struct:ident,
56 $Configuration:ident,
57 $Builder:ident,
58 $CACHE:ident,
59 $Db:ident,
60 ]
61 ) => {
62 $(#[$attr])*
63 #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
64 $vis struct $Struct(salsa::Id);
65
66 const _: () = {
67 use salsa::plumbing as $zalsa;
68 use $zalsa::input as $zalsa_struct;
69
70 type $Configuration = $Struct;
71
72 impl $zalsa_struct::Configuration for $Configuration {
73 const DEBUG_NAME: &'static str = stringify!($Struct);
74 const FIELD_DEBUG_NAMES: &'static [&'static str] = &[$(stringify!($field_id)),*];
75 type Singleton = $zalsa::macro_if! {if $is_singleton {$zalsa::input::Singleton} else {$zalsa::input::NotSingleton}};
76
77 type Struct = $Struct;
79
80 type Fields = ($($field_ty,)*);
82
83 type Stamps = $zalsa::Array<$zalsa::Stamp, $N>;
85 }
86
87 impl $Configuration {
88 pub fn ingredient(db: &dyn $zalsa::Database) -> &$zalsa_struct::IngredientImpl<Self> {
89 static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
90 $zalsa::IngredientCache::new();
91 CACHE.get_or_create(db, || {
92 db.zalsa().add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>()
93 })
94 }
95
96 pub fn ingredient_mut(db: &mut dyn $zalsa::Database) -> (&mut $zalsa_struct::IngredientImpl<Self>, &mut $zalsa::Runtime) {
97 let zalsa_mut = db.zalsa_mut();
98 let current_revision = zalsa_mut.new_revision();
99 let index = zalsa_mut.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>();
100 let (ingredient, runtime) = zalsa_mut.lookup_ingredient_mut(index);
101 let ingredient = ingredient.assert_type_mut::<$zalsa_struct::IngredientImpl<Self>>();
102 (ingredient, runtime)
103 }
104 }
105
106 impl $zalsa::FromId for $Struct {
107 fn from_id(id: salsa::Id) -> Self {
108 Self(id)
109 }
110 }
111
112 impl $zalsa::AsId for $Struct {
113 fn as_id(&self) -> salsa::Id {
114 self.0
115 }
116 }
117
118 unsafe impl $zalsa::Update for $Struct {
119 unsafe fn maybe_update(old_pointer: *mut Self, new_value: Self) -> bool {
120 if unsafe { *old_pointer } != new_value {
121 unsafe { *old_pointer = new_value };
122 true
123 } else {
124 false
125 }
126 }
127 }
128
129 $zalsa::macro_if! { $generate_debug_impl =>
130 impl std::fmt::Debug for $Struct {
131 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132 Self::default_debug_fmt(*self, f)
133 }
134 }
135 }
136
137 impl $zalsa::SalsaStructInDb for $Struct {
138 type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;
139
140 fn lookup_or_create_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
141 aux.add_or_lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().into()
142 }
143
144 #[inline]
145 fn cast(id: $zalsa::Id, type_id: $zalsa::TypeId) -> $zalsa::Option<Self> {
146 if type_id == $zalsa::TypeId::of::<$Struct>() {
147 $zalsa::Some($Struct(id))
148 } else {
149 $zalsa::None
150 }
151 }
152 }
153
154 impl $Struct {
155 #[inline]
156 pub fn $new_fn<$Db>(db: &$Db, $($required_field_id: $required_field_ty),*) -> Self
157 where
158 $Db: ?Sized + salsa::Database,
160 {
161 Self::builder($($required_field_id,)*).new(db)
162 }
163
164 pub fn builder($($required_field_id: $required_field_ty),*) -> <Self as $zalsa_struct::HasBuilder>::Builder
165 {
166 builder::new_builder($($zalsa::maybe_default!($field_option, $field_ty, $field_id,)),*)
167 }
168
169 $(
170 $field_getter_vis fn $field_getter_id<'db, $Db>(self, db: &'db $Db) -> $zalsa::maybe_cloned_ty!($field_option, 'db, $field_ty)
171 where
172 $Db: ?Sized + $zalsa::Database,
174 {
175 let fields = $Configuration::ingredient(db.as_dyn_database()).field(
176 db.as_dyn_database(),
177 self,
178 $field_index,
179 );
180 $zalsa::maybe_clone!(
181 $field_option,
182 $field_ty,
183 &fields.$field_index,
184 )
185 }
186 )*
187
188 $(
189 #[must_use]
190 $field_setter_vis fn $field_setter_id<'db, $Db>(self, db: &'db mut $Db) -> impl salsa::Setter<FieldTy = $field_ty> + 'db
191 where
192 $Db: ?Sized + $zalsa::Database,
194 {
195 let (ingredient, revision) = $Configuration::ingredient_mut(db.as_dyn_database_mut());
196 $zalsa::input::SetterImpl::new(
197 revision,
198 self,
199 $field_index,
200 ingredient,
201 |fields, f| std::mem::replace(&mut fields.$field_index, f),
202 )
203 }
204 )*
205
206 $zalsa::macro_if! { $is_singleton =>
207 pub fn try_get<$Db>(db: &$Db) -> Option<Self>
208 where
209 $Db: ?Sized + salsa::Database,
211 {
212 $Configuration::ingredient(db.as_dyn_database()).get_singleton_input(db)
213 }
214
215 #[track_caller]
216 pub fn get<$Db>(db: &$Db) -> Self
217 where
218 $Db: ?Sized + salsa::Database,
220 {
221 Self::try_get(db).unwrap()
222 }
223 }
224
225 pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
227 $zalsa::with_attached_database(|db| {
228 let fields = $Configuration::ingredient(db).leak_fields(db, this);
229 let mut f = f.debug_struct(stringify!($Struct));
230 let f = f.field("[salsa id]", &$zalsa::AsId::as_id(&this));
231 $(
232 let f = f.field(stringify!($field_id), &fields.$field_index);
233 )*
234 f.finish()
235 }).unwrap_or_else(|| {
236 f.debug_struct(stringify!($Struct))
237 .field("[salsa id]", &this.0)
238 .finish()
239 })
240 }
241 }
242
243 impl $zalsa_struct::HasBuilder for $Struct {
244 type Builder = builder::$Builder;
245 }
246
247 impl builder::$Builder {
250 #[must_use]
252 pub fn new<$Db>(self, db: &$Db) -> $Struct
253 where
254 $Db: ?Sized + salsa::Database
256 {
257 let current_revision = $zalsa::current_revision(db);
258 let ingredient = $Configuration::ingredient(db.as_dyn_database());
259 let (fields, stamps) = builder::builder_into_inner(self, current_revision);
260 ingredient.new_input(db.as_dyn_database(), fields, stamps)
261 }
262 }
263
264 mod builder {
265 use super::*;
266
267 use salsa::plumbing as $zalsa;
268 use $zalsa::input as $zalsa_struct;
269
270 pub(super) fn new_builder($($field_id: $field_ty),*) -> $Builder {
273 $Builder {
274 fields: ($($field_id,)*),
275 durabilities: [salsa::Durability::default(); $N],
276 }
277 }
278
279 pub(super) fn builder_into_inner(builder: $Builder, revision: $zalsa::Revision) -> (($($field_ty,)*), $zalsa::Array<$zalsa::Stamp, $N>) {
280 let stamps = $zalsa::Array::new([
281 $($zalsa::stamp(revision, builder.durabilities[$field_index])),*
282 ]);
283
284 (builder.fields, stamps)
285 }
286
287 #[must_use]
288 pub struct $Builder {
289 fields: ($($field_ty,)*),
291
292 durabilities: [salsa::Durability; $N],
294 }
295
296 impl $Builder {
297 pub fn durability(mut self, durability: salsa::Durability) -> Self {
301 self.durabilities = [durability; $N];
302 self
303 }
304
305 $($zalsa::maybe_default_tt! { $field_option =>
306 #[must_use]
308 pub fn $field_id(mut self, value: $field_ty) -> Self
309 {
310 self.fields.$field_index = value;
311 self
312 }
313 })*
314
315 $(
316 #[must_use]
318 pub fn $field_durability_id(mut self, durability: salsa::Durability) -> Self
319 {
320 self.durabilities[$field_index] = durability;
321 self
322 }
323 )*
324 }
325 }
326 };
327 };
328}