Open
Description
#![feature(min_specialization)]
trait Read {}
struct BufReader<I> {
inner: I
}
impl<I: Read> Read for BufReader<I> {}
trait SpecUsingRead {
fn use_it(self);
}
impl<T> SpecUsingRead for T where Self: Read {
default fn use_it(self) {}
}
// impl<I> SpecUsingRead for BufReader<I> where Self: Read { // <- works
impl<I> SpecUsingRead for BufReader<I> where Self: Read, I: Read { // <- doesn't
fn use_it(self) {}
}
error: cannot specialize on trait `Read`
--> src/lib.rs:20:61
|
20 | impl<I> SpecUsingRead for BufReader<I> where Self: Read, I: Read { // <- doesn't
| ^^^^
Since all types are owned by the same crate it should be possible for the compiler to see that BufReader<I> where Self: Read
can only be true if I: Read
because there exists no other impl Read for BufReader
that could satisfy the Self
clause without satisfying the I
one.
This is useful when combining a public trait that isn't #[rustc_specialization_trait]
and wrapper structs for those traits. The wrappers often place the bounds on the impls but not on the struct itself. Having that extra bound in the specialization can unlock additional methods.