Skip to content

Each shape enum lists its variants once

A shape enum (Curve, ElementaryCurve, Curve2D, Surface, ElementarySurface) writes its variant list once, in a crate-private *_variants! macro beside the enum: curve_variants! for the two 3D curve enums, the elementary list written once and extended with the B-spline and Bézier variants for Curve; curve_2d_variants!; surface_variants! for the two surface enums. Every match that only forwards a call to the shape a variant holds is expanded from that list, through curve_dispatch!(self, c => c.point_at(u)) and its siblings, in whichever folder it lives: the trait impls, bounding boxes, discretization, B-spline conversion, the 2D embedding. The From<Shape> for Enum impls and the widening From<ElementaryCurve> for Curve come from the same list, and narrowing (as_elementary, as_bspline) is a plain forward, since each shape answers it for itself. The enum declarations stay written out, so rustdoc shows them as they are and the compiler checks the list against them. A match whose arms do different work per variant stays written out too: it is where a reader looks for per-variant behaviour, and the compiler lists it when a variant is added.

Every enum forwards every method of its traits, provided methods included (tangent, curvature, is_periodic, project_points, normal), so a shape's override is what the enum answers. Before this decision each enum forwarded a hand-picked subset, and the rest fell back to the trait's provided body: the same answer today, but an override a shape adds later (the B-splines already override is_periodic) would be skipped without a sign. With one generated line per method, forwarding everything costs nothing to write.

The list is what makes a new variant cheap. BezierCurve, BezierCurve2D and BezierSurface join Curve, Curve2D and Surface as variants of their own, and are operands of Intersect in both orders against every curve and surface, routed like the B-spline each wraps (ADR 0011). Adding them took one line per list, the iso curves BezierSurface lacked, and an arm in each match that really differs per variant: B-spline conversion, curve-on-surface parametrization, and the 2D exact solvers, which report NotAnalytic for them as for a B-spline.

Considered options

  • Nest the elementary enum, Curve::Elementary(ElementaryCurve): rejected. It removes the duplicated elementary variants but adds a level to every pattern a caller writes (Curve::Elementary(ElementaryCurve::Line(l))), breaks every match on Curve outside the crate, and costs a second discriminant test on the hot dispatch path. One list macro gives the same single source without changing the enum's shape.
  • Generate the enum declaration from the list too: rejected. A macro-built enum hides its variants and their docs from a reader of the source, and the compiler already rejects a list that disagrees with the declaration, so writing it out costs one line per variant and loses nothing.
  • A trait object (Box<dyn ParametricCurve>): rejected. It allocates, loses exhaustive matching and the per-pair routing, and costs an indirect call where a match costs a jump; the enums exist to avoid all three.
  • A procedural derive: rejected. A proc-macro crate adds a build dependency and a second crate to maintain for what three declarative macros do.
  • Leave a Bézier type outside the enums, converted to its B-spline first: rejected. The caller would lose the Bézier type at every enum boundary, u_iso of a Surface could not return a Bézier curve, and a Python caller holding a Bézier object could not pass it to intersect or TrimmedCurve at all.

Consequences

Adding a curve or surface type means one variant in the enum, one line in its list, and the per-variant matches the compiler then points at. BezierSurface gains u_iso, v_iso, u_reversed_parameter and v_reversed_parameter, so Surface forwards those methods to every variant. A match on Curve, Curve2D or Surface outside the crate already needs a wildcard arm, since the enums are #[non_exhaustive], and keeps compiling.

2026-10-30: the variant lists also check the test samples (ADR 0027). A new variant needs a sample in tests/common/samples.rs, and cargo test names it until it has one.