Macro impl_discriminants

Source
macro_rules! impl_discriminants {
    (
        $(
            $(#[$meta:meta])*
            $ty:ty
        ),* $(,)?
    ) => { ... };
}
Expand description

Implements DiscriminantTag for one or more existing types.

This macro allows pre-existing types to act as discriminants without redefining them.

§Why Discriminants?

Discriminants provide a concrete type-level key that avoids ambiguity in generic trait resolution under Rust’s coherence rules.

By associating behavior with a discriminant instead of relying solely on generic types, implementations become:

  • uniquely identifiable
  • non-overlapping
  • composable across abstraction boundaries

§When to Use

Use this macro when:

  • a type already exists and should act as a discriminant
  • redefining it as a new ZST would be redundant or impossible

§Syntax

struct MyTag;
struct OtherTag;

impl_discriminants!(MyTag, OtherTag);

§Behavior

  • Implements DiscriminantTag for each provided type
  • Preserves the original type definition

§Notes

  • Types are expected to behave like discriminants (typically ZSTs)
  • No runtime guarantees are enforced; this is a semantic contract

§Summary

This macro extends the discriminant system to existing types, enabling reuse and integration without redefining identifiers.