Macro discriminants

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

Defines one or more public zero-sized discriminant types.

A discriminant is a concrete type-level key used to uniquely identify behavior, structure, or interpretation in generic systems.

§Why Discriminants?

In Rust, trait implementations over generic or associated types can become ambiguous under coherence rules:

  • generic types may unify in multiple ways
  • implementations may overlap when types are not fully concrete
  • the compiler must reject such cases conservatively

Discriminants solve this by introducing a concrete, unique type that disambiguates otherwise overlapping implementations.

This enables:

  • multiple interpretations over the same types
  • safe composition of generic abstractions
  • coherence-safe extensibility

§Syntax

discriminants!(
    /// Optional docs or attributes
    A,

    B,

    #[cfg(feature = "x")]
    C,
);

§Expansion

For each identifier, this macro generates:

§Properties

  • zero runtime cost (ZSTs)
  • purely type-level identifiers
  • stable and unambiguous across generic contexts
  • reusable across virtual fields, extensions, and plugin systems

§Why Always Public

Discriminants appear in type signatures and generic bounds, making them part of the public type-level API.

Restricting visibility would:

  • prevent use in external generic constraints
  • break composability across modules or crates
  • force duplication of identical identifiers

In practice, discriminants are type-level contracts, not internal details.