Sealed traits

I think this is something else that came up in zulip conversations as a related-but-different feature: #[final] methods in traits.

Basically, the problem is less that you don't want anyone else implementing the trait, but that you don't want anyone else changing the implementation of the method. And that's a separable problem -- for example, it might be nice to have

trait Copy: Clone {
    #[final]
    fn copy(&self) -> Self { *self }
}

where we obviously need Copy to be un-sealed, but it would be nice to let unsafe code rely on the method not doing something different.

That would also be a principled way to put methods on marker traits (marker_trait_attr - The Rust Unstable Book), and would be a nice feature in various other places too, like the super_* methods rustc has on visitors that should never be overridden.

9 Likes