Rfcs: Unimplementable ("Sealed") Traits

Created on 27 Nov 2019  路  7Comments  路  Source: rust-lang/rfcs

Hi,
a lot of times you want to have a trait in your library/crate that can't be implemented by the library users.
either because it's a marker trait for FFI related things, related to a custom type system, or because it's very highly unsafe and weird.

libstd's solution to this problem is exposing pub traits but marking them unstable to implement i.e. https://doc.rust-lang.org/std/slice/trait.SliceIndex.html

I propose we add something like #[non_exhaustive] but for traits, i.e. #[unimplementable]
we can already see similiar weird tricks in the wild, i.e.
https://docs.rs/zerocopy/0.2.8/src/zerocopy/lib.rs.html#207
https://github.com/rust-bitcoin/rust-secp256k1/blob/master/src/context.rs#L10
and more.

A-privacy A-traits T-lang

Most helpful comment

I think the general term for them is "sealed traits", so the attribute could be #[sealed].

Looks like there's even a Pre-RFC: https://internals.rust-lang.org/t/pre-rfc-sealed-traits/3108

All 7 comments

I think the general term for them is "sealed traits", so the attribute could be #[sealed].

Looks like there's even a Pre-RFC: https://internals.rust-lang.org/t/pre-rfc-sealed-traits/3108

Thanks. I guess this can be closed then.

EDIT: arghh, that thread got closed for no activity.

it seems like Niko wrote down most of my thoughts on the matter: https://internals.rust-lang.org/t/pre-rfc-sealed-traits/3108/11

@elichai note that you can't implement SliceIndex even on nightly. This is because std uses an approximation of sealed traits by giving SliceIndex an unnamable parent trait.

@elichai note that you can't implement SliceIndex even on nightly. This is because std uses an approximation of sealed traits by giving SliceIndex an unnamable parent trait.

Wait. can I use the same trick? https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7b20e99544632bdb16900f36644a2126

@elichai yes you can

Awesome :). so I think this discussion is kinda moot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

burdges picture burdges  路  3Comments

3442853561 picture 3442853561  路  3Comments

3442853561 picture 3442853561  路  3Comments

marinintim picture marinintim  路  3Comments

steveklabnik picture steveklabnik  路  4Comments