nativeview-codegen 0.1.0-rc.1

Multi-language ABI, policy, flat-op codec, and hybrid Direct-FFI code generator for Native View Engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! The word-table view-model every host table mirror renders: per-table metadata (inheritance,
//! `EventSet` word), per-field host specs (kind, `Option` encoding, wide value word, explicit
//! bit, resource kind), and the named value sets, all validated once so every host constructor
//! accepts them.

use std::collections::BTreeSet;

use nativeview_abi::table::{
    FieldDesc, FieldKind, MAX_TABLE_WORDS, NO_VAL_WORD, OptEncoding, Storage, TableDesc,
    ValueSetDesc, table_flags,
};
use nativeview_hash::Key64;

use super::model::U64Split;
use crate::error::CodegenError;

/// 0-based event discriminants and listener bitmasks derived from `TableDesc::events`
/// (`EventEnum::EVENT_SET`), where every `(name, bit)` has `bit == 1 << id`.
#[derive(Debug)]
pub struct EventEnumModel {
    /// Event enum name, e.g. `TextInputEvent` (derived by stripping `"Set"` from `"TextInputEventSet"`).
    pub name: String,
    /// `SCREAMING_SNAKE` prefix, e.g. `TEXT_INPUT_EVENT`.
    pub screaming: String,
    /// `(variant_name, event_id, bit_mask)` in declaration order (`bit_mask == 1 << event_id`).
    pub variants: Vec<(&'static str, u32, u32)>,
}

/// One word table, validated once so every host constructor accepts its field specs.
#[derive(Debug)]
pub struct TableModel<'a> {
    pub desc: &'a TableDesc,
    /// `nv.text_input` -> `TextInput` (hosts nest it under `NvTables`).
    pub ident: String,
    /// `nv.text_input` -> `TEXT_INPUT` (C macro / Java constant prefix).
    pub screaming: String,
    pub key: U64Split,
    pub schema: U64Split,
    pub fields: Vec<FieldModel<'a>>,
    pub event_enum: Option<EventEnumModel>,
}

impl TableModel<'_> {
    pub fn dense(&self) -> bool {
        self.desc.storage == Storage::Dense
    }

    /// Word of the `EventSet<E>` listener mask, or [`NO_VAL_WORD`].
    pub fn events_word(&self) -> u8 {
        self.desc.events_word
    }

    /// Word 0 is the explicit-override mask (`#[nv_table(inherited)]`).
    pub fn inherited(&self) -> bool {
        self.desc.inherited
    }

    /// Resource fields with their `FlatResource::KIND`.
    pub fn resource_fields(&self) -> impl Iterator<Item = (&FieldModel<'_>, U64Split)> {
        self.fields.iter().filter_map(|f| f.resource_kind.map(|k| (f, k)))
    }
}

/// One field as every host `FieldSpec` spells it.
#[derive(Debug)]
pub struct FieldModel<'a> {
    pub desc: &'a FieldDesc,
    /// Host bit width: a two-word field (`Str`, 64-bit) is `bits = 64` in Rust but `(shift 0,
    /// bits 32)` on every host (the second word is implied by the kind).
    pub bits: u8,
    /// `OptEncoding::WideBit` value word (`word` / `shift` hold the presence bit), else
    /// [`NO_VAL_WORD`].
    pub val_word: u8,
    /// Bit of the field in an inherited table's explicit mask (word 0), else [`NO_VAL_WORD`].
    pub explicit_bit: u8,
    /// An unset value resolves from the parent row (inherited table, not `#[no_inherit]`).
    pub inherits: bool,
    /// `FlatResource::KIND` of a `Resource` field.
    pub resource_kind: Option<U64Split>,
}

impl FieldModel<'_> {
    pub fn name(&self) -> &str {
        self.desc.name
    }
}

/// Fields spanning two whole words (`{ offset, len }` / `lo, hi`).
fn two_word(k: FieldKind) -> bool {
    matches!(k, FieldKind::Str | FieldKind::U64 | FieldKind::I64 | FieldKind::F64)
}

/// Validates `tables` (unique names, keys that match their names, fields every host reader
/// accepts) and wraps them for rendering, in the given order.
pub fn tables_model<'a>(tables: &[&'a TableDesc]) -> Result<Vec<TableModel<'a>>, CodegenError> {
    let mut names = BTreeSet::new();
    let mut out = Vec::with_capacity(tables.len());
    for desc in tables {
        super::validate::table(desc)?;
        let short = desc.name.strip_prefix("nv.").unwrap_or(desc.name);
        let ident = super::spell::pascal(short);
        if !names.insert(ident.clone()) {
            return Err(table_error(desc, "duplicate table name"));
        }
        if desc.key != Key64::of(desc.name) {
            return Err(table_error(desc, "key is not `xxh64(name)`"));
        }
        let words = desc.words();
        if words == 0 || words > MAX_TABLE_WORDS || desc.word_effects.len() != words {
            return Err(table_error(desc, "word count out of range"));
        }
        check_table_words(desc, words)?;
        let fields = desc
            .fields
            .iter()
            .enumerate()
            .map(|(i, f)| field_model(desc, i, f, words))
            .collect::<Result<Vec<_>, _>>()?;
        let event_enum = desc.events.map(|set| event_enum_model(desc, set)).transpose()?;
        out.push(TableModel {
            desc,
            screaming: short.replace('.', "_").to_ascii_uppercase(),
            ident,
            key: U64Split::new(desc.key.0),
            schema: U64Split::new(desc.schema_hash),
            fields,
            event_enum,
        });
    }
    Ok(out)
}

fn is_ident(s: &str) -> bool {
    s.chars().next().is_some_and(|c| c.is_ascii_alphabetic())
        && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

fn event_enum_model(desc: &TableDesc, set: &'static ValueSetDesc) -> Result<EventEnumModel, CodegenError> {
    if set.values.is_empty() {
        return Err(table_error(desc, "the `EventSet` must have at least one variant"));
    }
    let stripped = set
        .name
        .strip_suffix("Set")
        .filter(|s| !s.is_empty())
        .unwrap_or(set.name);
    // Avoid `NvNvFooEvent` / `NV_NV_FOO_EVENT` when an `EventSet` already carries an `Nv` prefix.
    let name = stripped
        .strip_prefix("Nv")
        .filter(|s| s.chars().next().is_some_and(|c| c.is_ascii_uppercase()))
        .unwrap_or(stripped)
        .to_owned();
    if !is_ident(&name) {
        return Err(table_error(desc, &format!("invalid `EventSet` enum name `{name}`")));
    }
    let mut variants = Vec::with_capacity(set.values.len());
    let (mut seen_bits, mut seen_screaming, mut seen_swift, mut seen_dart) =
        (BTreeSet::new(), BTreeSet::new(), BTreeSet::new(), BTreeSet::new());
    for &(variant, bit) in set.values {
        if !is_ident(variant) {
            return Err(table_error(desc, &format!("invalid event variant name `{variant}`")));
        }
        if !bit.is_power_of_two() {
            return Err(table_error(
                desc,
                &format!("event `{variant}` mask `{bit}` is not a single bit"),
            ));
        }
        if !seen_bits.insert(bit) {
            return Err(table_error(
                desc,
                &format!("duplicate event bit mask {bit} (`{variant}`)"),
            ));
        }
        if !seen_screaming.insert(super::spell::screaming(variant)) {
            return Err(table_error(
                desc,
                &format!("duplicate event variant name `{variant}`"),
            ));
        }
        if !seen_swift.insert(super::spell::swift::variant(variant))
            || !seen_dart.insert(super::spell::dart::variant(variant))
        {
            return Err(table_error(
                desc,
                &format!("event variant `{variant}` collides with another variant once escaped"),
            ));
        }
        variants.push((variant, bit.trailing_zeros(), bit));
    }
    let screaming = super::spell::screaming(&name);
    Ok(EventEnumModel { name, screaming, variants })
}

/// The hidden words (explicit mask, `EventSet`) agree with the table flags and stay field-free.
fn check_table_words(desc: &TableDesc, words: usize) -> Result<(), CodegenError> {
    let flag = |bit: u16| desc.flags & bit != 0;
    if flag(table_flags::INHERITED) != desc.inherited {
        return Err(table_error(desc, "`inherited` disagrees with `table_flags::INHERITED`"));
    }
    let has_events = desc.events_word != NO_VAL_WORD;
    if flag(table_flags::HAS_EVENTS) != has_events || desc.events.is_some() != has_events {
        return Err(table_error(
            desc,
            "`events_word`, `events`, and `table_flags::HAS_EVENTS` disagree",
        ));
    }
    if has_events && (usize::from(desc.events_word) >= words || desc.inherited) {
        return Err(table_error(desc, "the `EventSet` word is out of range or shares word 0"));
    }
    if desc.events.is_some_and(|set| !set.flags) {
        return Err(table_error(desc, "the `EventSet` values must be a flag set"));
    }
    if desc.inherited && desc.fields.len() > 32 {
        return Err(table_error(desc, "an inherited table holds at most 32 fields"));
    }
    Ok(())
}

fn field_model<'a>(
    desc: &TableDesc,
    index: usize,
    f: &'a FieldDesc,
    words: usize,
) -> Result<FieldModel<'a>, CodegenError> {
    let bad = |why: &str| Err(table_error(desc, &format!("field `{}` {why}", f.name)));
    let (span, shape_ok) = if two_word(f.kind) {
        (2, f.shift == 0 && f.bits == 64)
    } else {
        (1, (1..=32).contains(&f.bits) && u32::from(f.shift) + u32::from(f.bits) <= 32)
    };
    if !shape_ok || usize::from(f.word) + span > words {
        return bad("does not fit its row");
    }
    let wide = f.opt_encoding == OptEncoding::WideBit;
    if wide != (f.val_word != NO_VAL_WORD)
        || wide && (f.bits != 1 || usize::from(f.val_word) >= words || f.val_word == f.word)
    {
        return bad("has a value word that disagrees with its `Option` encoding");
    }
    let hidden = [desc.inherited.then_some(0), (desc.events_word != NO_VAL_WORD).then_some(desc.events_word)];
    let touched = f.word..f.word + span as u8;
    if hidden.into_iter().flatten().any(|w| touched.contains(&w) || wide && w == f.val_word) {
        return bad("overlaps the explicit-mask or `EventSet` word");
    }
    if f.no_inherit && !desc.inherited {
        return bad("is `#[no_inherit]` on a table that does not inherit");
    }
    let resource_kind = match (f.kind, desc.resource_kind(usize::from(f.word))) {
        (FieldKind::Resource, Some(kind)) => Some(U64Split::new(kind.0)),
        (FieldKind::Resource, None) => return bad("has no `FlatResource::KIND`"),
        _ => None,
    };
    Ok(FieldModel {
        desc: f,
        bits: if two_word(f.kind) { 32 } else { f.bits },
        val_word: f.val_word,
        explicit_bit: if desc.inherited { index as u8 } else { NO_VAL_WORD },
        inherits: desc.inherited && !f.no_inherit,
        resource_kind,
    })
}

/// Distinct named value sets (`WordEnum` variants, flag bits, `EventSet` listener bits) the
/// `tables` carry, in first-use order. Sets sharing a name must be identical; names must spell
/// identifiers, every emitted `NV_<SET>_<VALUE>` constant must be unique across all sets and
/// clear of the `NV_TABLE_*` namespace, and every value must fit its field once encoded.
pub fn value_sets(tables: &[TableModel<'_>]) -> Result<Vec<&'static ValueSetDesc>, CodegenError> {
    value_sets_with_core(tables, &[], true)
}

/// Like [`value_sets`], validating `tables` against already-emitted `shared_sets` (core
/// `NvTables` value sets plus any registered cross-module shared value sets, e.g. `nativeview-media-fit`).
///
/// Any value set whose name matches an entry in `shared_sets` MUST have an identical definition
/// (otherwise validation fails with `"defined twice differently"`). When `omit_shared` is `true`
/// (Swift, Dart, Java, TypeScript), shared sets are omitted from the returned list so they are
/// defined once in their owning module/core; when `false` (standalone C/C++ module headers using
/// `#ifndef`), they are retained so standalone inclusion stays self-contained. Every field
/// carrying a shared set is still checked for bit-width fit, and new module sets are checked for
/// constant collisions against `shared_sets`.
pub fn value_sets_with_core(
    tables: &[TableModel<'_>],
    shared_sets: &[&'static ValueSetDesc],
    omit_shared: bool,
) -> Result<Vec<&'static ValueSetDesc>, CodegenError> {
    let mut out: Vec<&'static ValueSetDesc> = Vec::new();
    let mut constants = BTreeSet::new();
    for shared in shared_sets {
        let prefix = super::spell::screaming(shared.name);
        for &(name, _) in shared.values {
            constants.insert(format!("{prefix}_{}", super::spell::screaming(name)));
        }
    }
    for t in tables {
        let fields = t.desc.fields.iter().filter_map(|f| f.values.map(|set| (set, Some(f))));
        for (set, field) in t.desc.events.map(|set| (set, None)).into_iter().chain(fields) {
            let fail = |why: String| table_error(t.desc, &format!("value set `{}`: {why}", set.name));
            // Every field that carries the set must hold every value, also when the set was
            // already emitted for a wider field.
            if let Some(f) = field
                && let Some(&(name, _)) = set.values.iter().find(|&&(_, v)| !fits(f, v))
            {
                return Err(fail(format!("value `{name}` exceeds field `{}`", f.name)));
            }
            if let Some(shared) = shared_sets.iter().find(|s| s.name == set.name) {
                if *shared != set {
                    return Err(fail("defined twice differently".into()));
                }
                if !omit_shared && out.iter().all(|s| s.name != set.name) {
                    out.push(set);
                }
                continue;
            }
            if let Some(seen) = out.iter().find(|s| s.name == set.name) {
                if *seen != set {
                    return Err(fail("defined twice differently".into()));
                }
                continue;
            }
            check_set(set, &mut constants).map_err(fail)?;
            out.push(set);
        }
    }
    Ok(out)
}

/// Validates and deduplicates standalone shared value sets (such as `nativeview-media-fit`'s `ImageFit`
/// and `ImageSampling`), rejecting empty/invalid names, duplicate constant spellings, or
/// conflicting redefinitions.
pub fn validate_shared_value_sets(
    sets: &[&'static ValueSetDesc],
) -> Result<Vec<&'static ValueSetDesc>, CodegenError> {
    let mut out: Vec<&'static ValueSetDesc> = Vec::with_capacity(sets.len());
    let mut constants = BTreeSet::new();
    for &set in sets {
        let fail = |why: String| CodegenError::validation(format!("shared value set `{}`: {why}", set.name));
        if let Some(seen) = out.iter().find(|s| s.name == set.name) {
            if *seen != set {
                return Err(fail("defined twice differently".into()));
            }
            continue;
        }
        check_set(set, &mut constants).map_err(fail)?;
        out.push(set);
    }
    Ok(out)
}

/// Merges core table value sets with registered cross-module shared value sets, allowing exact
/// identical definitions during transitions and rejecting conflicting definitions or C constant
/// collisions.
pub fn merge_shared_value_sets(
    core_sets: &[&'static ValueSetDesc],
    extra_shared: &[&'static ValueSetDesc],
) -> Result<Vec<&'static ValueSetDesc>, CodegenError> {
    let mut merged: Vec<&'static ValueSetDesc> = Vec::with_capacity(core_sets.len() + extra_shared.len());
    merged.extend_from_slice(core_sets);
    merged.extend_from_slice(extra_shared);
    validate_shared_value_sets(&merged)
}

/// `v` stored with `f`'s `Option` encoding fits `f.bits` (a wide value word holds any `u32`).
fn fits(f: &FieldDesc, v: u32) -> bool {
    let stored = match f.opt_encoding {
        OptEncoding::PlusOne => u64::from(v) + 1,
        OptEncoding::Bit0Present => (u64::from(v) << 1) | 1,
        OptEncoding::WideBit => return true,
        OptEncoding::Required | OptEncoding::ZeroNiche | OptEncoding::TagXor => u64::from(v),
    };
    f.bits >= 32 || stored >> f.bits == 0
}

/// A set's name and member spellings are identifiers unique after each host's escaping, its
/// values are distinct (single bits for a flag set), and its `NV_<SET>_<VALUE>` constants are
/// new.
fn check_set(set: &ValueSetDesc, constants: &mut BTreeSet<String>) -> Result<(), String> {
    let prefix = super::spell::screaming(set.name);
    if !is_ident(set.name) || set.values.is_empty() || prefix == "TABLE" || prefix.starts_with("TABLE_") {
        return Err("needs an identifier name outside `TABLE*` and at least one value".into());
    }
    // Per-set member spellings each host renders (keyword-escaped), and the values: a collision
    // after escaping or a repeated value would not compile (Swift raw values) or would alias two
    // members.
    let (mut swift, mut dart, mut values) = (BTreeSet::new(), BTreeSet::new(), BTreeSet::new());
    for &(name, value) in set.values {
        let constant = format!("{prefix}_{}", super::spell::screaming(name));
        if !is_ident(name) || !constants.insert(constant.clone()) {
            return Err(format!("bad or duplicate name `{name}` (NV_{constant})"));
        }
        if !swift.insert(super::spell::swift::variant(name)) || !dart.insert(super::spell::dart::variant(name)) {
            return Err(format!("name `{name}` collides with another member once escaped"));
        }
        if !values.insert(value) {
            return Err(format!("duplicate value {value} (`{name}`)"));
        }
        if set.flags && !value.is_power_of_two() {
            return Err(format!("flag `{name}` is not a single bit"));
        }
    }
    Ok(())
}

pub(super) fn table_error(desc: &TableDesc, why: &str) -> CodegenError {
    CodegenError::validation(format!("table `{}`: {why}", desc.name))
}

/// `FieldKind` as spelled by each host runtime's enum.
pub mod kind {
    use nativeview_abi::table::FieldKind;

    /// `(kind, C, Swift / Dart, TypeScript, Java)`.
    type Row = (FieldKind, &'static str, &'static str, &'static str, &'static str);

    const NAMES: [Row; 14] = [
        (FieldKind::U32, "NV_FIELD_U32", "u32", "U32", "U32"),
        (FieldKind::I32, "NV_FIELD_I32", "i32", "I32", "I32"),
        (FieldKind::F32, "NV_FIELD_F32", "f32", "F32", "F32"),
        (FieldKind::Bool, "NV_FIELD_BOOL", "bool", "Bool", "BOOL"),
        (FieldKind::Enum, "NV_FIELD_ENUM", "enumeration", "Enum", "ENUM"),
        (FieldKind::Color, "NV_FIELD_COLOR", "color", "Color", "COLOR"),
        (FieldKind::Length, "NV_FIELD_LENGTH", "length", "Length", "LENGTH"),
        (FieldKind::Str, "NV_FIELD_STR", "str", "Str", "STR"),
        (FieldKind::Resource, "NV_FIELD_RESOURCE", "resource", "Resource", "RESOURCE"),
        (FieldKind::Node, "NV_FIELD_NODE", "node", "Node", "NODE"),
        (FieldKind::U64, "NV_FIELD_U64", "u64", "U64", "U64"),
        (FieldKind::I64, "NV_FIELD_I64", "i64", "I64", "I64"),
        (FieldKind::F64, "NV_FIELD_F64", "f64", "F64", "F64"),
        (FieldKind::GridLine, "NV_FIELD_GRID_LINE", "gridLine", "GridLine", "GRID_LINE"),
    ];

    fn row(k: FieldKind) -> Row {
        NAMES[k as usize]
    }

    /// C `NvFieldKind` constant.
    pub fn c(k: &FieldKind) -> &'static str {
        row(*k).1
    }

    /// Swift `FieldKind` case.
    pub fn swift(k: &FieldKind) -> &'static str {
        row(*k).2
    }

    /// Java `FieldKind` constant.
    pub fn java(k: &FieldKind) -> &'static str {
        row(*k).4
    }

    /// Dart `FieldKind` value (`bool` is a keyword there).
    pub fn dart(k: &FieldKind) -> &'static str {
        match k {
            FieldKind::Bool => "boolean",
            _ => row(*k).2,
        }
    }

    /// TypeScript `FieldKind` key.
    pub fn ts(k: &FieldKind) -> &'static str {
        row(*k).3
    }

    #[cfg(test)]
    mod tests {
        #[test]
        fn names_are_indexed_by_discriminant() {
            for (i, row) in super::NAMES.iter().enumerate() {
                assert_eq!(row.0 as usize, i);
            }
        }
    }
}

/// `OptEncoding` as spelled by each host runtime's enum.
pub mod opt {
    use nativeview_abi::table::OptEncoding;

    /// `(encoding, C, Swift / Dart, TypeScript, Java)`.
    type Row = (OptEncoding, &'static str, &'static str, &'static str, &'static str);

    const NAMES: [Row; 6] = [
        (OptEncoding::Required, "NV_OPT_REQUIRED", "required", "Required", "REQUIRED"),
        (OptEncoding::ZeroNiche, "NV_OPT_ZERO_NICHE", "zeroNiche", "ZeroNiche", "ZERO_NICHE"),
        (OptEncoding::Bit0Present, "NV_OPT_BIT0_PRESENT", "bit0Present", "Bit0Present", "BIT0_PRESENT"),
        (OptEncoding::PlusOne, "NV_OPT_PLUS_ONE", "plusOne", "PlusOne", "PLUS_ONE"),
        (OptEncoding::TagXor, "NV_OPT_TAG_XOR", "tagXor", "TagXor", "TAG_XOR"),
        (OptEncoding::WideBit, "NV_OPT_WIDE_BIT", "wideBit", "WideBit", "WIDE_BIT"),
    ];

    fn row(o: OptEncoding) -> Row {
        NAMES[o as usize]
    }

    /// C `NvOptEncoding` constant.
    pub fn c(o: &OptEncoding) -> &'static str {
        row(*o).1
    }

    /// Swift / Dart `OptEncoding` case.
    pub fn swift(o: &OptEncoding) -> &'static str {
        row(*o).2
    }

    /// Java `OptEncoding` constant.
    pub fn java(o: &OptEncoding) -> &'static str {
        row(*o).4
    }

    /// TypeScript `OptEncoding` key.
    pub fn ts(o: &OptEncoding) -> &'static str {
        row(*o).3
    }

    #[cfg(test)]
    mod tests {
        #[test]
        fn names_are_indexed_by_discriminant() {
            for (i, row) in super::NAMES.iter().enumerate() {
                assert_eq!(row.0 as usize, i);
            }
        }
    }
}