Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sealed traits pattern #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/future-proofing.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ non-breaking way by using the sealed trait pattern.
pub trait TheTrait: private::Sealed {
// Zero or more methods that the user is allowed to call.
fn ...();

// Zero or more private methods, not allowed for user to call.
#[doc(hidden)]
fn ...();
}

// Implement for some types.
Expand All @@ -25,19 +21,24 @@ impl TheTrait for usize {
}

mod private {
pub trait Sealed {}
pub trait Sealed {
// Zero or more private methods, not allowed for user to call.
fn ...();
}

// Implement for those same types, but no others.
impl Sealed for usize {}
impl Sealed for usize {
/* ... */
}
}
```

The empty private `Sealed` supertrait cannot be named by downstream crates, so
we are guaranteed that implementations of `Sealed` (and therefore `TheTrait`)
only exist in the current crate. We are free to add methods to `TheTrait` in a
The private `Sealed` supertrait cannot be named by downstream crates, so we are
guaranteed that implementations of `Sealed` (and therefore `TheTrait`) only
exist in the current crate. We are free to add items to `TheTrait` in a
non-breaking release even though that would ordinarily be a breaking change for
traits that are not sealed. Also we are free to change the signature of methods
that are not publicly documented.
traits that are not sealed. We are also free to change the signature of
`Sealed` in any way, as its items are not visible to downstream crates.

Note that removing a public method or changing the signature of a public method
in a sealed trait are still breaking changes.
Expand Down