-
Notifications
You must be signed in to change notification settings - Fork 965
Remove feature gate from enum Error #645
Copy link
Copy link
Closed
Labels
1.0Issues and PRs required or helping to stabilize the APIIssues and PRs required or helping to stabilize the APIAPI breakThis PR requires a version bump for the next releaseThis PR requires a version bump for the next releasebug
Description
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
1.0Issues and PRs required or helping to stabilize the APIIssues and PRs required or helping to stabilize the APIAPI breakThis PR requires a version bump for the next releaseThis PR requires a version bump for the next releasebug