| 1 |
# Bit-Sequence Buffer Encoding |
| 2 |
|
| 3 |
This macro accepts a sequence of bit expressions from the public macros and |
| 4 |
creates encoded `[T; N]` arrays from them. The public macros can then use these |
| 5 |
encoded arrays as the basis of the requested data structure. |
| 6 |
|
| 7 |
This is a complex macro that uses recursion to modify and inspect its input |
| 8 |
tokens. It is divided into three major sections. |
| 9 |
|
| 10 |
## Entry Points |
| 11 |
|
| 12 |
The first section provides a series of entry points that the public macros |
| 13 |
invoke. Each arm matches the syntax provided by public macros, and detects a |
| 14 |
specific `BitStore` implementor name: `uN`, `Cell<uN>`, `AtomicUN`, or |
| 15 |
`RadiumUN`, for each `N` in `8`, `16`, `32`, `64`, and `size`. |
| 16 |
|
| 17 |
These arms then recurse, adding a token for the raw unsigned integer used as the |
| 18 |
basis of the encoding. The `usize` arms take an additional recursion that routes |
| 19 |
to the 32-bit or 64-bit encoding, depending on the target. |
| 20 |
|
| 21 |
## Zero Extension |
| 22 |
|
| 23 |
The next two arms handle extending the list of bit-expressions with 64 `0,`s. |
| 24 |
The first arm captures initial reëntry and appends the zero-comma tokens, then |
| 25 |
recurses to enter the chunking group. The second arm traps when recursion has |
| 26 |
chunked all user-provided tokens, and only the literal `0,` tokens appended by |
| 27 |
the first arm remain. |
| 28 |
|
| 29 |
The second arm dispatches the chunked bit-expressions into the element encoder, |
| 30 |
and is the exit point of the macro. Its output is an array of encoded memory |
| 31 |
elements, typed as the initially-requested `BitStore` name. |
| 32 |
|
| 33 |
The `0,` tokens remain matchable as text literals because they never depart |
| 34 |
this macro: recursion within the same macro does not change the types in the |
| 35 |
AST, while invoking a new macro causes already-known tokens to become opacified |
| 36 |
into `:tt` whose contents cannot be matched. This is the reason that the macro |
| 37 |
is recursive rather than dispatching. |
| 38 |
|
| 39 |
## Chunking |
| 40 |
|
| 41 |
The stream of user-provided bit-expressions, followed by the appended zero-comma |
| 42 |
tokens, is divided into chunks by the width of the storage type. |
| 43 |
|
| 44 |
Each width (8, 16, 32, 64) has an arm that munches from the token stream and |
| 45 |
grows an opaque token-list containing munched groups. In syntax, this is |
| 46 |
represented by the `[$([$($bit:tt,)+],)*];` cluster: |
| 47 |
|
| 48 |
- it is an array |
| 49 |
- of zero or more arrays |
| 50 |
- of one or more bit expressions |
| 51 |
- each followed by a comma |
| 52 |
- each followed by a comma |
| 53 |
- followed by a semicolon |
| 54 |
|
| 55 |
By placing this array ahead of the bit-expression stream, we can use the array |
| 56 |
as an append-only list (matched as `[$($elem:tt)*]`, emitted as |
| 57 |
`[$($elem)* [new]]`) grown by munching from the token stream of unknown length |
| 58 |
at the end of the argument set. |
| 59 |
|
| 60 |
On each recursion, the second arm in zero-extension attempts to trap the input. |
| 61 |
If it fails, then user-provided tokens remain; if it succeeds, then it discards |
| 62 |
any remaining macro-appended zeros and terminates. |