#macro-utility #record #struct #enums #match-enums #match-variant #exhaustively

macro struct-record

Macro utility for defining structs with fields that exhaustively match enum variants, like a 'Record'

6 releases

Uses new Rust 2024

new 0.1.5 Feb 22, 2026
0.1.4 Feb 22, 2026

#702 in Procedural macros

MIT/Apache and LGPL-3.0-or-later

15KB
67 lines

struct-record

This is a macro utility to generate struct definitions from enum definitions, in a similar way to Typescript's Record<K, V> type when used with a string union type as K.

Usage

The following enum definition with the macro is more or less analogous to type ExampleEnumToF32 = Record<ExampleEnum, f32>:

#[record(f32, ExampleEnumToF32)]
enum ExampleEnum {
  FooExample,
  BarExample,
  BazExample,
}

const ex: ExampleEnumToF32 = ExampleEnumToF32 {
  foo_example: 1.0,
  bar_example: 2.0,
  baz_example: 3.0,
}

Installation

cargo add struct-record

Dependencies

~20–54MB
~843K SLoC