1#[macro_export]
3macro_rules! setup_tracked_struct {
4 (
5 attrs: [$(#[$attr:meta]),*],
7
8 vis: $vis:vis,
10
11 Struct: $Struct:ident,
13
14 db_lt: $db_lt:lifetime,
16
17 new_fn: $new_fn:ident,
19
20 field_ids: [$($field_id:ident),*],
22
23 tracked_ids: [$($tracked_id:ident),*],
25
26 tracked_getters: [$($tracked_getter_vis:vis $tracked_getter_id:ident),*],
28
29 untracked_getters: [$($untracked_getter_vis:vis $untracked_getter_id:ident),*],
31
32 field_tys: [$($field_ty:ty),*],
34
35 assert_fields_are_salsa_values: {$($assert_fields_are_salsa_values:tt)*},
36
37 tracked_tys: [$($tracked_ty:ty),*],
39
40 untracked_tys: [$($untracked_ty:ty),*],
42
43 field_indices: [$($field_index:tt),*],
45
46 absolute_tracked_indices: [$($absolute_tracked_index:tt),*],
48
49 relative_tracked_indices: [$($relative_tracked_index:tt),*],
51
52 absolute_untracked_indices: [$($absolute_untracked_index:tt),*],
54
55 tracked_field_equalities: [$($tracked_field_equality:tt),*],
57
58 untracked_field_equalities: [$($untracked_field_equality:tt),*],
60
61 tracked_options: [$($tracked_option:tt),*],
71
72 untracked_options: [$($untracked_option:tt),*],
75
76 tracked_field_attrs: [$([$(#[$tracked_field_attr:meta]),*]),*],
78 untracked_field_attrs: [$([$(#[$untracked_field_attr:meta]),*]),*],
79
80 num_tracked_fields: $N:literal,
82
83 generate_debug_impl: $generate_debug_impl:tt,
85
86 heap_size_fn: $($heap_size_fn:path)?,
88
89 persist: $persist:tt,
91
92 serialize_fn: $($serialize_fn:path)?,
94
95 deserialize_fn: $($deserialize_fn:path)?,
97
98 unused_names: [
102 $zalsa:ident,
103 $zalsa_struct:ident,
104 $Configuration:ident,
105 $CACHE:ident,
106 $Db:ident,
107 $Revision:ident,
108 ]
109 ) => {
110 $(#[$attr])*
111 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
112 $vis struct $Struct<$db_lt>(
113 ::salsa::Id,
114 ::std::marker::PhantomData<fn() -> &$db_lt ()>
115 );
116
117 #[allow(dead_code)]
118 #[allow(clippy::all)]
119 const _: () = {
120 use ::salsa::plumbing as $zalsa;
121 use $zalsa::tracked_struct as $zalsa_struct;
122 use $zalsa::Revision as $Revision;
123
124 type $Configuration = $Struct<'static>;
125
126 #[allow(unused_lifetimes)]
127 fn _assert_fields_are_salsa_values<$db_lt>() {
128 use $zalsa::{SalsaValueDispatch, SalsaValueFallback as _};
129 $($assert_fields_are_salsa_values)*
130 }
131 let _ = _assert_fields_are_salsa_values;
132
133 impl<$db_lt> $zalsa::HasJar for $Struct<$db_lt> {
134 type Jar = $zalsa_struct::JarImpl<$Configuration>;
135 const KIND: $zalsa::JarKind = $zalsa::JarKind::Struct;
136 }
137
138 $zalsa::register_jar! {
139 $zalsa::ErasedJar::erase::<$Struct<'static>>()
140 }
141
142 unsafe impl $zalsa_struct::Configuration for $Configuration {
145 const LOCATION: $zalsa::Location = $zalsa::Location {
146 file: file!(),
147 line: line!(),
148 };
149 const DEBUG_NAME: &'static str = stringify!($Struct);
150
151 const TRACKED_FIELD_NAMES: &'static [&'static str] = &[
152 $(stringify!($tracked_id),)*
153 ];
154
155 const TRACKED_FIELD_INDICES: &'static [usize] = &[
156 $($relative_tracked_index,)*
157 ];
158
159 const PERSIST: bool = $persist;
160
161 type Fields<$db_lt> = ($($field_ty,)*);
162
163 type Revisions = [$zalsa::AtomicRevision; $N];
164
165 type Struct<$db_lt> = $Struct<$db_lt>;
166
167 fn untracked_fields(fields: &Self::Fields<'_>) -> impl ::std::hash::Hash {
168 ( $( &fields.$absolute_untracked_index ),* )
169 }
170
171 fn new_revisions(current_revision: $Revision) -> Self::Revisions {
172 std::array::from_fn(|_| $zalsa::AtomicRevision::new(current_revision))
173 }
174
175 fn update_fields<$db_lt>(
176 current_revision: $Revision,
177 revisions: &Self::Revisions,
178 old_fields: &mut Self::Fields<$db_lt>,
179 new_fields: Self::Fields<$db_lt>,
180 ) -> bool {
181 $(
182 if $zalsa::update_field(
183 &mut old_fields.$absolute_tracked_index,
184 new_fields.$absolute_tracked_index,
185 $tracked_field_equality,
186 ) {
187 revisions[$relative_tracked_index].store(current_revision);
188 }
189 )*;
190
191 $(
194 $zalsa::update_field(
195 &mut old_fields.$absolute_untracked_index,
196 new_fields.$absolute_untracked_index,
197 $untracked_field_equality,
198 )
199 |
200 )* false
201 }
202
203 $(
204 fn heap_size(value: &Self::Fields<'_>) -> Option<usize> {
205 Some($heap_size_fn(value))
206 }
207 )?
208
209 fn serialize<S: $zalsa::serde::Serializer>(
210 fields: &Self::Fields<'_>,
211 serializer: S,
212 ) -> ::std::result::Result<S::Ok, S::Error> {
213 $zalsa::macro_if! {
214 if $persist {
215 $($serialize_fn(fields, serializer))?
216 } else {
217 panic!("attempted to serialize value not marked with `persist` attribute")
218 }
219 }
220 }
221
222 fn deserialize<'de, D: $zalsa::serde::Deserializer<'de>>(
223 deserializer: D,
224 ) -> ::std::result::Result<Self::Fields<'static>, D::Error> {
225 $zalsa::macro_if! {
226 if $persist {
227 $($deserialize_fn(deserializer))?
228 } else {
229 panic!("attempted to deserialize value not marked with `persist` attribute")
230 }
231 }
232 }
233 }
234
235 impl $Configuration {
236 pub fn ingredient(db: &dyn $zalsa::Database) -> &$zalsa_struct::IngredientImpl<Self> {
237 Self::ingredient_(db.zalsa())
238 }
239
240 fn ingredient_(zalsa: &$zalsa::Zalsa) -> &$zalsa_struct::IngredientImpl<Self> {
241 static CACHE: $zalsa::IngredientCache<$zalsa_struct::IngredientImpl<$Configuration>> =
242 $zalsa::IngredientCache::new();
243
244 unsafe {
247 CACHE.get_or_create::<$zalsa_struct::JarImpl<$Configuration>, 0>(zalsa)
248 }
249 }
250 }
251
252 impl<$db_lt> $zalsa::FromId for $Struct<$db_lt> {
253 #[inline]
254 fn from_id(id: ::salsa::Id) -> Self {
255 $Struct(id, ::std::marker::PhantomData)
256 }
257 }
258
259 impl $zalsa::AsId for $Struct<'_> {
260 #[inline]
261 fn as_id(&self) -> $zalsa::Id {
262 self.0
263 }
264 }
265
266 impl $zalsa::SalsaStructInDb for $Struct<'_> {
267 type MemoIngredientMap = $zalsa::MemoIngredientSingletonIndex;
268 const LEAF_TYPE_IDS: &'static [$zalsa::ConstTypeId] = &[$zalsa::ConstTypeId::of::<$Struct<'static>>()];
269
270 fn lookup_ingredient_index(aux: &$zalsa::Zalsa) -> $zalsa::IngredientIndices {
271 aux.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>().into()
272 }
273
274 fn entries(
275 zalsa: &$zalsa::Zalsa
276 ) -> impl Iterator<Item = $zalsa::DatabaseKeyIndex> + '_ {
277 let ingredient_index = zalsa.lookup_jar_by_type::<$zalsa_struct::JarImpl<$Configuration>>();
278 <$Configuration>::ingredient_(zalsa).entries(zalsa).map(|entry| entry.key())
279 }
280
281 #[inline]
282 fn cast(id: $zalsa::Id, type_id: $zalsa::TypeId) -> $zalsa::Option<Self> {
283 if type_id == $zalsa::TypeId::of::<$Struct<'static>>() {
284 $zalsa::Some(<$Struct<'static> as $zalsa::FromId>::from_id(id))
285 } else {
286 $zalsa::None
287 }
288 }
289
290 #[inline]
291 unsafe fn memo_table(
292 zalsa: &$zalsa::Zalsa,
293 id: $zalsa::Id,
294 current_revision: $zalsa::Revision,
295 ) -> $zalsa::MemoTableWithTypes<'_> {
296 unsafe { zalsa.table().memos::<$zalsa_struct::Value<$Configuration>>(id, current_revision) }
298 }
299 }
300
301 impl $zalsa::TrackedStructInDb for $Struct<'_> {
302 fn database_key_index(zalsa: &$zalsa::Zalsa, id: $zalsa::Id) -> $zalsa::DatabaseKeyIndex {
303 $Configuration::ingredient_(zalsa).database_key_index(id)
304 }
305 }
306
307 $zalsa::macro_if! { $persist =>
308 impl $zalsa::serde::Serialize for $Struct<'_> {
309 fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
310 where
311 S: $zalsa::serde::Serializer,
312 {
313 $zalsa::serde::Serialize::serialize(&$zalsa::AsId::as_id(self), serializer)
314 }
315 }
316
317 impl<'de> $zalsa::serde::Deserialize<'de> for $Struct<'_> {
318 fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
319 where
320 D: $zalsa::serde::Deserializer<'de>,
321 {
322 let id = $zalsa::Id::deserialize(deserializer)?;
323 Ok($zalsa::FromId::from_id(id))
324 }
325 }
326 }
327
328
329 unsafe impl Send for $Struct<'_> {}
330
331 unsafe impl Sync for $Struct<'_> {}
332
333 $zalsa::macro_if! { $generate_debug_impl =>
334 impl ::std::fmt::Debug for $Struct<'_> {
335 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
336 Self::default_debug_fmt(*self, f)
337 }
338 }
339 }
340
341 unsafe impl $zalsa::SalsaValue for $Struct<'_> {}
342
343 impl<$db_lt> $Struct<$db_lt> {
344 pub fn $new_fn<$Db>(db: &$db_lt $Db, $($field_id: $field_ty),*) -> Self
345 where
346 $Db: ?Sized + $zalsa::Database,
348 {
349 let (zalsa, zalsa_local) = db.zalsas();
350 $Configuration::ingredient_(zalsa).new_struct(
351 zalsa,zalsa_local,
352 ($($field_id,)*)
353 )
354 }
355
356 $(
357 $(#[$tracked_field_attr])*
358 $tracked_getter_vis fn $tracked_getter_id<$Db>(self, db: &$db_lt $Db) -> $crate::return_mode_ty!($tracked_option, $db_lt, $tracked_ty)
359 where
360 $Db: ?Sized + $zalsa::Database,
362 {
363 let (zalsa, zalsa_local) = db.zalsas();
364 let fields = $Configuration::ingredient_(zalsa).tracked_field(zalsa, zalsa_local, self, $relative_tracked_index);
365 $crate::return_mode_expression!(
366 $tracked_option,
367 $tracked_ty,
368 &fields.$absolute_tracked_index,
369 )
370 }
371 )*
372
373 $(
374 $(#[$untracked_field_attr])*
375 $untracked_getter_vis fn $untracked_getter_id<$Db>(self, db: &$db_lt $Db) -> $crate::return_mode_ty!($untracked_option, $db_lt, $untracked_ty)
376 where
377 $Db: ?Sized + $zalsa::Database,
379 {
380 let zalsa = db.zalsa();
381 let fields = $Configuration::ingredient_(zalsa).untracked_field(zalsa, self);
382 $crate::return_mode_expression!(
383 $untracked_option,
384 $untracked_ty,
385 &fields.$absolute_untracked_index,
386 )
387 }
388 )*
389 }
390
391 #[allow(unused_lifetimes)]
392 impl<'_db> $Struct<'_db> {
393 pub fn default_debug_fmt(this: Self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result
395 where
396 $(for<$db_lt> $field_ty: ::std::fmt::Debug),*
401 {
402 $zalsa::with_attached_database(|db| {
403 let zalsa = db.zalsa();
404 let fields = $Configuration::ingredient_(zalsa).leak_fields(zalsa, this);
405 let mut f = f.debug_struct(stringify!($Struct));
406 let f = f.field("[salsa id]", &$zalsa::AsId::as_id(&this));
407 $(
408 let f = f.field(stringify!($field_id), &fields.$field_index);
409 )*
410 f.finish()
411 }).unwrap_or_else(|| {
412 f.debug_struct(stringify!($Struct))
413 .field("[salsa id]", &$zalsa::AsId::as_id(&this))
414 .finish()
415 })
416 }
417 }
418 };
419 };
420}