Skip to content

refactor: adopt enum_dispatch or ambassador where they fit #147

Description

@AbysmalBiscuit

Decide where enum_dispatch or ambassador would replace hand-written forwarding, and adopt them there. A survey on 2026-09-26 found no site that clearly qualifies, so the likely outcome is closing this with the criteria recorded, unless new code has changed that.

Follow-up to #55 (#146), which moved the catalog and PowerShell cmdlet dispatch onto strum enums. Neither crate applies to those: enum_dispatch forwards a trait's methods to an enum's inner types, and ambassador delegates a trait impl to a field or variant. Both need a trait, and those parses have none.

When each one earns its place

  • enum_dispatch: a trait with a closed set of implementors, held as Box<dyn Trait> or &dyn Trait on a path where the vtable call or the allocation shows up in a profile.
  • ambassador: a newtype or enum whose trait impls are several methods of pure forwarding, where adding a method to the trait means editing the forwarder by hand.

Neither buys exhaustiveness. A trait already makes every implementor supply every method, so the gain is boilerplate or speed only.

Candidates found

  1. Tracker (devkit-common/src/tracker/), held as Box<dyn Tracker>. Implementors are GithubTracker, LinearTracker, NoneTracker and FakeTracker. Every call goes to the network or gh, so the vtable cost is noise, and FakeTracker would have to join a production enum or sit behind a cfg-gated variant.
  2. PortProbe, ProcTable (devkit-ports/src/strays/) and Registry (devkit-docs/src/lookup.rs). These are test seams whose other implementors are stubs inside test modules. A closed enum would pull those stubs into non-test code.
  3. LockContext (devkit-docs/src/importers.rs), a two-variant enum over JsContext and TomlContext. Only select forwards uniformly; at_lock_root, member_dirs and rollup do different things per variant. Adopting either crate means inventing a trait for one method.
  4. Replay (devkit-docs/src/importers.rs), a newtype forwarding Display and Error::source. Two short foreign-trait impls, which ambassador handles only through delegatable_trait_remote.

Done when

Each candidate above, plus any trait object or forwarding enum added since, is either converted or listed here with the reason it stays as is.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions