skip to content

As lead of a widely used Rust HTTP-client crate approaching 1.0, what policy would you set for `#[non_exhaustive]`, deprecation and cutting major versions?

level: principalimportance: should knowfreq 30%

answer

  1. decide per type at first release
  2. open sets versus closed sets
  3. deprecate in a minor, remove in a major
  4. batch breaking changes deliberately

basics

~20 s

Decide before 1.0, type by type: #[non_exhaustive] on error enums, open-ended enums and option structs, exhaustive enums for closed sets. Deprecate with #[deprecated(since, note)] in a minor release, remove only in a major, and batch breaking changes into rare majors.

solid answer

~40 s

There is no single right answer; the policy trades caller convenience against room to grow. I would mark error enums, open-ended enums (protocol versions, events) and option structs `#[non_exhaustive]` from their first release, because adding the attribute later is itself major, and leave enums whose values are closed by definition exhaustive so callers keep compile-time exhaustiveness. Structs that hold invariants get private fields and constructors. Traits users only call are sealed; traits users implement only gain defaulted methods. Public dependencies are kept few and listed, because their majors become ours, and `Send` and `Sync` promises are asserted in tests. Replacements ship in a minor with `#[deprecated(since = "1.4.0", note = "...")]` on the old item, removal waits for the next major, and breaking changes are batched so majors stay rare and announced.

code

rust · 37 lines
rust
pub mod httpc {
    /// Every failure the client reports; new kinds arrive in minor releases.
    #[non_exhaustive]
    #[derive(Debug)]
    pub enum Error {
        Timeout,
        Connect(String),
    }

    /// Plain options: public fields, built from Default.
    #[non_exhaustive]
    #[derive(Debug, Default)]
    pub struct Config {
        pub max_redirects: u32,
    }

    pub struct Client;

    impl Client {
        pub fn send(&self, _cfg: &Config) -> Result<(), Error> { Ok(()) }

        #[deprecated(since = "1.4.0", note = "use `Client::send` instead")]
        pub fn send_raw(&self) -> Result<(), Error> { self.send(&Config::default()) }
    }

    // Fails to compile if a later change makes these types lose Send or Sync.
    const _: () = {
        const fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Client>();
        assert_send_sync::<Error>();
    };
}

fn main() {
    let client = httpc::Client;
    println!("{:?}", client.send(&httpc::Config::default()));
}

go deeper

for a junior

Recall that #[deprecated] warns callers without breaking them, and that removing a public item needs a major version.

for a middle

Explain which changes #[non_exhaustive] turns from major into minor, and why it has to be present from a type's first release.

for a senior

Apply the rules type by type, including traits, public dependencies and Send and Sync promises, and plan deprecations ahead of each major.

for a principal

Own the trade-off between room to grow and callers' exhaustiveness checking, write the policy down before 1.0, and keep majors rare and announced.

## Why this is a lead's decision Every rule in this area has a mechanical answer: adding a variant to a plain public enum is major, adding one to a `#[non_exhaustive]` enum is minor. The **policy** is not mechanical. Each choice moves cost between the library and its callers, and most can only be made at a type's **first release**, because Cargo's SemVer guide classifies adding `#[non_exhaustive]` to an existing enum, variant or all-public struct as a major change. For a widely used crate, the policy sets how often thousands of downstream crates have to do migration work. ## Per-type defaults | Public item | Default I would set | Reason | |---|---|---| | Error enums | `#[non_exhaustive]` | new failure kinds are near-certain | | Open-ended enums (protocol versions, events, header categories) | `#[non_exhaustive]` | the outside world adds values | | Closed enums (a connection's two directions, a parsed boolean setting) | exhaustive | callers benefit from E0004 when matching | | Option or config structs | `#[non_exhaustive]` with `Default`, or private fields with a builder | fields will be added | | Structs with invariants | private fields plus constructors | callers must not break invariants | | Traits users only call | sealed | methods can be added in minors | | Traits users implement | open, only defaulted additions | extensibility is the point | The trade-off to state openly: `#[non_exhaustive]` forces callers to write a `_` arm, which then silently absorbs new variants. On stable Rust nothing warns them (rustc's `non_exhaustive_omitted_patterns` lint is unstable), so release notes must announce every new variant. Clippy's restriction lints `exhaustive_enums` and `exhaustive_structs`, which flag exported types without the attribute, can enforce the default in CI if the team wants it on everything. ## Hidden commitments to manage Two parts of the API never appear as `#[non_exhaustive]` decisions but break just as hard: - **Public dependencies.** Any crate whose types appear in the API ties our major releases to its own. The policy keeps a short list of them, prefers stable (1.0+) ones, and wraps or hides the rest. - **Auto traits.** Whether `Client` and the futures returned by `async fn`s are `Send` and `Sync` is part of the contract. Compile-time assertions in tests stop a private refactor from silently withdrawing it. ## Deprecation as the path to a major Deprecation is how a breaking change is spread over time: 1. Ship the replacement in a minor release and mark the old item `#[deprecated(since = "1.4.0", note = "use `Client::send` instead")]`. Cargo's guide counts introducing a new lint warning as a **minor** change. rustc then prints `use of deprecated method `httpc::Client::send_raw`: use `Client::send` instead` at each call site. 2. Keep it working for the rest of the 1.x line; remove it only in **2.0.0**. 3. For renames, a deprecated **type alias** or a deprecated wrapper function carries the warning. A `#[deprecated]` on a `pub use` re-export produced no warning at all in a test with rustc 1.99.1, so it is the wrong tool. 4. Remember callers that build with warnings denied: a new deprecation can fail a direct dependent's `-D warnings` build. The Cargo guide suggests putting deprecations behind an opt-in feature with `cfg_attr` for crates where that matters. ## Cutting majors - **Batch** breaking changes. Each major splits the ecosystem for a while: crates using 1.x and 2.x types cannot pass them to each other, which is the public-dependency problem seen from our users' side. - **Announce** the major in advance, with the deprecations that point to it already shipped. - Before 1.0, Cargo treats the left-most non-zero component as the major (0.4 to 0.5 is breaking), so a pre-1.0 crate pays the same cost on every `y` bump. Whether to stay pre-1.0 is the general SemVer question; what is Rust-specific is that every type-level decision above should be settled *before* 1.0 is tagged. - Run an automated semver check in CI so an accidental break is caught before release; which tool to use belongs with Cargo publishing. Minimum supported Rust version is a separate policy, covered with the release train. ## A defensible summary A reasonable policy for this crate: open-world types are `#[non_exhaustive]` from day one, closed ones are not; nothing breaks without a deprecation that has shipped first; and majors are rare, batched and announced. A different team could defend more exhaustive enums for the sake of callers' exhaustiveness checking. What makes either position defensible is that it is written down and applied to every public type before 1.0.

  • When should a public Rust enum in such a crate stay exhaustive?
    When its set of values is closed by definition, such as a connection's two directions, and callers gain from rustc reporting E0004 if a match misses a case. Marking it `#[non_exhaustive]` would force them into `_` arms that can never be reached, for no future benefit.
  • How do you deprecate a renamed public type in a Rust crate so callers actually see a warning?
    Keep a type alias under the old name, `#[deprecated(since = "1.4.0", note = "renamed to `Request`")] pub type HttpRequest = Request;`, which makes rustc warn wherever the old name is used. In a test with rustc 1.99.1, the same attribute on a `pub use Request as HttpRequest;` re-export produced no warning.
  • Why is deleting an item deprecated in 1.4.0 not allowed in 1.5.0?
    Removing a public item is a major change in Cargo's SemVer guide whatever its history: callers who still use it stop compiling. The deprecation only gives them warning; the removal itself has to wait for 2.0.0.

  • Mark every public enum #[non_exhaustive], closed sets included, at no cost to callers.
  • Breaking changes are fine in a minor release if the changelog lists them.
  • Deprecated items can be removed in the next minor release.
  • Adding #[non_exhaustive] in 1.1 keeps options open without breaking anyone.
  • A #[deprecated] pub use alias is the way to warn about a renamed type.