Skip to content

Remove feature gate from enum Error #645

@Kixunil

Description

@Kixunil

Feature gating enum variants of public not non_exhaustive enum is compatibility hazard as it makes Cargo features non-additive. See https://github.com/rust-bitcoin/rust-bitcoin/pull/633/files#r704806113

Suggested solution:

enum Uninhabited {}

#[cfg(bitcoinconsensus)]
type BitcoinConsensusError = bitcoinconsensus::Error;

#[cfg(not(bitcoinconsensus))]
pub struct BitcoinConsensusError {
    _uninhabited: Uninhabited,
}

// perhaps implement a few things `bitcoinconsensus::Error` implements using match self._uninhabited {}

enum Error {
    // ...
   BitcoinConsensus(BitcoinConsensusError),
}

This way we can get quite clean code matching on enums, we guarantee that no crate importing BitcoinConsensusError can do anything interesting with it unless the feature is turned on. We also prove to the compiler that we never actually construct that variant if the feature is off, so performance doesn't change (uninhabited variants are optimized-out automatically) and we avoid accidentally creating it.

Alternative

Make the Error enum non_exhaustive and keep the gate. AFAIR this requires MSRV bump.

Metadata

Metadata

Assignees

No one assigned

    Labels

    1.0Issues and PRs required or helping to stabilize the APIAPI breakThis PR requires a version bump for the next releasebug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions