This is related to a design question I have in the user space.
Traits are great for open interfaces, but how to use them for a closed set of possibilities?
In current Rust, I can find only one model to hide implementation details in my scenario, abusing a Private supertrait. Rust is marching towards progress, and this bug will be fixed eventually. (Edit: See below, maybe this case is not a bug)
Judging from the PR, lots of crates prefer to keep implementation details private, sometimes it’s really unfortunate when they have to be public.
// Marker types A and B
pub enum A { }
pub enum B { }
pub trait Marker: PrivateMarker { }
impl Marker for A { }
impl Marker for B { }
// Problem: How to use only the bound M: Marker,
// but still use a private trait implemented by A and B?
mod internal {
// The privatemarker trait is private --
// we want full freedom in changing the implementation details.
pub trait PrivateMarker {
fn private();
}
}
use internal::PrivateMarker;
Rust Playground Link
Is there a language feature on the horizon to help with this? I’m not sure specialization covers it; in this case we just want to close the trait to implementors A and B, and that’s it.
I don’t think there is a fundamental problem in a public trait that can’t be implemented by the user. Traits have many different roles, and this is a role I’d like them to fill better; API flexibility without being open for extension.