Module uniques

Source
Expand description

Create unique type lists at compile time

This is an excercise in the capabilities of macros and const fns.

From a very high level, the process goes like this:

  1. We recursively look at a type, counting how many types it contains, WITHOUT considering de-duplication. This is used as an “upper bound” of the number of potential types we could have to report
  2. Create an array of [Option<&NamedType>; MAX] that we use something like an append-only vec.
  3. Recursively traverse the type AGAIN, this time collecting all unique non-primitive types we encounter, and adding them to the list. This is outrageously inefficient, but it is done at const time with all the restrictions it entails, because we don’t pay at runtime.
  4. Record how many types we ACTUALLY collected in step 3, and create a new array, [&NamedType; ACTUAL], and copy the unique types into this new array
  5. Convert this [&NamedType; N] array into a &'static [&NamedType] array to make it possible to handle with multiple types
  6. If we are collecting MULTIPLE types into a single aggregate report, then we make a new array of [Option<&NamedType>; sum(all types)], by calculating the sum of types contained for each list calculated in step 4.
  7. We then perform the same “merging” process from 3, pushing any unique type we find into the aggregate list, and recording the number of unique types we found in the entire set.
  8. We then perform the same “shrinking” process from step 4, leaving us with a single array, [&NamedType; TOTAL] containing all unique types
  9. We then perform the same “slicing” process from step 5, to get our final &'static [&NamedType].

Functions§

combine_with_copy
,
cruncher
This function reduces a &[Option<&NamedType>] to a [&NamedType; A].
merge_nty_lists
This function turns an array of type lists into a single list of unique types
total_len
Get the sum of the length of all arrays
type_chewer_nty
This function collects the set of unique types, reporting the entire list (which might only be partially used), as well as the used length.
unique_types_nty_upper
Count the number of unique types contained by this NamedType, including children and this type itself.